Skip to content

Commit

Permalink
feat(test): add unit test (#749)
Browse files Browse the repository at this point in the history
* feat(test): add unit test

* feat(test): add unit test
  • Loading branch information
dongzl committed Aug 25, 2023
1 parent 9cbe6a1 commit f285e71
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/util/bufferpool/bufferpool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 bufferpool

import (
"bytes"
"testing"
)

func TestGet(t *testing.T) {
buf := Get()
if buf == nil {
t.Error("Expected non-nil buffer")
}
}

func TestPut(t *testing.T) {
buf := new(bytes.Buffer)
Put(buf)
// Ensure buffer is returned to the pool
// and can be borrowed again
buf2 := Get()
if buf != buf2 {
t.Error("Expected borrowed buffer to be the same as the put buffer")
}
}

func TestPutNilBuffer(t *testing.T) {
Put(nil)
// Ensure that putting a nil buffer does not cause any error
}

func TestPutHugeBuffer(t *testing.T) {
hugeBuf := bytes.NewBuffer(make([]byte, 0, 2*1024*1024))
Put(hugeBuf)
// Ensure that putting a huge buffer does not cause any error
}

func TestPutResetBuffer(t *testing.T) {
buf := new(bytes.Buffer)
buf.WriteString("Hello, World!")
Put(buf)
// Ensure buffer is reset before it is put back to the pool
buf2 := Get()
if buf2.Len() != 0 {
t.Error("Expected reset buffer to have zero length")
}
}
39 changes: 39 additions & 0 deletions pkg/util/bytefmt/bytefmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func TestByteSize(t *testing.T) {
{in: 10 * KILOBYTE, want: "10K"},
{in: 10.5 * KILOBYTE, want: "10.5K"},
{in: 268435456, want: "256M"},
{in: 0, want: "0"},
{in: 1000, want: "1000B"},
{in: 1024, want: "1K"},
}
for i := 0; i < len(tables); i++ {
assert.Equal(t, tables[i].want, ByteSize(tables[i].in))
Expand All @@ -55,6 +58,7 @@ func TestToBytes(t *testing.T) {
in string
want uint64
}{
{in: "10B", want: 10},
{in: "4.5KB", want: 4608},
{in: "13.5KB", want: 13824},
{in: "5MB", want: 5 * MEGABYTE},
Expand All @@ -72,3 +76,38 @@ func TestToBytes(t *testing.T) {
assert.Equal(t, tables[i].want, byteSize)
}
}

func TestToBytes_Error(t *testing.T) {
testCases := []struct {
input string
expectedResult uint64
expectedError error
}{
// Invalid input cases
{"", 0, errInvalidByteQuantity},
{" ", 0, errInvalidByteQuantity},
{"-1B", 0, errInvalidByteQuantity},
{"1KB1B", 0, errInvalidByteQuantity},
{"1MB1KB", 0, errInvalidByteQuantity},
{"1GB1MB", 0, errInvalidByteQuantity},
{"1TB1GB", 0, errInvalidByteQuantity},
{"1PB1TB", 0, errInvalidByteQuantity},
{"1EB1PB", 0, errInvalidByteQuantity},
{"1ZB", 0, errInvalidByteQuantity},
{"1ZIB", 0, errInvalidByteQuantity},
}

for _, testCase := range testCases {
result, err := ToBytes(testCase.input)

// Assert the result
if result != testCase.expectedResult {
t.Errorf("Expected result: %d, but got: %d", testCase.expectedResult, result)
}

// Assert the error
if err != testCase.expectedError {
t.Errorf("Expected error: %v, but got: %v", testCase.expectedError, err)
}
}
}
52 changes: 52 additions & 0 deletions pkg/util/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 config

import (
"testing"
)

func TestIsEnableLocalMathCompu(t *testing.T) {
// Test case 1: Enable local math computation
_enableLocalMathCompu = false
got := IsEnableLocalMathCompu(true)
if got != true {
t.Errorf("Expected true, got %v", got)
}

// Test case 2: Disable local math computation
_enableLocalMathCompu = true
got = IsEnableLocalMathCompu(false)
if got != true {
t.Errorf("Expected true, got %v", got)
}

// Test case 3: Local math computation already enabled
_enableLocalMathCompu = true
got = IsEnableLocalMathCompu(true)
if got != true {
t.Errorf("Expected true, got %v", got)
}

// Test case 4: Local math computation already disabled
_enableLocalMathCompu = false
got = IsEnableLocalMathCompu(false)
if got != false {
t.Errorf("Expected false, got %v", got)
}
}

0 comments on commit f285e71

Please sign in to comment.