|
1 | 1 | package com.thealgorithms.datastructures.lists;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
4 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
6 | 6 |
|
7 | 7 | import java.util.Arrays;
|
8 | 8 | import java.util.stream.IntStream;
|
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
9 | 11 | import org.junit.jupiter.api.Test;
|
10 | 12 |
|
11 | 13 | class SkipListTest {
|
12 | 14 |
|
| 15 | + private SkipList<String> skipList; |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + void setUp() { |
| 19 | + skipList = new SkipList<>(); |
| 20 | + } |
| 21 | + |
13 | 22 | @Test
|
14 |
| - void add() { |
15 |
| - SkipList<String> skipList = new SkipList<>(); |
| 23 | + @DisplayName("Add element and verify size and retrieval") |
| 24 | + void testAdd() { |
16 | 25 | assertEquals(0, skipList.size());
|
17 | 26 |
|
18 | 27 | skipList.add("value");
|
19 | 28 |
|
20 |
| - print(skipList); |
21 | 29 | assertEquals(1, skipList.size());
|
| 30 | + assertEquals("value", skipList.get(0)); |
22 | 31 | }
|
23 | 32 |
|
24 | 33 | @Test
|
25 |
| - void get() { |
26 |
| - SkipList<String> skipList = new SkipList<>(); |
| 34 | + @DisplayName("Get retrieves correct element by index") |
| 35 | + void testGet() { |
27 | 36 | skipList.add("value");
|
28 |
| - |
29 |
| - String actualValue = skipList.get(0); |
30 |
| - |
31 |
| - print(skipList); |
32 |
| - assertEquals("value", actualValue); |
| 37 | + assertEquals("value", skipList.get(0)); |
33 | 38 | }
|
34 | 39 |
|
35 | 40 | @Test
|
36 |
| - void contains() { |
37 |
| - SkipList<String> skipList = createSkipList(); |
38 |
| - print(skipList); |
39 |
| - |
40 |
| - boolean contains = skipList.contains("b"); |
41 |
| - |
42 |
| - assertTrue(contains); |
| 41 | + @DisplayName("Contains returns true if element exists") |
| 42 | + void testContains() { |
| 43 | + skipList = createSkipList(); |
| 44 | + assertTrue(skipList.contains("b")); |
| 45 | + assertTrue(skipList.contains("a")); |
| 46 | + assertFalse(skipList.contains("z")); // negative test |
43 | 47 | }
|
44 | 48 |
|
45 | 49 | @Test
|
46 |
| - void removeFromHead() { |
47 |
| - SkipList<String> skipList = createSkipList(); |
48 |
| - String mostLeftElement = skipList.get(0); |
| 50 | + @DisplayName("Remove element from head and check size and order") |
| 51 | + void testRemoveFromHead() { |
| 52 | + skipList = createSkipList(); |
| 53 | + String first = skipList.get(0); |
49 | 54 | int initialSize = skipList.size();
|
50 |
| - print(skipList); |
51 | 55 |
|
52 |
| - skipList.remove(mostLeftElement); |
| 56 | + skipList.remove(first); |
53 | 57 |
|
54 |
| - print(skipList); |
55 | 58 | assertEquals(initialSize - 1, skipList.size());
|
| 59 | + assertFalse(skipList.contains(first)); |
56 | 60 | }
|
57 | 61 |
|
58 | 62 | @Test
|
59 |
| - void removeFromTail() { |
60 |
| - SkipList<String> skipList = createSkipList(); |
61 |
| - String mostRightValue = skipList.get(skipList.size() - 1); |
| 63 | + @DisplayName("Remove element from tail and check size and order") |
| 64 | + void testRemoveFromTail() { |
| 65 | + skipList = createSkipList(); |
| 66 | + String last = skipList.get(skipList.size() - 1); |
62 | 67 | int initialSize = skipList.size();
|
63 |
| - print(skipList); |
64 | 68 |
|
65 |
| - skipList.remove(mostRightValue); |
| 69 | + skipList.remove(last); |
66 | 70 |
|
67 |
| - print(skipList); |
68 | 71 | assertEquals(initialSize - 1, skipList.size());
|
| 72 | + assertFalse(skipList.contains(last)); |
69 | 73 | }
|
70 | 74 |
|
71 | 75 | @Test
|
72 |
| - void checkSortedOnLowestLayer() { |
73 |
| - SkipList<String> skipList = new SkipList<>(); |
| 76 | + @DisplayName("Elements should be sorted at base level") |
| 77 | + void testSortedOrderOnBaseLevel() { |
74 | 78 | String[] values = {"d", "b", "a", "c"};
|
75 | 79 | Arrays.stream(values).forEach(skipList::add);
|
76 |
| - print(skipList); |
77 | 80 |
|
78 | 81 | String[] actualOrder = IntStream.range(0, values.length).mapToObj(skipList::get).toArray(String[] ::new);
|
79 | 82 |
|
80 |
| - assertArrayEquals(new String[] {"a", "b", "c", "d"}, actualOrder); |
| 83 | + org.junit.jupiter.api.Assertions.assertArrayEquals(new String[] {"a", "b", "c", "d"}, actualOrder); |
81 | 84 | }
|
82 | 85 |
|
83 |
| - private SkipList<String> createSkipList() { |
84 |
| - SkipList<String> skipList = new SkipList<>(); |
85 |
| - String[] values = { |
86 |
| - "a", |
87 |
| - "b", |
88 |
| - "c", |
89 |
| - "d", |
90 |
| - "e", |
91 |
| - "f", |
92 |
| - "g", |
93 |
| - "h", |
94 |
| - "i", |
95 |
| - "j", |
96 |
| - "k", |
97 |
| - }; |
| 86 | + @Test |
| 87 | + @DisplayName("Duplicate elements can be added and count correctly") |
| 88 | + void testAddDuplicates() { |
| 89 | + skipList.add("x"); |
| 90 | + skipList.add("x"); |
| 91 | + assertEquals(2, skipList.size()); |
| 92 | + assertEquals("x", skipList.get(0)); |
| 93 | + assertEquals("x", skipList.get(1)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @DisplayName("Add multiple and remove all") |
| 98 | + void testClearViaRemovals() { |
| 99 | + String[] values = {"a", "b", "c"}; |
98 | 100 | Arrays.stream(values).forEach(skipList::add);
|
99 |
| - return skipList; |
| 101 | + |
| 102 | + for (String v : values) { |
| 103 | + skipList.remove(v); |
| 104 | + } |
| 105 | + |
| 106 | + assertEquals(0, skipList.size()); |
100 | 107 | }
|
101 | 108 |
|
102 |
| - /** |
103 |
| - * Print Skip List representation to console. |
104 |
| - * Optional method not involved in testing process. Used only for visualisation purposes. |
105 |
| - * @param skipList to print |
106 |
| - */ |
107 |
| - private void print(SkipList<?> skipList) { |
108 |
| - System.out.println(skipList); |
| 109 | + private SkipList<String> createSkipList() { |
| 110 | + SkipList<String> s = new SkipList<>(); |
| 111 | + String[] values = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}; |
| 112 | + Arrays.stream(values).forEach(s::add); |
| 113 | + return s; |
109 | 114 | }
|
110 | 115 | }
|
0 commit comments