github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/math/overflow/overflow_test.gno (about) 1 package overflow 2 3 import ( 4 "math" 5 "testing" 6 ) 7 8 // sample all possibilities of 8 bit numbers 9 // by checking against 64 bit numbers 10 11 func TestAlgorithms(t *testing.T) { 12 errors := 0 13 14 for a64 := int64(math.MinInt8); a64 <= int64(math.MaxInt8); a64++ { 15 for b64 := int64(math.MinInt8); b64 <= int64(math.MaxInt8) && errors < 10; b64++ { 16 17 a8 := int8(a64) 18 b8 := int8(b64) 19 20 if int64(a8) != a64 || int64(b8) != b64 { 21 t.Fatal("LOGIC FAILURE IN TEST") 22 } 23 24 // ADDITION 25 { 26 r64 := a64 + b64 27 28 // now the verification 29 result, ok := Add8(a8, b8) 30 if ok && int64(result) != r64 { 31 t.Errorf("failed to fail on %v + %v = %v instead of %v\n", 32 a8, b8, result, r64) 33 errors++ 34 } 35 if !ok && int64(result) == r64 { 36 t.Fail() 37 errors++ 38 } 39 } 40 41 // SUBTRACTION 42 { 43 r64 := a64 - b64 44 45 // now the verification 46 result, ok := Sub8(a8, b8) 47 if ok && int64(result) != r64 { 48 t.Errorf("failed to fail on %v - %v = %v instead of %v\n", 49 a8, b8, result, r64) 50 } 51 if !ok && int64(result) == r64 { 52 t.Fail() 53 errors++ 54 } 55 } 56 57 // MULTIPLICATION 58 { 59 r64 := a64 * b64 60 61 // now the verification 62 result, ok := Mul8(a8, b8) 63 if ok && int64(result) != r64 { 64 t.Errorf("failed to fail on %v * %v = %v instead of %v\n", 65 a8, b8, result, r64) 66 errors++ 67 } 68 if !ok && int64(result) == r64 { 69 t.Fail() 70 errors++ 71 } 72 } 73 74 // DIVISION 75 if b8 != 0 { 76 r64 := a64 / b64 77 rem64 := a64 % b64 78 79 // now the verification 80 result, rem, ok := Quo8(a8, b8) 81 if ok && int64(result) != r64 { 82 t.Errorf("failed to fail on %v / %v = %v instead of %v\n", 83 a8, b8, result, r64) 84 errors++ 85 } 86 if ok && int64(rem) != rem64 { 87 t.Errorf("failed to fail on %v %% %v = %v instead of %v\n", 88 a8, b8, rem, rem64) 89 errors++ 90 } 91 } 92 } 93 } 94 } 95 96 func TestQuotient(t *testing.T) { 97 q, r, ok := Quo(100, 3) 98 if r != 1 || q != 33 || !ok { 99 t.Errorf("expected 100/3 => 33, r=1") 100 } 101 if _, _, ok = Quo(1, 0); ok { 102 t.Error("unexpected lack of failure") 103 } 104 } 105 106 func TestLong(t *testing.T) { 107 if testing.Short() { 108 t.Skip() 109 } 110 111 ctr := int64(0) 112 113 for a64 := int64(math.MinInt16); a64 <= int64(math.MaxInt16); a64++ { 114 for b64 := int64(math.MinInt16); b64 <= int64(math.MaxInt16); b64++ { 115 a16 := int16(a64) 116 b16 := int16(b64) 117 if int64(a16) != a64 || int64(b16) != b64 { 118 panic("LOGIC FAILURE IN TEST") 119 } 120 ctr++ 121 122 // ADDITION 123 { 124 r64 := a64 + b64 125 126 // now the verification 127 result, ok := Add16(a16, b16) 128 if int64(math.MinInt16) <= r64 && r64 <= int64(math.MaxInt16) { 129 if !ok || int64(result) != r64 { 130 println("add", a16, b16, result, r64) 131 panic("incorrect result for non-overflow") 132 } 133 } else { 134 if ok { 135 println("add", a16, b16, result, r64) 136 panic("incorrect ok result") 137 } 138 } 139 } 140 141 // SUBTRACTION 142 { 143 r64 := a64 - b64 144 145 // now the verification 146 result, ok := Sub16(a16, b16) 147 if int64(math.MinInt16) <= r64 && r64 <= int64(math.MaxInt16) { 148 if !ok || int64(result) != r64 { 149 println("sub", a16, b16, result, r64) 150 panic("incorrect result for non-overflow") 151 } 152 } else { 153 if ok { 154 println("sub", a16, b16, result, r64) 155 panic("incorrect ok result") 156 } 157 } 158 } 159 160 // MULTIPLICATION 161 { 162 r64 := a64 * b64 163 164 // now the verification 165 result, ok := Mul16(a16, b16) 166 if int64(math.MinInt16) <= r64 && r64 <= int64(math.MaxInt16) { 167 if !ok || int64(result) != r64 { 168 println("mul", a16, b16, result, r64) 169 panic("incorrect result for non-overflow") 170 } 171 } else { 172 if ok { 173 println("mul", a16, b16, result, r64) 174 panic("incorrect ok result") 175 } 176 } 177 } 178 179 // DIVISION 180 if b16 != 0 { 181 r64 := a64 / b64 182 183 // now the verification 184 result, _, ok := Quo16(a16, b16) 185 if int64(math.MinInt16) <= r64 && r64 <= int64(math.MaxInt16) { 186 if !ok || int64(result) != r64 { 187 println("quo", a16, b16, result, r64) 188 panic("incorrect result for non-overflow") 189 } 190 } else { 191 if ok { 192 println("quo", a16, b16, result, r64) 193 panic("incorrect ok result") 194 } 195 } 196 } 197 } 198 } 199 println("done", ctr) 200 }