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 ;
21 CONSTANT: endian-bytes-0f B{
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
38 CONSTANT: endian-bytes-f0 B{
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
55 : endian-test-struct-0f ( -- obj )
56 endian-bytes-0f endian-struct memory>struct ;
58 : endian-test-struct-f0 ( -- obj )
59 endian-bytes-f0 endian-struct memory>struct ;
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
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
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
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
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
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
93 [ endian-test-struct-0f binary [ write ] with-byte-writer endian-bytes-0f = ] unit-test
96 [ endian-test-struct-f0 binary [ write ] with-byte-writer endian-bytes-f0 = ] unit-test
98 LE-STRUCT: le-endian-struct
114 endian-bytes-0f le-endian-struct memory>struct
115 binary [ write ] with-byte-writer endian-bytes-0f =
120 endian-bytes-f0 le-endian-struct memory>struct
121 binary [ write ] with-byte-writer endian-bytes-f0 =
125 BE-STRUCT: be-endian-struct
141 endian-bytes-0f be-endian-struct memory>struct
142 binary [ write ] with-byte-writer endian-bytes-0f =
147 endian-bytes-f0 be-endian-struct memory>struct
148 binary [ write ] with-byte-writer endian-bytes-f0 =
151 LE-STRUCT: le-override-struct
167 endian-bytes-0f le-override-struct memory>struct
168 binary [ write ] with-byte-writer endian-bytes-0f =
173 endian-bytes-f0 le-override-struct memory>struct
174 binary [ write ] with-byte-writer endian-bytes-f0 =
177 BE-STRUCT: be-override-struct
193 endian-bytes-0f be-override-struct memory>struct
194 binary [ write ] with-byte-writer endian-bytes-0f =
199 endian-bytes-f0 be-override-struct memory>struct
200 binary [ write ] with-byte-writer endian-bytes-f0 =
204 LE-PACKED-STRUCT: le-packed-struct
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
218 B{ 0 0 0 0 0 0 0 3 0 0 0 } le-packed-struct memory>struct
223 BE-PACKED-STRUCT: be-packed-struct
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
237 B{ 0 0 0 0 0 0 0 0 0 0 3 } be-packed-struct memory>struct