generated from ohjelmointi2/gradle-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMapBasicsTest.java
More file actions
172 lines (136 loc) · 5.52 KB
/
MapBasicsTest.java
File metadata and controls
172 lines (136 loc) · 5.52 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
package part01;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
public class MapBasicsTest {
private static final Map<String, String> ips = Map.of(
"home", "127.0.0.1",
"google", "8.8.8.8",
"cloudflare", "1.1.1.1");
private static final Map<String, String> hexColors = Map.of(
"red", "#ff0000",
"green", "#00ff00",
"blue", "#0000ff",
"pink", "#ff00ff",
"yellow", "#ffff00");
// The class under test:
private MapBasics mapBasics = new MapBasics();
@Test
void testCreateMap() {
Map<String, String> map = mapBasics.createMap();
assertTrue(map != null, "The returned map should not be null.");
assertTrue(map instanceof Map, "The returned map should be an instance of Map.");
}
@Test
void testAddEntry() {
Map<String, String> map = new HashMap<>();
mapBasics.addEntry(map, "hello", "world");
mapBasics.addEntry(map, "terve", "maailma");
assertEquals(2, map.size(), "The map should have two entries.");
assertEquals("world", map.get("hello"), "The map should have an entry with key 'hello' and value 'world'.");
}
@Test
void testGetPopulations() {
Map<String, Integer> populations = mapBasics.getPopulations();
assertEquals(5, populations.size(), "The map should have five entries.");
assertEquals(5_894_687, populations.get("Denmark"), "The population of Denmark should be 5894687.");
assertEquals(5_587_442, populations.get("Finland"), "The population of Finland should be 5587442.");
assertEquals(354_234, populations.get("Iceland"), "The population of Iceland should be 354234.");
assertEquals(5_509_591, populations.get("Norway"), "The population of Norway should be 5509591.");
assertEquals(10_261_767, populations.get("Sweden"), "The population of Sweden should be 10261767.");
}
@Test
void testGetValue() {
assertEquals("127.0.0.1", mapBasics.getValue(ips, "home"));
assertEquals("8.8.8.8", mapBasics.getValue(ips, "google"));
assertEquals("1.1.1.1", mapBasics.getValue(ips, "cloudflare"));
assertEquals(null, mapBasics.getValue(ips, "unknown"));
}
@Test
void testHasKey() {
assertTrue(mapBasics.hasKey(ips, "home"));
assertFalse(mapBasics.hasKey(ips, "unknown"));
}
@Test
void testHasValue() {
assertTrue(mapBasics.hasValue(hexColors, "#ff0000"));
assertFalse(mapBasics.hasKey(hexColors, "#000000"));
}
@Test
void testAddIfNotPresent() {
Map<String, String> map = new HashMap<>();
map.put("hello", "world");
mapBasics.addIfNotPresent(map, "hello", "THIS SHOULD NOT BE ADDED");
mapBasics.addIfNotPresent(map, "terve", "maailma");
assertEquals(2, map.size(), "The map should have two entries.");
assertEquals("world", map.get("hello"), "should have an entry with key 'hello' and original value 'world'.");
assertEquals("maailma", map.get("terve"), "should have an entry with key 'terve' and value 'maailma'.");
}
@Test
void testRemoveEntry() {
Map<String, String> capitals = new HashMap<>();
capitals.put("Finland", "Helsinki");
capitals.put("Sweden", "Stockholm");
capitals.put("Norway", "Oslo");
mapBasics.removeEntry(capitals, "Sweden");
assertFalse(capitals.containsKey("Sweden"));
assertEquals(2, capitals.size());
}
@Test
void testCountEntries() {
assertEquals(3, mapBasics.countEntries(ips));
}
@Test
void testIsEmpty() {
assertTrue(mapBasics.isEmpty(Collections.emptyMap()));
assertFalse(mapBasics.isEmpty(hexColors));
}
@Test
void testLargestValue() {
Map<String, Integer> numbers = Map.of(
"one", 1,
"two", 2,
"three", 3,
"four", 4);
assertEquals(4, mapBasics.largestValue(numbers));
}
@Test
void testSumOfValues() {
Map<String, Integer> numbers = Map.of(
"one", 1,
"two", 2,
"three", 3,
"four", 4);
assertEquals(10, mapBasics.sumOfValues(numbers));
}
@Test
void testCombineMaps() {
Map<String, String> capitals1 = Map.of(
"Finland", "Helsinki",
"Sweden", "Stockholm",
"Norway", "Oslo");
Map<String, String> capitals2 = Map.of(
"Finland", "TURKU", // this should not override the value from the first map
"Denmark", "Copenhagen",
"Iceland", "Reykjavik");
Map<String, String> combined = mapBasics.combineMaps(capitals1, capitals2);
assertEquals(5, combined.size());
assertEquals("Helsinki", combined.get("Finland"), "should use the value from the first map");
}
@Test
void testIncrementValues() {
Map<String, Integer> numbers = new HashMap<>(Map.of(
"one", 1,
"two", 2,
"three", 3,
"four", 4));
mapBasics.incrementValues(numbers, 100);
assertEquals(101, numbers.get("one"));
assertEquals(102, numbers.get("two"));
assertEquals(103, numbers.get("three"));
}
}