- Notifications
You must be signed in to change notification settings - Fork20.1k
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:masterChoose a base branch fromalxkm:testing/SkipListTest
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+62 −57
Open
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
119 changes: 62 additions & 57 deletionssrc/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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.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 | ||
@DisplayName("Add element and verify size and retrieval") | ||
void testAdd() { | ||
assertEquals(0, skipList.size()); | ||
skipList.add("value"); | ||
assertEquals(1, skipList.size()); | ||
assertEquals("value", skipList.get(0)); | ||
} | ||
@Test | ||
@DisplayName("Get retrieves correct element by index") | ||
void testGet() { | ||
skipList.add("value"); | ||
assertEquals("value", skipList.get(0)); | ||
} | ||
@Test | ||
@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 | ||
@DisplayName("Remove element from head and check size and order") | ||
void testRemoveFromHead() { | ||
skipList = createSkipList(); | ||
String first = skipList.get(0); | ||
int initialSize = skipList.size(); | ||
skipList.remove(first); | ||
assertEquals(initialSize - 1, skipList.size()); | ||
assertFalse(skipList.contains(first)); | ||
} | ||
@Test | ||
@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(); | ||
skipList.remove(last); | ||
assertEquals(initialSize - 1, skipList.size()); | ||
assertFalse(skipList.contains(last)); | ||
} | ||
@Test | ||
@DisplayName("Elements should be sorted at base level") | ||
void testSortedOrderOnBaseLevel() { | ||
String[] values = {"d", "b", "a", "c"}; | ||
Arrays.stream(values).forEach(skipList::add); | ||
String[] actualOrder = IntStream.range(0, values.length).mapToObj(skipList::get).toArray(String[] ::new); | ||
org.junit.jupiter.api.Assertions.assertArrayEquals(new String[] {"a", "b", "c", "d"}, actualOrder); | ||
} | ||
@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); | ||
for (String v : values) { | ||
skipList.remove(v); | ||
} | ||
assertEquals(0, skipList.size()); | ||
} | ||
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.