
UtilsTest.pudu
Pudu45 lines2.2 KB
1/** @Test.Utils.Suite — saturating arithmetic, float conversion, and shared state */2module PuduLangResilience.Utils.UtilsTest34import Std.Concurrent as Concurrent5import Std.Io as Io6import Std.Test as Test7import PuduLangResilience.Utils.Numeric as Numeric8import PuduLangResilience.Utils.Shared as Shared91011fn main() -> Int {12 let total = Shared.shared(0)13 var actions: Array[fn() -> ()] = []14 for _worker in [1, 2, 3, 4, 5, 6, 7, 8] {15 actions = actions.push(fn() -> () {16 for _step in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] {17 Shared.update(&total, fn(held: Int) -> Int { held + 1 })18 }19 })20 }21 let _ran = Concurrent.parallel(&actions)22 let swapped = Shared.change(&total, fn(held: Int) -> (Int, Int) { (0, held) })23 let checks = Test.suite("Utils", &[24 Test.equals("addition below the limit is exact", &Numeric.add(2, 3), &5),25 Test.equals("addition saturates", &Numeric.add(Numeric.LARGEST, 1), &Numeric.LARGEST),26 Test.equals("addition at the limit is exact", &Numeric.add(Numeric.LARGEST - 1, 1), &Numeric.LARGEST),27 Test.equals("multiplication below the limit is exact", &Numeric.multiply(6, 7), &42),28 Test.equals("multiplication by zero is zero", &(Numeric.multiply(0, 9), Numeric.multiply(9, 0)), &(0, 0)),29 Test.equals("multiplication saturates", &Numeric.multiply(Numeric.LARGEST / 2 + 1, 2), &Numeric.LARGEST),30 Test.equals("a whole float converts exactly", &Numeric.toInt(Numeric.toFloat(86400000)), &86400000),31 Test.equals("a fraction is rounded down", &Numeric.toInt(2.99), &2),32 Test.equals("negative floats clamp to zero", &Numeric.toInt(-4.0), &0),33 Test.equals("huge and infinite floats saturate", &(Numeric.toInt(1.0e30), Numeric.toInt(1.0 / 0.0)), &(Numeric.LARGEST, Numeric.LARGEST)),34 Test.equals("a large float in range converts", &Numeric.toInt(1.5e10), &15000000000),35 Test.equals("parts of a ratio round down", &(Numeric.partsOf(0.001d, 1000000000), Numeric.partsOf(0.3333d, 10)), &(1000000, 3)),36 Test.equals("concurrent updates lose nothing", &swapped, &80),37 Test.equals("a change stores its new value", &Shared.current(&total), &0)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