From 8b3121bb154680b03bfe038b0cd8d78e566c4988 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Wed, 24 Jul 2024 10:43:26 +0800 Subject: [PATCH 1/3] [Improve] add StrBuffer unit test Signed-off-by: yuluo-yx --- .../hertzbeat/common/util/StrBufferTest.java | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java new file mode 100644 index 00000000000..3c8f3b8dcf9 --- /dev/null +++ b/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java @@ -0,0 +1,137 @@ +package org.apache.hertzbeat.common.util; + +import org.junit.jupiter.api.Test; + +import static java.lang.Double.NEGATIVE_INFINITY; +import static java.lang.Double.POSITIVE_INFINITY; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * test case for {@link StrBuffer} + */ + +class StrBufferTest { + + private static final String POSITIVE_INF = "+inf"; + private static final String NEGATIVE_INF = "-inf"; + private static final long POSITIVE_INF_VALUE = 0x7FF0000000000000L; + private static final long NEGATIVE_INF_VALUE = 0xFFF0000000000000L; + + @Test + void testRead() { + + StrBuffer buffer = new StrBuffer("hello"); + + assertEquals('h', buffer.read()); + assertEquals('e', buffer.read()); + assertEquals('l', buffer.read()); + assertEquals('l', buffer.read()); + assertEquals('o', buffer.read()); + assertThrows(IndexOutOfBoundsException.class, buffer::read); + } + + @Test + void testRollback() { + + StrBuffer buffer = new StrBuffer("hello"); + + assertEquals('h', buffer.read()); + buffer.rollback(); + assertEquals('h', buffer.read()); + buffer.read(); + buffer.read(); + buffer.rollback(); + assertEquals('l', buffer.read()); + } + + @Test + void testCharAt() { + + StrBuffer buffer = new StrBuffer("hello"); + + assertEquals('h', buffer.charAt(0)); + assertEquals('e', buffer.charAt(1)); + assertEquals('l', buffer.charAt(2)); + assertEquals('l', buffer.charAt(3)); + assertEquals('o', buffer.charAt(4)); + assertThrows(IndexOutOfBoundsException.class, () -> buffer.charAt(5)); + } + + @Test + void testToStr() { + + StrBuffer buffer = new StrBuffer("hello"); + + assertEquals("hello", buffer.toStr()); + buffer.read(); + assertEquals("ello", buffer.toStr()); + } + + @Test + void testToDouble() { + + StrBuffer buffer = new StrBuffer("123.45"); + assertEquals(123.45, buffer.toDouble()); + + buffer = new StrBuffer("+inf"); + assertEquals(POSITIVE_INFINITY, buffer.toDouble()); + + buffer = new StrBuffer("-inf"); + assertEquals(NEGATIVE_INFINITY, buffer.toDouble()); + } + + @Test + void testToLong() { + + StrBuffer buffer = new StrBuffer("12345"); + assertEquals(12345L, buffer.toLong()); + + buffer = new StrBuffer("+inf"); + assertEquals(POSITIVE_INF_VALUE, buffer.toLong()); + + buffer = new StrBuffer("-inf"); + assertEquals(NEGATIVE_INF_VALUE, buffer.toLong()); + } + + @Test + void testSkipBlankTabs() { + + StrBuffer buffer = new StrBuffer(" \t hello \t "); + buffer.skipBlankTabs(); + assertEquals("hello", buffer.toStr()); + } + + @Test + void testIsEmpty() { + + StrBuffer buffer = new StrBuffer(""); + assertTrue(buffer.isEmpty()); + + buffer = new StrBuffer(" \t "); + buffer.skipBlankTabs(); + assertTrue(buffer.isEmpty()); + + buffer = new StrBuffer("hello"); + assertFalse(buffer.isEmpty()); + } + + @Test + void testParseLong() { + + assertEquals(12345L, StrBuffer.parseLong("12345")); + assertEquals(POSITIVE_INF_VALUE, StrBuffer.parseLong("+inf")); + assertEquals(NEGATIVE_INF_VALUE, StrBuffer.parseLong("-inf")); + } + + @Test + void testParseDouble() { + + assertEquals(123.45, StrBuffer.parseDouble("123.45")); + assertEquals(POSITIVE_INFINITY, StrBuffer.parseDouble("+inf")); + assertEquals(NEGATIVE_INFINITY, StrBuffer.parseDouble("-inf")); + } + +} From 95192aa40ffe5582b182145d0e9f044d52633713 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Wed, 24 Jul 2024 10:45:29 +0800 Subject: [PATCH 2/3] fix Signed-off-by: yuluo-yx --- .../org/apache/hertzbeat/common/util/StrBufferTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java index 3c8f3b8dcf9..b54d591134e 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java @@ -77,10 +77,10 @@ void testToDouble() { assertEquals(123.45, buffer.toDouble()); buffer = new StrBuffer("+inf"); - assertEquals(POSITIVE_INFINITY, buffer.toDouble()); + assertEquals(POSITIVE_INF_VALUE, buffer.toDouble()); buffer = new StrBuffer("-inf"); - assertEquals(NEGATIVE_INFINITY, buffer.toDouble()); + assertEquals(NEGATIVE_INF_VALUE, buffer.toDouble()); } @Test @@ -130,8 +130,8 @@ void testParseLong() { void testParseDouble() { assertEquals(123.45, StrBuffer.parseDouble("123.45")); - assertEquals(POSITIVE_INFINITY, StrBuffer.parseDouble("+inf")); - assertEquals(NEGATIVE_INFINITY, StrBuffer.parseDouble("-inf")); + assertEquals(POSITIVE_INF_VALUE, StrBuffer.parseDouble("+inf")); + assertEquals(NEGATIVE_INF_VALUE, StrBuffer.parseDouble("-inf")); } } From f2d5b5fec9d97d2cbd5ae63baed3d8d6ed611b6f Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Wed, 24 Jul 2024 10:46:38 +0800 Subject: [PATCH 3/3] fix Signed-off-by: yuluo-yx --- .../hertzbeat/common/util/StrBufferTest.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java index b54d591134e..ff1e117e50e 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java @@ -1,9 +1,24 @@ +/* + * 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 org.apache.hertzbeat.common.util; import org.junit.jupiter.api.Test; -import static java.lang.Double.NEGATIVE_INFINITY; -import static java.lang.Double.POSITIVE_INFINITY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -122,8 +137,8 @@ void testIsEmpty() { void testParseLong() { assertEquals(12345L, StrBuffer.parseLong("12345")); - assertEquals(POSITIVE_INF_VALUE, StrBuffer.parseLong("+inf")); - assertEquals(NEGATIVE_INF_VALUE, StrBuffer.parseLong("-inf")); + assertEquals(POSITIVE_INF_VALUE, StrBuffer.parseLong(POSITIVE_INF)); + assertEquals(NEGATIVE_INF_VALUE, StrBuffer.parseLong(NEGATIVE_INF)); } @Test