Skip to content

Commit

Permalink
feat(test): add unit test (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
dongzl authored Sep 4, 2023
1 parent 304e9ff commit 01c7adf
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/reduce/reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

import (
"github.com/shopspring/decimal"

"github.com/stretchr/testify/assert"
)

Expand Down
39 changes: 39 additions & 0 deletions pkg/util/math/abs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package math

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestAbs(t *testing.T) {
// Test cases for positive numbers
assert.Equal(t, 5, Abs(5))
assert.Equal(t, int32(10), Abs(int32(10)))

// Test cases for negative numbers
assert.Equal(t, 5, Abs(-5))
assert.Equal(t, int64(10), Abs(int64(-10)))

// Test case for zero
assert.Equal(t, 0, Abs(0))
}
61 changes: 61 additions & 0 deletions pkg/util/math/compare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package math

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestMax(t *testing.T) {
// Test case 1:a > b
result := Max(5, 3)
assert.Equal(t, 5, result)

// Test case 2:a < b
// result = Max(2.5, 6.7)
// expected = 6.7
// if result != expected {
// t.Errorf("Max(2.5, 6.7) returned:%f, expected:%f", result, expected)
// }

// Test case 3:a == b
result = Max(-10, -10)
assert.Equal(t, -10, result)
}

func TestMin(t *testing.T) {
// Test case 1: a is smaller than b
result := Min(2, 5)
assert.Equal(t, 2, result)

// Test case 2: b is smaller than a
result = Min(7, 3)
assert.Equal(t, 3, result)

// Test case 3: a and b are equal
result = Min(4, 4)
assert.Equal(t, 4, result)

// Test case 4: a is a floating-point number
resultFloat := Min(2.5, 1.5)
assert.Equal(t, 1.5, resultFloat)
}
126 changes: 126 additions & 0 deletions pkg/util/math/decimal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package math

import (
"testing"
)

import (
gxbig "github.com/dubbogo/gost/math/big"

"github.com/stretchr/testify/assert"
)

func TestToDecimal(t *testing.T) {
// Test cases for *gxbig.Decimal input
decimalInput, _ := gxbig.NewDecFromString("10.5")
result := ToDecimal(decimalInput)
assert.Equal(t, decimalInput, result)

// Test cases for float64 input
float64Input := 10.5
expectedResult, _ := gxbig.NewDecFromString("10.5")
result = ToDecimal(float64Input)
assert.Equal(t, expectedResult, result)

// Test cases for float32 input
float32Input := float32(10.5)
expectedResult, _ = gxbig.NewDecFromString("10.5")
result = ToDecimal(float32Input)
assert.Equal(t, expectedResult, result)

// Test cases for int input
intInput := 10
expectedResult = gxbig.NewDecFromInt(int64(10))
result = ToDecimal(intInput)
assert.Equal(t, expectedResult, result)

// Test cases for int64 input
int64Input := int64(10)
expectedResult = gxbig.NewDecFromInt(int64(10))
result = ToDecimal(int64Input)
assert.Equal(t, expectedResult, result)

// Test cases for int32 input
int32Input := int32(10)
expectedResult = gxbig.NewDecFromInt(int64(10))
result = ToDecimal(int32Input)
assert.Equal(t, expectedResult, result)

// Test cases for int16 input
int16Input := int16(10)
expectedResult = gxbig.NewDecFromInt(int64(10))
result = ToDecimal(int16Input)
assert.Equal(t, expectedResult, result)

// Test cases for int8 input
int8Input := int8(10)
expectedResult = gxbig.NewDecFromInt(int64(10))
result = ToDecimal(int8Input)
assert.Equal(t, expectedResult, result)

// Test cases for uint8 input
uint8Input := uint8(10)
expectedResult = gxbig.NewDecFromUint(uint64(10))
result = ToDecimal(uint8Input)
assert.Equal(t, expectedResult, result)

// Test cases for uint16 input
uint16Input := uint16(10)
expectedResult = gxbig.NewDecFromUint(uint64(10))
result = ToDecimal(uint16Input)
assert.Equal(t, expectedResult, result)

// Test cases for uint32 input
uint32Input := uint32(10)
expectedResult = gxbig.NewDecFromUint(uint64(10))
result = ToDecimal(uint32Input)
assert.Equal(t, expectedResult, result)

// Test cases for uint64 input
uint64Input := uint64(10)
expectedResult = gxbig.NewDecFromUint(uint64(10))
result = ToDecimal(uint64Input)
assert.Equal(t, expectedResult, result)

// Test cases for uint input
uintInput := uint(10)
expectedResult = gxbig.NewDecFromUint(uint64(10))
result = ToDecimal(uintInput)
assert.Equal(t, expectedResult, result)

// Test case for unknown input type
unknownInput := "invalid"
expectedResult, _ = gxbig.NewDecFromString("0.0")
result = ToDecimal(unknownInput)
assert.Equal(t, expectedResult, result)
}

func TestIsZero(t *testing.T) {
// Test case 1: d is nil, should return false
assert.False(t, IsZero(nil))

// Test case 2: d is not nil and is zero, should return true
d, _ := gxbig.NewDecFromString("0")
assert.True(t, IsZero(d))

// Test case 3: d is not nil and not zero, should return false
f, _ := gxbig.NewDecFromString("10")
assert.False(t, IsZero(f))
}

0 comments on commit 01c7adf

Please sign in to comment.