Skip to content

Commit 9faa94d

Browse files
authored
Create Dictionary(Sorted)Test.java
1 parent 81561dd commit 9faa94d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.datastructures.dictionary;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class DictionaryTest {
7+
8+
@Test
9+
void testDictionaryOperations() {
10+
// Create dictionary with initial values
11+
DictionarySort dict = new DictionarySort("key1", "value1", "key2", "value2");
12+
13+
// Test put and get
14+
Assertions.assertEquals("value1", dict.get("key1"));
15+
Assertions.assertEquals("value2", dict.get("key2"));
16+
dict.put("key3", "value3");
17+
Assertions.assertEquals("value3", dict.get("key3"));
18+
dict.put("key1", "updatedValue1");
19+
Assertions.assertEquals("updatedValue1", dict.get("key1"));
20+
21+
// Test remove
22+
dict.remove("key1");
23+
Assertions.assertNull(dict.get("key1"));
24+
dict.remove("key1"); // Removing again should not cause issues
25+
Assertions.assertNull(dict.get("key1"));
26+
27+
// Test containsKey
28+
Assertions.assertTrue(dict.containsKey("key2"));
29+
Assertions.assertFalse(dict.containsKey("key1"));
30+
31+
// Test setSortStrategy and toString
32+
dict.setSortStrategy(DictionarySort.SortStrategy.KEY);
33+
String expectedKeySort = "{\n \"key2\": \"value2\",\n \"key3\": \"value3\"}"; // Adjusted based on your actual sorting implementation
34+
Assertions.assertEquals(expectedKeySort, dict.toString(true));
35+
36+
dict.setSortStrategy(DictionarySort.SortStrategy.VALUE);
37+
String expectedValueSort = "{\n \"key2\": \"value2\",\n \"key3\": \"value3\"}"; // Adjusted based on your actual sorting implementation
38+
Assertions.assertEquals(expectedValueSort, dict.toString(true)); // Order might differ based on value sort
39+
40+
// Test toString without formatting
41+
String expectedNoFormat = "{\"key2\": \"value2\", \"key3\": \"value3\"}"; // Adjusted based on your actual implementation
42+
Assertions.assertEquals(expectedNoFormat, dict.toString());
43+
44+
// Test invalid constructor
45+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
46+
new DictionarySort("key1", "value1", "key2");
47+
});
48+
}
49+
}

0 commit comments

Comments
 (0)