|
1 |
| -plan(90); |
| 1 | +plan(30); |
2 | 2 |
|
3 | 3 | class Buffer is repr('VMArray') is array_type(uint8) {
|
4 | 4 | }
|
5 | 5 |
|
| 6 | +sub buf_dump($buf) { |
| 7 | + my @parts; |
| 8 | + my $i := 0; |
| 9 | + while $i < nqp::elems($buf) { |
| 10 | + @parts.push(nqp::sprintf("0x%.2X", [nqp::atpos_i($buf, $i)])); |
| 11 | + $i := $i + 1; |
| 12 | + } |
| 13 | + nqp::join(" ", @parts); |
| 14 | +}; |
| 15 | + |
6 | 16 | my $buf := Buffer.new;
|
7 | 17 |
|
8 | 18 | nqp::writeuint($buf, 0, 0x1234, nqp::const::BINARY_ENDIAN_LITTLE);
|
9 |
| -is(nqp::atpos_i($buf, 0), 0x34); |
10 |
| -is(nqp::elems($buf), 1); |
| 19 | +is(buf_dump($buf), '0x34', 'nqp::writeuint - 8bit little endian'); |
11 | 20 |
|
12 | 21 | $buf := Buffer.new;
|
13 | 22 | nqp::writeint($buf, 0, -128, nqp::const::BINARY_ENDIAN_LITTLE);
|
14 |
| -is(nqp::atpos_i($buf, 0), 128); |
15 |
| -is(nqp::elems($buf), 1); |
| 23 | +is(buf_dump($buf), '0x80', 'nqp::writeint - 8bit little endian'); |
16 | 24 |
|
17 | 25 | $buf := Buffer.new;
|
18 | 26 | nqp::writeuint($buf, 0, 0x1234, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_16_BIT);
|
19 |
| -is(nqp::atpos_i($buf, 0), 0x34); |
20 |
| -is(nqp::atpos_i($buf, 1), 0x12); |
21 |
| -is(nqp::elems($buf), 2); |
| 27 | +is(buf_dump($buf), '0x34 0x12', 'nqp::writeuint - 16bit little endian'); |
22 | 28 |
|
23 | 29 | $buf := Buffer.new;
|
24 | 30 | nqp::writeint($buf, 0, -32768, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_16_BIT);
|
25 |
| -is(nqp::atpos_i($buf, 0), 0); |
26 |
| -is(nqp::atpos_i($buf, 1), 128); |
27 |
| -is(nqp::elems($buf), 2); |
| 31 | +is(buf_dump($buf), '0x00 0x80', 'nqp::writeint - 16bit little endian'); |
28 | 32 |
|
29 | 33 | $buf := Buffer.new;
|
30 | 34 | nqp::writeuint($buf, 0, 0x1234, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_16_BIT);
|
31 |
| -is(nqp::atpos_i($buf, 0), 0x12); |
32 |
| -is(nqp::atpos_i($buf, 1), 0x34); |
33 |
| -is(nqp::elems($buf), 2); |
| 35 | +is(buf_dump($buf), '0x12 0x34', 'nqp::writeuint - 16bit big endian'); |
34 | 36 |
|
35 | 37 | $buf := Buffer.new;
|
36 | 38 | nqp::writeint($buf, 0, -32768, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_16_BIT);
|
37 |
| -is(nqp::atpos_i($buf, 0), 128); |
38 |
| -is(nqp::atpos_i($buf, 1), 0); |
39 |
| -is(nqp::elems($buf), 2); |
| 39 | +is(buf_dump($buf), '0x80 0x00', 'nqp::writeint - 16bit big endian'); |
40 | 40 |
|
41 | 41 | $buf := Buffer.new;
|
42 | 42 | nqp::writeuint($buf, 1, 0x12345678, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT);
|
43 |
| -is(nqp::atpos_i($buf, 1), 0x78); |
44 |
| -is(nqp::atpos_i($buf, 2), 0x56); |
45 |
| -is(nqp::atpos_i($buf, 3), 0x34); |
46 |
| -is(nqp::atpos_i($buf, 4), 0x12); |
47 |
| -is(nqp::elems($buf), 5); |
| 43 | +is(buf_dump($buf), '0x00 0x78 0x56 0x34 0x12', 'nqp::writeuint - 32bit little endian'); |
48 | 44 |
|
49 | 45 | $buf := Buffer.new;
|
50 | 46 | nqp::writeuint($buf, 1, -2147483648, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT);
|
51 |
| -is(nqp::atpos_i($buf, 1), 0); |
52 |
| -is(nqp::atpos_i($buf, 2), 0); |
53 |
| -is(nqp::atpos_i($buf, 3), 0); |
54 |
| -is(nqp::atpos_i($buf, 4), 128); |
55 |
| -is(nqp::elems($buf), 5); |
| 47 | + |
| 48 | +is(buf_dump($buf), '0x00 0x00 0x00 0x00 0x80', 'nqp::writeuint - 32bit little endian'); |
56 | 49 |
|
57 | 50 | $buf := Buffer.new;
|
58 | 51 | nqp::writeuint($buf, 1, 0x12345678, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT);
|
59 |
| -is(nqp::atpos_i($buf, 1), 0x12); |
60 |
| -is(nqp::atpos_i($buf, 2), 0x34); |
61 |
| -is(nqp::atpos_i($buf, 3), 0x56); |
62 |
| -is(nqp::atpos_i($buf, 4), 0x78); |
63 |
| -is(nqp::elems($buf), 5); |
| 52 | +is(buf_dump($buf), '0x00 0x12 0x34 0x56 0x78', 'nqp::writeuint - 32bit big endian'); |
64 | 53 |
|
65 | 54 | $buf := Buffer.new;
|
66 | 55 | nqp::writeuint($buf, 1, -2147483648, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT);
|
67 |
| -is(nqp::atpos_i($buf, 1), 128); |
68 |
| -is(nqp::atpos_i($buf, 2), 0); |
69 |
| -is(nqp::atpos_i($buf, 3), 0); |
70 |
| -is(nqp::atpos_i($buf, 4), 0); |
71 |
| -is(nqp::elems($buf), 5); |
| 56 | +is(buf_dump($buf), '0x00 0x80 0x00 0x00 0x00', 'nqp::writeuint - 32bit big endian'); |
72 | 57 |
|
73 | 58 | # work around NQP's literals defaulting to num
|
74 | 59 | my uint64 $val := nqp::bitor_i(nqp::bitshiftl_i(0x01234567, 32), 0x89ABCDEF);
|
75 | 60 | $buf := Buffer.new;
|
76 | 61 | nqp::writeuint($buf, 1, $val, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_64_BIT);
|
77 |
| -is(nqp::atpos_i($buf, 1), 0xEF); |
78 |
| -is(nqp::atpos_i($buf, 2), 0xCD); |
79 |
| -is(nqp::atpos_i($buf, 3), 0xAB); |
80 |
| -is(nqp::atpos_i($buf, 4), 0x89); |
81 |
| -is(nqp::atpos_i($buf, 5), 0x67); |
82 |
| -is(nqp::atpos_i($buf, 6), 0x45); |
83 |
| -is(nqp::atpos_i($buf, 7), 0x23); |
84 |
| -is(nqp::atpos_i($buf, 8), 0x01); |
| 62 | + |
| 63 | +is(buf_dump($buf), '0x00 0xEF 0xCD 0xAB 0x89 0x67 0x45 0x23 0x01', 'nqp::writeuint with 64bit little endian'); |
| 64 | + |
85 | 65 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE), 0xEF, 'read byte');
|
86 | 66 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_16_BIT), 0xCDEF, 'read word');
|
87 | 67 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT), 0x89ABCDEF, 'read dword');
|
88 | 68 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_64_BIT), $val, 'read qword');
|
89 |
| -is(nqp::elems($buf), 9); |
| 69 | + |
| 70 | +is(nqp::elems($buf), 9, 'nqp::readuint does not change the size of buffer'); |
90 | 71 |
|
91 | 72 | $buf := Buffer.new;
|
92 | 73 | nqp::writeuint($buf, 1, $val, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_64_BIT);
|
93 |
| -is(nqp::atpos_i($buf, 1), 0x01); |
94 |
| -is(nqp::atpos_i($buf, 2), 0x23); |
95 |
| -is(nqp::atpos_i($buf, 3), 0x45); |
96 |
| -is(nqp::atpos_i($buf, 4), 0x67); |
97 |
| -is(nqp::atpos_i($buf, 5), 0x89); |
98 |
| -is(nqp::atpos_i($buf, 6), 0xAB); |
99 |
| -is(nqp::atpos_i($buf, 7), 0xCD); |
100 |
| -is(nqp::atpos_i($buf, 8), 0xEF); |
| 74 | + |
| 75 | +is(buf_dump($buf), '0x00 0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF', 'nqp::writeuint with 64bit big endian'); |
| 76 | + |
101 | 77 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_BIG), 0x01, 'read big endian byte');
|
102 | 78 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_16_BIT), 0x0123, 'read big endian word');
|
103 | 79 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT), 0x01234567, 'read big endian dword');
|
104 | 80 | is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_64_BIT), $val, 'read big endian qword');
|
105 |
| -is(nqp::elems($buf), 9); |
| 81 | +is(nqp::elems($buf), 9, 'nqp::readuint does not change the size of buffer'); |
106 | 82 |
|
107 | 83 | $buf := Buffer.new;
|
108 | 84 | my num64 $num := 1234567.89;
|
109 | 85 | nqp::writenum($buf, 1, $num, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_64_BIT);
|
110 | 86 | # https://baseconvert.com/ieee-754-floating-point 0x4132D687E3D70A3D
|
111 |
| -is(nqp::atpos_i($buf, 1), 0x3d); |
112 |
| -is(nqp::atpos_i($buf, 2), 0x0a); |
113 |
| -is(nqp::atpos_i($buf, 3), 0xd7); |
114 |
| -is(nqp::atpos_i($buf, 4), 0xe3); |
115 |
| -is(nqp::atpos_i($buf, 5), 0x87); |
116 |
| -is(nqp::atpos_i($buf, 6), 0xd6); |
117 |
| -is(nqp::atpos_i($buf, 7), 0x32); |
118 |
| -is(nqp::atpos_i($buf, 8), 0x41); |
119 |
| -is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_64_BIT), $num); |
| 87 | + |
| 88 | +is(buf_dump($buf), '0x00 0x3D 0x0A 0xD7 0xE3 0x87 0xD6 0x32 0x41', 'nqp::writenum with 64bit little endian'); |
| 89 | + |
| 90 | +is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_64_BIT), $num, 'nqp::readum with 64bit little endian'); |
120 | 91 |
|
121 | 92 | $buf := Buffer.new;
|
122 | 93 | $num := 1234567.89;
|
123 | 94 | nqp::writenum($buf, 1, $num, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_64_BIT);
|
124 | 95 | # https://baseconvert.com/ieee-754-floating-point 0x4132D687E3D70A3D
|
125 |
| -is(nqp::atpos_i($buf, 1), 0x41); |
126 |
| -is(nqp::atpos_i($buf, 2), 0x32); |
127 |
| -is(nqp::atpos_i($buf, 3), 0xd6); |
128 |
| -is(nqp::atpos_i($buf, 4), 0x87); |
129 |
| -is(nqp::atpos_i($buf, 5), 0xe3); |
130 |
| -is(nqp::atpos_i($buf, 6), 0xd7); |
131 |
| -is(nqp::atpos_i($buf, 7), 0x0a); |
132 |
| -is(nqp::atpos_i($buf, 8), 0x3d); |
133 |
| -is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_64_BIT), $num); |
| 96 | + |
| 97 | +is(buf_dump($buf), '0x00 0x41 0x32 0xD6 0x87 0xE3 0xD7 0x0A 0x3D', 'nqp::writenum with 64bit big endian'); |
| 98 | +is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_64_BIT), $num, 'nqp::readnum with 64bit big endian'); |
134 | 99 |
|
135 | 100 | $buf := Buffer.new;
|
136 | 101 | my num32 $num32 := 1234567.89;
|
137 | 102 | nqp::writenum($buf, 1, $num32, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT);
|
138 | 103 | # https://baseconvert.com/ieee-754-floating-point 0x4996B43F
|
139 |
| -is(nqp::atpos_i($buf, 1), 0x3f); |
140 |
| -is(nqp::atpos_i($buf, 2), 0xb4); |
141 |
| -is(nqp::atpos_i($buf, 3), 0x96); |
142 |
| -is(nqp::atpos_i($buf, 4), 0x49); |
143 |
| -is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT), $num32); |
| 104 | + |
| 105 | +is(buf_dump($buf), '0x00 0x3F 0xB4 0x96 0x49', 'nqp::writenum with 32bit little endian'); |
| 106 | + |
| 107 | + |
| 108 | +is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT), $num32, 'nqp::readnum with 32bit little endian'); |
144 | 109 |
|
145 | 110 | $buf := Buffer.new;
|
146 | 111 | $num32 := 1234567.89;
|
147 | 112 | nqp::writenum($buf, 1, $num32, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT);
|
148 | 113 | # https://baseconvert.com/ieee-754-floating-point 0x4996B43F
|
149 |
| -is(nqp::atpos_i($buf, 1), 0x49); |
150 |
| -is(nqp::atpos_i($buf, 2), 0x96); |
151 |
| -is(nqp::atpos_i($buf, 3), 0xb4); |
152 |
| -is(nqp::atpos_i($buf, 4), 0x3f); |
153 |
| -is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT), $num32); |
| 114 | + |
| 115 | +is(buf_dump($buf), '0x00 0x49 0x96 0xB4 0x3F', 'nqp::writenum with 32bit big endian'); |
| 116 | + |
| 117 | +is(nqp::readnum($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT), $num32, 'nqp::readnum with 32bit big endian'); |
0 commit comments