Skip to content

testing: improving SkipListTest #6411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,110 +1,115 @@
package com.thealgorithms.datastructures.lists;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
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.Arrays;
import java.util.stream.IntStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class SkipListTest {

private SkipList<String> skipList;

@BeforeEach
void setUp() {
skipList = new SkipList<>();
}

@Test
void add() {
SkipList<String> skipList = new SkipList<>();
@DisplayName("Add element and verify size and retrieval")
void testAdd() {
assertEquals(0, skipList.size());

skipList.add("value");

print(skipList);
assertEquals(1, skipList.size());
assertEquals("value", skipList.get(0));
}

@Test
void get() {
SkipList<String> skipList = new SkipList<>();
@DisplayName("Get retrieves correct element by index")
void testGet() {
skipList.add("value");

String actualValue = skipList.get(0);

print(skipList);
assertEquals("value", actualValue);
assertEquals("value", skipList.get(0));
}

@Test
void contains() {
SkipList<String> skipList = createSkipList();
print(skipList);

boolean contains = skipList.contains("b");

assertTrue(contains);
@DisplayName("Contains returns true if element exists")
void testContains() {
skipList = createSkipList();
assertTrue(skipList.contains("b"));
assertTrue(skipList.contains("a"));
assertFalse(skipList.contains("z")); // negative test
}

@Test
void removeFromHead() {
SkipList<String> skipList = createSkipList();
String mostLeftElement = skipList.get(0);
@DisplayName("Remove element from head and check size and order")
void testRemoveFromHead() {
skipList = createSkipList();
String first = skipList.get(0);
int initialSize = skipList.size();
print(skipList);

skipList.remove(mostLeftElement);
skipList.remove(first);

print(skipList);
assertEquals(initialSize - 1, skipList.size());
assertFalse(skipList.contains(first));
}

@Test
void removeFromTail() {
SkipList<String> skipList = createSkipList();
String mostRightValue = skipList.get(skipList.size() - 1);
@DisplayName("Remove element from tail and check size and order")
void testRemoveFromTail() {
skipList = createSkipList();
String last = skipList.get(skipList.size() - 1);
int initialSize = skipList.size();
print(skipList);

skipList.remove(mostRightValue);
skipList.remove(last);

print(skipList);
assertEquals(initialSize - 1, skipList.size());
assertFalse(skipList.contains(last));
}

@Test
void checkSortedOnLowestLayer() {
SkipList<String> skipList = new SkipList<>();
@DisplayName("Elements should be sorted at base level")
void testSortedOrderOnBaseLevel() {
String[] values = {"d", "b", "a", "c"};
Arrays.stream(values).forEach(skipList::add);
print(skipList);

String[] actualOrder = IntStream.range(0, values.length).mapToObj(skipList::get).toArray(String[] ::new);

assertArrayEquals(new String[] {"a", "b", "c", "d"}, actualOrder);
org.junit.jupiter.api.Assertions.assertArrayEquals(new String[] {"a", "b", "c", "d"}, actualOrder);
}

private SkipList<String> createSkipList() {
SkipList<String> skipList = new SkipList<>();
String[] values = {
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
};
@Test
@DisplayName("Duplicate elements can be added and count correctly")
void testAddDuplicates() {
skipList.add("x");
skipList.add("x");
assertEquals(2, skipList.size());
assertEquals("x", skipList.get(0));
assertEquals("x", skipList.get(1));
}

@Test
@DisplayName("Add multiple and remove all")
void testClearViaRemovals() {
String[] values = {"a", "b", "c"};
Arrays.stream(values).forEach(skipList::add);
return skipList;

for (String v : values) {
skipList.remove(v);
}

assertEquals(0, skipList.size());
}

/**
* Print Skip List representation to console.
* Optional method not involved in testing process. Used only for visualisation purposes.
* @param skipList to print
*/
private void print(SkipList<?> skipList) {
System.out.println(skipList);
private SkipList<String> createSkipList() {
SkipList<String> s = new SkipList<>();
String[] values = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};
Arrays.stream(values).forEach(s::add);
return s;
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy