Skip to content

Commit 4a20f57

Browse files
committed
Refactor test
1 parent 00c6f6b commit 4a20f57

File tree

1 file changed

+47
-83
lines changed

1 file changed

+47
-83
lines changed

t/moar/13-writeint.t

Lines changed: 47 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,117 @@
1-
plan(90);
1+
plan(30);
22

33
class Buffer is repr('VMArray') is array_type(uint8) {
44
}
55

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+
616
my $buf := Buffer.new;
717

818
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');
1120

1221
$buf := Buffer.new;
1322
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');
1624

1725
$buf := Buffer.new;
1826
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');
2228

2329
$buf := Buffer.new;
2430
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');
2832

2933
$buf := Buffer.new;
3034
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');
3436

3537
$buf := Buffer.new;
3638
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');
4040

4141
$buf := Buffer.new;
4242
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');
4844

4945
$buf := Buffer.new;
5046
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');
5649

5750
$buf := Buffer.new;
5851
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');
6453

6554
$buf := Buffer.new;
6655
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');
7257

7358
# work around NQP's literals defaulting to num
7459
my uint64 $val := nqp::bitor_i(nqp::bitshiftl_i(0x01234567, 32), 0x89ABCDEF);
7560
$buf := Buffer.new;
7661
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+
8565
is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE), 0xEF, 'read byte');
8666
is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_16_BIT), 0xCDEF, 'read word');
8767
is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT), 0x89ABCDEF, 'read dword');
8868
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');
9071

9172
$buf := Buffer.new;
9273
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+
10177
is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_BIG), 0x01, 'read big endian byte');
10278
is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_16_BIT), 0x0123, 'read big endian word');
10379
is(nqp::readuint($buf, 1, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT), 0x01234567, 'read big endian dword');
10480
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');
10682

10783
$buf := Buffer.new;
10884
my num64 $num := 1234567.89;
10985
nqp::writenum($buf, 1, $num, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_64_BIT);
11086
# 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');
12091

12192
$buf := Buffer.new;
12293
$num := 1234567.89;
12394
nqp::writenum($buf, 1, $num, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_64_BIT);
12495
# 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');
13499

135100
$buf := Buffer.new;
136101
my num32 $num32 := 1234567.89;
137102
nqp::writenum($buf, 1, $num32, nqp::const::BINARY_ENDIAN_LITTLE +| nqp::const::BINARY_SIZE_32_BIT);
138103
# 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');
144109

145110
$buf := Buffer.new;
146111
$num32 := 1234567.89;
147112
nqp::writenum($buf, 1, $num32, nqp::const::BINARY_ENDIAN_BIG +| nqp::const::BINARY_SIZE_32_BIT);
148113
# 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

Comments
 (0)