|
1 | 1 | package com.thealgorithms.strings; |
2 | 2 |
|
| 3 | +import org.junit.jupiter.api.DisplayName; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.Arguments; |
| 7 | +import org.junit.jupiter.params.provider.MethodSource; |
| 8 | + |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
3 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
4 | 13 |
|
5 | | -import org.junit.jupiter.params.ParameterizedTest; |
6 | | -import org.junit.jupiter.params.provider.CsvSource; |
| 14 | +@DisplayName("Alphabetical.isAlphabetical()") |
| 15 | +class AlphabeticalTest { |
| 16 | + |
| 17 | + static Stream<Arguments> testCases() { |
| 18 | + return Stream.of( |
| 19 | + arguments("", false, "Should return false for empty string"), |
| 20 | + arguments(" ", false, "Should return false for blank string"), |
| 21 | + arguments("a1b2", false, "Should return false when string contains numbers"), |
| 22 | + arguments("abc!DEF", false, "Should return false when string contains symbols"), |
| 23 | + arguments("#abc", false, "Should return false when first character is not a letter"), |
| 24 | + arguments("abc", true, "Should return true for non-decreasing order"), |
| 25 | + arguments("aBcD", true, "Should return true for mixed case increasing sequence"), |
| 26 | + arguments("a", true, "Should return true for single letter"), |
| 27 | + arguments("'", false, "Should return false for single symbol"), |
| 28 | + arguments("aabbcc", true, "Should return true for repeated letters"), |
| 29 | + arguments("cba", false, "Should return false when order decreases"), |
| 30 | + arguments("abzba", false, "Should return false when middle breaks order") |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + private void assertAlphabetical(String input, boolean expected, String message) { |
| 35 | + // Arrange & Act |
| 36 | + boolean result = Alphabetical.isAlphabetical(input); |
7 | 37 |
|
8 | | -public class AlphabeticalTest { |
| 38 | + // Assertion |
| 39 | + assertEquals(expected, result, message); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + @DisplayName("Should return false for null input") |
| 44 | + void nullInputTest() { |
| 45 | + assertAlphabetical(null, false, "Should return false for null input"); |
| 46 | + } |
9 | 47 |
|
10 | | - @ParameterizedTest(name = "\"{0}\" → Expected: {1}") |
11 | | - @CsvSource({"'abcdefghijklmno', true", "'abcdxxxyzzzz', true", "'123a', false", "'abcABC', false", "'abcdefghikjlmno', false", "'aBC', true", "'abc', true", "'xyzabc', false", "'abcxyz', true", "'', false", "'1', false"}) |
12 | | - void testIsAlphabetical(String input, boolean expected) { |
13 | | - assertEquals(expected, Alphabetical.isAlphabetical(input)); |
| 48 | + @ParameterizedTest(name = "{2}") |
| 49 | + @MethodSource("testCases") |
| 50 | + @DisplayName("Alphabetical cases") |
| 51 | + void isAlphabeticalTest(String input, boolean expected, String message) { |
| 52 | + assertAlphabetical(input, expected, message); |
14 | 53 | } |
15 | 54 | } |
0 commit comments