|
| 1 | +package com.thealgorithms.sorts; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class TagSortTest { |
| 10 | + |
| 11 | + private TagSort tagSort; |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + public void setUp() { |
| 15 | + tagSort = new TagSort(); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + @DisplayName("TagSort empty array") |
| 20 | + public void testEmptyArray() { |
| 21 | + Integer[] input = {}; |
| 22 | + Integer[] result = tagSort.sort(input); |
| 23 | + assertArrayEquals(new Integer[] {}, result); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + @DisplayName("TagSort single element array") |
| 28 | + public void testSingleElement() { |
| 29 | + Integer[] input = {42}; |
| 30 | + Integer[] result = tagSort.sort(input); |
| 31 | + assertArrayEquals(new Integer[] {42}, result); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + @DisplayName("TagSort non-duplicate integer array") |
| 36 | + public void testNonDuplicateIntegers() { |
| 37 | + Integer[] input = {5, 3, 8, 1, 9, 2}; |
| 38 | + Integer[] result = tagSort.sort(input); |
| 39 | + assertArrayEquals(new Integer[] {1, 2, 3, 5, 8, 9}, result); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + @DisplayName("TagSort integer array with duplicates") |
| 44 | + public void testDuplicateIntegers() { |
| 45 | + Integer[] input = {4, 2, 7, 2, 4, 1}; |
| 46 | + Integer[] result = tagSort.sort(input); |
| 47 | + assertArrayEquals(new Integer[] {1, 2, 2, 4, 4, 7}, result); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @DisplayName("TagSort negative integers") |
| 52 | + public void testNegativeIntegers() { |
| 53 | + Integer[] input = {-3, 5, -1, 0, 2, -8}; |
| 54 | + Integer[] result = tagSort.sort(input); |
| 55 | + assertArrayEquals(new Integer[] {-8, -3, -1, 0, 2, 5}, result); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @DisplayName("TagSort already sorted array") |
| 60 | + public void testAlreadySorted() { |
| 61 | + Integer[] input = {1, 2, 3, 4, 5}; |
| 62 | + Integer[] result = tagSort.sort(input); |
| 63 | + assertArrayEquals(new Integer[] {1, 2, 3, 4, 5}, result); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @DisplayName("TagSort reverse sorted array") |
| 68 | + public void testReverseSorted() { |
| 69 | + Integer[] input = {5, 4, 3, 2, 1}; |
| 70 | + Integer[] result = tagSort.sort(input); |
| 71 | + assertArrayEquals(new Integer[] {1, 2, 3, 4, 5}, result); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("TagSort string array") |
| 76 | + public void testStringArray() { |
| 77 | + String[] input = {"banana", "apple", "cherry", "date"}; |
| 78 | + String[] result = tagSort.sort(input); |
| 79 | + assertArrayEquals(new String[] {"apple", "banana", "cherry", "date"}, result); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("TagSort getSortedTags returns correct indices") |
| 84 | + public void testGetSortedTags() { |
| 85 | + Integer[] input = {30, 10, 20}; |
| 86 | + Integer[] tags = tagSort.getSortedTags(input); |
| 87 | + // index 1 (value 10), index 2 (value 20), index 0 (value 30) |
| 88 | + assertArrayEquals(new Integer[] {1, 2, 0}, tags); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @DisplayName("TagSort all equal elements") |
| 93 | + public void testAllEqualElements() { |
| 94 | + Integer[] input = {7, 7, 7, 7}; |
| 95 | + Integer[] result = tagSort.sort(input); |
| 96 | + assertArrayEquals(new Integer[] {7, 7, 7, 7}, result); |
| 97 | + } |
| 98 | +} |
0 commit comments