Skip to content

Commit 48d84fa

Browse files
committed
add unit test
1 parent 173ae8c commit 48d84fa

File tree

7 files changed

+589
-2
lines changed

7 files changed

+589
-2
lines changed

src/main/java/cn/ipman/cache/core/operator/StringOperator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import cn.ipman.cache.core.AbstractOperator;
44

55
import java.util.ArrayList;
6-
import java.util.Arrays;
76
import java.util.List;
8-
import java.util.Objects;
97

108
/**
119
* Description for this class
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cn.ipman.cache.operator;
2+
import cn.ipman.cache.core.AbstractOperator;
3+
import cn.ipman.cache.core.operator.CommonOperator;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import static junit.framework.TestCase.assertEquals;
7+
8+
/**
9+
* Description for this class
10+
*
11+
* @Author IpMan
12+
* @Date 2024/6/23 23:09
13+
*/
14+
public class CommonOperatorTest {
15+
private CommonOperator operator;
16+
17+
@Before
18+
public void setUp() {
19+
operator = new CommonOperator();
20+
// Assume that operator.map is initialized here
21+
}
22+
23+
@Test
24+
public void testDelWithNullKeys() {
25+
assertEquals("Expected 0 when keys are null", 0, operator.del((String[]) null));
26+
}
27+
28+
@Test
29+
public void testDelWithEmptyKeys() {
30+
assertEquals("Expected 0 when keys are empty", 0, operator.del());
31+
}
32+
33+
@Test
34+
public void testExistsWithNullKeys() {
35+
assertEquals("Expected 0 when keys are null", 0, operator.exists((String[]) null));
36+
}
37+
38+
@Test
39+
public void testExistsWithEmptyKeys() {
40+
assertEquals("Expected 0 when keys are empty", 0, operator.exists());
41+
}
42+
43+
@Test
44+
public void testExpireWithNonExistentKey() {
45+
assertEquals("Expected 0 when key does not exist", 0, operator.expire("nonexistentKey", 10));
46+
}
47+
48+
@Test
49+
public void testExpireWithValidKey() {
50+
// Assume that operator.map contains a key "key1"
51+
assertEquals("Expected 1 when expiring an existing key", 1, operator.expire("key1", 10));
52+
}
53+
54+
@Test
55+
public void testTtlWithNonExistentKey() {
56+
assertEquals("Expected -2 when key does not exist", -2, operator.ttl("nonexistentKey"));
57+
}
58+
59+
@Test
60+
public void testTtlWithKeyHavingDefaultTtl() {
61+
// Assume that operator.map contains a key "key1" with default ttl
62+
assertEquals("Expected -1 when key has default ttl", -1, operator.ttl("key1"));
63+
}
64+
65+
@Test
66+
public void testTtlWithValidKey() {
67+
// Assume that operator.map contains a key "key1" with a custom ttl and timestamp
68+
long current = System.currentTimeMillis();
69+
long ttl = 10;
70+
long ts = current - (ttl * 1000L);
71+
AbstractOperator.CacheEntry<Object> entry = new AbstractOperator.CacheEntry<>(null, ttl, ts);
72+
operator.map.put("key1", entry);
73+
74+
long expectedTtl = (ts + ttl * 1000L - current) / 1000;
75+
if (expectedTtl > 0) {
76+
assertEquals("Expected ttl to be greater than 0 for valid key", (int) expectedTtl, operator.ttl("key1"));
77+
} else {
78+
assertEquals("Expected -1 when key has expired", -1, operator.ttl("key1"));
79+
}
80+
}
81+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package cn.ipman.cache.operator;
2+
3+
import cn.ipman.cache.core.operator.HashOperator;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import java.util.Arrays;
7+
import static junit.framework.TestCase.*;
8+
9+
10+
/**
11+
* Description for this class
12+
*
13+
* @Author IpMan
14+
* @Date 2024/6/23 23:12
15+
*/
16+
public class HashOperatorTest {
17+
private HashOperator operator;
18+
19+
@Before
20+
public void setUp() {
21+
operator = new HashOperator();
22+
}
23+
24+
@Test
25+
public void testHSet() {
26+
String key = "testKey";
27+
String[] hKeys = {"key1", "key2"};
28+
String[] hVals = {"val1", "val2"};
29+
30+
Integer result = operator.hSet(key, hKeys, hVals);
31+
assertNotNull(result);
32+
assertEquals(2, result.intValue());
33+
34+
String[] getAllResult = operator.hGetAll(key);
35+
assertNotNull(getAllResult);
36+
assertEquals(4, getAllResult.length);
37+
assertTrue(Arrays.asList(getAllResult).containsAll(Arrays.asList(hKeys)));
38+
assertTrue(Arrays.asList(getAllResult).containsAll(Arrays.asList(hVals)));
39+
}
40+
41+
@Test
42+
public void testHGet() {
43+
String key = "testKey";
44+
String hKey = "key1";
45+
String expectedValue = "val1";
46+
operator.hSet(key, new String[]{"key1"}, new String[]{expectedValue});
47+
String result = operator.hGet(key, hKey);
48+
assertEquals(expectedValue, result);
49+
}
50+
51+
@Test
52+
public void testHGetAll() {
53+
String key = "testKey";
54+
String[] hKeys = {"key1", "key2"};
55+
String[] hVals = {"val1", "val2"};
56+
operator.hSet(key, hKeys, hVals);
57+
String[] result = operator.hGetAll(key);
58+
assertNotNull(result);
59+
assertEquals(4, result.length);
60+
assertTrue(Arrays.asList(result).containsAll(Arrays.asList(hKeys)));
61+
assertTrue(Arrays.asList(result).containsAll(Arrays.asList(hVals)));
62+
}
63+
64+
@Test
65+
public void testHMGet() {
66+
String key = "testKey";
67+
String[] hKeys = {"key1", "key2"};
68+
String[] hVals = {"val1", "val2"};
69+
operator.hSet(key, hKeys, hVals);
70+
String[] result = operator.hMGet(key, hKeys);
71+
assertNotNull(result);
72+
assertEquals(2, result.length);
73+
assertEquals("val1", result[0]);
74+
assertEquals("val2", result[1]);
75+
}
76+
77+
@Test
78+
public void testHLen() {
79+
String key = "testKey";
80+
String[] hKeys = {"key1", "key2"};
81+
String[] hVals = {"val1", "val2"};
82+
operator.hSet(key, hKeys, hVals);
83+
Integer result = operator.hLen(key);
84+
assertNotNull(result);
85+
assertEquals(2, result.intValue());
86+
}
87+
88+
@Test
89+
public void testHExists() {
90+
String key = "testKey";
91+
String hKey = "key1";
92+
operator.hSet(key, new String[]{hKey}, new String[]{"val1"});
93+
Integer result = operator.hExists(key, hKey);
94+
assertNotNull(result);
95+
assertEquals(1, result.intValue());
96+
}
97+
98+
@Test
99+
public void testHDel() {
100+
String key = "testKey";
101+
String[] hKeys = {"key1", "key2"};
102+
String[] hVals = {"val1", "val2"};
103+
operator.hSet(key, hKeys, hVals);
104+
Integer result = operator.hDel(key, hKeys);
105+
assertNotNull(result);
106+
assertEquals(2, result.intValue());
107+
assertNull(operator.hGet(key, hKeys[0]));
108+
assertNull(operator.hGet(key, hKeys[1]));
109+
}
110+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package cn.ipman.cache.operator;
2+
import cn.ipman.cache.core.operator.CommonOperator;
3+
import cn.ipman.cache.core.operator.ListOperator;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import static org.junit.Assert.*;
7+
8+
/**
9+
* Description for this class
10+
*
11+
* @Author IpMan
12+
* @Date 2024/6/23 23:15
13+
*/
14+
public class ListOperatorTest {
15+
private ListOperator operator;
16+
private CommonOperator commonOperator;
17+
18+
@Before
19+
public void setUp() {
20+
operator = new ListOperator();
21+
commonOperator = new CommonOperator();
22+
}
23+
24+
@Test
25+
public void testLPush() {
26+
String key = "listKey";
27+
commonOperator.del(key);
28+
String[] vals = {"val1", "val2", "val3"};
29+
Integer result = operator.lPush(key, vals);
30+
assertEquals(Integer.valueOf(3), result);
31+
}
32+
33+
@Test
34+
public void testLPop() {
35+
String key = "listKey";
36+
commonOperator.del(key);
37+
String[] vals = {"val1", "val2", "val3"};
38+
operator.lPush(key, vals);
39+
String[] result = operator.lPop(key, 2);
40+
assertNotNull(result);
41+
assertEquals(2, result.length);
42+
}
43+
44+
@Test
45+
public void testRPush() {
46+
String key = "listKey";
47+
commonOperator.del(key);
48+
String[] vals = {"val1", "val2", "val3"};
49+
Integer result = operator.rPush(key, vals);
50+
assertEquals(Integer.valueOf(3), result);
51+
}
52+
53+
@Test
54+
public void testRPop() {
55+
String key = "listKey";
56+
commonOperator.del(key);
57+
String[] vals = {"val1", "val2", "val3"};
58+
operator.rPush(key, vals);
59+
String[] result = operator.rPop(key, 2);
60+
assertNotNull(result);
61+
assertEquals(2, result.length);
62+
assertEquals("val3", result[0]);
63+
assertEquals("val2", result[1]);
64+
}
65+
66+
@Test
67+
public void testLLen() {
68+
String key = "listKey";
69+
commonOperator.del(key);
70+
String[] vals = {"val1", "val2", "val3"};
71+
operator.rPush(key, vals);
72+
Integer result = operator.lLen(key);
73+
assertEquals(Integer.valueOf(3), result);
74+
}
75+
76+
@Test
77+
public void testLIndex() {
78+
String key = "listKey";
79+
commonOperator.del(key);
80+
String[] vals = {"val1", "val2", "val3"};
81+
operator.rPush(key, vals);
82+
String result = operator.lIndex(key, 1);
83+
assertEquals("val2", result);
84+
}
85+
86+
@Test
87+
public void testLRange() {
88+
String key = "listKey";
89+
commonOperator.del(key);
90+
String[] vals = {"val1", "val2", "val3"};
91+
operator.rPush(key, vals);
92+
String[] result = operator.lRange(key, 0, 1);
93+
assertNotNull(result);
94+
assertEquals(2, result.length);
95+
assertEquals("val1", result[0]);
96+
assertEquals("val2", result[1]);
97+
}
98+
}

0 commit comments

Comments
 (0)