|
1 | | -mod from_bytes32 { |
2 | | - use crate::uint256::{from, from_field}; |
3 | | - use dep::bignum::bignum::BigNum; |
4 | | - use dep::bignum::U256; |
| 1 | +use crate::uint256::U256; |
| 2 | + |
| 3 | +global high: u128 = 0x10000000000000000000000000000000; |
| 4 | +global low: u128 = 0; |
| 5 | + |
| 6 | +global big_number: U256 = U256::new(high, low); |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn success() { |
| 10 | + assert_eq(big_number.high, high); |
| 11 | + assert_eq(big_number.low, low); |
| 12 | +} |
| 13 | + |
| 14 | +#[test] |
| 15 | +fn new() { |
| 16 | + let big_number = U256::new(high, low); |
| 17 | + assert_eq(big_number.high, high); |
| 18 | + assert_eq(big_number.low, low); |
| 19 | +} |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn zero_and_one() { |
| 23 | + let zero = U256::zero(); |
| 24 | + assert_eq(zero.high, 0); |
| 25 | + assert_eq(zero.low, 0); |
5 | 26 |
|
| 27 | + let one = U256::one(); |
| 28 | + assert_eq(one.high, 0); |
| 29 | + assert_eq(one.low, 1); |
| 30 | +} |
| 31 | + |
| 32 | +mod from_bytes32 { |
| 33 | + use crate::uint256::U256; |
6 | 34 | global high: u128 = 0x10000000000000000000000000000000; |
7 | 35 | global low: u128 = 0; |
8 | | - global limit_u256: U256 = U256::zero() - U256::one(); |
| 36 | + global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; |
9 | 37 |
|
10 | | - global big_number: U256 = from_field(0x10000000000000000000000000000000); |
| 38 | + global big_number: U256 = U256::new(high, low); |
| 39 | + global limit_u256: U256 = U256::new(limit, limit); |
11 | 40 |
|
12 | 41 | #[test] |
13 | 42 | fn zero() { |
14 | 43 | let bytes = [0x00; 32]; |
15 | | - assert_eq(from(bytes), U256::zero()); |
| 44 | + assert_eq(U256::from(bytes), U256::zero()); |
16 | 45 | } |
17 | 46 |
|
18 | 47 | #[test] |
19 | 48 | fn success() { |
20 | 49 | let mut bytes = [0x00; 32]; |
21 | 50 | bytes[31] = 0x10; |
22 | | - assert_eq(from(bytes), big_number); |
| 51 | + assert_eq(U256::from(bytes), big_number); |
23 | 52 | } |
24 | 53 |
|
25 | 54 | #[test] |
26 | 55 | fn u256_limit() { |
27 | 56 | let bytes = [0xff; 32]; |
28 | | - assert_eq(from(bytes), limit_u256); |
| 57 | + assert_eq(U256::from(bytes), limit_u256); |
29 | 58 | } |
30 | 59 | } |
31 | 60 |
|
32 | 61 | mod into_bytes32 { |
33 | | - use crate::uint256::{from_field, into}; |
34 | | - use dep::bignum::bignum::BigNum; |
35 | | - use dep::bignum::U256; |
| 62 | + use crate::uint256::U256; |
36 | 63 | global high: u128 = 0x10000000000000000000000000000000; |
37 | 64 | global low: u128 = 0; |
| 65 | + global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; |
38 | 66 |
|
39 | | - global big_number: U256 = from_field(0x10000000000000000000000000000000); |
40 | | - global limit_u256: U256 = U256::zero() - U256::one(); |
| 67 | + global big_number: U256 = U256::new(high, low); |
| 68 | + global limit_u256: U256 = U256::new(limit, limit); |
41 | 69 |
|
42 | 70 | #[test] |
43 | 71 | fn zero() { |
44 | 72 | let bytes = [0x00; 32]; |
45 | | - assert_eq(into(U256::zero()), bytes); |
| 73 | + assert_eq(U256::into(U256::zero()), bytes); |
46 | 74 | } |
47 | 75 |
|
48 | 76 | #[test] |
49 | 77 | fn success() { |
50 | 78 | let mut bytes = [0x00; 32]; |
51 | 79 | bytes[31] = 0x10; |
52 | | - assert_eq(into(big_number), bytes); |
| 80 | + assert_eq(U256::into(big_number), bytes); |
53 | 81 | } |
54 | 82 |
|
55 | 83 | #[test] |
56 | 84 | fn u256_limit() { |
57 | 85 | let bytes = [0xff; 32]; |
58 | | - assert_eq(into(limit_u256), bytes); |
| 86 | + assert_eq(U256::into(limit_u256), bytes); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn eq() { |
| 92 | + assert_eq(big_number, big_number); |
| 93 | + |
| 94 | + let big_number2 = U256::new(high, low); |
| 95 | + assert_eq(big_number, big_number2); |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn not_eq() { |
| 100 | + let big_number2 = U256 { high, low: high }; |
| 101 | + assert(big_number != big_number2); |
| 102 | + |
| 103 | + let big_number3 = U256 { high: low, low }; |
| 104 | + assert(big_number != big_number3); |
| 105 | +} |
| 106 | + |
| 107 | +mod trait_add { |
| 108 | + use crate::uint256::U256; |
| 109 | + global high: u128 = 0x10000000000000000000000000000000; |
| 110 | + global low: u128 = 0; |
| 111 | + global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; |
| 112 | + |
| 113 | + global big_number: U256 = U256::new(high, low); |
| 114 | + global limit_u256: U256 = U256::new(limit, limit); |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn sum() { |
| 118 | + let sum = big_number + big_number; |
| 119 | + |
| 120 | + assert_eq(sum, U256 { high: 0x20000000000000000000000000000000, low }); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn sum_with_carry() { |
| 125 | + let big_number = U256 { high, low: limit }; |
| 126 | + let sum = big_number + U256::one(); |
| 127 | + |
| 128 | + assert_eq(sum, U256 { high: 0x10000000000000000000000000000001, low }); |
| 129 | + } |
| 130 | + |
| 131 | + #[test(should_fail_with = "attempt to add with overflow")] |
| 132 | + fn sum_overflow() { |
| 133 | + let _ = limit_u256 + U256::one(); |
59 | 134 | } |
60 | 135 | } |
0 commit comments