
UtilsTest.pudu
Pudu45 lines1.9 KB
1/** @Test.Utils.Suite — templates, shared state, and thread reasons */2module PuduLangMediator.Utils.UtilsTest34import Std.Concurrent as Concurrent5import Std.Io as Io6import Std.Test as Test7import PuduLangMediator.Utils.Shared as Shared8import PuduLangMediator.Utils.Template as Template9import PuduLangMediator.Utils.Threads as Threads101112fn main() -> Int {13 let counter = Shared.shared(0)14 var workers: Array[Concurrent.Task] = []15 var index = 016 while index < 8 {17 if let Ok(worker) = Concurrent.start(fn() -> () {18 var turn = 019 while turn < 100 {20 Shared.update(&counter, |held: Int| held + 1)21 turn = turn + 122 }23 }) { workers = workers.push(worker) }24 index = index + 125 }26 let _joined = Concurrent.joinAll(&workers)27 let swapped = Shared.change(&counter, fn(held: Int) -> (Int, Int) { (0, held) })28 let crashed = Concurrent.contain(fn() -> () { panic("boom") })29 let checks = Test.suite("Utils", &[30 Test.equals("slots are filled in order", &Template.fill("<1> and <2>", &["a", "b"]), &"a and b"),31 Test.equals("a slot without a value is kept", &Template.fill("<1> <3>", &["a"]), &"a <3>"),32 Test.equals("a filled value is not filled again", &Template.fill("<1>", &["<1>"]), &"<1>"),33 Test.equals("text that is not a slot stays", &Template.fill("a < b > c <x>", &["z"]), &"a < b > c <x>"),34 Test.equals("no concurrent update is lost", &swapped, &800),35 Test.equals("a change answers its second half and stores its first", &Shared.current(&counter), &0),36 Test.equals("a stopped thread's message is its reason", &(match crashed { case Err(problem) => Threads.reasonOf(&problem) case Ok(_) => "" }), &"boom"),37 Test.equals("an error without a message is shown", &Threads.reasonOf(&Concurrent.Missing), &"Missing")38 ])39 let ran = Test.run(&checks)40 for failure in Test.failuresOf(&ran) {41 let _reported = Io.writeErrorLine(failure)42 }43 Test.report(&ran)44}45