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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
package bjc.funcdata;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
@SuppressWarnings("javadoc")
public class TestMapOperations {
private MapEx<String, String> map;
@Before
public void setUp() throws Exception {
map = MapEx.of("a", "A", "b", "B");
}
@Test
public void sizeMatchesExpected() {
assertEquals("Constructed map is of size 2", 2, map.size());
}
@Test
public void containsExpectedKey() {
assertTrue("Constructed map contains key 'a'", map.containsKey("a"));
}
@Test
public void getYieldsExpectedValue() {
assertEquals("Constructed map has key 'a' mapped to value 'A'", "A", map.get("a"));
}
@Test
public void doesNotContainNotAddedKey() {
assertFalse("Constructed map doesn't contain key 'c'", map.containsKey("c"));
}
@Test(expected = IllegalArgumentException.class)
public void getOfNonexistentKeyThrows() {
map.get("c");
}
@Test
public void getOrDefaultOfExistingKeyYieldsExistingValue() {
assertEquals("Constructed map getOrDefault for an existing key yields the existing value", "A", map.get("a"));
}
@Test
public void putOfNonExistingKeyAddsValue() {
map.put("c", "C");
assertEquals("Constructed map now has 3 items", 3, map.size());
assertEquals("Constructed map now has 'c' mapped to 'C'", "C", map.get("c"));
}
@Test
public void putOfExistingKeyUpdatesValue() {
String val = map.put("a", "D");
assertEquals("Constructed map still contains 2 items", 2, map.size());
assertEquals("Constructed map now has 'a' mapped to 'D'", "D", map.get("a"));
assertEquals("put method returned old value of 'A'", "A", val);
}
@Test
public void forEachGetsExpectedElements() {
List<String> result = new ArrayList<>();
map.forEach((key, value) -> {
result.add(key + " = " + value);
});
assertArrayEquals("For-each has the expected elements",
new String[] {"a = A", "b = B"},
result.toArray());
}
@Test
public void forEachKeyGetsAddedKeys() {
List<String> keys = new ArrayList<>();
map.forEachKey((key) -> {
keys.add(key);
});
assertArrayEquals("forEachKey gives the expected keys",
new String[] {"a", "b"},
keys.toArray());
}
@Test
public void forEachValueGetsAddedValues() {
List<String> keys = new ArrayList<>();
map.forEachValue((key) -> {
keys.add(key);
});
assertArrayEquals("forEachKey gives the expected values",
new String[] {"A", "B"},
keys.toArray());
}
@Test
public void clearRemovesAllValues() {
map.clear();
assertEquals("A cleared map contains no items", 0, map.size());
}
@Test
public void removeOfExistingKeyRemovesKey() {
String removed = map.remove("a");
assertEquals("Constructed map now has one less element", 1, map.size());
assertFalse("Constructed map no longer contains a removed key", map.containsKey("a"));
assertEquals("Remove returns the removed value", "A", removed);
}
@Test
public void removeOfNonExistingKeyDoesntRemoveAnything() {
String removed = map.remove("c");
assertEquals("Constructed map still contains 2 elements", 2, map.size());
assertNull("remove of a non-existing key returns null", removed);
}
@Test
public void keyListReturnsListOfKeys() {
assertArrayEquals("Constructed map key-list has the expected elements",
new Object[] {"a", "b"},
map.keyList().toArray(new String[0]));
}
@Test
public void valueListReturnsListOfValues() {
assertArrayEquals("Constructed map value-list has the expected elements",
new Object[] {"A", "B"},
map.valueList().toArray(new String[0]));
}
@Test
public void mapIsThawedByDefault() {
assertFalse("isFrozen is false by default for a map", map.isFrozen());
}
@Test
public void canFreezeMap() {
assertTrue("canFreeze is true for a map", map.canFreeze());
assertTrue("freeze freezes a map", map.freeze());
assertTrue("isFrozen indicates a map is frozen", map.isFrozen());
}
@Test(expected = ObjectFrozen.class)
public void clearOfFrozenMapFails() {
map.freeze();
map.clear();
}
}
|