| 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 |
{0, 0, '+', int64(0)}, |
| 34 |
{3, 2, '-', int64(1)}, |
| 35 |
{3, 2, '*', int64(6)}, |
| 36 |
{3, 2, '/', int64(1)}, |
| 37 |
{3.0, 2, '+', float64(5)}, |
| 38 |
{0.0, 0, '+', float64(0.0)}, |
| 39 |
{3.0, 2, '-', float64(1)}, |
| 40 |
{3.0, 2, '*', float64(6)}, |
| 41 |
{3.0, 2, '/', float64(1.5)}, |
| 42 |
{3, 2.0, '+', float64(5)}, |
| 43 |
{3, 2.0, '-', float64(1)}, |
| 44 |
{3, 2.0, '*', float64(6)}, |
| 45 |
{3, 2.0, '/', float64(1.5)}, |
| 46 |
{3.0, 2.0, '+', float64(5)}, |
| 47 |
{0.0, 0.0, '+', float64(0.0)}, |
| 48 |
{3.0, 2.0, '-', float64(1)}, |
| 49 |
{3.0, 2.0, '*', float64(6)}, |
| 50 |
{3.0, 2.0, '/', float64(1.5)}, |
| 51 |
{uint(3), uint(2), '+', uint64(5)}, |
| 52 |
{uint(0), uint(0), '+', uint64(0)}, |
| 53 |
{uint(3), uint(2), '-', uint64(1)}, |
| 54 |
{uint(3), uint(2), '*', uint64(6)}, |
| 55 |
{uint(3), uint(2), '/', uint64(1)}, |
| 56 |
{uint(3), 2, '+', uint64(5)}, |
| 57 |
{uint(0), 0, '+', uint64(0)}, |
| 58 |
{uint(3), 2, '-', uint64(1)}, |
| 59 |
{uint(3), 2, '*', uint64(6)}, |
| 60 |
{uint(3), 2, '/', uint64(1)}, |
| 61 |
{3, uint(2), '+', uint64(5)}, |
| 62 |
{0, uint(0), '+', uint64(0)}, |
| 63 |
{3, uint(2), '-', uint64(1)}, |
| 64 |
{3, uint(2), '*', uint64(6)}, |
| 65 |
{3, uint(2), '/', uint64(1)}, |
| 66 |
{uint(3), -2, '+', int64(1)}, |
| 67 |
{uint(3), -2, '-', int64(5)}, |
| 68 |
{uint(3), -2, '*', int64(-6)}, |
| 69 |
{uint(3), -2, '/', int64(-1)}, |
| 70 |
{-3, uint(2), '+', int64(-1)}, |
| 71 |
{-3, uint(2), '-', int64(-5)}, |
| 72 |
{-3, uint(2), '*', int64(-6)}, |
| 73 |
{-3, uint(2), '/', int64(-1)}, |
| 74 |
{uint(3), 2.0, '+', float64(5)}, |
| 75 |
{uint(0), 0.0, '+', float64(0)}, |
| 76 |
{uint(3), 2.0, '-', float64(1)}, |
| 77 |
{uint(3), 2.0, '*', float64(6)}, |
| 78 |
{uint(3), 2.0, '/', float64(1.5)}, |
| 79 |
{3.0, uint(2), '+', float64(5)}, |
| 80 |
{0.0, uint(0), '+', float64(0)}, |
| 81 |
{3.0, uint(2), '-', float64(1)}, |
| 82 |
{3.0, uint(2), '*', float64(6)}, |
| 83 |
{3.0, uint(2), '/', float64(1.5)}, |
| 84 |
{"foo", "bar", '+', "foobar"}, |
| 85 |
{3, 0, '/', false}, |
| 86 |
{3.0, 0, '/', false}, |
| 87 |
{3, 0.0, '/', false}, |
| 88 |
{uint(3), uint(0), '/', false}, |
| 89 |
{3, uint(0), '/', false}, |
| 90 |
{-3, uint(0), '/', false}, |
| 91 |
{uint(3), 0, '/', false}, |
| 92 |
{3.0, uint(0), '/', false}, |
| 93 |
{uint(3), 0.0, '/', false}, |
| 94 |
{3, "foo", '+', false}, |
| 95 |
{3.0, "foo", '+', false}, |
| 96 |
{uint(3), "foo", '+', false}, |
| 97 |
{"foo", 3, '+', false}, |
| 98 |
{"foo", "bar", '-', false}, |
| 99 |
{3, 2, '%', false}, |
| 100 |
} { |
| 101 |
result, err := DoArithmetic(test.a, test.b, test.op) |
| 102 |
|
| 103 |
if b, ok := test.expect.(bool); ok && !b { |
| 104 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 105 |
continue |
| 106 |
} |
| 107 |
|
| 108 |
c.Assert(err, qt.IsNil) |
| 109 |
c.Assert(test.expect, qt.Equals, result) |
| 110 |
} |
| 111 |
} |