Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-mediator

In-process messaging for Pudu: requests, notifications, streams, pipeline behaviors, processors, and exception handling

0.1.0Apache-2.01

InstallClose

ErasureTest.pudu

Pudu47 lines1.9 KB

GitHub ↗
1/** @Test.Utils.Erasure.Suite — packed values read back only through their slot */2module PuduLangMediator.Utils.ErasureTest34import Std.Concurrent as Concurrent5import Std.Io as Io6import Std.Test as Test7import PuduLangMediator.Utils.Erasure as Erasure89/// Runs the suite.10fn main() -> Int {11  let numbers: Erasure.Slot[Int] = Erasure.slot()12  let others: Erasure.Slot[Int] = Erasure.slot()13  let words: Erasure.Slot[Str] = Erasure.slot()14  let seven = Erasure.pack(&numbers, 7)15  let word = Erasure.pack(&words, "seven")16  let pairs: Erasure.Slot[(Int, Str)] = Erasure.slot()17  let pair = Erasure.pack(&pairs, (1, "one"))18  var readers: Array[Concurrent.Task] = []19  var index = 020  while index < 8 {21    let started = Concurrent.start(fn() -> () {22        var turn = 023        while turn < 50 {24          if Erasure.unpack(&numbers, &seven) != Some(7) { panic("a concurrent read saw another value") }25          turn = turn + 126        }27      })28    if let Ok(worker) = started { readers = readers.push(worker) }29    index = index + 130  }31  let joined = Concurrent.joinAll(&readers)32  let checks = Test.suite("Utils.Erasure", &[33      Test.equals("a value reads back through its slot", &Erasure.unpack(&numbers, &seven), &Some(7)),34      Test.equals("it reads back more than once", &Erasure.unpack(&numbers, &seven), &Some(7)),35      Test.equals("another slot of the same type does not read it", &Erasure.unpack(&others, &seven), &None),36      Test.equals("a slot of another type does not read it", &Erasure.unpack(&words, &seven), &None),37      Test.equals("a slot does not read a value of another slot", &Erasure.unpack(&numbers, &word), &None),38      Test.equals("a compound value reads back whole", &Erasure.unpack(&pairs, &pair), &Some((1, "one"))),39      Test.equals("concurrent reads each see their own value", &joined, &Ok(()))40    ])41  let ran = Test.run(&checks)42  for failure in Test.failuresOf(&ran) {43    let _reported = Io.writeErrorLine(failure)44  }45  Test.report(&ran)46}47