Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+204
View File
@@ -0,0 +1,204 @@
#! @file corpus.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the runner of the corpus, the programs of `tests/programs`: each is
#! run by a command given, and what it writes and how it stops are compared
#! with `NAME.out` beside it, so the C compiler and the rebuild are held to one
#! corpus. Run it from the root of the repository:
#!
#! sakura run --lib std=STD tools/corpus.sakura -- [--write] [--only TEXT] [--dir DIR] COMMAND...
#!
#! The command is given each program's path after its own words, as
#! `sakura run --lib std=STD`, the C compiler's `sakura run --std STD`, or the
#! rebuild's `entry run --lib std=STD` built as an executable. `--write` writes what each program gives as its `NAME.out`
#! rather than comparing, as the C compiler does to make them, `--only TEXT`
#! runs only the programs whose name holds the text, and `--dir DIR` runs those
#! of another directory: `tests/checked`, whose programs stop only where the
#! program checks, as `run` and `build --checks` make it, and `tests/memory`,
#! whose programs are run with `run --checks`.
#!
#! `NAME.out` holds what the program wrote to its output, and, where it stopped
#! with any status but 0 or wrote to its error, a line `--- exit N`, or
#! `--- signal N`, after it, then what it wrote to its error.
import std::io;
import std::process;
import system;
#! Where the programs are, from the root of the repository, unless `--dir`
#! names another directory.
const corpus: strview = "tests/programs";
def main(args: span<strview>): void {
var writes := false;
var only := string::new(nothing {});
var directory: strview = corpus;
var command := vector::<strview>::new();
var i := 0u;
while i < args.length {
val arg := args.at(i);
if command.length == 0 && arg == "--write" {
writes = true;
} else if command.length == 0 && arg == "--only" && i + 1 < args.length {
i++;
only = string::from(args.at(i), nothing {});
} else if command.length == 0 && arg == "--dir" && i + 1 < args.length {
i++;
directory = args.at(i);
} else {
command.append(arg);
}
i++;
}
if command.length == 0 {
system::fd::stderr.put("corpus: give the command each program is run by, as `sakura run --lib std=../std`\n");
exit(2);
}
var names := program_names(directory);
var passed := 0u;
var failed := 0u;
for &name in names {
continue if only.length != 0 && !contains(name.span, only.span);
val program := $"{directory}/{name}.sakura";
val expected_at := $"{directory}/{name}.out";
var got := run(command.span, program.span);
var at := path::parse(expected_at.span) else (_) { continue; };
if writes {
_ = io::write_file(&at, got.span);
continue;
}
var expected := io::read_file(&at) else (_) {
println($"[ FAIL ] {name}: it has no `{name}.out`");
failed++;
continue;
};
if expected.span == got.span {
passed++;
continue;
}
failed++;
println($"[ FAIL ] {name}");
show_difference(expected.span, got.span);
}
if writes {
println($"wrote what {names.length} programs give");
return;
}
println($"{passed} passed, {failed} failed");
exit(failed == 0 ? 0 : 1);
}
#! The name of each program of a directory, without `.sakura`, in order.
#! @param directory The directory.
#! @returns The names.
def program_names(directory: strview): vector<string> {
var made := vector::<string>::new();
var at := path::parse(directory) else (_) { return made; };
var listing := io::directory::open(&at) else (_) { return made; };
while listing.next() is some(each) {
val name := each.name;
continue if name.length <= 7 || name.substr(name.length - 7, 7) != ".sakura";
made.append(string::from(name.substr(0, name.length - 7), nothing {}));
}
# In order, so the runs read the same each time.
var sorted := vector::<string>::new();
for &each in made {
var at := sorted.length;
while at > 0 && sorted.at(at - 1).compare(each.span) > 0 {
at--;
}
sorted.insert_at(at, string::copy_of(&each));
}
return sorted;
}
#! Runs a program by the command, and gives what it wrote and how it stopped,
#! as `NAME.out` holds them.
#! @param command The command's words.
#! @param program The program's path.
#! @returns The text.
def run(command: span<strview>, program: strview): string {
var started := process::command::new(command.at(0));
for i in 1..command.length {
started.argument(command.at(i));
}
started.argument(program);
var made := string::new(nothing {});
val got := started.capture() else (why) {
made.append($"--- not started: {why}\n");
return made;
};
made.append(got.output.span);
var stopped := string::new(nothing {});
if got.status is exited(code) {
if code != 0 {
stopped.append($"--- exit {code}");
}
} else if got.status is signaled(number) {
stopped.append($"--- signal {number}");
}
if stopped.length == 0 && got.error.length != 0 {
stopped.append("--- exit 0");
}
if stopped.length != 0 {
made.append("\n");
made.append(stopped.span);
made.append("\n");
made.append(got.error.span);
}
return made;
}
#! Whether text holds other text.
#! @param text The text searched.
#! @param piece The text looked for.
#! @returns Whether it does.
def contains(text: strview, piece: strview): bool {
return true if piece.length == 0;
return false if piece.length > text.length;
for i in 0..text.length - piece.length + 1 {
return true if text.substr(i, piece.length) == piece;
}
return false;
}
#! Shows the first line where what a program gave differs from what was
#! expected.
#! @param expected What was expected.
#! @param got What it gave.
def show_difference(expected: strview, got: strview): void {
var line := 1u;
var at := 0u;
while at < expected.length && at < got.length && expected.at(at) == got.at(at) {
if expected.at(at) == '\n' {
line++;
}
at++;
}
println($" line {line}:");
println($" expected: {line_at(expected, at)}");
println($" got: {line_at(got, at)}");
}
#! The line of text holding a byte, without its line break.
#! @param text The text.
#! @param at The byte.
#! @returns The line.
def line_at(text: strview, at: u64): strview {
var start := at < text.length ? at : text.length;
while start > 0 && text.at(start - 1) != '\n' {
start--;
}
var end := start;
while end < text.length && text.at(end) != '\n' {
end++;
}
return text.substr(start, end - start);
}