From 68e5f73e3940d29394144bd424129cbb3b1b33ce Mon Sep 17 00:00:00 2001 From: zihang Date: Mon, 27 Jan 2025 16:23:28 +0800 Subject: [PATCH] refactor: improve test speed Use LCG instead of chacha8 Use from_array instead of from_iter --- immut/array/array_mix_wbtest.mbt | 18 ++++++------------ immut/array/utils_wbtest.mbt | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/immut/array/array_mix_wbtest.mbt b/immut/array/array_mix_wbtest.mbt index b45eb003a..0e457be27 100644 --- a/immut/array/array_mix_wbtest.mbt +++ b/immut/array/array_mix_wbtest.mbt @@ -12,31 +12,25 @@ // See the License for the specific language governing permissions and // limitations under the License. -/// The naming of test follows the format -/// random seed - number of operations - maximum level of the tree -/// If the given random seed is shorter than 32, it will be repeated -/// up to 32 chars. - test "DEADBEEF-10-4" { - run_test!("DEADBEEF", 10, 4) + run_test!(104UL, 10, 4) } test "LIVEBEEF-50-3" { - run_test!("LIVEBEEF", 50, 3) + run_test!(503UL, 50, 3) } test "HAPPYDOG-1000-2" { - run_test!("HAPPYDOG", 200, 2) + run_test!(10002UL, 200, 2) } test "HELLOWOLRD-5-3" { - run_test!("HELLOWOLRD", 4, 1) + run_test!(53UL, 4, 1) } ///| -fn run_test(seed : String, rep : Int, max_lvl : Int) -> Unit! { - let seed = repeat_up_to_32(seed) - let rng = @random.new(seed~) +fn run_test(seed : UInt64, rep : Int, max_lvl : Int) -> Unit! { + let rng = Random({ val: seed }) let rs = random_test_gen(rng, rep, max_lvl) execute_array_test!(rs) } diff --git a/immut/array/utils_wbtest.mbt b/immut/array/utils_wbtest.mbt index 30ba32b66..1ac7a2acc 100644 --- a/immut/array/utils_wbtest.mbt +++ b/immut/array/utils_wbtest.mbt @@ -27,7 +27,7 @@ enum Op { /// - `rng`: a random number generator /// - `times`: the number of operations to be generated /// - `max_lvl`: the maximum level of the tree -fn random_test_gen(rng : @random.Rand, times : Int, max_lvl : Int) -> Array[Op] { +fn random_test_gen(rng : Random, times : Int, max_lvl : Int) -> Array[Op] { // Hyperparameters let op_count = 3 let max_len = branching_factor_power(max_lvl) @@ -93,7 +93,7 @@ fn execute_array_test(rs : Array[Op]) -> Unit! { for i = 0; i < len; i = i + 1 { a.push(v[i]) } - t = t.concat(from_iter(v.iter())) + t = t.concat(from_array(v)) check_array_eq!(a, t) } Set(idx, v) => { @@ -159,6 +159,19 @@ fn check_fixedarray_eq(a : FixedArray[Int], t : T[Int]) -> Unit! { ///| /// Generate a random array of length `n`. fn random_array(n : Int) -> Array[Int] { - let rng = @random.new(seed=b"DEADBEEFLIVEBEEFDEADBEEFDEADBEEF") + let rng = Random({ val: 0 }) Array::makei(n, fn(i) { rng.int(limit=i) }) } + +///| +type Random Ref[UInt64] + +///| +fn int(self : Random, limit~ : Int) -> Int { + let limit = if limit == 0 { 1L << 32 } else { limit.to_int64() } + let a = 1UL << 48 + let c = 11UL + let m = 25214903917UL + self._.val = (a * self._.val + c) % m + (self._.val.reinterpret_as_int64() % limit).to_int() +}