From f92dda3bd7078c4c721c7caa2899f332d4f378ee Mon Sep 17 00:00:00 2001 From: aitorfi Date: Fri, 16 Sep 2022 17:05:59 +0200 Subject: [PATCH 1/2] Closes #3274 Add BubbleSort unit tests --- .../com/thealgorithms/sorts/BubbleSort.java | 15 ++---- .../thealgorithms/sorts/BubbleSortTest.java | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/test/java/com/thealgorithms/sorts/BubbleSortTest.java diff --git a/src/main/java/com/thealgorithms/sorts/BubbleSort.java b/src/main/java/com/thealgorithms/sorts/BubbleSort.java index 3e5aae21d6b0..fd23c162b254 100644 --- a/src/main/java/com/thealgorithms/sorts/BubbleSort.java +++ b/src/main/java/com/thealgorithms/sorts/BubbleSort.java @@ -37,23 +37,14 @@ public > T[] sort(T[] array) { * Driver Code */ public static void main(String[] args) { + BubbleSort bubbleSort = new BubbleSort(); Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; - BubbleSort bubbleSort = new BubbleSort(); bubbleSort.sort(integers); - - for (int i = 0; i < integers.length - 1; ++i) { - assert integers[i] <= integers[i + 1]; - } - print(integers); - /* output: [1, 4, 6, 9, 12, 23, 54, 78, 231] */ + print(integers); // output: [1, 4, 6, 9, 12, 23, 54, 78, 231] String[] strings = {"c", "a", "e", "b", "d"}; bubbleSort.sort(strings); - for (int i = 0; i < strings.length - 1; i++) { - assert strings[i].compareTo(strings[i + 1]) <= 0; - } - print(bubbleSort.sort(strings)); - /* output: [a, b, c, d, e] */ + print(bubbleSort.sort(strings)); // output: [a, b, c, d, e] } } diff --git a/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java new file mode 100644 index 000000000000..e1a2ca2f7ff8 --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java @@ -0,0 +1,53 @@ +package com.thealgorithms.sorts; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +/** + * @author Aitor Fidalgo (https://github.com/aitorfi) + * @see BubbleSort + */ +public class BubbleSortTest { + private BubbleSort bubbleSort = new BubbleSort(); + + @Test + public void bubbleSortEmptyArray() { + Integer[] inputArray = {}; + Integer[] outputArray = bubbleSort.sort(inputArray); + Integer[] expectedOutput = {}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bubbleSortSingleIntegerElementArray() { + Integer[] inputArray = {4}; + Integer[] outputArray = bubbleSort.sort(inputArray); + Integer[] expectedOutput = {4}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bubbleSortSingleStringElementArray() { + String[] inputArray = {"s"}; + String[] outputArray = bubbleSort.sort(inputArray); + String[] expectedOutput = {"s"}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bubbleSortIntegerArray() { + Integer[] inputArray = {4, 23, -6, 78, 1, 54, 23, -6, -231, 9, 12}; + Integer[] outputArray = bubbleSort.sort(inputArray); + Integer[] expectedOutput = {-231, -6, -6, 1, 4, 9, 12, 23, 23, 54, 78}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bubbleSortStringArray() { + String[] inputArray = {"cbf", "auk", "ó", "(b", "a", ")", "au", "á", "cba", "auk", "(a", "bhy", "cba"}; + String[] outputArray = bubbleSort.sort(inputArray); + String[] expectedOutput = {"(a", "(b", ")", "a", "au", "auk", "auk", "bhy", "cba", "cba", "cbf", "á", "ó"}; + assertArrayEquals(outputArray, expectedOutput); + } +} From a874599d6890dc49b3a6ddcf1346c6aee63cfecc Mon Sep 17 00:00:00 2001 From: aitorfi Date: Mon, 19 Sep 2022 18:11:26 +0200 Subject: [PATCH 2/2] Remove redundant main method from BubbleSort.java --- .../java/com/thealgorithms/sorts/BubbleSort.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/BubbleSort.java b/src/main/java/com/thealgorithms/sorts/BubbleSort.java index fd23c162b254..46f30291d346 100644 --- a/src/main/java/com/thealgorithms/sorts/BubbleSort.java +++ b/src/main/java/com/thealgorithms/sorts/BubbleSort.java @@ -32,19 +32,4 @@ public > T[] sort(T[] array) { } return array; } - - /** - * Driver Code - */ - public static void main(String[] args) { - BubbleSort bubbleSort = new BubbleSort(); - - Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}; - bubbleSort.sort(integers); - print(integers); // output: [1, 4, 6, 9, 12, 23, 54, 78, 231] - - String[] strings = {"c", "a", "e", "b", "d"}; - bubbleSort.sort(strings); - print(bubbleSort.sort(strings)); // output: [a, b, c, d, e] - } }