]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/endian/endian-tests.factor
Switch to https urls
[factor.git] / basis / alien / endian / endian-tests.factor
1 ! Copyright (C) 2011 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.endian classes.struct io
4 io.encodings.binary io.streams.byte-array kernel tools.test ;
5 IN: alien.endian.tests
6
7 STRUCT: endian-struct
8     { a ule16 }
9     { b le16 }
10     { c ube16 }
11     { d be16 }
12     { e ule32 }
13     { f le32 }
14     { g ube32 }
15     { h be32 }
16     { i ule64 }
17     { j le64 }
18     { k ube64 }
19     { l be64 } ;
20
21 CONSTANT: endian-bytes-0f B{
22         0x0 0xff
23         0x0 0xff
24         0x0 0xff
25         0x0 0xff
26
27         0x0 0x0 0x0 0xff
28         0x0 0x0 0x0 0xff
29         0x0 0x0 0x0 0xff
30         0x0 0x0 0x0 0xff
31
32         0x0 0x0 0x0 0x0 0x0 0x0 0x0 0xff
33         0x0 0x0 0x0 0x0 0x0 0x0 0x0 0xff
34         0x0 0x0 0x0 0x0 0x0 0x0 0x0 0xff
35         0x0 0x0 0x0 0x0 0x0 0x0 0x0 0xff
36     }
37
38 CONSTANT: endian-bytes-f0 B{
39         0xff 0x0
40         0xff 0x0
41         0xff 0x0
42         0xff 0x0
43
44         0xff 0x0 0x0 0x0
45         0xff 0x0 0x0 0x0
46         0xff 0x0 0x0 0x0
47         0xff 0x0 0x0 0x0
48
49         0xff 0x0 0x0 0x0 0x0 0x0 0x0 0x0
50         0xff 0x0 0x0 0x0 0x0 0x0 0x0 0x0
51         0xff 0x0 0x0 0x0 0x0 0x0 0x0 0x0
52         0xff 0x0 0x0 0x0 0x0 0x0 0x0 0x0
53     }
54
55 : endian-test-struct-0f ( -- obj )
56     endian-bytes-0f endian-struct memory>struct ;
57
58 : endian-test-struct-f0 ( -- obj )
59     endian-bytes-f0 endian-struct memory>struct ;
60
61 { 0xff00 } [ endian-test-struct-0f a>> ] unit-test
62 { -256 } [ endian-test-struct-0f b>> ] unit-test
63 { 0x00ff } [ endian-test-struct-0f c>> ] unit-test
64 { 0x00ff } [ endian-test-struct-0f d>> ] unit-test
65
66 { 0xff000000 } [ endian-test-struct-0f e>> ] unit-test
67 { -16777216 } [ endian-test-struct-0f f>> ] unit-test
68 { 0x000000ff } [ endian-test-struct-0f g>> ] unit-test
69 { 0x000000ff } [ endian-test-struct-0f h>> ] unit-test
70
71 { 0xff00000000000000 } [ endian-test-struct-0f i>> ] unit-test
72 { -72057594037927936 } [ endian-test-struct-0f j>> ] unit-test
73 { 0x00000000000000ff } [ endian-test-struct-0f k>> ] unit-test
74 { 0x00000000000000ff } [ endian-test-struct-0f l>> ] unit-test
75
76
77 { 0xff00 } [ endian-test-struct-f0 c>> ] unit-test
78 { -256 } [ endian-test-struct-f0 d>> ] unit-test
79 { 0x00ff } [ endian-test-struct-f0 a>> ] unit-test
80 { 0x00ff } [ endian-test-struct-f0 b>> ] unit-test
81
82 { 0xff000000 } [ endian-test-struct-f0 g>> ] unit-test
83 { -16777216 } [ endian-test-struct-f0 h>> ] unit-test
84 { 0x000000ff } [ endian-test-struct-f0 e>> ] unit-test
85 { 0x000000ff } [ endian-test-struct-f0 f>> ] unit-test
86
87 { 0xff00000000000000 } [ endian-test-struct-f0 k>> ] unit-test
88 { -72057594037927936 } [ endian-test-struct-f0 l>> ] unit-test
89 { 0x00000000000000ff } [ endian-test-struct-f0 i>> ] unit-test
90 { 0x00000000000000ff } [ endian-test-struct-f0 j>> ] unit-test
91
92 { t }
93 [ endian-test-struct-0f binary [ write ] with-byte-writer endian-bytes-0f = ] unit-test
94
95 { t }
96 [ endian-test-struct-f0 binary [ write ] with-byte-writer endian-bytes-f0 = ] unit-test
97
98 LE-STRUCT: le-endian-struct
99     { a ule16 }
100     { b le16 }
101     { c ube16 }
102     { d be16 }
103     { e ule32 }
104     { f le32 }
105     { g ube32 }
106     { h be32 }
107     { i ule64 }
108     { j le64 }
109     { k ube64 }
110     { l be64 } ;
111
112 { t }
113 [
114     endian-bytes-0f le-endian-struct memory>struct
115     binary [ write ] with-byte-writer endian-bytes-0f =
116 ] unit-test
117
118 { t }
119 [
120     endian-bytes-f0 le-endian-struct memory>struct
121     binary [ write ] with-byte-writer endian-bytes-f0 =
122 ] unit-test
123
124
125 BE-STRUCT: be-endian-struct
126     { a ule16 }
127     { b le16 }
128     { c ube16 }
129     { d be16 }
130     { e ule32 }
131     { f le32 }
132     { g ube32 }
133     { h be32 }
134     { i ule64 }
135     { j le64 }
136     { k ube64 }
137     { l be64 } ;
138
139 { t }
140 [
141     endian-bytes-0f be-endian-struct memory>struct
142     binary [ write ] with-byte-writer endian-bytes-0f =
143 ] unit-test
144
145 { t }
146 [
147     endian-bytes-f0 be-endian-struct memory>struct
148     binary [ write ] with-byte-writer endian-bytes-f0 =
149 ] unit-test
150
151 LE-STRUCT: le-override-struct
152     { a ushort }
153     { b short }
154     { c ube16 }
155     { d be16 }
156     { e uint }
157     { f int }
158     { g ube32 }
159     { h be32 }
160     { i ulonglong }
161     { j longlong }
162     { k ube64 }
163     { l be64 } ;
164
165 { t }
166 [
167     endian-bytes-0f le-override-struct memory>struct
168     binary [ write ] with-byte-writer endian-bytes-0f =
169 ] unit-test
170
171 { t }
172 [
173     endian-bytes-f0 le-override-struct memory>struct
174     binary [ write ] with-byte-writer endian-bytes-f0 =
175 ] unit-test
176
177 BE-STRUCT: be-override-struct
178     { a ule16 }
179     { b le16 }
180     { c ushort }
181     { d short }
182     { e ule32 }
183     { f le32 }
184     { g uint }
185     { h int }
186     { i ule64 }
187     { j le64 }
188     { k ulonglong }
189     { l longlong } ;
190
191 { t }
192 [
193     endian-bytes-0f be-override-struct memory>struct
194     binary [ write ] with-byte-writer endian-bytes-0f =
195 ] unit-test
196
197 { t }
198 [
199     endian-bytes-f0 be-override-struct memory>struct
200     binary [ write ] with-byte-writer endian-bytes-f0 =
201 ] unit-test
202
203
204 LE-PACKED-STRUCT: le-packed-struct
205     { a char[7] }
206     { b int } ;
207
208 { t }
209 [
210     B{ 0 0 0 0 0 0 0  3 0 0 0 } [
211         le-packed-struct memory>struct
212         binary [ write ] with-byte-writer
213     ] keep =
214 ] unit-test
215
216 { 3 }
217 [
218     B{ 0 0 0 0 0 0 0  3 0 0 0 } le-packed-struct memory>struct
219     b>>
220 ] unit-test
221
222
223 BE-PACKED-STRUCT: be-packed-struct
224     { a char[7] }
225     { b int } ;
226
227 { t }
228 [
229     B{ 0 0 0 0 0 0 0  0 0 0 3 } [
230         be-packed-struct memory>struct
231         binary [ write ] with-byte-writer
232     ] keep =
233 ] unit-test
234
235 { 3 }
236 [
237     B{ 0 0 0 0 0 0 0  0 0 0 3 } be-packed-struct memory>struct
238     b>>
239 ] unit-test