.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
mem/
heap/
heap.rad
5.8 KiB
tests.rad
6.2 KiB
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/mem/tests.rad
raw
| 1 | //! Tests for byte access and memory operations. |
| 2 | |
| 3 | use std::mem; |
| 4 | use std::testing; |
| 5 | |
| 6 | /// Fixed-width accesses use little-endian byte order at every alignment. |
| 7 | @test fn integerBytes() throws (testing::TestError) { |
| 8 | for offset in 0 .. 16 { |
| 9 | let mut bytes: [u8; 24] = [0xa5; 24]; |
| 10 | mem::store16Le(&mut bytes[.. offset + 2], offset, 0x80ff); |
| 11 | assert mem::load16Le(&bytes[.. offset + 2], offset) as u64 == 0x80ff; |
| 12 | for j in 0 .. bytes.len { |
| 13 | let expected: u8 = 0xa5 |
| 14 | if j < offset or j >= offset + 2 |
| 15 | else (0x80ff as u64 >> ((j - offset) * 8) as u64) as u8; |
| 16 | assert bytes[j] == expected; |
| 17 | } |
| 18 | mem::store32Le(&mut bytes[.. offset + 4], offset, 0x89abcdef); |
| 19 | assert mem::load32Le(&bytes[.. offset + 4], offset) as u64 == 0x89abcdef; |
| 20 | for j in 0 .. bytes.len { |
| 21 | let expected: u8 = 0xa5 |
| 22 | if j < offset or j >= offset + 4 |
| 23 | else (0x89abcdef as u64 >> ((j - offset) * 8) as u64) as u8; |
| 24 | assert bytes[j] == expected; |
| 25 | } |
| 26 | mem::store64Le(&mut bytes[.. offset + 8], offset, 0x8877665544332211); |
| 27 | assert mem::load64Le(&bytes[.. offset + 8], offset) == 0x8877665544332211; |
| 28 | for j in 0 .. bytes.len { |
| 29 | let expected: u8 = 0xa5 |
| 30 | if j < offset or j >= offset + 8 |
| 31 | else (0x8877665544332211 as u64 >> ((j - offset) * 8) as u64) as u8; |
| 32 | assert bytes[j] == expected; |
| 33 | } |
| 34 | mem::store64Le(&mut bytes[..], offset, 0); |
| 35 | assert mem::load64Le(&bytes[..], offset) == 0; |
| 36 | mem::store64Le(&mut bytes[..], offset, mem::U64_MAX); |
| 37 | assert mem::load64Le(&bytes[..], offset) == mem::U64_MAX; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// Copy, equality and comparison handle word boundaries and every mismatch. |
| 42 | @test fn byteSequences() throws (testing::TestError) { |
| 43 | let mut source: [u8; 80] = [0; 80]; |
| 44 | for i in 0 .. source.len { |
| 45 | set source[i] = (i * 7) as u8; |
| 46 | } |
| 47 | for start in 0 .. 8 { |
| 48 | for length in 0 .. 65 { |
| 49 | let mut target: [u8; 80] = [0xff; 80]; |
| 50 | let input = &source[start .. start + length]; |
| 51 | assert try! mem::copy(&mut target[1 ..], input) == length; |
| 52 | assert target[0] == 0xff; |
| 53 | for i in 0 .. length { |
| 54 | assert target[1 + i] == source[start + i]; |
| 55 | } |
| 56 | assert target[length + 1] == 0xff; |
| 57 | assert mem::eq(input, &target[1 .. length + 1]); |
| 58 | assert mem::cmp(input, &target[1 .. length + 1]) == 0; |
| 59 | for i in 0 .. length { |
| 60 | let saved = target[1 + i]; |
| 61 | set target[1 + i] = saved ^ 0x80; |
| 62 | let order: i32 = -1 if saved < (saved ^ 0x80) else 1; |
| 63 | assert not mem::eq(input, &target[1 .. length + 1]); |
| 64 | assert mem::cmp(input, &target[1 .. length + 1]) == order; |
| 65 | assert mem::cmp(&target[1 .. length + 1], input) == -order; |
| 66 | set target[1 + i] = saved; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | let mut tooSmall: [u8; 7] = [0xa5; 7]; |
| 71 | let mut failed = false; |
| 72 | try mem::copy(&mut tooSmall[..], &source[.. 8]) catch { |
| 73 | set failed = true; |
| 74 | }; |
| 75 | assert failed; |
| 76 | for byte in tooSmall { |
| 77 | assert byte == 0xa5; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Variable-width encoders preserve byte order and the surrounding bytes. |
| 82 | @test fn variableIntegerBytes() throws (testing::TestError) { |
| 83 | let value: u64 = 0x8877665544332211; |
| 84 | for offset in 0 .. 16 { |
| 85 | for size in 0 .. 9 { |
| 86 | let mut bytes: [u8; 24] = [0xa5; 24]; |
| 87 | mem::writeLe(&mut bytes[..], offset, value, size); |
| 88 | let mut expected: u64 = 0; |
| 89 | for i in 0 .. bytes.len { |
| 90 | if i < offset or i >= offset + size { |
| 91 | assert bytes[i] == 0xa5; |
| 92 | } else { |
| 93 | let shift = ((i - offset) * 8) as u64; |
| 94 | assert bytes[i] == (value >> shift) as u8; |
| 95 | set expected |= bytes[i] as u64 << shift; |
| 96 | } |
| 97 | } |
| 98 | assert mem::readLe(&bytes[..], offset, size) == expected; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Bulk fills preserve guard bytes at every alignment and vector boundary. |
| 104 | @test fn bulkFills() throws (testing::TestError) { |
| 105 | let pattern: [u8; 3] = [0x12, 0x80, 0xff]; |
| 106 | for start in 0 .. 8 { |
| 107 | for length in &[0 as u32, 1, 63, 64, 65, 255, 256, 257, 513] { |
| 108 | let mut bytes: [u8; 536] = [0xa5; 536]; |
| 109 | mem::fill(&mut bytes[start .. start + length], 0x73); |
| 110 | for byte, i in &bytes[..] { |
| 111 | assert byte == (0x73 if i >= start and i < start + length else 0xa5); |
| 112 | } |
| 113 | } |
| 114 | let source: [u8; 536] = [0x71; 536]; |
| 115 | for length in &[0 as u32, 1, 63, 64, 65, 255, 256, 257, 513] { |
| 116 | let mut bytes: [u8; 536] = [0xa5; 536]; |
| 117 | assert try! mem::copy( |
| 118 | &mut bytes[start .. start + length], &source[.. length] |
| 119 | ) == length; |
| 120 | for byte, i in &bytes[..] { |
| 121 | assert byte == (0x71 if i >= start and i < start + length else 0xa5); |
| 122 | } |
| 123 | } |
| 124 | for count in &[0 as u32, 1, 63, 64, 65, 255, 256, 257] { |
| 125 | let length = count * 3; |
| 126 | let mut bytes: [u8; 792] = [0xa5; 792]; |
| 127 | mem::fillPattern(&mut bytes[start .. start + length], &pattern[..]); |
| 128 | for byte, i in &bytes[..] { |
| 129 | let expected = pattern[(i - start) % 3] |
| 130 | if i >= start and i < start + length |
| 131 | else 0xa5; |
| 132 | assert byte == expected; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /// Pattern fills cover each serializer width and whole-pattern copies. |
| 139 | @test fn bulkPatterns() throws (testing::TestError) { |
| 140 | let pattern: [u8; 16] = [ |
| 141 | 0, 1, 0x80, 0xff, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 142 | ]; |
| 143 | for width in &[1 as u32, 2, 4, 8, 16] { |
| 144 | for count in &[0 as u32, 1, 2, 65] { |
| 145 | let length = width * count; |
| 146 | let mut bytes: [u8; 1042] = [0xa5; 1042]; |
| 147 | mem::fillPattern(&mut bytes[1 .. length + 1], &pattern[.. width]); |
| 148 | assert bytes[0] == 0xa5 and bytes[length + 1] == 0xa5; |
| 149 | for i in 0 .. length { |
| 150 | assert bytes[i + 1] == pattern[i % width]; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |