76 lines
2.6 KiB
Plaintext
76 lines
2.6 KiB
Plaintext
# Tests of std's scanning of text, which an executable does sixteen bytes at a
|
|||
|
|
# time: what it looks for at every place, so on either side of where each
|
||
|
|
# sixteen ends, against the same work done a byte at a time.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import std::testing;
|
||
|
|
|
||
|
|
# The text of a length whose bytes at the places a pattern sets are a byte, and
|
||
|
|
# letters otherwise.
|
||
|
|
def text_with(length: u64, pattern: u64, byte: u8): string {
|
||
|
|
var made := string::new(nothing {});
|
||
|
|
for i in 0..length {
|
||
|
|
val set := ((pattern >> (i % 61)) & 1) != 0;
|
||
|
|
made.push(set ? byte : ('a' as u64 + i % 26) as u8);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def splitting_finds_every_delimiter_wherever_it_falls(t: &mut tester): void {
|
||
|
|
for length in 0..80u {
|
||
|
|
for pattern in [0 as u64, 1, 0x8000, 0x10001, 0x5555555555555555, 0xFFFFFFFFFFFFFFFF, 0x0123456789ABCDEF] {
|
||
|
|
val text := text_with(length, pattern, ',');
|
||
|
|
val parts := text.view().split(',').to_vector();
|
||
|
|
# Each part, joined by the delimiter, is the text again, and none holds it.
|
||
|
|
var joined := string::new(nothing {});
|
||
|
|
var clean := true;
|
||
|
|
for i in 0..parts.length {
|
||
|
|
if i != 0 {
|
||
|
|
joined.push(',');
|
||
|
|
}
|
||
|
|
joined.append(parts[i]);
|
||
|
|
for k in 0..parts[i].length {
|
||
|
|
clean = clean && parts[i][k] != ',';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
t.expects(joined.view() == text.view() && clean);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def text_compares_as_its_bytes_do(t: &mut tester): void {
|
||
|
|
for length in 0..70u {
|
||
|
|
val a := text_with(length, 0x0F0F0F0F0F0F0F0F, 'x');
|
||
|
|
t.expects(a.view().compare(a.view()) == 0);
|
||
|
|
for place in 0..length {
|
||
|
|
for byte in [0 as u8, 'a', 'x', 0x80, 0xFF] {
|
||
|
|
var b := text_with(length, 0x0F0F0F0F0F0F0F0F, 'x');
|
||
|
|
b[place] = byte;
|
||
|
|
val was: u8 = a[place];
|
||
|
|
val want: i64 = was == byte ? 0 : (was < byte ? -1 : 1);
|
||
|
|
t.expects(a.view().compare(b.view()) == want);
|
||
|
|
}
|
||
|
|
# A prefix orders first.
|
||
|
|
t.expects(a.view().substr(0, place).compare(a.view()) == -1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def lines_are_read_wherever_their_breaks_fall(t: &mut tester): void {
|
||
|
|
for length in 0..70u {
|
||
|
|
val text := text_with(length, 0x2104210421042104, '\n');
|
||
|
|
var input := io::reader_of(text.view());
|
||
|
|
var seen := string::new(nothing {});
|
||
|
|
for line in input.lines() {
|
||
|
|
seen.append(line.view());
|
||
|
|
seen.push('\n');
|
||
|
|
}
|
||
|
|
# The lines, each with its break, are the text, but for a last line without one.
|
||
|
|
var want := string::new(nothing {});
|
||
|
|
want.append(text.view());
|
||
|
|
if want.length != 0 && want[want.length - 1] != '\n' {
|
||
|
|
want.push('\n');
|
||
|
|
}
|
||
|
|
t.expects(seen.view() == want.view());
|
||
|
|
}
|
||
|
|
}
|