3
3
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
4
4
5
5
import java .util .Arrays ;
6
- import java .util .List ;
7
6
import org .junit .jupiter .api .Test ;
8
7
9
8
public class BucketSortHashBehaviorTest {
10
9
11
10
private static <T extends Comparable <T >> int pseudoHash (final T element , final T min , final T max , final int numberOfBuckets ) {
12
- // Reproduces the production hash() logic
11
+ //Reproduces the production hash() logic
13
12
double range = max .compareTo (min );
14
13
double normalizedValue = element .compareTo (min ) / range ; // -1/0/1 divided by -1/0/1
15
14
return (int ) (normalizedValue * (numberOfBuckets - 1 ));
16
15
}
17
16
18
- @ Test // Test case when all numbers are equal
19
- void sort_stillCorrect_whenAllEqual () {
17
+ @ Test //Test case when all numbers are equal
18
+ void sortStillCorrectWhenAllEqual () {
20
19
Integer [] arr = {1 , 1 , 1 , 1 , 1 };
21
20
Integer [] expected = arr .clone ();
22
21
23
22
new BucketSort ().sort (arr );
24
23
assertArrayEquals (expected , arr );
25
24
26
- // Observe bucket mapping (all collapse to index 0)
25
+ //Observe bucket mapping (all collapse to index 0)
27
26
Integer min = 1 , max = 1 ;
28
27
int numberOfBuckets = Math .max (arr .length / 10 , 1 ); // same as BUCKET_DIVISOR rule
29
28
int idx = pseudoHash (1 , min , max , numberOfBuckets );
30
29
// idx will be 0 because NaN cast to int -> 0 in Java
31
30
System .out .println ("All-equal case -> bucket index: " + idx );
32
31
}
33
32
34
- @ Test // Test case with non-equal integers
35
- void sort_stillCorrect_nonEqualIntegers () {
33
+ @ Test //Test case with non-equal integers
34
+ void sortStillCorrectNonEqualIntegers () {
36
35
Integer [] arr = {20 , 40 , 30 , 10 };
37
36
Integer [] expected = {10 , 20 , 30 , 40 };
38
37
@@ -48,11 +47,11 @@ void sort_stillCorrect_nonEqualIntegers() {
48
47
int idx = pseudoHash (x , min , max , numberOfBuckets );
49
48
System .out .println ("Value " + x + " -> bucket " + idx );
50
49
}
51
- // Expect only two distinct buckets because compareTo gives -1/0/1
50
+ //Expect only two distinct buckets because compareTo gives -1/0/1
52
51
}
53
52
54
- @ Test // Test case when the Array contains Strings
55
- void sort_stillCorrect_whenStrings () {
53
+ @ Test //Test case when the Array contains Strings
54
+ void sortStillCorrectWhenStrings () {
56
55
String [] arr = {"apple" , "banana" , "carrot" };
57
56
String [] expected = arr .clone ();
58
57
0 commit comments