From 206a4c5778c85c50e84fa9844486bcedf3578c37 Mon Sep 17 00:00:00 2001 From: alxkm Date: Fri, 18 Jul 2025 21:29:16 +0200 Subject: [PATCH 1/2] testing: improving SkipListTest --- .../datastructures/lists/SkipListTest.java | 121 +++++++++--------- 1 file changed, 63 insertions(+), 58 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java index c572739ffbbf..cd1ce63b231b 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java @@ -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 skipList; + + @BeforeEach + void setUp() { + skipList = new SkipList<>(); + } + @Test - void add() { - SkipList 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 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 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 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 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 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); + 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 createSkipList() { - SkipList 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 createSkipList() { + SkipList s = new SkipList<>(); + String[] values = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}; + Arrays.stream(values).forEach(s::add); + return s; } } From 084596d73dee81e19e13ee921e5a4c42f494b6be Mon Sep 17 00:00:00 2001 From: alxkm Date: Fri, 18 Jul 2025 21:32:35 +0200 Subject: [PATCH 2/2] style: fixed formatting --- .../com/thealgorithms/datastructures/lists/SkipListTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java index cd1ce63b231b..16d1a015a4d9 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java @@ -78,9 +78,9 @@ 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); + 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); + org.junit.jupiter.api.Assertions.assertArrayEquals(new String[] {"a", "b", "c", "d"}, actualOrder); } @Test 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