File tree Expand file tree Collapse file tree 4 files changed +35
-0
lines changed Expand file tree Collapse file tree 4 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ fn max_value ( ) -> int {
2
+ ret min_value ( ) - 1 ;
3
+ }
4
+
5
+ fn min_value ( ) -> int {
6
+ ret ( -1 << ( sys:: size_of :: < int > ( ) * 8 u as int - 1 ) ) as int ;
7
+ }
1
8
2
9
3
10
pure fn add ( x : int , y : int ) -> int { ret x + y; }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Return the minimal value for an uint.
3
+ *
4
+ * This is always 0
5
+ */
1
6
pure fn min_value ( ) -> uint { ret 0 u; }
2
7
8
+ /**
9
+ * Return the maximal value for an uint.
10
+ *
11
+ * This is 2^wordsize - 1
12
+ */
13
+ pure fn max_value ( ) -> uint {
14
+ ret 0 u - 1 u;
15
+ }
16
+
3
17
fn add ( x : uint , y : uint ) -> uint { ret x + y; }
4
18
5
19
fn sub ( x : uint , y : uint ) -> uint { ret x - y; }
Original file line number Diff line number Diff line change @@ -23,3 +23,10 @@ fn test_pow() {
23
23
assert ( int:: pow ( -3 , 3 u) == -27 ) ;
24
24
assert ( int:: pow ( 4 , 9 u) == 262144 ) ;
25
25
}
26
+
27
+ #[ test]
28
+ fn test_overflows ( ) {
29
+ assert ( int:: max_value ( ) > 0 ) ;
30
+ assert ( int:: min_value ( ) <= 0 ) ;
31
+ assert ( int:: min_value ( ) + int:: max_value ( ) + 1 == 0 ) ;
32
+ }
Original file line number Diff line number Diff line change @@ -47,3 +47,10 @@ fn test_next_power_of_two() {
47
47
assert ( uint:: next_power_of_two ( 38 u) == 64 u) ;
48
48
assert ( uint:: next_power_of_two ( 39 u) == 64 u) ;
49
49
}
50
+
51
+ #[ test]
52
+ fn test_overflows ( ) {
53
+ assert ( uint:: max_value ( ) > 0 u) ;
54
+ assert ( uint:: min_value ( ) <= 0 u) ;
55
+ assert ( uint:: min_value ( ) + uint:: max_value ( ) + 1 u == 0 u) ;
56
+ }
You can’t perform that action at this time.
0 commit comments