solidity-scale-codec 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/package.json +5 -2
- package/src/LittleEndian/LittleEndianU128.sol +49 -0
- package/src/LittleEndian/LittleEndianU16.sol +29 -0
- package/src/LittleEndian/LittleEndianU256.sol +101 -0
- package/src/LittleEndian/LittleEndianU32.sol +42 -0
- package/src/LittleEndian/LittleEndianU64.sol +45 -0
- package/src/LittleEndian/LittleEndianU8.sol +26 -0
- package/src/Scale/Array/BoolArr.sol +49 -35
- package/src/Scale/Array/I128Arr.sol +49 -35
- package/src/Scale/Array/I16Arr.sol +49 -35
- package/src/Scale/Array/I256Arr.sol +49 -35
- package/src/Scale/Array/I32Arr.sol +49 -35
- package/src/Scale/Array/I64Arr.sol +49 -35
- package/src/Scale/Array/I8Arr.sol +49 -35
- package/src/Scale/Array/U128Arr.sol +49 -35
- package/src/Scale/Array/U16Arr.sol +49 -35
- package/src/Scale/Array/U256Arr.sol +49 -35
- package/src/Scale/Array/U32Arr.sol +49 -35
- package/src/Scale/Array/U64Arr.sol +49 -35
- package/src/Scale/Array/U8Arr.sol +49 -35
- package/src/Scale/Array.sol +14 -15
- package/src/Scale/Bool/Bool.sol +1 -1
- package/src/Scale/Bool.sol +1 -1
- package/src/Scale/Compact/Compact.sol +89 -460
- package/src/Scale/Compact.sol +1 -1
- package/src/Scale/Signed/I128.sol +13 -6
- package/src/Scale/Signed/I16.sol +10 -5
- package/src/Scale/Signed/I256.sol +13 -6
- package/src/Scale/Signed/I32.sol +10 -5
- package/src/Scale/Signed/I64.sol +10 -5
- package/src/Scale/Signed/I8.sol +10 -5
- package/src/Scale/Signed.sol +7 -8
- package/src/Scale/Unsigned/U128.sol +15 -9
- package/src/Scale/Unsigned/U16.sol +15 -9
- package/src/Scale/Unsigned/U256.sol +15 -9
- package/src/Scale/Unsigned/U32.sol +15 -9
- package/src/Scale/Unsigned/U64.sol +15 -9
- package/src/Scale/Unsigned/U8.sol +13 -9
- package/src/Scale/Unsigned.sol +7 -8
- package/src/Scale/Compact/Compact.t.sol +0 -326
- package/src/Utils/LittleEndian/LittleEndian.sol +0 -354
- package/src/Utils/LittleEndian/LittleEndian.t.sol +0 -542
|
@@ -1,542 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
pragma solidity ^0.8.20;
|
|
3
|
-
|
|
4
|
-
import {LittleEndian} from "./LittleEndian.sol";
|
|
5
|
-
import {Test} from "forge-std/Test.sol";
|
|
6
|
-
|
|
7
|
-
contract LittleEndianTest is Test {
|
|
8
|
-
// ============ uint8 / bytes1 ============
|
|
9
|
-
|
|
10
|
-
function test_Convert8_Basic() public pure {
|
|
11
|
-
assertEq(LittleEndian.toLittleEndianU8(0xAB), bytes1(0xAB));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function test_Convert8_Zero() public pure {
|
|
15
|
-
assertEq(LittleEndian.toLittleEndianU8(0), bytes1(0));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function test_Convert8_Max() public pure {
|
|
19
|
-
assertEq(LittleEndian.toLittleEndianU8(0xFF), bytes1(0xFF));
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function testFuzz_Convert8(uint8 value) public pure {
|
|
23
|
-
assertEq(uint8(LittleEndian.toLittleEndianU8(value)), value);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// ============ uint16 / bytes2 ============
|
|
27
|
-
|
|
28
|
-
function test_Convert16_Basic() public pure {
|
|
29
|
-
assertEq(LittleEndian.toLittleEndianU16(0xABCD), bytes2(0xCDAB));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function test_Convert16_Zero() public pure {
|
|
33
|
-
assertEq(LittleEndian.toLittleEndianU16(0), bytes2(0));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function test_Convert16_Max() public pure {
|
|
37
|
-
assertEq(LittleEndian.toLittleEndianU16(0xFFFF), bytes2(0xFFFF));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function testFuzz_Roundtrip16(uint16 value) public pure {
|
|
41
|
-
bytes2 le = LittleEndian.toLittleEndianU16(value);
|
|
42
|
-
uint16 reconstructed = uint16(uint8(le[0])) |
|
|
43
|
-
(uint16(uint8(le[1])) << 8);
|
|
44
|
-
assertEq(reconstructed, value);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// ============ uint32 / bytes4 ============
|
|
48
|
-
|
|
49
|
-
function test_Convert32_Basic() public pure {
|
|
50
|
-
assertEq(
|
|
51
|
-
LittleEndian.toLittleEndianU32(0x12345678),
|
|
52
|
-
bytes4(0x78563412)
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function test_Convert32_Zero() public pure {
|
|
57
|
-
assertEq(LittleEndian.toLittleEndianU32(0), bytes4(0));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function test_Convert32_Max() public pure {
|
|
61
|
-
assertEq(
|
|
62
|
-
LittleEndian.toLittleEndianU32(0xFFFFFFFF),
|
|
63
|
-
bytes4(0xFFFFFFFF)
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function testFuzz_Roundtrip32(uint32 value) public pure {
|
|
68
|
-
bytes4 le = LittleEndian.toLittleEndianU32(value);
|
|
69
|
-
uint32 reconstructed = uint32(uint8(le[0])) |
|
|
70
|
-
(uint32(uint8(le[1])) << 8) |
|
|
71
|
-
(uint32(uint8(le[2])) << 16) |
|
|
72
|
-
(uint32(uint8(le[3])) << 24);
|
|
73
|
-
assertEq(reconstructed, value);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ============ uint64 / bytes8 ============
|
|
77
|
-
|
|
78
|
-
function test_Convert64_Basic() public pure {
|
|
79
|
-
assertEq(
|
|
80
|
-
LittleEndian.toLittleEndianU64(0x0102030405060708),
|
|
81
|
-
bytes8(0x0807060504030201)
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function test_Convert64_Zero() public pure {
|
|
86
|
-
assertEq(LittleEndian.toLittleEndianU64(0), bytes8(0));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function test_Convert64_Max() public pure {
|
|
90
|
-
assertEq(
|
|
91
|
-
LittleEndian.toLittleEndianU64(type(uint64).max),
|
|
92
|
-
bytes8(0xFFFFFFFFFFFFFFFF)
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function testFuzz_Roundtrip64(uint64 value) public pure {
|
|
97
|
-
bytes8 le = LittleEndian.toLittleEndianU64(value);
|
|
98
|
-
uint64 reconstructed;
|
|
99
|
-
for (uint256 i = 0; i < 8; i++) {
|
|
100
|
-
reconstructed |= uint64(uint8(le[i])) << (i * 8);
|
|
101
|
-
}
|
|
102
|
-
assertEq(reconstructed, value);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// ============ uint128 / bytes16 ============
|
|
106
|
-
|
|
107
|
-
function test_Convert128_Basic() public pure {
|
|
108
|
-
assertEq(
|
|
109
|
-
LittleEndian.toLittleEndianU128(0x0102030405060708090a0b0c0d0e0f10),
|
|
110
|
-
bytes16(0x100f0e0d0c0b0a090807060504030201)
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function test_Convert128_Zero() public pure {
|
|
115
|
-
assertEq(LittleEndian.toLittleEndianU128(0), bytes16(0));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function test_Convert128_Max() public pure {
|
|
119
|
-
assertEq(
|
|
120
|
-
LittleEndian.toLittleEndianU128(type(uint128).max),
|
|
121
|
-
bytes16(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function testFuzz_Roundtrip128(uint128 value) public pure {
|
|
126
|
-
bytes16 le = LittleEndian.toLittleEndianU128(value);
|
|
127
|
-
uint128 reconstructed;
|
|
128
|
-
for (uint256 i = 0; i < 16; i++) {
|
|
129
|
-
reconstructed |= uint128(uint8(le[i])) << (i * 8);
|
|
130
|
-
}
|
|
131
|
-
assertEq(reconstructed, value);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// ============ uint256 / bytes32 ============
|
|
135
|
-
|
|
136
|
-
function test_Convert256_Basic() public pure {
|
|
137
|
-
uint256 value = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20;
|
|
138
|
-
assertEq(
|
|
139
|
-
LittleEndian.toLittleEndianU256(value),
|
|
140
|
-
bytes32(
|
|
141
|
-
0x201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a090807060504030201
|
|
142
|
-
)
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function test_Convert256_Zero() public pure {
|
|
147
|
-
assertEq(LittleEndian.toLittleEndianU256(0), bytes32(0));
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function test_Convert256_Max() public pure {
|
|
151
|
-
assertEq(
|
|
152
|
-
LittleEndian.toLittleEndianU256(type(uint256).max),
|
|
153
|
-
bytes32(type(uint256).max)
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function testFuzz_Roundtrip256(uint256 value) public pure {
|
|
158
|
-
bytes32 le = LittleEndian.toLittleEndianU256(value);
|
|
159
|
-
uint256 reconstructed;
|
|
160
|
-
for (uint256 i = 0; i < 32; i++) {
|
|
161
|
-
reconstructed |= uint256(uint8(le[i])) << (i * 8);
|
|
162
|
-
}
|
|
163
|
-
assertEq(reconstructed, value);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// ============ Idempotency: f(f(x)) == x ============
|
|
167
|
-
|
|
168
|
-
function testFuzz_Idempotent16(uint16 value) public pure {
|
|
169
|
-
bytes2 le = LittleEndian.toLittleEndianU16(value);
|
|
170
|
-
bytes2 back = LittleEndian.toLittleEndianU16(uint16(le));
|
|
171
|
-
assertEq(uint16(back), value);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function testFuzz_Idempotent32(uint32 value) public pure {
|
|
175
|
-
bytes4 le = LittleEndian.toLittleEndianU32(value);
|
|
176
|
-
bytes4 back = LittleEndian.toLittleEndianU32(uint32(le));
|
|
177
|
-
assertEq(uint32(back), value);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function testFuzz_Idempotent64(uint64 value) public pure {
|
|
181
|
-
bytes8 le = LittleEndian.toLittleEndianU64(value);
|
|
182
|
-
bytes8 back = LittleEndian.toLittleEndianU64(uint64(le));
|
|
183
|
-
assertEq(uint64(back), value);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function testFuzz_Idempotent128(uint128 value) public pure {
|
|
187
|
-
bytes16 le = LittleEndian.toLittleEndianU128(value);
|
|
188
|
-
bytes16 back = LittleEndian.toLittleEndianU128(uint128(le));
|
|
189
|
-
assertEq(uint128(back), value);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function testFuzz_Idempotent256(uint256 value) public pure {
|
|
193
|
-
bytes32 le = LittleEndian.toLittleEndianU256(value);
|
|
194
|
-
bytes32 back = LittleEndian.toLittleEndianU256(uint256(le));
|
|
195
|
-
assertEq(uint256(back), value);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// ============ Signed int8 ============
|
|
199
|
-
|
|
200
|
-
function test_ConvertSigned8_Positive() public pure {
|
|
201
|
-
assertEq(LittleEndian.toLittleEndianI8(int8(1)), bytes1(0x01));
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function test_ConvertSigned8_Negative() public pure {
|
|
205
|
-
assertEq(LittleEndian.toLittleEndianI8(int8(-1)), bytes1(0xff));
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function test_ConvertSigned8_Min() public pure {
|
|
209
|
-
assertEq(LittleEndian.toLittleEndianI8(type(int8).min), bytes1(0x80));
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function test_ConvertSigned8_Max() public pure {
|
|
213
|
-
assertEq(LittleEndian.toLittleEndianI8(type(int8).max), bytes1(0x7f));
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// ============ Signed int16 ============
|
|
217
|
-
|
|
218
|
-
function test_ConvertSigned16_Positive() public pure {
|
|
219
|
-
assertEq(LittleEndian.toLittleEndianI16(int16(1)), bytes2(0x0100));
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function test_ConvertSigned16_Negative() public pure {
|
|
223
|
-
assertEq(LittleEndian.toLittleEndianI16(int16(-1)), bytes2(0xffff));
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function test_ConvertSigned16_Neg256() public pure {
|
|
227
|
-
assertEq(LittleEndian.toLittleEndianI16(int16(-256)), bytes2(0x00ff));
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function test_ConvertSigned16_Min() public pure {
|
|
231
|
-
assertEq(
|
|
232
|
-
LittleEndian.toLittleEndianI16(type(int16).min),
|
|
233
|
-
bytes2(0x0080)
|
|
234
|
-
);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function test_ConvertSigned16_Max() public pure {
|
|
238
|
-
assertEq(
|
|
239
|
-
LittleEndian.toLittleEndianI16(type(int16).max),
|
|
240
|
-
bytes2(0xff7f)
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// ============ Signed int32 ============
|
|
245
|
-
|
|
246
|
-
function test_ConvertSigned32_Negative() public pure {
|
|
247
|
-
assertEq(LittleEndian.toLittleEndianI32(int32(-1)), bytes4(0xffffffff));
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
function test_ConvertSigned32_Min() public pure {
|
|
251
|
-
assertEq(
|
|
252
|
-
LittleEndian.toLittleEndianI32(type(int32).min),
|
|
253
|
-
bytes4(0x00000080)
|
|
254
|
-
);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function test_ConvertSigned32_Max() public pure {
|
|
258
|
-
assertEq(
|
|
259
|
-
LittleEndian.toLittleEndianI32(type(int32).max),
|
|
260
|
-
bytes4(0xffffff7f)
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// ============ Signed int64 ============
|
|
265
|
-
|
|
266
|
-
function test_ConvertSigned64_Negative() public pure {
|
|
267
|
-
assertEq(
|
|
268
|
-
LittleEndian.toLittleEndianI64(int64(-1)),
|
|
269
|
-
bytes8(0xffffffffffffffff)
|
|
270
|
-
);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function test_ConvertSigned64_Min() public pure {
|
|
274
|
-
assertEq(
|
|
275
|
-
LittleEndian.toLittleEndianI64(type(int64).min),
|
|
276
|
-
bytes8(0x0000000000000080)
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
function test_ConvertSigned64_Max() public pure {
|
|
281
|
-
assertEq(
|
|
282
|
-
LittleEndian.toLittleEndianI64(type(int64).max),
|
|
283
|
-
bytes8(0xffffffffffffff7f)
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// ============ Signed int128 ============
|
|
288
|
-
|
|
289
|
-
function test_ConvertSigned128_Negative() public pure {
|
|
290
|
-
assertEq(
|
|
291
|
-
LittleEndian.toLittleEndianI128(int128(-1)),
|
|
292
|
-
bytes16(0xffffffffffffffffffffffffffffffff)
|
|
293
|
-
);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function test_ConvertSigned128_Min() public pure {
|
|
297
|
-
assertEq(
|
|
298
|
-
LittleEndian.toLittleEndianI128(type(int128).min),
|
|
299
|
-
bytes16(0x00000000000000000000000000000080)
|
|
300
|
-
);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// ============ Signed int256 ============
|
|
304
|
-
|
|
305
|
-
function test_ConvertSigned256_Negative() public pure {
|
|
306
|
-
assertEq(
|
|
307
|
-
LittleEndian.toLittleEndianI256(int256(-1)),
|
|
308
|
-
bytes32(type(uint256).max)
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function test_ConvertSigned256_Min() public pure {
|
|
313
|
-
assertEq(
|
|
314
|
-
LittleEndian.toLittleEndianI256(type(int256).min),
|
|
315
|
-
bytes32(
|
|
316
|
-
0x0000000000000000000000000000000000000000000000000000000000000080
|
|
317
|
-
)
|
|
318
|
-
);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// ============ Fuzz signed ============
|
|
322
|
-
|
|
323
|
-
function testFuzz_RoundtripSigned16(int16 value) public pure {
|
|
324
|
-
bytes2 le = LittleEndian.toLittleEndianI16(value);
|
|
325
|
-
int16 reconstructed = int16(
|
|
326
|
-
uint16(uint8(le[0])) | (uint16(uint8(le[1])) << 8)
|
|
327
|
-
);
|
|
328
|
-
assertEq(reconstructed, value);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function testFuzz_RoundtripSigned32(int32 value) public pure {
|
|
332
|
-
bytes4 le = LittleEndian.toLittleEndianI32(value);
|
|
333
|
-
int32 reconstructed = int32(
|
|
334
|
-
uint32(uint8(le[0])) |
|
|
335
|
-
(uint32(uint8(le[1])) << 8) |
|
|
336
|
-
(uint32(uint8(le[2])) << 16) |
|
|
337
|
-
(uint32(uint8(le[3])) << 24)
|
|
338
|
-
);
|
|
339
|
-
assertEq(reconstructed, value);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// ============ fromLittleEndian unsigned ============
|
|
343
|
-
|
|
344
|
-
function test_FromLE8_Basic() public pure {
|
|
345
|
-
assertEq(LittleEndian.fromLittleEndianU8(bytes1(0xAB)), 0xAB);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
function test_FromLE16_Basic() public pure {
|
|
349
|
-
// 0xCDAB (LE) → 0xABCD
|
|
350
|
-
assertEq(LittleEndian.fromLittleEndianU16(bytes2(0xCDAB)), 0xABCD);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
function test_FromLE32_Basic() public pure {
|
|
354
|
-
// 0x78563412 (LE) → 0x12345678
|
|
355
|
-
assertEq(
|
|
356
|
-
LittleEndian.fromLittleEndianU32(bytes4(0x78563412)),
|
|
357
|
-
0x12345678
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
function test_FromLE64_Basic() public pure {
|
|
362
|
-
// 0x0807060504030201 (LE) → 0x0102030405060708
|
|
363
|
-
assertEq(
|
|
364
|
-
LittleEndian.fromLittleEndianU64(bytes8(0x0807060504030201)),
|
|
365
|
-
0x0102030405060708
|
|
366
|
-
);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
function test_FromLE128_Basic() public pure {
|
|
370
|
-
assertEq(
|
|
371
|
-
LittleEndian.fromLittleEndianU128(
|
|
372
|
-
bytes16(0x100f0e0d0c0b0a090807060504030201)
|
|
373
|
-
),
|
|
374
|
-
0x0102030405060708090a0b0c0d0e0f10
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
function test_FromLE256_Basic() public pure {
|
|
379
|
-
assertEq(
|
|
380
|
-
LittleEndian.fromLittleEndianU256(
|
|
381
|
-
bytes32(
|
|
382
|
-
0x201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a090807060504030201
|
|
383
|
-
)
|
|
384
|
-
),
|
|
385
|
-
0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// ============ fromLittleEndian signed ============
|
|
390
|
-
|
|
391
|
-
function test_FromLEi8_Negative() public pure {
|
|
392
|
-
assertEq(LittleEndian.fromLittleEndianI8(bytes1(0xff)), int8(-1));
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
function test_FromLEi16_Negative() public pure {
|
|
396
|
-
assertEq(LittleEndian.fromLittleEndianI16(bytes2(0xffff)), int16(-1));
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
function test_FromLEi16_Neg256() public pure {
|
|
400
|
-
// 0x00ff (LE) → 0xff00 (BE) → -256
|
|
401
|
-
assertEq(LittleEndian.fromLittleEndianI16(bytes2(0x00ff)), int16(-256));
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
function test_FromLEi32_Negative() public pure {
|
|
405
|
-
assertEq(
|
|
406
|
-
LittleEndian.fromLittleEndianI32(bytes4(0xffffffff)),
|
|
407
|
-
int32(-1)
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
function test_FromLEi32_Min() public pure {
|
|
412
|
-
// 0x00000080 (LE) → 0x80000000 (BE) → int32.min
|
|
413
|
-
assertEq(
|
|
414
|
-
LittleEndian.fromLittleEndianI32(bytes4(0x00000080)),
|
|
415
|
-
type(int32).min
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
function test_FromLEi64_Negative() public pure {
|
|
420
|
-
assertEq(
|
|
421
|
-
LittleEndian.fromLittleEndianI64(bytes8(0xffffffffffffffff)),
|
|
422
|
-
int64(-1)
|
|
423
|
-
);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
function test_FromLEi128_Negative() public pure {
|
|
427
|
-
assertEq(
|
|
428
|
-
LittleEndian.fromLittleEndianI128(
|
|
429
|
-
bytes16(0xffffffffffffffffffffffffffffffff)
|
|
430
|
-
),
|
|
431
|
-
int128(-1)
|
|
432
|
-
);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
function test_FromLEi256_Negative() public pure {
|
|
436
|
-
assertEq(
|
|
437
|
-
LittleEndian.fromLittleEndianI256(bytes32(type(uint256).max)),
|
|
438
|
-
int256(-1)
|
|
439
|
-
);
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
// ============ Fuzz roundtrip: toLE → fromLE ============
|
|
443
|
-
|
|
444
|
-
function testFuzz_RoundtripFromLE8(uint8 value) public pure {
|
|
445
|
-
assertEq(
|
|
446
|
-
LittleEndian.fromLittleEndianU8(
|
|
447
|
-
LittleEndian.toLittleEndianU8(value)
|
|
448
|
-
),
|
|
449
|
-
value
|
|
450
|
-
);
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
function testFuzz_RoundtripFromLE16(uint16 value) public pure {
|
|
454
|
-
assertEq(
|
|
455
|
-
LittleEndian.fromLittleEndianU16(
|
|
456
|
-
LittleEndian.toLittleEndianU16(value)
|
|
457
|
-
),
|
|
458
|
-
value
|
|
459
|
-
);
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
function testFuzz_RoundtripFromLE32(uint32 value) public pure {
|
|
463
|
-
assertEq(
|
|
464
|
-
LittleEndian.fromLittleEndianU32(
|
|
465
|
-
LittleEndian.toLittleEndianU32(value)
|
|
466
|
-
),
|
|
467
|
-
value
|
|
468
|
-
);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
function testFuzz_RoundtripFromLE64(uint64 value) public pure {
|
|
472
|
-
assertEq(
|
|
473
|
-
LittleEndian.fromLittleEndianU64(
|
|
474
|
-
LittleEndian.toLittleEndianU64(value)
|
|
475
|
-
),
|
|
476
|
-
value
|
|
477
|
-
);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
function testFuzz_RoundtripFromLE128(uint128 value) public pure {
|
|
481
|
-
assertEq(
|
|
482
|
-
LittleEndian.fromLittleEndianU128(
|
|
483
|
-
LittleEndian.toLittleEndianU128(value)
|
|
484
|
-
),
|
|
485
|
-
value
|
|
486
|
-
);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
function testFuzz_RoundtripFromLE256(uint256 value) public pure {
|
|
490
|
-
assertEq(
|
|
491
|
-
LittleEndian.fromLittleEndianU256(
|
|
492
|
-
LittleEndian.toLittleEndianU256(value)
|
|
493
|
-
),
|
|
494
|
-
value
|
|
495
|
-
);
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
function testFuzz_RoundtripFromLEi16(int16 value) public pure {
|
|
499
|
-
assertEq(
|
|
500
|
-
LittleEndian.fromLittleEndianI16(
|
|
501
|
-
LittleEndian.toLittleEndianI16(value)
|
|
502
|
-
),
|
|
503
|
-
value
|
|
504
|
-
);
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
function testFuzz_RoundtripFromLEi32(int32 value) public pure {
|
|
508
|
-
assertEq(
|
|
509
|
-
LittleEndian.fromLittleEndianI32(
|
|
510
|
-
LittleEndian.toLittleEndianI32(value)
|
|
511
|
-
),
|
|
512
|
-
value
|
|
513
|
-
);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
function testFuzz_RoundtripFromLEi64(int64 value) public pure {
|
|
517
|
-
assertEq(
|
|
518
|
-
LittleEndian.fromLittleEndianI64(
|
|
519
|
-
LittleEndian.toLittleEndianI64(value)
|
|
520
|
-
),
|
|
521
|
-
value
|
|
522
|
-
);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
function testFuzz_RoundtripFromLEi128(int128 value) public pure {
|
|
526
|
-
assertEq(
|
|
527
|
-
LittleEndian.fromLittleEndianI128(
|
|
528
|
-
LittleEndian.toLittleEndianI128(value)
|
|
529
|
-
),
|
|
530
|
-
value
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
function testFuzz_RoundtripFromLEi256(int256 value) public pure {
|
|
535
|
-
assertEq(
|
|
536
|
-
LittleEndian.fromLittleEndianI256(
|
|
537
|
-
LittleEndian.toLittleEndianI256(value)
|
|
538
|
-
),
|
|
539
|
-
value
|
|
540
|
-
);
|
|
541
|
-
}
|
|
542
|
-
}
|