Skip to content

Commit

Permalink
test: get rid of external dependency by implementing generic Min/Max …
Browse files Browse the repository at this point in the history
…functions (#2713)
  • Loading branch information
torwig authored Jan 8, 2025
1 parent 1b583ab commit 77b8b1f
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 12 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/kvrocks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: apache/skywalking-eyes/[email protected]
with:
config: .github/config/licenserc.yml
- uses: actions/setup-go@v5
with:
go-version-file: 'tests/gocase/go.mod'
Expand All @@ -77,9 +80,6 @@ jobs:
run: |
sudo apt update
sudo apt install -y clang-format-14 clang-tidy-14
- uses: apache/skywalking-eyes/[email protected]
with:
config: .github/config/licenserc.yml
- name: Check with clang-format
id: check-format
run: ./x.py check format --clang-format-path clang-format-14
Expand All @@ -89,7 +89,6 @@ jobs:
./x.py check tidy -j $(nproc) --clang-tidy-path clang-tidy-14 --run-clang-tidy-path run-clang-tidy-14
- name: Lint with golangci-lint
run: ./x.py check golangci-lint

- name: Prepare format patch
if: always() && steps.check-format.outcome != 'success'
run: |
Expand Down
2 changes: 0 additions & 2 deletions tests/gocase/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/shirou/gopsutil/v4 v4.24.12
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
modernc.org/mathutil v1.7.1
)

require (
Expand All @@ -19,7 +18,6 @@ require (
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
Expand Down
4 changes: 0 additions & 4 deletions tests/gocase/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/shirou/gopsutil/v4 v4.24.12 h1:qvePBOk20e0IKA1QXrIIU+jmk+zEiYVVx06WjBRlZo4=
github.com/shirou/gopsutil/v4 v4.24.12/go.mod h1:DCtMPAad2XceTeIAbGyVfycbYQNBGk2P8cvDi7/VN9o=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
Expand All @@ -48,5 +46,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
3 changes: 1 addition & 2 deletions tests/gocase/unit/type/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/apache/kvrocks/tests/gocase/util"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
"modernc.org/mathutil"
)

// We need a value larger than list-max-ziplist-value to make sure
Expand Down Expand Up @@ -85,7 +84,7 @@ func testLTRIM(t *testing.T, configs util.KvrocksServerConfigs) {
lo := int64(rand.Float64() * float64(startLen))
hi := int64(float64(lo) + rand.Float64()*float64(startLen))

myList = myList[lo:mathutil.Min(int(hi+1), len(myList))]
myList = myList[lo:util.Min(int(hi+1), len(myList))]
require.NoError(t, rdb.LTrim(ctx, key, lo, hi).Err())
require.Equal(t, myList, rdb.LRange(ctx, key, 0, -1).Val(), "failed trim")

Expand Down
38 changes: 38 additions & 0 deletions tests/gocase/util/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 util

import "cmp"

// Min returns the smaller of two values.
func Min[T cmp.Ordered](a, b T) T {
if a < b {
return a
}
return b
}

// Max returns the larger of two values.
func Max[T cmp.Ordered](a, b T) T {
if a > b {
return a
}
return b
}
105 changes: 105 additions & 0 deletions tests/gocase/util/math_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 util_test

import (
"testing"

"github.com/apache/kvrocks/tests/gocase/util"
"github.com/stretchr/testify/assert"
)

func TestMin(t *testing.T) {
// Arrange
tests := []struct {
name string
a int
b int
expectedResult int
}{
{
name: "a is smaller than b",
a: 1,
b: 2,
expectedResult: 1,
},
{
name: "a is greater than b",
a: 2,
b: 1,
expectedResult: 1,
},
{
name: "a is equal to b",
a: 1,
b: 1,
expectedResult: 1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Act
result := util.Min(tt.a, tt.b)

//Assert
assert.Equal(t, tt.expectedResult, result)
})
}
}

func TestMax(t *testing.T) {
// Arrange
tests := []struct {
name string
a int
b int
expectedResult int
}{
{
name: "a is smaller than b",
a: 1,
b: 2,
expectedResult: 2,
},
{
name: "a is greater than b",
a: 2,
b: 1,
expectedResult: 2,
},
{
name: "a is equal to b",
a: 1,
b: 1,
expectedResult: 1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Act
result := util.Max(tt.a, tt.b)

//Assert
assert.Equal(t, tt.expectedResult, result)
})
}
}

0 comments on commit 77b8b1f

Please sign in to comment.