|
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); |
26 | | - |
27 | | - let one = U256::one(); |
28 | | - assert_eq(one.high, 0); |
29 | | - assert_eq(one.low, 1); |
30 | | -} |
31 | | - |
32 | 1 | mod from_bytes32 { |
33 | | - use crate::uint256::U256; |
| 2 | + use crate::uint256::{from, from_field}; |
| 3 | + use dep::bignum::bignum::BigNum; |
| 4 | + use dep::bignum::U256; |
| 5 | + |
34 | 6 | global high: u128 = 0x10000000000000000000000000000000; |
35 | 7 | global low: u128 = 0; |
36 | | - global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; |
| 8 | + global limit_u256: U256 = U256::zero() - U256::one(); |
37 | 9 |
|
38 | | - global big_number: U256 = U256::new(high, low); |
39 | | - global limit_u256: U256 = U256::new(limit, limit); |
| 10 | + global big_number: U256 = from_field(0x10000000000000000000000000000000); |
40 | 11 |
|
41 | 12 | #[test] |
42 | 13 | fn zero() { |
43 | 14 | let bytes = [0x00; 32]; |
44 | | - assert_eq(U256::from(bytes), U256::zero()); |
| 15 | + assert_eq(from(bytes), U256::zero()); |
45 | 16 | } |
46 | 17 |
|
47 | 18 | #[test] |
48 | 19 | fn success() { |
49 | 20 | let mut bytes = [0x00; 32]; |
50 | 21 | bytes[31] = 0x10; |
51 | | - assert_eq(U256::from(bytes), big_number); |
| 22 | + assert_eq(from(bytes), big_number); |
52 | 23 | } |
53 | 24 |
|
54 | 25 | #[test] |
55 | 26 | fn u256_limit() { |
56 | 27 | let bytes = [0xff; 32]; |
57 | | - assert_eq(U256::from(bytes), limit_u256); |
| 28 | + assert_eq(from(bytes), limit_u256); |
58 | 29 | } |
59 | 30 | } |
60 | 31 |
|
61 | 32 | mod into_bytes32 { |
62 | | - use crate::uint256::U256; |
| 33 | + use crate::uint256::{from_field, into}; |
| 34 | + use dep::bignum::bignum::BigNum; |
| 35 | + use dep::bignum::U256; |
63 | 36 | global high: u128 = 0x10000000000000000000000000000000; |
64 | 37 | global low: u128 = 0; |
65 | | - global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; |
66 | 38 |
|
67 | | - global big_number: U256 = U256::new(high, low); |
68 | | - global limit_u256: U256 = U256::new(limit, limit); |
| 39 | + global big_number: U256 = from_field(0x10000000000000000000000000000000); |
| 40 | + global limit_u256: U256 = U256::zero() - U256::one(); |
69 | 41 |
|
70 | 42 | #[test] |
71 | 43 | fn zero() { |
72 | 44 | let bytes = [0x00; 32]; |
73 | | - assert_eq(U256::into(U256::zero()), bytes); |
| 45 | + assert_eq(into(U256::zero()), bytes); |
74 | 46 | } |
75 | 47 |
|
76 | 48 | #[test] |
77 | 49 | fn success() { |
78 | 50 | let mut bytes = [0x00; 32]; |
79 | 51 | bytes[31] = 0x10; |
80 | | - assert_eq(U256::into(big_number), bytes); |
| 52 | + assert_eq(into(big_number), bytes); |
81 | 53 | } |
82 | 54 |
|
83 | 55 | #[test] |
84 | 56 | fn u256_limit() { |
85 | 57 | let bytes = [0xff; 32]; |
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(); |
| 58 | + assert_eq(into(limit_u256), bytes); |
134 | 59 | } |
135 | 60 | } |
0 commit comments