-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTest.java
More file actions
100 lines (81 loc) · 2.49 KB
/
HashTest.java
File metadata and controls
100 lines (81 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.heedi.spring;
import com.heedi.spring.model.Cat;
import com.heedi.spring.model.Character;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class HashTest {
private static final Character KIND = Character
.builder()
.tendency("kind")
.build();
private static final Cat MOMO = Cat.builder()
.name("momo")
.age(5)
.character(KIND)
.build();
private static final Cat DD = Cat.builder()
.name("dd")
.age(6)
.character(KIND)
.build();
private Hashtable<Character, Cat> table = new Hashtable<>();
private HashMap<Cat, Integer> map = new HashMap<>();
private ConcurrentHashMap<Cat, String> concurrentHashMap = new ConcurrentHashMap<>();
@Test
void hashtable_synchronized() {
// method 자체에 synchronized 처리
// this에 Lock이 걸림 (성능 저하)
table.put(KIND, MOMO);
table.get(KIND);
}
@Test
void hashtable_null_value() {
assertThatNullPointerException()
.isThrownBy(() -> table.put(KIND, null));
}
@Test
void hashtable_null_key() {
assertThatNullPointerException()
.isThrownBy(() -> table.put(null, DD));
}
@Test
void map_synchronized() {
// no synchronized
map.put(MOMO, 3);
// no synchronized
map.get(KIND);
}
@Test
void hashMap_null_value() {
map.put(DD, null);
assertNull(map.get(DD));
}
@Test
void hashMap_null_key() {
map.put(null, 0);
assertEquals(0, (int) map.get(null));
}
@Test
void concurrentHashMap_s() {
// Node에 synchronized 처리
// MultiThread에 유리
concurrentHashMap.put(MOMO, "MOMO");
// no synchronized
concurrentHashMap.get(MOMO);
}
@Test
void concurrentHashMap_null_value() {
assertThatNullPointerException()
.isThrownBy(() -> concurrentHashMap.put(MOMO, null));
}
@Test
void concurrentHashMap_null_key() {
assertThatNullPointerException()
.isThrownBy(() -> concurrentHashMap.put(null, "DD"));
}
}