|
| 1 | +package com.thealgorithms.sorts; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.PrintStream; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 13 | + |
| 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 | + |
| 33 | + @Test |
| 34 | + public void testSleepSort() { |
| 35 | + ArrayList<Integer> numbers = new ArrayList<>(); |
| 36 | + Collections.addAll(numbers, 4, 6, 8, 1, 10); |
| 37 | + |
| 38 | + // Capture output |
| 39 | + String output = captureOutput(() -> SleepSort.sleepSort(numbers)); |
| 40 | + |
| 41 | + // Expected output |
| 42 | + String expected = "1 4 6 8 10"; |
| 43 | + |
| 44 | + // Check if the captured output matches the expected output |
| 45 | + assertEquals(expected, output); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testSleepSortWithAdjacentNumbers() { |
| 50 | + ArrayList<Integer> numbers = new ArrayList<>(); |
| 51 | + Collections.addAll(numbers, 1, 2, 3, 4); |
| 52 | + |
| 53 | + // Capture output |
| 54 | + String output = captureOutput(() -> SleepSort.sleepSort(numbers)); |
| 55 | + |
| 56 | + // Expected output |
| 57 | + String expected = "1 2 3 4"; |
| 58 | + |
| 59 | + // Check if the output is sorted |
| 60 | + List<Integer> outputNumbers = parseOutput(output); |
| 61 | + assertTrue(isSorted(outputNumbers), "The output is not sorted."); |
| 62 | + |
| 63 | + // Check if the output contains all expected numbers |
| 64 | + assertEquals(expected, output, "The output does not contain the expected numbers."); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testSleepSortWithLargeNumbers() { |
| 69 | + ArrayList<Integer> numbers = new ArrayList<>(); |
| 70 | + Collections.addAll(numbers, 1000, 500, 2000, 1500); |
| 71 | + |
| 72 | + // Capture output |
| 73 | + String output = captureOutput(() -> SleepSort.sleepSort(numbers)); |
| 74 | + |
| 75 | + // Expected output |
| 76 | + String expected = "500 1000 1500 2000"; |
| 77 | + |
| 78 | + // Check if the captured output matches the expected output |
| 79 | + assertEquals(expected, output); |
| 80 | + } |
| 81 | + |
| 82 | + // Helper method to parse the output string to a list of integers |
| 83 | + private List<Integer> parseOutput(String output) { |
| 84 | + String[] parts = output.split("\\s+"); |
| 85 | + List<Integer> numbers = new ArrayList<>(); |
| 86 | + for (String part : parts) { |
| 87 | + numbers.add(Integer.parseInt(part)); |
| 88 | + } |
| 89 | + return numbers; |
| 90 | + } |
| 91 | + private boolean isSorted(List<Integer> list) { |
| 92 | + for (int i = 1; i < list.size(); i++) { |
| 93 | + if (list.get(i - 1) > list.get(i)) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | +} |
0 commit comments