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