Skip to content

Commit

Permalink
refactor: improve test speed
Browse files Browse the repository at this point in the history
Use LCG instead of chacha8

Use from_array instead of from_iter
  • Loading branch information
peter-jerry-ye committed Jan 27, 2025
1 parent 4a6e310 commit 68e5f73
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
18 changes: 6 additions & 12 deletions immut/array/array_mix_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 16 additions & 3 deletions immut/array/utils_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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()
}

0 comments on commit 68e5f73

Please sign in to comment.