|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | 5 |
|
6 |
| -import java.io.ByteArrayOutputStream; |
7 |
| -import java.io.PrintStream; |
8 | 6 | import java.util.ArrayList;
|
9 |
| -import java.util.Arrays; |
10 | 7 | import java.util.Collections;
|
11 | 8 | import java.util.List;
|
12 |
| -import java.util.stream.Collectors; |
13 | 9 | import org.junit.jupiter.api.Test;
|
14 |
| -public class SleepSortTest { |
15 |
| - |
16 |
| - // Method to capture the output of the SleepSort |
17 |
| - private String captureOutput(Runnable task) { |
18 |
| - // Create a stream to capture output |
19 |
| - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
20 |
| - PrintStream originalOut = System.out; |
21 |
| - System.setOut(new PrintStream(outputStream)); |
22 |
| - |
23 |
| - // Run the task |
24 |
| - task.run(); |
25 |
| - |
26 |
| - // Restore original output |
27 |
| - System.setOut(originalOut); |
28 |
| - |
29 |
| - // Return the captured output |
30 |
| - return outputStream.toString().trim(); |
31 |
| - } |
32 | 10 |
|
| 11 | +public class SleepSortTest { |
33 | 12 | @Test
|
34 | 13 | public void testSleepSort() {
|
35 |
| - ArrayList<Integer> numbers = new ArrayList<>(); |
| 14 | + List<Integer> numbers = new ArrayList<>(); |
36 | 15 | Collections.addAll(numbers, 4, 6, 8, 1, 10);
|
37 | 16 |
|
38 |
| - // Capture output |
39 |
| - String output = captureOutput(() -> SleepSort.sleepSort(numbers)); |
| 17 | + // Get sorted result from sleepSort |
| 18 | + List<Integer> sortedNumbers = SleepSort.sleepSort(numbers); |
40 | 19 |
|
41 |
| - // Expected output |
42 |
| - String expected = "1 4 6 8 10"; |
| 20 | + // Expected sorted result |
| 21 | + List<Integer> expected = List.of(1, 4, 6, 8, 10); |
43 | 22 |
|
44 |
| - // Check if the captured output matches the expected output |
45 |
| - assertEquals(expected, output); |
| 23 | + // Check if the sorted list matches the expected result |
| 24 | + assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list."); |
46 | 25 | }
|
47 | 26 |
|
48 | 27 | @Test
|
49 | 28 | public void testSleepSortWithAdjacentNumbers() {
|
50 |
| - ArrayList<Integer> numbers = new ArrayList<>(); |
| 29 | + List<Integer> numbers = new ArrayList<>(); |
51 | 30 | Collections.addAll(numbers, 1, 2, 3, 4);
|
52 | 31 |
|
53 |
| - // Capture output |
54 |
| - String output = captureOutput(() -> SleepSort.sleepSort(numbers)); |
55 |
| - |
56 |
| - // Expected output |
57 |
| - String expected = "1 2 3 4"; |
| 32 | + // Get sorted result from sleepSort |
| 33 | + List<Integer> sortedNumbers = SleepSort.sleepSort(numbers); |
58 | 34 |
|
59 |
| - // Check if the output is sorted |
60 |
| - List<Integer> outputNumbers = parseOutput(output); |
61 |
| - assertTrue(isSorted(outputNumbers), "The output is not sorted."); |
| 35 | + // Expected sorted result |
| 36 | + List<Integer> expected = List.of(1, 2, 3, 4); |
62 | 37 |
|
63 |
| - // Check if the output contains all expected numbers |
64 |
| - assertEquals(expected, output, "The output does not contain the expected numbers."); |
| 38 | + // Check if the sorted list matches the expected result |
| 39 | + assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list."); |
65 | 40 | }
|
66 | 41 |
|
67 | 42 | @Test
|
68 | 43 | public void testSleepSortWithLargeNumbers() {
|
69 |
| - ArrayList<Integer> numbers = new ArrayList<>(); |
| 44 | + List<Integer> numbers = new ArrayList<>(); |
70 | 45 | Collections.addAll(numbers, 1000, 500, 2000, 1500);
|
71 | 46 |
|
72 |
| - // Capture output |
73 |
| - String output = captureOutput(() -> SleepSort.sleepSort(numbers)); |
| 47 | + // Get sorted result from sleepSort |
| 48 | + List<Integer> sortedNumbers = SleepSort.sleepSort(numbers); |
74 | 49 |
|
75 |
| - // Expected output |
76 |
| - String expected = "500 1000 1500 2000"; |
| 50 | + // Expected sorted result |
| 51 | + List<Integer> expected = List.of(500, 1000, 1500, 2000); |
77 | 52 |
|
78 |
| - // Check if the captured output matches the expected output |
79 |
| - assertEquals(expected, output); |
| 53 | + // Check if the sorted list matches the expected result |
| 54 | + assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list."); |
80 | 55 | }
|
81 | 56 |
|
82 |
| - // Helper method to parse the output string to a list of integers |
83 |
| - private List<Integer> parseOutput(String output) { |
84 |
| - return Arrays.stream(output.trim().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList()); |
85 |
| - } |
86 |
| - private boolean isSorted(List<Integer> list) { |
87 |
| - for (int i = 1; i < list.size(); i++) { |
88 |
| - if (list.get(i - 1) > list.get(i)) { |
89 |
| - return false; |
90 |
| - } |
91 |
| - } |
92 |
| - return true; |
| 57 | + @Test |
| 58 | + public void testSleepSortWithNegativeNumbers() { |
| 59 | + List<Integer> numbers = List.of(15, -23, 8, 41, 30); |
| 60 | + |
| 61 | + // Expect IllegalArgumentException when a negative number is present |
| 62 | + assertThrows(IllegalArgumentException.class, () -> SleepSort.sleepSort(numbers), "Expected sleepSort() to throw IllegalArgumentException when negative number is present"); |
93 | 63 | }
|
94 | 64 | }
|
0 commit comments