github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/chains/tm34/libs/math/fraction_test.go (about) 1 package math 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestParseFraction(t *testing.T) { 10 11 testCases := []struct { 12 f string 13 exp Fraction 14 err bool 15 }{ 16 { 17 f: "2/3", 18 exp: Fraction{2, 3}, 19 err: false, 20 }, 21 { 22 f: "15/5", 23 exp: Fraction{15, 5}, 24 err: false, 25 }, 26 // test divide by zero error 27 { 28 f: "2/0", 29 exp: Fraction{}, 30 err: true, 31 }, 32 // test negative 33 { 34 f: "-1/2", 35 exp: Fraction{}, 36 err: true, 37 }, 38 { 39 f: "1/-2", 40 exp: Fraction{}, 41 err: true, 42 }, 43 // test overflow 44 { 45 f: "9223372036854775808/2", 46 exp: Fraction{}, 47 err: true, 48 }, 49 { 50 f: "2/9223372036854775808", 51 exp: Fraction{}, 52 err: true, 53 }, 54 { 55 f: "2/3/4", 56 exp: Fraction{}, 57 err: true, 58 }, 59 { 60 f: "123", 61 exp: Fraction{}, 62 err: true, 63 }, 64 { 65 f: "1a2/4", 66 exp: Fraction{}, 67 err: true, 68 }, 69 { 70 f: "1/3bc4", 71 exp: Fraction{}, 72 err: true, 73 }, 74 } 75 76 for idx, tc := range testCases { 77 output, err := ParseFraction(tc.f) 78 if tc.err { 79 assert.Error(t, err, idx) 80 } else { 81 assert.NoError(t, err, idx) 82 } 83 assert.Equal(t, tc.exp, output, idx) 84 } 85 86 }