Files

146 lines
5.8 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
#! @file reading.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the tests of reading files on threads, which must read them as one
#! thread does: each word given the same ID, each file the same tokens and
#! nodes, and the same notes heard of each, and of hearing notes as they are
#! made, rather than finding them kept.
import std::testing;
module sakura::analyzer;
#! What reading files made: the names, each file's syntax and `document` node,
#! what was noted of each, and how many times each was heard read whole.
struct read_whole {
var names: names;
var sources: vector<source>;
var syntaxes: vector<syntax>;
var roots: vector<u32>;
var notes: vector<vector<note>>;
var reads: vector<u32>;
}
#! Reads files on a number of threads.
#! @param threads How many threads read them.
#! @param texts The files' texts.
#! @returns What reading them made.
def read_on(threads: u32, texts: span<strview>): read_whole {
var made := read_whole {
names = names::new(),
sources = vector::<source>::new(),
syntaxes = vector::<syntax>::new(),
roots = vector::<u32>::new(),
notes = vector::<vector<note>>::new(),
reads = vector::<u32>::new()
};
made.sources.reserve(texts.length);
made.syntaxes.reserve(texts.length);
for i in 0..texts.length {
made.sources.append(source::from("test.sakura", texts[i]));
made.syntaxes.append(syntax::new());
}
made.notes.resize(texts.length);
made.reads.resize(texts.length);
# What is heard of a file is heard on the thread reading it, so each file's
# vector is written by one thread alone.
val notes := &mut made.notes;
val reads := &mut made.reads;
val heard := listener {
noted = def(each: &mut note): void { notes[each.file as u64].append(move(each)); },
read = def(file: u32): void { reads[file as u64] += 1; }
};
read_files(&made.sources, &mut made.syntaxes, &mut made.roots, &mut made.names, threads, &heard, 0, texts.length);
return made;
}
#! Whether two readings made the same: names, tokens, nodes, and notes.
#! @param a One reading.
#! @param b The other.
#! @returns Whether they did.
def read_alike(a: &read_whole, b: &read_whole): bool {
return false if a.names.count != b.names.count;
for id in 0..a.names.count {
return false if a.names.text(id as u32) != b.names.text(id as u32);
}
for j in 0..a.syntaxes.length {
val x := &a.syntaxes.at(j);
val y := &b.syntaxes.at(j);
return false if a.roots.at(j) != b.roots.at(j) || x.token_count != y.token_count || x.node_count != y.node_count;
for k in 0..x.token_count {
val p := x.token(k as u32, &a.names);
val q := y.token(k as u32, &b.names);
return false if p.kind != q.kind || p.offset != q.offset || p.name != q.name;
}
for k in 0..x.node_count {
return false if x.node_kinds.at(k) != y.node_kinds.at(k) || x.node_tokens.at(k) != y.node_tokens.at(k);
}
return false if a.notes.at(j).length != b.notes.at(j).length || a.reads.at(j) != 1 || b.reads.at(j) != 1;
for k in 0..a.notes.at(j).length {
val p := &a.notes.at(j).at(k);
val q := &b.notes.at(j).at(k);
return false if p.offset != q.offset || p.code != q.code || p.length != q.length;
}
}
return true;
}
@test def files_read_on_threads_are_read_as_one_thread_reads_them(t: &mut tester): void {
# The files share some names and not others, and first use them in other
# orders, and some have mistakes, so what joins the names when, and what is
# heard of which file, would show.
val texts: strview[9] = [
"def alpha(beta: i64): i64 { return beta + gamma; }\nval gamma: i64 = 3;\n",
"struct point { var x: f64; var y: f64; }\ndef length(p: point): f64 { return p.x * p.x + p.y * p.y; }\n",
"def gamma2(): void { var delta := alpha(1); delta = delta * 2; }\n",
"def broken(: i64 { return 1 }\nval ok := 1;\n",
"enum colour { red, green, blue }\ndef pick(c: colour): u8 { return c == colour::red ? 1 : 2; }\n",
"def lexed(): void { val a := 1 @ 2; }\n",
"def x(): i64 { return y() + z(); }\ndef y(): i64 { return 1; }\ndef z(): i64 { return 2; }\n",
"def words(): strview { return $\"{alpha(2)} and {beta}\"; }\n",
"val last := point { x = 1.0, y = 2.0 };\ndef zeta(): void {}\n"
];
val one := read_on(1, texts[:]);
t.expects(one.notes.at(3).length != 0 && one.notes.at(5).length != 0);
for threads in 2..6u {
val many := read_on(threads as u32, texts[:]);
t.expects(read_alike(&one, &many));
}
}
@test def notes_heard_as_they_are_made_are_not_kept(t: &mut tester): void {
var names := names::new();
var notes := vector::<note>::new();
var heard := vector::<note>::new();
var syntax := syntax::new();
val source := source::from("test.sakura", "def f(): void {}\ndef g(): void { val a := 1 @ 2; }\ndef f(): void {}\n");
val hear := def(each: &mut note): void { heard.append(move(each)); };
var lexing := lexer::new(&source, &mut names, &mut notes, 0);
lexing.sink.heard = hear;
lexing.tokenize_all(&mut syntax);
var parsing := parser::new(&mut syntax, &names, &mut notes, 0);
parsing.sink.heard = hear;
val root := parsing.parse();
var checker := checker::new(&mut names, &mut notes);
checker.sink.heard = hear;
checker.add(&source, &syntax, root);
val read := heard.length;
checker.check();
# The lexer's note first, and the checker's of `f` declared twice, which says
# where it was first declared, as one kept would.
t.expects(notes.length == 0 && read != 0 && heard.length > read);
t.expects(heard.at(0).code == ec::token);
var related := false;
for &each in heard {
if each.code == ec::duplicate {
related = each.related.length != 0 && each.related_length != 0 && each.related_offset < each.offset;
}
}
t.expects(related);
t.expects(checker.notes_made as u64 == heard.length - read);
}