-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_splitter.cairo
110 lines (85 loc) · 3.13 KB
/
test_splitter.cairo
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
use src::Splitter;
use starknet::testing::set_caller_address;
// use debug::PrintTrait;
use starknet::ContractAddress;
use starknet::Felt252TryIntoContractAddress;
use traits::TryInto;
use starknet::OptionTrait;
// TESTS
#[test]
#[available_gas(2000000)]
fn split_even_number() {
let amount: u128 = 100_u128;
let two: u128 = 2_u128;
let split_amount: u128 = amount / two;
let remaining: u128 = amount - (split_amount * two);
let userA: ContractAddress = get_address(1);
let userB: ContractAddress = get_address(2);
let caller: ContractAddress = get_address(3);
set_caller_address(caller);
Splitter::split(amount, userA, userB);
// assert((Splitter::get_balance(userA) == split_amount), 'Wrong Balance A');
assert_get_balance(userA, split_amount);
assert_get_balance(userB, split_amount);
assert_get_balance(caller, remaining);
}
#[test]
#[available_gas(2000000)]
fn split_odd_number() {
let amount: u128 = 101_u128;
let two: u128 = 2_u128;
let split_amount: u128 = amount / two;
let remaining: u128 = amount - (split_amount * two);
let userA: ContractAddress = get_address(1);
let userB: ContractAddress = get_address(2);
let caller: ContractAddress = get_address(3);
set_caller_address(caller);
Splitter::split(amount, userA, userB);
assert_get_balance(userA, split_amount);
assert_get_balance(userB, split_amount);
assert_get_balance(caller, remaining);
}
#[test]
#[available_gas(2000000)]
fn previous_value_preserved() {
let amount: u128 = 101_u128;
let two: u128 = 2_u128;
let split_amount: u128 = amount / two;
let remaining: u128 = amount - (split_amount * two);
let userA: ContractAddress = get_address(1);
let userB: ContractAddress = get_address(2);
let userC: ContractAddress = get_address(3);
let caller: ContractAddress = get_address(4);
set_caller_address(caller);
Splitter::split(amount, userA, userB);
Splitter::split(amount, userB, userC);
assert_get_balance(userA, split_amount);
assert_get_balance(userB, amount - remaining);
assert_get_balance(userC, split_amount);
assert_get_balance(caller, remaining * two);
}
#[test]
#[available_gas(2000000)]
#[should_panic(expected = ('u128_add Overflow', ))]
fn max_value_passed() {
let amount: u128 = 340282366920938463463374607431768211455_u128;
let two: u128 = 2_u128;
let split_amount: u128 = amount / two;
let remaining: u128 = amount - (split_amount * two);
let userA: ContractAddress = get_address(1);
let userB: ContractAddress = get_address(2);
let caller: ContractAddress = get_address(4);
set_caller_address(caller);
Splitter::split(amount, userA, userB); // (amount - 1)/2
// Splitter::get_balance(userA).print();
Splitter::split(amount, userA, userB); // (amount - 1)
// Splitter::get_balance(userA).print();
Splitter::split(amount, userA, userB); // Overflow
}
// HELPER FUNCTIONS
fn get_address(value: felt252) -> ContractAddress {
value.try_into().unwrap()
}
fn assert_get_balance(addr: ContractAddress, value: u128) {
assert((Splitter::get_balance(addr) == value), 'Wrong Balance');
}