Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a pseudorandom generator for move tests #7554

Merged
merged 17 commits into from
Jan 24, 2023
Merged
Prev Previous commit
Next Next commit
Add tests for range functions
  • Loading branch information
jonas-lj committed Jan 23, 2023
commit 2fba9b4694afc5ef016c3469e8f22d6f3405cc97
90 changes: 90 additions & 0 deletions crates/sui-framework/sources/test/random.move
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,60 @@ module sui::random {
}
}

#[test]
fun test_next_u8_in_range() {
let random = new(b"seed");

let i = 0;
let bounds = vector[1, 7, 8, 9, 15, 16, 17];
let tests = 10;
while (i < vector::length(&bounds)) {
let upper_bound = *vector::borrow(&bounds, i);
let j = 0;
while (j < tests) {
assert!(next_u8_in_range(&mut random, upper_bound) < upper_bound, 0);
j = j + 1;
};
i = i + 1;
}
}

#[test]
fun test_next_u16_in_range() {
let random = new(b"seed");

let i = 0;
let bounds = vector[1, 7, 8, 9, 15, 16, 17];
let tests = 10;
while (i < vector::length(&bounds)) {
let upper_bound = *vector::borrow(&bounds, i);
let j = 0;
while (j < tests) {
assert!(next_u16_in_range(&mut random, upper_bound) < upper_bound, 0);
j = j + 1;
};
i = i + 1;
}
}

#[test]
fun test_next_u32_in_range() {
let random = new(b"seed");

let i = 0;
let bounds = vector[1, 7, 8, 9, 15, 16, 17];
let tests = 10;
while (i < vector::length(&bounds)) {
let upper_bound = *vector::borrow(&bounds, i);
let j = 0;
while (j < tests) {
assert!(next_u32_in_range(&mut random, upper_bound) < upper_bound, 0);
j = j + 1;
};
i = i + 1;
}
}

#[test]
fun test_next_u64_in_range() {
let random = new(b"seed");
Expand All @@ -198,4 +252,40 @@ module sui::random {
}
}

#[test]
fun test_next_u128_in_range() {
let random = new(b"seed");

let i = 0;
let bounds = vector[1, 7, 8, 9, 15, 16, 17];
let tests = 10;
while (i < vector::length(&bounds)) {
let upper_bound = *vector::borrow(&bounds, i);
let j = 0;
while (j < tests) {
assert!(next_u128_in_range(&mut random, upper_bound) < upper_bound, 0);
j = j + 1;
};
i = i + 1;
}
}

#[test]
fun test_next_u256_in_range() {
let random = new(b"seed");

let i = 0;
let bounds = vector[1, 7, 8, 9, 15, 16, 17];
let tests = 10;
while (i < vector::length(&bounds)) {
let upper_bound = *vector::borrow(&bounds, i);
let j = 0;
while (j < tests) {
assert!(next_u256_in_range(&mut random, upper_bound) < upper_bound, 0);
j = j + 1;
};
i = i + 1;
}
}

}