-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtest_utils.move
More file actions
116 lines (99 loc) · 2.99 KB
/
test_utils.move
File metadata and controls
116 lines (99 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
#[test_only]
module iota::test_utils;
public fun assert_eq<T: drop>(t1: T, t2: T) {
assert_ref_eq(&t1, &t2)
}
public fun assert_ref_eq<T>(t1: &T, t2: &T) {
let res = t1 == t2;
if (!res) {
print(b"Assertion failed:");
std::debug::print(t1);
print(b"!=");
std::debug::print(t2);
abort (0)
}
}
/// Function checks that the two passed vectors contain the same elements,
/// regardless of their position in the vector.
public fun assert_same_elems<T: drop>(t1: vector<T>, t2: vector<T>) {
let len1 = t1.length();
let len2 = t2.length();
// If lengths are different, they can't be equal
if (len1 != len2) {
print(b"Assertion failed: lengths do not match");
std::debug::print(&len1);
print(b"!=");
std::debug::print(&len2);
abort (0)
};
// Vectors to store unique elements and their counts
let mut unique_values = vector<u64>[];
let mut counts1 = vector<u64>[];
let mut counts2 = vector<u64>[];
// Count occurrences in v1
let mut i = 0;
while (i < len1) {
let value = &t1[i];
let mut found = false;
let mut j = 0;
while (j < vector::length(&unique_values)) {
if (&t1[unique_values[j]] == value) {
let count = counts1[j];
*vector::borrow_mut(&mut counts1, j) = count + 1;
found = true;
break
};
j = j + 1;
};
if (!found) {
vector::push_back(&mut unique_values, i);
vector::push_back(&mut counts1, 1);
vector::push_back(&mut counts2, 0);
};
i = i + 1;
};
// Count occurrences in v2
let mut i = 0;
while (i < len2) {
let value = &t2[i];
let mut found = false;
let mut j = 0;
while (j < vector::length(&unique_values)) {
if (&t1[unique_values[j]] == value) {
let count = counts2[j];
*vector::borrow_mut(&mut counts2, j) = count + 1;
found = true;
break
};
j = j + 1;
};
if (!found) {
print(b"Assertion failed: elements do not match");
std::debug::print(&t1);
print(b"!=");
std::debug::print(&t2);
abort (0)
};
i = i + 1;
};
// Compare counts
let mut i = 0;
while (i < vector::length(&counts1)) {
if (counts1[i] != counts2[i]) {
print(b"Assertion failed: elements do not match");
std::debug::print(&t1);
print(b"!=");
std::debug::print(&t2);
abort (0)
};
i = i + 1;
};
}
public fun print(str: vector<u8>) {
std::debug::print(&str.to_ascii_string())
}
public native fun destroy<T>(x: T);
public native fun create_one_time_witness<T: drop>(): T;