93 lines
2.6 KiB
Plaintext
93 lines
2.6 KiB
Plaintext
# Tests of std's memory routines against the same work done a byte at a time:
|
|||
|
|
# every length up to past where they work 16 bytes at a time, buffers that
|
||
|
|
# overlap either way, and bytes differing at every place.
|
||
|
|
|
||
|
|
import std::math;
|
||
|
|
import std::testing;
|
||
|
|
|
||
|
|
# Fills a buffer with bytes that differ from place to place, its state wrapping
|
||
|
|
# as it's mixed.
|
||
|
|
def scramble(at: *mut u8, count: u64, seed: u64): void {
|
||
|
|
var state := math::wrapping_add(math::wrapping_mul(seed, 0x9E3779B97F4A7C15), 1);
|
||
|
|
for i in 0..count {
|
||
|
|
state = state ^ (state << 13);
|
||
|
|
state = state ^ (state >> 7);
|
||
|
|
state = state ^ (state << 17);
|
||
|
|
at[i] = (state & 0xFF) as u8;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Whether two buffers hold the same bytes.
|
||
|
|
def same_bytes(a: *u8, b: *u8, count: u64): bool {
|
||
|
|
for i in 0..count {
|
||
|
|
return false if a[i] != b[i];
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def copies_agree_with_bytes_where_buffers_overlap_or_not(t: &mut tester): void {
|
||
|
|
val room: u64 = 256;
|
||
|
|
val made := raw_allocate(room);
|
||
|
|
val want := raw_allocate(room);
|
||
|
|
for length in 0..101u {
|
||
|
|
for from in 0..40u {
|
||
|
|
for to in 0..40u {
|
||
|
|
scramble(made, room, length * 131 + from * 7 + to);
|
||
|
|
scramble(want, room, length * 131 + from * 7 + to);
|
||
|
|
# The bytes copied, as `memmove` gives them: read whole before any is written.
|
||
|
|
var held: u8[128];
|
||
|
|
for i in 0..length {
|
||
|
|
held[i] = want[64 + from + i];
|
||
|
|
}
|
||
|
|
for i in 0..length {
|
||
|
|
want[64 + to + i] = held[i];
|
||
|
|
}
|
||
|
|
memcpy(made + 64 + to, made + 64 + from, length);
|
||
|
|
t.expects(same_bytes(made, want, room));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
raw_release(made);
|
||
|
|
raw_release(want);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def setting_agrees_with_bytes(t: &mut tester): void {
|
||
|
|
val room: u64 = 160;
|
||
|
|
val made := raw_allocate(room);
|
||
|
|
val want := raw_allocate(room);
|
||
|
|
for length in 0..101u {
|
||
|
|
for at in 0..20u {
|
||
|
|
scramble(made, room, length + at);
|
||
|
|
scramble(want, room, length + at);
|
||
|
|
for i in 0..length {
|
||
|
|
want[at + i] = 0xA5;
|
||
|
|
}
|
||
|
|
memset(made + at, 0xA5, length);
|
||
|
|
t.expects(same_bytes(made, want, room));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
raw_release(made);
|
||
|
|
raw_release(want);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def comparing_agrees_with_bytes(t: &mut tester): void {
|
||
|
|
val a := raw_allocate(128);
|
||
|
|
val b := raw_allocate(128);
|
||
|
|
for length in 0..101u {
|
||
|
|
scramble(a, 128, length);
|
||
|
|
for i in 0..128u {
|
||
|
|
b[i] = a[i];
|
||
|
|
}
|
||
|
|
t.expects(memcmp(a, b, length) == 0);
|
||
|
|
for place in 0..length {
|
||
|
|
for other in [0 as u8, 0x7F, 0x80, 0xFF] {
|
||
|
|
b[place] = other;
|
||
|
|
val want: i64 = a[place] == other ? 0 : (a[place] < other ? -1 : 1);
|
||
|
|
t.expects(memcmp(a, b, length) == want);
|
||
|
|
}
|
||
|
|
b[place] = a[place];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
raw_release(a);
|
||
|
|
raw_release(b);
|
||
|
|
}
|