Skip to content

Commit a2dd154

Browse files
authored
Add BubbleSort unit tests (TheAlgorithms#3275)
1 parent 276bbe2 commit a2dd154

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

src/main/java/com/thealgorithms/sorts/BubbleSort.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,4 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
3232
}
3333
return array;
3434
}
35-
36-
/**
37-
* Driver Code
38-
*/
39-
public static void main(String[] args) {
40-
41-
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
42-
BubbleSort bubbleSort = new BubbleSort();
43-
bubbleSort.sort(integers);
44-
45-
for (int i = 0; i < integers.length - 1; ++i) {
46-
assert integers[i] <= integers[i + 1];
47-
}
48-
print(integers);
49-
/* output: [1, 4, 6, 9, 12, 23, 54, 78, 231] */
50-
51-
String[] strings = {"c", "a", "e", "b", "d"};
52-
bubbleSort.sort(strings);
53-
for (int i = 0; i < strings.length - 1; i++) {
54-
assert strings[i].compareTo(strings[i + 1]) <= 0;
55-
}
56-
print(bubbleSort.sort(strings));
57-
/* output: [a, b, c, d, e] */
58-
}
5935
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* @author Aitor Fidalgo (https://github.com/aitorfi)
9+
* @see BubbleSort
10+
*/
11+
public class BubbleSortTest {
12+
private BubbleSort bubbleSort = new BubbleSort();
13+
14+
@Test
15+
public void bubbleSortEmptyArray() {
16+
Integer[] inputArray = {};
17+
Integer[] outputArray = bubbleSort.sort(inputArray);
18+
Integer[] expectedOutput = {};
19+
assertArrayEquals(outputArray, expectedOutput);
20+
}
21+
22+
@Test
23+
public void bubbleSortSingleIntegerElementArray() {
24+
Integer[] inputArray = {4};
25+
Integer[] outputArray = bubbleSort.sort(inputArray);
26+
Integer[] expectedOutput = {4};
27+
assertArrayEquals(outputArray, expectedOutput);
28+
}
29+
30+
@Test
31+
public void bubbleSortSingleStringElementArray() {
32+
String[] inputArray = {"s"};
33+
String[] outputArray = bubbleSort.sort(inputArray);
34+
String[] expectedOutput = {"s"};
35+
assertArrayEquals(outputArray, expectedOutput);
36+
}
37+
38+
@Test
39+
public void bubbleSortIntegerArray() {
40+
Integer[] inputArray = {4, 23, -6, 78, 1, 54, 23, -6, -231, 9, 12};
41+
Integer[] outputArray = bubbleSort.sort(inputArray);
42+
Integer[] expectedOutput = {-231, -6, -6, 1, 4, 9, 12, 23, 23, 54, 78};
43+
assertArrayEquals(outputArray, expectedOutput);
44+
}
45+
46+
@Test
47+
public void bubbleSortStringArray() {
48+
String[] inputArray = {"cbf", "auk", "ó", "(b", "a", ")", "au", "á", "cba", "auk", "(a", "bhy", "cba"};
49+
String[] outputArray = bubbleSort.sort(inputArray);
50+
String[] expectedOutput = {"(a", "(b", ")", "a", "au", "auk", "auk", "bhy", "cba", "cba", "cbf", "á", "ó"};
51+
assertArrayEquals(outputArray, expectedOutput);
52+
}
53+
}

0 commit comments

Comments
 (0)