forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBogoSortTest.java
More file actions
150 lines (129 loc) · 4.72 KB
/
BogoSortTest.java
File metadata and controls
150 lines (129 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.util.Objects;
import org.junit.jupiter.api.Test;
public class BogoSortTest {
private BogoSort bogoSort = new BogoSort();
@Test
public void bogoSortEmptyArray() {
Integer[] inputArray = {};
Integer[] outputArray = bogoSort.sort(inputArray);
Integer[] expectedOutput = {};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortSingleIntegerArray() {
Integer[] inputArray = {4};
Integer[] outputArray = bogoSort.sort(inputArray);
Integer[] expectedOutput = {4};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortSingleStringArray() {
String[] inputArray = {"s"};
String[] outputArray = bogoSort.sort(inputArray);
String[] expectedOutput = {"s"};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortNonDuplicateIntegerArray() {
Integer[] inputArray = {6, -1, 99, 27, -15, 23, -36};
Integer[] outputArray = bogoSort.sort(inputArray);
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 27, 99};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortDuplicateIntegerArray() {
Integer[] inputArray = {6, -1, 27, -15, 23, 27, -36, 23};
Integer[] outputArray = bogoSort.sort(inputArray);
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 23, 27, 27};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortNonDuplicateStringArray() {
String[] inputArray = {"s", "b", "k", "a", "d", "c", "h"};
String[] outputArray = bogoSort.sort(inputArray);
String[] expectedOutput = {"a", "b", "c", "d", "h", "k", "s"};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortDuplicateStringArray() {
String[] inputArray = {"s", "b", "d", "a", "d", "c", "h", "b"};
String[] outputArray = bogoSort.sort(inputArray);
String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortAlreadySortedArray() {
Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46};
Integer[] outputArray = bogoSort.sort(inputArray);
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortReversedSortedArray() {
Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12};
Integer[] outputArray = bogoSort.sort(inputArray);
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortAllEqualArray() {
Integer[] inputArray = {2, 2, 2, 2, 2};
Integer[] outputArray = bogoSort.sort(inputArray);
Integer[] expectedOutput = {2, 2, 2, 2, 2};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void bogoSortMixedCaseStrings() {
String[] inputArray = {"banana", "Apple", "apple", "Banana"};
String[] expectedOutput = {"Apple", "Banana", "apple", "banana"};
String[] outputArray = bogoSort.sort(inputArray);
assertArrayEquals(expectedOutput, outputArray);
}
/**
* Custom Comparable class for testing.
**/
static class Person implements Comparable<Person> {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person o) {
return Integer.compare(this.age, o.age);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
@Test
public void bogoSortCustomObjects() {
Person[] inputArray = {
new Person("Alice", 32),
new Person("Bob", 25),
new Person("Charlie", 28),
};
Person[] expectedOutput = {
new Person("Bob", 25),
new Person("Charlie", 28),
new Person("Alice", 32),
};
Person[] outputArray = bogoSort.sort(inputArray);
assertArrayEquals(expectedOutput, outputArray);
}
}