diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-10-14 23:30:12 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-10-14 23:30:38 +0200 |
| commit | ee93f48729e0cd7a2ba79ea9d2a3b13a3bbe5a40 (patch) | |
| tree | 7bc4900efe09cdf906d0e12b07726eedb6a13387 /tests | |
| parent | 347eae3a66a1e528254b1742162e1d17318fa434 (diff) | |
core:math/bits: Finish docs, add `bitfield_extract` + `bitfield_insert` test.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/core/math/bits/test_core_math_bits.odin | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/core/math/bits/test_core_math_bits.odin b/tests/core/math/bits/test_core_math_bits.odin index bd9d762aa..fd9ee24c9 100644 --- a/tests/core/math/bits/test_core_math_bits.odin +++ b/tests/core/math/bits/test_core_math_bits.odin @@ -1,6 +1,7 @@ package test_core_math_bits import "core:math/bits" +import "core:math/rand" import "core:testing" @test @@ -145,4 +146,26 @@ test_rotate :: proc(t: ^testing.T) { } testing.expect_value(t, r, 0b1101) } +} + +@test +test_insert_extract :: proc(t: ^testing.T) { + // replace 1..=8 bits in a random 64-bit number at all possible offsets + // extract them again, and compare to the original insert + for pattern_to_insert in u64(1)..<255 { + base := rand.uint64() + bit_count := uint(bits.len(pattern_to_insert)) + max_offset := uint(64) - bit_count + for offset in uint(0)..<max_offset { + replaced := bits.bitfield_insert(base, pattern_to_insert, offset, bit_count) + extracted := bits.bitfield_extract(replaced, offset, bit_count) + + testing.expect_value(t, extracted, pattern_to_insert) + + // Test that the original number and the replaced number + // are the same, except for the replaced bits + mask := ~(u64(1<<bit_count - 1) << offset) + testing.expect_value(t, base & mask, replaced & mask) + } + } }
\ No newline at end of file |