Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+315
View File
@@ -0,0 +1,315 @@
# Tests of words of bytes, `core::swar`: every test against the same choice made a
# byte at a time, for every value in every place, and the text functions that go
# by words against their loops a byte at a time, for every length to past a few
# words, wherever the bytes they look for fall.
import std::serial;
import core::swar;
import std::testing;
var swar_seed: u64 = 0x2545F4914F6CDD1D;
def swar_random(): u64 {
swar_seed = swar_seed ^ (swar_seed << 13);
swar_seed = swar_seed ^ (swar_seed >> 7);
swar_seed = swar_seed ^ (swar_seed << 17);
return swar_seed;
}
# What a search found, or a number where it found nothing.
def swar_or(found: maybe<u64>, none: u64): u64 {
if found is some(at) {
return at;
}
return none;
}
# The mask choosing the bytes of a word a test chooses, made a byte at a time.
def swar_choice(word: u64, test: u64, a: u8, b: u8): u64 {
var mask: u64 = 0;
for k in 0..8u {
val byte := ((word >> (k * 8)) & 0xFF) as u8;
val chosen := match test {
0 => byte == 0,
1 => byte == a,
2 => byte < a,
3 => byte > a,
4 => byte >= a && byte <= b,
else => byte < 0x80,
};
if chosen {
mask = mask | (0x80u << (k * 8));
}
}
return mask;
}
# The same choice by words.
def swar_chosen(word: swar::bytes8, test: u64, a: u8, b: u8): u64 {
return match test {
0 => word.zero(),
1 => word.equal(a),
2 => word.below(a),
3 => word.above(a),
4 => word.within(a, b),
else => word.ascii(),
};
}
@test def swar_tests_choose_exactly_the_bytes_they_say(t: &mut tester): void {
# Bounds at their edges, and bytes the tests are given.
val bounds: u8[12] = [0, 1, 9, 13, 32, 127, 128, 129, 200, 254, 255, '0'];
var wrong: u64 = 0;
for place in 0..8u {
for value in 0..256u {
# Each value in each place, with the other places random, and sometimes a
# copy of the value, so a borrow from one byte would reach the next.
var word := swar_random();
if (value & 3) == 0 {
word = word & 0x00FF00FF00FF00FF;
}
word = (word & ~(0xFFu << (place * 8))) | (value << (place * 8));
if swar_chosen(word, 0, 0, 0) != swar_choice(word, 0, 0, 0) { wrong++; }
if swar_chosen(word, 5, 0, 0) != swar_choice(word, 5, 0, 0) { wrong++; }
for i in 0..12u {
val a := bounds[i];
val b := bounds[(i + 3) % 12];
for test in 1..5u {
if swar_chosen(word, test, a, b) != swar_choice(word, test, a, b) { wrong++; }
}
# The byte itself is equal, and within itself.
if swar_chosen(word, 1, value as u8, 0) != swar_choice(word, 1, value as u8, 0) { wrong++; }
if swar_chosen(word, 4, value as u8, value as u8) != swar_choice(word, 4, value as u8, value as u8) { wrong++; }
}
}
}
t.expects(wrong == 0);
}
@test def swar_masks_say_where_and_how_many_they_choose(t: &mut tester): void {
var wrong: u64 = 0;
for bits in 0..256u {
var mask: u64 = 0;
var spread: u64 = 0;
var first: u64 = 8;
var last: u64 = 8;
var count: u64 = 0;
for k in 0..8u {
if ((bits >> k) & 1) != 0 {
mask = mask | (0x80u << (k * 8));
spread = spread | (0xFFu << (k * 8));
first = first == 8 ? k : first;
last = k;
count++;
}
}
val chosen: swar::mask8 = mask;
val rest: u64 = first == 8 ? 0 : mask & ~(0x80u << (first * 8));
if chosen.any() != (bits != 0) { wrong++; }
if chosen.first() != first || chosen.last() != last || chosen.count() != count { wrong++; }
if chosen.rest() != rest || chosen.spread() != spread { wrong++; }
}
t.expects(wrong == 0);
for count in 0..10u {
val want: u64 = count >= 8 ? 0x8080808080808080 : 0x8080808080808080 & ((1u << (count * 8)) - 1);
t.expects(swar::mask8::leading(count) == want);
}
}
@test def swar_words_change_case_and_read_digits(t: &mut tester): void {
var wrong: u64 = 0;
for place in 0..8u {
for value in 0..256u {
val word: swar::bytes8 = (swar_random() & ~(0xFFu << (place * 8))) | (value << (place * 8));
for k in 0..8u {
val byte := word.byte(k);
if word.upper().byte(k) != byte.to_upper || word.lower().byte(k) != byte.to_lower { wrong++; }
}
}
}
t.expects(wrong == 0);
t.expects(swar::repeat(0xAB) == 0xABABABABABABABAB && swar::repeat(0) == 0);
t.expects(swar::choose(0x0000800000000080, 0x1111111111111111, 0x2222222222222222) == 0x2222112222222211);
t.expects(swar_or(swar::load("20261016", 0).digits(), 1) == 20261016);
t.expects(swar_or(swar::load("00000000", 0).digits(), 1) == 0);
t.expects(swar_or(swar::load("99999999", 0).digits(), 1) == 99999999);
t.expects(swar_or(swar::load("01234567", 0).digits(), 1) == 1234567);
# Each place not a digit in turn, by a byte on either side of the digits.
for place in 0..8u {
for byte in ['/' as u8, ':', 'a', ' ', 0, 0xB0] {
var text := string::from("12345678", nothing {});
text[place] = byte;
t.expects(swar::load(text.view(), 0).digits() is none);
}
}
}
@test def swar_loads_read_only_the_bytes_of_the_view(t: &mut tester): void {
val buffer: strview = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for start in 0..8u {
for length in 1..12u {
# A view whose next byte is set, so a read past its end would show.
val view := buffer.substr(start, length);
for at in 0..length {
var want: u64 = 0;
val left := length - at;
for k in 0..(left < 8 ? left : 8) {
want = want | (('A' as u64 + start + at + k) << (k * 8));
}
t.expects(swar::load_last(view, at) == want);
if left >= 8 {
t.expects(swar::load(view, at) == want);
}
}
}
}
t.expects(swar::load_last("".data, 0) == 0);
}
# Text of a length over a few bytes, so each is found often, after `start` bytes
# of a buffer, which then views it.
def swar_text(buffer: &mut string, start: u64, length: u64, alphabet: strview): strview {
buffer.clear();
for _ in 0..start {
buffer.push('#');
}
for _ in 0..length {
buffer.push(alphabet[swar_random() % alphabet.length]);
}
buffer.push('#');
return buffer.view().substr(start, length);
}
@test def text_functions_find_what_a_byte_at_a_time_finds(t: &mut tester): void {
var buffer := string::new(nothing {});
var wrong: u64 = 0;
for length in 0..41u {
for start in 0..8u {
val text := swar_text(&mut buffer, start, length, length % 3 == 0 ? "abcdefgh" : "ab");
for byte in ['a' as u8, 'b', 'h', '#', 0] {
var first: u64 = length;
var last: u64 = length;
var count: u64 = 0;
for k in 0..length {
if text[k] == byte {
first = first == length ? k : first;
last = k;
count++;
}
}
val found := text.index_of(&byte);
val found_last := text.last_index_of(&byte);
if swar_or(found, length) != first { wrong++; }
if swar_or(found_last, length) != last { wrong++; }
if text.count_of(&byte) != count || text.contains(&byte) != (count != 0) { wrong++; }
}
# Runs of the text and others, and any of a few bytes.
for size in 0..6u {
var sought := string::new(nothing {});
val from := length > size ? swar_random() % (length - size + 1) : 0;
for k in 0..size {
sought.push(k + from < length && (swar_random() & 3) != 0 ? text[k + from] : 'a');
}
var want: u64 = length + 1;
if sought.length <= length {
for at in 0..(length - sought.length + 1) {
if text.substr(at, sought.length) == sought.view() {
want = at;
break;
}
}
}
val got := text.find(sought.view());
if swar_or(got, length + 1) != want { wrong++; }
var any: u64 = length;
for at in 0..length {
if sought.view().contains(&text[at]) {
any = at;
break;
}
}
val got_any := text.find_any(sought.view());
if swar_or(got_any, length) != any { wrong++; }
}
}
}
t.expects(wrong == 0);
# More than four bytes are looked for through a set.
val text: strview = "the quick brown fox jumps over the lazy dog";
t.expects(swar_or(text.find_any("xyzq!"), 99) == 4 && text.find_any("QWERTY") is none);
t.expects(swar_or(text.find("the"), 99) == 0 && swar_or(text.find("lazy"), 99) == 35);
t.expects(swar_or(text.find("dog"), 99) == 40 && text.find("dogs") is none);
t.expects(swar_or(text.find(""), 99) == 0 && swar_or("".find(""), 99) == 0 && "".find("a") is none);
}
@test def text_functions_change_and_check_as_a_byte_at_a_time_does(t: &mut tester): void {
var buffer := string::new(nothing {});
var wrong: u64 = 0;
for length in 0..41u {
for start in 0..8u {
val text := swar_text(&mut buffer, start, length, "aZz@[`{ \t\n\r\x0B\x0C9");
val upper := text.upper();
val lower := text.lower();
for k in 0..length {
if upper[k] != text[k].to_upper || lower[k] != text[k].to_lower { wrong++; }
}
if upper.length != length || lower.length != length { wrong++; }
# Trimming, where the text is mostly whitespace.
var left: u64 = 0;
while left < length && text[left].is_space { left++; }
var right := length;
while right > 0 && text[right - 1].is_space { right--; }
if text.ltrim() != text.substr(left, length - left) { wrong++; }
if text.rtrim() != text.substr(0, right) { wrong++; }
if text.trim() != (left < right ? text.substr(left, right - left) : text.substr(0, 0)) { wrong++; }
# ASCII, and a byte past it in each place.
if !text.is_ascii() || !serial::is_utf8(text) { wrong++; }
for place in 0..length {
var wide := string::from(text, nothing {});
wide[place] = 0x80 | (place as u8);
if wide.view().is_ascii() || serial::is_utf8(wide.view()) { wrong++; }
wide[place] = 0xC3;
if place + 1 < length {
wide[place + 1] = 0xA9;
if !serial::is_utf8(wide.view()) { wrong++; }
}
}
}
}
t.expects(wrong == 0);
# Whitespace of every length on either side.
for spaces in 0..20u {
var text := string::new(nothing {});
for k in 0..spaces { text.push(k % 3 == 0 ? '\t' : ' '); }
text.append("word");
for k in 0..spaces { text.push(k % 2 == 0 ? '\n' : ' '); }
t.expects(text.view().trim() == "word" && text.view().ltrim().length == 4 + spaces && text.view().rtrim().length == 4 + spaces);
}
}
# Where a run of text is found, while compiling and while running.
def swar_found(text: strview, sought: strview): u64 {
return swar_or(text.find(sought), text.length);
}
def swar_date(text: strview): u64 {
return swar_or(swar::load(text, 0).digits(), 0);
}
const swar_compiled_place: u64 = swar_found("a needle in a haystack of needles", "haystack");
const swar_compiled_date: u64 = swar_date("20261016");
const swar_compiled_count: u64 = "one\ntwo\nthree\nfour\n".count_of(&'\n');
@test def swar_words_work_alike_while_compiling(t: &mut tester): void {
t.expects(swar_compiled_place == 14 && swar_compiled_place == swar_found("a needle in a haystack of needles", "haystack"));
t.expects(swar_compiled_date == 20261016 && swar_compiled_date == swar_date("20261016"));
t.expects(swar_compiled_count == 4);
}