math_test.go (2990B)
1 // Copyright 2018 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package math 15 16 import ( 17 "testing" 18 19 qt "github.com/frankban/quicktest" 20 ) 21 22 func TestDoArithmetic(t *testing.T) { 23 t.Parallel() 24 c := qt.New(t) 25 26 for _, test := range []struct { 27 a any 28 b any 29 op rune 30 expect any 31 }{ 32 {3, 2, '+', int64(5)}, 33 {3, 2, '-', int64(1)}, 34 {3, 2, '*', int64(6)}, 35 {3, 2, '/', int64(1)}, 36 {3.0, 2, '+', float64(5)}, 37 {3.0, 2, '-', float64(1)}, 38 {3.0, 2, '*', float64(6)}, 39 {3.0, 2, '/', float64(1.5)}, 40 {3, 2.0, '+', float64(5)}, 41 {3, 2.0, '-', float64(1)}, 42 {3, 2.0, '*', float64(6)}, 43 {3, 2.0, '/', float64(1.5)}, 44 {3.0, 2.0, '+', float64(5)}, 45 {3.0, 2.0, '-', float64(1)}, 46 {3.0, 2.0, '*', float64(6)}, 47 {3.0, 2.0, '/', float64(1.5)}, 48 {uint(3), uint(2), '+', uint64(5)}, 49 {uint(3), uint(2), '-', uint64(1)}, 50 {uint(3), uint(2), '*', uint64(6)}, 51 {uint(3), uint(2), '/', uint64(1)}, 52 {uint(3), 2, '+', uint64(5)}, 53 {uint(3), 2, '-', uint64(1)}, 54 {uint(3), 2, '*', uint64(6)}, 55 {uint(3), 2, '/', uint64(1)}, 56 {3, uint(2), '+', uint64(5)}, 57 {3, uint(2), '-', uint64(1)}, 58 {3, uint(2), '*', uint64(6)}, 59 {3, uint(2), '/', uint64(1)}, 60 {uint(3), -2, '+', int64(1)}, 61 {uint(3), -2, '-', int64(5)}, 62 {uint(3), -2, '*', int64(-6)}, 63 {uint(3), -2, '/', int64(-1)}, 64 {-3, uint(2), '+', int64(-1)}, 65 {-3, uint(2), '-', int64(-5)}, 66 {-3, uint(2), '*', int64(-6)}, 67 {-3, uint(2), '/', int64(-1)}, 68 {uint(3), 2.0, '+', float64(5)}, 69 {uint(3), 2.0, '-', float64(1)}, 70 {uint(3), 2.0, '*', float64(6)}, 71 {uint(3), 2.0, '/', float64(1.5)}, 72 {3.0, uint(2), '+', float64(5)}, 73 {3.0, uint(2), '-', float64(1)}, 74 {3.0, uint(2), '*', float64(6)}, 75 {3.0, uint(2), '/', float64(1.5)}, 76 {0, 0, '+', 0}, 77 {0, 0, '-', 0}, 78 {0, 0, '*', 0}, 79 {"foo", "bar", '+', "foobar"}, 80 {3, 0, '/', false}, 81 {3.0, 0, '/', false}, 82 {3, 0.0, '/', false}, 83 {uint(3), uint(0), '/', false}, 84 {3, uint(0), '/', false}, 85 {-3, uint(0), '/', false}, 86 {uint(3), 0, '/', false}, 87 {3.0, uint(0), '/', false}, 88 {uint(3), 0.0, '/', false}, 89 {3, "foo", '+', false}, 90 {3.0, "foo", '+', false}, 91 {uint(3), "foo", '+', false}, 92 {"foo", 3, '+', false}, 93 {"foo", "bar", '-', false}, 94 {3, 2, '%', false}, 95 } { 96 result, err := DoArithmetic(test.a, test.b, test.op) 97 98 if b, ok := test.expect.(bool); ok && !b { 99 c.Assert(err, qt.Not(qt.IsNil)) 100 continue 101 } 102 103 c.Assert(err, qt.IsNil) 104 c.Assert(test.expect, qt.Equals, result) 105 } 106 }