Skip to content

Commit ee9eefd

Browse files
authored
math.unsigned: fix put_bytes(), add test (#25718)
1 parent eb4363b commit ee9eefd

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

vlib/math/unsigned/uint128.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ pub fn (u_ Uint128) str() string {
413413

414414
// put_bytes stores u in b in little-endian order
415415
pub fn (u Uint128) put_bytes(mut b []u8) {
416-
binary.little_endian_put_u64(mut b, u.lo)
417-
binary.little_endian_put_u64(mut b, u.hi)
416+
binary.little_endian_put_u64(mut b[..8], u.lo)
417+
binary.little_endian_put_u64(mut b[8..], u.hi)
418418
}
419419

420420
// uint128_from_64 converts v to a Uint128 value

vlib/math/unsigned/uint128_test.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math.big
22
import math.unsigned
3+
import rand
34

45
fn test_str() {
56
x := unsigned.uint128_from_dec_str('170141183460469231713240559642174554112') or { panic('') }
@@ -241,3 +242,13 @@ fn test_div_128() {
241242
}
242243
}
243244
}
245+
246+
fn test_put_bytes() {
247+
a := unsigned.uint128_new(rand.u64(), rand.u64())
248+
b := a.reverse_bytes()
249+
mut buf_a := []u8{len: 16}
250+
mut buf_b := []u8{len: 16}
251+
a.put_bytes(mut buf_a)
252+
b.put_bytes(mut buf_b)
253+
assert buf_a == buf_b.reverse()
254+
}

0 commit comments

Comments
 (0)