Skip to content

Commit f55cc6f

Browse files
refactor: improve test speed
- Use LCG instead of chacha8 - Use from_array instead of from_iter
1 parent 133e21a commit f55cc6f

File tree

4 files changed

+24
-46
lines changed

4 files changed

+24
-46
lines changed

immut/array/array_mix_wbtest.mbt

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,16 @@
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-
20-
test "DEADBEEF-10-4" {
21-
run_test!("DEADBEEF", 10, 4)
22-
}
23-
24-
test "LIVEBEEF-50-3" {
25-
run_test!("LIVEBEEF", 50, 3)
26-
}
27-
28-
test "HAPPYDOG-1000-2" {
29-
run_test!("HAPPYDOG", 200, 2)
30-
}
31-
32-
test "HELLOWOLRD-5-3" {
33-
run_test!("HELLOWOLRD", 4, 1)
15+
test "random property tests" {
16+
run_test!(104UL, 10, 4)
17+
run_test!(503UL, 50, 3)
18+
run_test!(10002UL, 200, 2)
19+
run_test!(53UL, 4, 1)
3420
}
3521

3622
///|
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~)
23+
fn run_test(seed : UInt64, rep : Int, max_lvl : Int) -> Unit! {
24+
let rng = Random({ val: seed })
4025
let rs = random_test_gen(rng, rep, max_lvl)
4126
execute_array_test!(rs)
4227
}

immut/array/array_wbtest.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ test "concat-multiple-full-tree" {
114114

115115
test "concat-multiple-random-tree" {
116116
let bf = branching_factor_power(2)
117-
let rng = @random.new(seed=b"DEADBEEFDEADBEEFDEADBEEFDEADBEEF")
117+
let rng = Random({ val: 0UL })
118118
execute_array_test!(gen_concat_seq(2, fn(_i) { rng.int(limit=bf) }))
119119
}
120120

immut/array/moon.pkg.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
"alias": "_core/array"
88
}
99
],
10-
"wbtest-import": [
11-
"moonbitlang/core/random",
12-
"moonbitlang/core/bytes"
13-
],
1410
"targets": {
1511
"panic_test.mbt": [
1612
"not",

immut/array/utils_wbtest.mbt

Lines changed: 16 additions & 19 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) => {
@@ -116,22 +116,6 @@ fn branching_factor_power(a : Int) -> Int {
116116
ret
117117
}
118118

119-
///|
120-
/// Repeat the given string up to 32 characters.
121-
/// Used to generate a random seed for the test.
122-
fn repeat_up_to_32(s : String) -> Bytes {
123-
let a_len = 32
124-
let s_len = s.length()
125-
let a = FixedArray::make(a_len, b'0')
126-
let mut j = 0
127-
for i = 0; i < a_len; i = i + 1 {
128-
let l = a.set_utf8_char(i, s[j])
129-
guard l == 1
130-
j = (j + 1) % s_len
131-
}
132-
Bytes::from_fixedarray(a)
133-
}
134-
135119
///|
136120
/// Use this function to check if the array and the @immut/array are equal.
137121
/// If we `inspect` the array, it will raise an error if the arrays are too long.
@@ -159,6 +143,19 @@ fn check_fixedarray_eq(a : FixedArray[Int], t : T[Int]) -> Unit! {
159143
///|
160144
/// Generate a random array of length `n`.
161145
fn random_array(n : Int) -> Array[Int] {
162-
let rng = @random.new(seed=b"DEADBEEFLIVEBEEFDEADBEEFDEADBEEF")
146+
let rng = Random({ val: 0 })
163147
Array::makei(n, fn(i) { rng.int(limit=i) })
164148
}
149+
150+
///|
151+
type Random Ref[UInt64]
152+
153+
///|
154+
fn int(self : Random, limit~ : Int) -> Int {
155+
let limit = if limit == 0 { 1L << 32 } else { limit.to_int64() }
156+
let a = 1UL << 48
157+
let c = 11UL
158+
let m = 25214903917UL
159+
self._.val = (a * self._.val + c) % m
160+
(self._.val.reinterpret_as_int64() % limit).to_int()
161+
}

0 commit comments

Comments
 (0)