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
22 CONSTANT: endian-bytes-0f B{
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
39 CONSTANT: endian-bytes-f0 B{
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
56 : endian-test-struct-0f ( -- obj )
57 endian-bytes-0f endian-struct memory>struct ;
59 : endian-test-struct-f0 ( -- obj )
60 endian-bytes-f0 endian-struct memory>struct ;
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
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
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
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
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
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
94 [ endian-test-struct-0f binary [ write ] with-byte-writer endian-bytes-0f = ] unit-test
97 [ endian-test-struct-f0 binary [ write ] with-byte-writer endian-bytes-f0 = ] unit-test
99 LE-STRUCT: le-endian-struct
115 endian-bytes-0f le-endian-struct memory>struct
116 binary [ write ] with-byte-writer endian-bytes-0f =
121 endian-bytes-f0 le-endian-struct memory>struct
122 binary [ write ] with-byte-writer endian-bytes-f0 =
126 BE-STRUCT: be-endian-struct
142 endian-bytes-0f be-endian-struct memory>struct
143 binary [ write ] with-byte-writer endian-bytes-0f =
148 endian-bytes-f0 be-endian-struct memory>struct
149 binary [ write ] with-byte-writer endian-bytes-f0 =
152 LE-STRUCT: le-override-struct
168 endian-bytes-0f le-override-struct memory>struct
169 binary [ write ] with-byte-writer endian-bytes-0f =
174 endian-bytes-f0 le-override-struct memory>struct
175 binary [ write ] with-byte-writer endian-bytes-f0 =
178 BE-STRUCT: be-override-struct
194 endian-bytes-0f be-override-struct memory>struct
195 binary [ write ] with-byte-writer endian-bytes-0f =
200 endian-bytes-f0 be-override-struct memory>struct
201 binary [ write ] with-byte-writer endian-bytes-f0 =
205 LE-PACKED-STRUCT: le-packed-struct
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
219 B{ 0 0 0 0 0 0 0 3 0 0 0 } le-packed-struct memory>struct
224 BE-PACKED-STRUCT: be-packed-struct
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
238 B{ 0 0 0 0 0 0 0 0 0 0 3 } be-packed-struct memory>struct