Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

testing: improvingSkipListTest#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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
alxkm wants to merge4 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromalxkm:testing/SkipListTest
Open
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
}
}

[8]ページ先頭

©2009-2025 Movatter.jp