Skip to content

Commit a01177e

Browse files
committed
chore: add more tests for Move 2 features (#111)
1 parent ea4118b commit a01177e

14 files changed

+1495
-249
lines changed

move-mutator/tests/basic_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const PACKAGE_PATHS: &[&str] = &[
3737
"tests/move-assets/skip_mutation_examples",
3838
"tests/move-assets/check_swap_operator",
3939
"tests/move-assets/simple_move_2_features",
40-
"tests/move-assets/function_values",
4140
];
4241

4342
// Check if the mutator works correctly on the basic packages.

move-mutator/tests/move-assets/function_values/Move.toml

Lines changed: 0 additions & 9 deletions
This file was deleted.

move-mutator/tests/move-assets/function_values/report.txt.mutation-exp

Lines changed: 0 additions & 235 deletions
This file was deleted.

move-mutator/tests/move-assets/simple_move_2_features/report.txt.mutation-exp

Lines changed: 551 additions & 2 deletions
Large diffs are not rendered by default.

move-mutator/tests/move-assets/simple_move_2_features/report.txt.spec-exp

Lines changed: 572 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module TestAccount::Enums {
2+
use std::string::{String, utf8};
3+
4+
enum Shape has drop {
5+
Rectangle { width: u64, height: u64 }
6+
}
7+
8+
fun rectangle_area(shape: Shape): u64 {
9+
assert!(shape is Shape::Rectangle);
10+
shape.width*shape.height
11+
}
12+
13+
enum Colour { Red, Green, Blue }
14+
15+
fun colour_enum_to_string(colour: Colour): String {
16+
match (colour) {
17+
Red => utf8(b"red"),
18+
Green => utf8(b"green"),
19+
Blue => utf8(b"blue"),
20+
}
21+
}
22+
23+
#[test]
24+
fun area_of_rectangle() {
25+
assert!(rectangle_area(Shape::Rectangle{width: 5, height: 5}) == 25);
26+
}
27+
28+
#[test]
29+
fun print_color_from_enum() {
30+
assert!(colour_enum_to_string(Colour::Red) == utf8(b"red"));
31+
}
32+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module TestAccount::FriendVisibility {
2+
friend TestAccount::FriendHelper;
3+
4+
public(friend) fun multiply(a: u64, b: u64): u64 {
5+
a * b
6+
}
7+
8+
friend fun divide(a: u64, b: u64): u64 {
9+
a / b
10+
}
11+
12+
public(friend) fun is_greater(a: u64, b: u64): bool {
13+
a > b
14+
}
15+
16+
#[test]
17+
fun test_multiply() {
18+
assert!(multiply(5, 10) == 50);
19+
assert!(multiply(0, 100) == 0);
20+
assert!(multiply(7, 7) == 49);
21+
}
22+
23+
#[test]
24+
fun test_divide() {
25+
assert!(divide(10, 2) == 5);
26+
assert!(divide(100, 10) == 10);
27+
assert!(divide(7, 2) == 3);
28+
}
29+
30+
#[test]
31+
fun test_is_greater() {
32+
assert!(is_greater(10, 5) == true);
33+
assert!(is_greater(5, 10) == false);
34+
assert!(is_greater(5, 5) == false);
35+
}
36+
}
37+
38+
module TestAccount::FriendHelper {
39+
use TestAccount::FriendVisibility;
40+
41+
fun compute_area(width: u64, height: u64): u64 {
42+
FriendVisibility::multiply(width, height)
43+
}
44+
45+
fun average(total: u64, count: u64): u64 {
46+
FriendVisibility::divide(total, count)
47+
}
48+
49+
fun max(a: u64, b: u64): u64 {
50+
if (FriendVisibility::is_greater(a, b)) { a } else { b }
51+
}
52+
53+
#[test]
54+
fun test_compute_area() {
55+
assert!(compute_area(5, 10) == 50);
56+
assert!(compute_area(10, 10) == 100);
57+
}
58+
59+
#[test]
60+
fun test_average() {
61+
assert!(average(100, 10) == 10);
62+
assert!(average(50, 2) == 25);
63+
}
64+
65+
#[test]
66+
fun test_max() {
67+
assert!(max(10, 5) == 10);
68+
assert!(max(5, 10) == 10);
69+
assert!(max(7, 7) == 7);
70+
}
71+
}
File renamed without changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module TestAccount::IndexNotation {
2+
fun get_element(v: &vector<u64>, i: u64): u64 {
3+
v[i]
4+
}
5+
6+
fun sum_first_two(v: &vector<u64>): u64 {
7+
v[0] + v[1]
8+
}
9+
10+
fun increment_element(v: &mut vector<u64>, i: u64) {
11+
*(&mut v[i]) = v[i] + 1;
12+
}
13+
14+
fun is_element_greater(v: &vector<u64>, i: u64, threshold: u64): bool {
15+
v[i] > threshold
16+
}
17+
18+
#[test]
19+
fun test_get_element() {
20+
let v = vector[10, 20, 30, 40];
21+
assert!(get_element(&v, 0) == 10);
22+
assert!(get_element(&v, 1) == 20);
23+
assert!(get_element(&v, 2) == 30);
24+
assert!(get_element(&v, 3) == 40);
25+
}
26+
27+
#[test]
28+
fun test_sum_first_two() {
29+
let v = vector[5, 10, 15];
30+
assert!(sum_first_two(&v) == 15);
31+
32+
let v2 = vector[100, 200, 300];
33+
assert!(sum_first_two(&v2) == 300);
34+
}
35+
36+
#[test]
37+
fun test_increment_element() {
38+
let v = vector[10, 20, 30];
39+
increment_element(&mut v, 0);
40+
assert!(v[0] == 11);
41+
assert!(v[1] == 20);
42+
43+
increment_element(&mut v, 1);
44+
assert!(v[1] == 21);
45+
}
46+
47+
#[test]
48+
fun test_is_element_greater() {
49+
let v = vector[10, 20, 30];
50+
assert!(is_element_greater(&v, 0, 5) == true);
51+
assert!(is_element_greater(&v, 0, 10) == false);
52+
assert!(is_element_greater(&v, 2, 25) == true);
53+
assert!(is_element_greater(&v, 2, 30) == false);
54+
}
55+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module TestAccount::NewCastSyntax {
2+
fun u64_to_u128(x: u64): u128 {
3+
x as u128
4+
}
5+
6+
fun add_and_cast(a: u64, b: u64): u128 {
7+
(a + b) as u128
8+
}
9+
10+
fun multiply_and_widen(a: u64, b: u64): u128 {
11+
(a as u128) * (b as u128)
12+
}
13+
14+
fun compare_after_cast(x: u64, threshold: u128): bool {
15+
(x as u128) > threshold
16+
}
17+
18+
fun cast_down_if_small(x: u128): u64 {
19+
if (x <= 18446744073709551615) {
20+
x as u64
21+
} else {
22+
0
23+
}
24+
}
25+
26+
fun divide_and_cast(numerator: u64, denominator: u64): u128 {
27+
(numerator / denominator) as u128
28+
}
29+
30+
#[test]
31+
fun test_u64_to_u128() {
32+
assert!(u64_to_u128(0) == 0);
33+
assert!(u64_to_u128(100) == 100);
34+
assert!(u64_to_u128(999999) == 999999);
35+
}
36+
37+
#[test]
38+
fun test_add_and_cast() {
39+
assert!(add_and_cast(10, 20) == 30);
40+
assert!(add_and_cast(0, 0) == 0);
41+
assert!(add_and_cast(1000, 2000) == 3000);
42+
}
43+
44+
#[test]
45+
fun test_multiply_and_widen() {
46+
assert!(multiply_and_widen(10, 20) == 200);
47+
assert!(multiply_and_widen(100, 100) == 10000);
48+
assert!(multiply_and_widen(0, 999) == 0);
49+
}
50+
51+
#[test]
52+
fun test_compare_after_cast() {
53+
assert!(compare_after_cast(100, 50) == true);
54+
assert!(compare_after_cast(50, 100) == false);
55+
assert!(compare_after_cast(100, 100) == false);
56+
}
57+
58+
#[test]
59+
fun test_cast_down_if_small() {
60+
assert!(cast_down_if_small(100) == 100);
61+
assert!(cast_down_if_small(0) == 0);
62+
assert!(cast_down_if_small(1000) == 1000);
63+
}
64+
65+
#[test]
66+
fun test_divide_and_cast() {
67+
assert!(divide_and_cast(100, 10) == 10);
68+
assert!(divide_and_cast(50, 2) == 25);
69+
assert!(divide_and_cast(7, 2) == 3);
70+
}
71+
}

0 commit comments

Comments
 (0)