|
1 | 1 | package com.jwetherell.algorithms.search.test; |
2 | 2 |
|
3 | | -import java.text.DecimalFormat; |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import org.junit.Test; |
4 | 6 |
|
5 | 7 | import com.jwetherell.algorithms.search.BinarySearch; |
6 | | -import com.jwetherell.algorithms.search.LinearSearch; |
7 | 8 | import com.jwetherell.algorithms.search.InterpolationSearch; |
| 9 | +import com.jwetherell.algorithms.search.LinearSearch; |
8 | 10 | import com.jwetherell.algorithms.search.QuickSelect; |
9 | 11 |
|
10 | 12 | public class Search { |
11 | 13 |
|
12 | | - private static final DecimalFormat FORMAT = new DecimalFormat("#.######"); |
13 | | - private static final int SIZE = 9999; |
14 | | - private static final int offset = 123; |
15 | | - |
16 | | - private static int[] sorted = null; |
| 14 | + private static final int SIZE = 9999; |
| 15 | + private static final int offset = 123; |
17 | 16 |
|
18 | | - public static void main(String[] args) { |
19 | | - System.out.println("Generating sorted array."); |
20 | | - sorted = new int[SIZE]; |
21 | | - for (int i = offset; i < offset + sorted.length; i++) { |
22 | | - sorted[i - offset] = i; |
| 17 | + private static int[] sorted = new int[SIZE]; |
| 18 | + static { |
| 19 | + for (int i = offset; i<offset+sorted.length; i++) { |
| 20 | + sorted[i-offset] = i; |
23 | 21 | } |
24 | | - System.out.println("Generated sorted array."); |
25 | | - System.out.println(); |
26 | | - |
27 | | - int valueInArray = sorted[SIZE - (SIZE / 4)]; |
28 | | - int valueNotInArray = sorted[SIZE - 1] + offset; |
| 22 | + } |
| 23 | + private static int valueIndex = SIZE-(SIZE/4); |
| 24 | + private static int valueInArray = sorted[valueIndex]; |
| 25 | + private static int valueNotInArray = sorted[SIZE-1]+offset; |
29 | 26 |
|
30 | | - { |
31 | | - System.out.println("Brute Force."); |
32 | | - long before = System.nanoTime(); |
33 | | - LinearSearch.find(valueInArray, sorted); |
34 | | - long after = System.nanoTime(); |
35 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
36 | | - before = System.nanoTime(); |
37 | | - LinearSearch.find(valueNotInArray, sorted); |
38 | | - after = System.nanoTime(); |
39 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
40 | | - System.out.println(); |
41 | | - System.gc(); |
42 | | - } |
| 27 | + @Test |
| 28 | + public void testBruteForceSearch() { |
| 29 | + int index = LinearSearch.find(valueInArray, sorted); |
| 30 | + assertTrue("Brute force error. expected="+valueIndex+" got="+index, (index==valueIndex)); |
| 31 | + index = LinearSearch.find(valueNotInArray, sorted); |
| 32 | + assertTrue("Brute force error. expected="+Integer.MAX_VALUE+" got="+index, (index==Integer.MAX_VALUE)); |
| 33 | + } |
43 | 34 |
|
44 | | - { |
45 | | - System.out.println("Binary Search."); |
46 | | - long before = System.nanoTime(); |
47 | | - BinarySearch.find(valueInArray, sorted, false); |
48 | | - long after = System.nanoTime(); |
49 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
50 | | - before = System.nanoTime(); |
51 | | - BinarySearch.find(valueNotInArray, sorted, false); |
52 | | - after = System.nanoTime(); |
53 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
54 | | - System.out.println(); |
55 | | - System.gc(); |
56 | | - } |
| 35 | + @Test |
| 36 | + public void testBinarySearch() { |
| 37 | + int index = BinarySearch.find(valueInArray, sorted, false); |
| 38 | + assertTrue("Brute force error. expected="+valueIndex+" got="+index, (index==valueIndex)); |
| 39 | + index = BinarySearch.find(valueNotInArray, sorted, false); |
| 40 | + assertTrue("Brute force error. expected="+Integer.MAX_VALUE+" got="+index, (index==Integer.MAX_VALUE)); |
| 41 | + } |
57 | 42 |
|
58 | | - { |
59 | | - System.out.println("Optimized Binary Search."); |
60 | | - long before = System.nanoTime(); |
61 | | - BinarySearch.find(valueInArray, sorted, true); |
62 | | - long after = System.nanoTime(); |
63 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
64 | | - before = System.nanoTime(); |
65 | | - BinarySearch.find(valueNotInArray, sorted, true); |
66 | | - after = System.nanoTime(); |
67 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
68 | | - System.out.println(); |
69 | | - System.gc(); |
70 | | - } |
| 43 | + @Test |
| 44 | + public void testOptimizedBinarySearch() { |
| 45 | + int index = BinarySearch.find(valueInArray, sorted, true); |
| 46 | + assertTrue("Brute force error. expected="+valueIndex+" got="+index, (index==valueIndex)); |
| 47 | + index = BinarySearch.find(valueNotInArray, sorted, true); |
| 48 | + assertTrue("Brute force error. expected="+Integer.MAX_VALUE+" got="+index, (index==Integer.MAX_VALUE)); |
| 49 | + } |
71 | 50 |
|
72 | | - { |
73 | | - System.out.println("Interpolation Search."); |
74 | | - long before = System.nanoTime(); |
75 | | - InterpolationSearch.find(valueInArray, sorted); |
76 | | - long after = System.nanoTime(); |
77 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
78 | | - before = System.nanoTime(); |
79 | | - InterpolationSearch.find(valueNotInArray, sorted); |
80 | | - after = System.nanoTime(); |
81 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
82 | | - System.out.println(); |
83 | | - System.gc(); |
84 | | - } |
| 51 | + @Test |
| 52 | + public void testInterpolationSearch() { |
| 53 | + int index = InterpolationSearch.find(valueInArray, sorted); |
| 54 | + assertTrue("Brute force error. expected="+valueIndex+" got="+index, (index==valueIndex)); |
| 55 | + index = InterpolationSearch.find(valueNotInArray, sorted); |
| 56 | + assertTrue("Brute force error. expected="+Integer.MAX_VALUE+" got="+index, (index==Integer.MAX_VALUE)); |
| 57 | + } |
85 | 58 |
|
86 | | - { |
87 | | - System.out.println("Quick Select"); |
88 | | - long before = System.nanoTime(); |
89 | | - QuickSelect.find(valueInArray, sorted); |
90 | | - long after = System.nanoTime(); |
91 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
92 | | - before = System.nanoTime(); |
93 | | - QuickSelect.find(valueNotInArray, sorted); |
94 | | - after = System.nanoTime(); |
95 | | - System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
96 | | - System.out.println(); |
97 | | - System.gc(); |
98 | | - } |
| 59 | + @Test |
| 60 | + public void testQuickSelect() { |
| 61 | + int index = QuickSelect.find(valueInArray, sorted); |
| 62 | + assertTrue("Brute force error. expected="+valueIndex+" got="+index, (index==valueIndex)); |
| 63 | + index = QuickSelect.find(valueNotInArray, sorted); |
| 64 | + assertTrue("Brute force error. expected="+Integer.MAX_VALUE+" got="+index, (index==Integer.MAX_VALUE)); |
99 | 65 | } |
100 | 66 | } |
0 commit comments