From f285e719b821b176d13b65e983c7ec5f2d568348 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Fri, 25 Aug 2023 09:38:09 +0800 Subject: [PATCH] feat(test): add unit test (#749) * feat(test): add unit test * feat(test): add unit test --- pkg/util/bufferpool/bufferpool_test.go | 63 ++++++++++++++++++++++++++ pkg/util/bytefmt/bytefmt_test.go | 39 ++++++++++++++++ pkg/util/config/config_test.go | 52 +++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 pkg/util/bufferpool/bufferpool_test.go create mode 100644 pkg/util/config/config_test.go diff --git a/pkg/util/bufferpool/bufferpool_test.go b/pkg/util/bufferpool/bufferpool_test.go new file mode 100644 index 00000000..e86b8c20 --- /dev/null +++ b/pkg/util/bufferpool/bufferpool_test.go @@ -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") + } +} diff --git a/pkg/util/bytefmt/bytefmt_test.go b/pkg/util/bytefmt/bytefmt_test.go index 1d504642..4e4b31a0 100644 --- a/pkg/util/bytefmt/bytefmt_test.go +++ b/pkg/util/bytefmt/bytefmt_test.go @@ -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)) @@ -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}, @@ -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) + } + } +} diff --git a/pkg/util/config/config_test.go b/pkg/util/config/config_test.go new file mode 100644 index 00000000..bcabbfe3 --- /dev/null +++ b/pkg/util/config/config_test.go @@ -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) + } +}