This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new c7d192996 [Improve] add MapCap & LruMap unit test (#2227)
c7d192996 is described below
commit c7d192996afbb64d20471a1d74920a895750808f
Author: YuLuo <[email protected]>
AuthorDate: Sat Jul 6 22:55:58 2024 +0800
[Improve] add MapCap & LruMap unit test (#2227)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
---
.../hertzbeat/common/util/LruHashMapTest.java | 50 +++++++++++++++++++++-
.../{LruHashMapTest.java => MapCapUtilTest.java} | 24 ++++++++---
2 files changed, 68 insertions(+), 6 deletions(-)
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java
index 8890f7401..73a4642d8 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java
@@ -19,12 +19,60 @@ package org.apache.hertzbeat.common.util;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
/**
* Test case for {@link LruHashMap}
*/
class LruHashMapTest {
@Test
- void removeEldestEntry() {
+ void testLruHashMap() {
+
+ int initThreshold = 3;
+ LruHashMap<Integer, String> initLruMap = new
LruHashMap<>(initThreshold);
+ assertNotNull(initLruMap);
+ assertEquals(0, initLruMap.size());
+
+ int putAndGetThreshold = 2;
+ LruHashMap<Integer, String> putAndGetLruMap = new
LruHashMap<>(putAndGetThreshold);
+
+ putAndGetLruMap.put(1, "one");
+ putAndGetLruMap.put(2, "two");
+
+ // Both entries should be present
+ assertEquals("one", putAndGetLruMap.get(1));
+ assertEquals("two", putAndGetLruMap.get(2));
+
+ int evictionThreshold = 2;
+ LruHashMap<Integer, String> evictionLruMap = new
LruHashMap<>(evictionThreshold);
+
+ evictionLruMap.put(1, "one");
+ evictionLruMap.put(2, "two");
+ evictionLruMap.put(3, "three");
+
+ // The least recently used entry (1, "one") should be evicted
+ assertNull(evictionLruMap.get(1));
+ assertEquals("two", evictionLruMap.get(2));
+ assertEquals("three", evictionLruMap.get(3));
+
+ int accessOrderThreshold = 2;
+ LruHashMap<Integer, String> accessOrderLruMap = new
LruHashMap<>(accessOrderThreshold);
+
+ accessOrderLruMap.put(1, "one");
+ accessOrderLruMap.put(2, "two");
+
+ // Access the first entry to make it recently used
+ accessOrderLruMap.get(1);
+
+ accessOrderLruMap.put(3, "three");
+
+ // The least recently used entry (2, "two") should be evicted
+ assertEquals("one", accessOrderLruMap.get(1));
+ assertNull(accessOrderLruMap.get(2));
+ assertEquals("three", accessOrderLruMap.get(3));
}
+
}
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java
similarity index 64%
copy from
common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java
copy to
common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java
index 8890f7401..fac11a9cd 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/LruHashMapTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/MapCapUtilTest.java
@@ -19,12 +19,26 @@ package org.apache.hertzbeat.common.util;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
/**
- * Test case for {@link LruHashMap}
+ * Test for {@link MapCapUtil}
*/
-class LruHashMapTest {
+class MapCapUtilTest {
+
+ @Test
+ public void testCalInitMap() {
+ int size = 0;
+ int expectedCapacity = (int) Math.ceil(size / 0.75);
+ int actualCapacity = MapCapUtil.calInitMap(size);
+
+ assertEquals(expectedCapacity, actualCapacity);
+
+ size = 10;
+ expectedCapacity = (int) Math.ceil(size / 0.75);
+ actualCapacity = MapCapUtil.calInitMap(size);
+
+ assertEquals(expectedCapacity, actualCapacity);
+ }
- @Test
- void removeEldestEntry() {
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]