Skip to content

Commit 68e5f73

Browse files
refactor: improve test speed
Use LCG instead of chacha8 Use from_array instead of from_iter
1 parent 4a6e310 commit 68e5f73

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

immut/array/array_mix_wbtest.mbt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,25 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
/// The naming of test follows the format
16-
/// random seed - number of operations - maximum level of the tree
17-
/// If the given random seed is shorter than 32, it will be repeated
18-
/// up to 32 chars.
19-
2015
test "DEADBEEF-10-4" {
21-
run_test!("DEADBEEF", 10, 4)
16+
run_test!(104UL, 10, 4)
2217
}
2318

2419
test "LIVEBEEF-50-3" {
25-
run_test!("LIVEBEEF", 50, 3)
20+
run_test!(503UL, 50, 3)
2621
}
2722

2823
test "HAPPYDOG-1000-2" {
29-
run_test!("HAPPYDOG", 200, 2)
24+
run_test!(10002UL, 200, 2)
3025
}
3126

3227
test "HELLOWOLRD-5-3" {
33-
run_test!("HELLOWOLRD", 4, 1)
28+
run_test!(53UL, 4, 1)
3429
}
3530

3631
///|
37-
fn run_test(seed : String, rep : Int, max_lvl : Int) -> Unit! {
38-
let seed = repeat_up_to_32(seed)
39-
let rng = @random.new(seed~)
32+
fn run_test(seed : UInt64, rep : Int, max_lvl : Int) -> Unit! {
33+
let rng = Random({ val: seed })
4034
let rs = random_test_gen(rng, rep, max_lvl)
4135
execute_array_test!(rs)
4236
}

immut/array/utils_wbtest.mbt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum Op {
2727
/// - `rng`: a random number generator
2828
/// - `times`: the number of operations to be generated
2929
/// - `max_lvl`: the maximum level of the tree
30-
fn random_test_gen(rng : @random.Rand, times : Int, max_lvl : Int) -> Array[Op] {
30+
fn random_test_gen(rng : Random, times : Int, max_lvl : Int) -> Array[Op] {
3131
// Hyperparameters
3232
let op_count = 3
3333
let max_len = branching_factor_power(max_lvl)
@@ -93,7 +93,7 @@ fn execute_array_test(rs : Array[Op]) -> Unit! {
9393
for i = 0; i < len; i = i + 1 {
9494
a.push(v[i])
9595
}
96-
t = t.concat(from_iter(v.iter()))
96+
t = t.concat(from_array(v))
9797
check_array_eq!(a, t)
9898
}
9999
Set(idx, v) => {
@@ -159,6 +159,19 @@ fn check_fixedarray_eq(a : FixedArray[Int], t : T[Int]) -> Unit! {
159159
///|
160160
/// Generate a random array of length `n`.
161161
fn random_array(n : Int) -> Array[Int] {
162-
let rng = @random.new(seed=b"DEADBEEFLIVEBEEFDEADBEEFDEADBEEF")
162+
let rng = Random({ val: 0 })
163163
Array::makei(n, fn(i) { rng.int(limit=i) })
164164
}
165+
166+
///|
167+
type Random Ref[UInt64]
168+
169+
///|
170+
fn int(self : Random, limit~ : Int) -> Int {
171+
let limit = if limit == 0 { 1L << 32 } else { limit.to_int64() }
172+
let a = 1UL << 48
173+
let c = 11UL
174+
let m = 25214903917UL
175+
self._.val = (a * self._.val + c) % m
176+
(self._.val.reinterpret_as_int64() % limit).to_int()
177+
}

0 commit comments

Comments
 (0)