|
| 1 | +package com.baeldung.arraysort; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNotSame; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.FixMethodOrder; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runners.MethodSorters; |
| 15 | + |
| 16 | +/** |
| 17 | + * Time taken by JUnit test cases can be seen in JUnit Runner |
| 18 | + * @author rchaudhary23 |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 23 | +public class SortComparisonUnitTest { |
| 24 | + |
| 25 | + private int[] sizeOfArrays = { 1000, 10000, 100000, 1000000 }; |
| 26 | + |
| 27 | + private int[] _1000_elements_array; |
| 28 | + private int[] _10000_elements_array; |
| 29 | + private int[] _100000_elements_array; |
| 30 | + private int[] _1000000_elements_array; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() throws Exception { |
| 34 | + |
| 35 | + _1000_elements_array = new int[sizeOfArrays[0]]; |
| 36 | + _10000_elements_array = new int[sizeOfArrays[1]]; |
| 37 | + _100000_elements_array = new int[sizeOfArrays[2]]; |
| 38 | + _1000000_elements_array = new int[sizeOfArrays[3]]; |
| 39 | + |
| 40 | + Random random = new Random(); |
| 41 | + for (int i = 0; i < sizeOfArrays[0]; i++) { |
| 42 | + _1000_elements_array[i] = random.nextInt(sizeOfArrays[0]) + random.nextInt(sizeOfArrays[0]); |
| 43 | + } |
| 44 | + |
| 45 | + for (int i = 0; i < sizeOfArrays[1]; i++) { |
| 46 | + _10000_elements_array[i] = random.nextInt(sizeOfArrays[1]) + random.nextInt(sizeOfArrays[1]); |
| 47 | + } |
| 48 | + |
| 49 | + for (int i = 0; i < sizeOfArrays[2]; i++) { |
| 50 | + _100000_elements_array[i] = random.nextInt(sizeOfArrays[2]) + random.nextInt(sizeOfArrays[2]); |
| 51 | + } |
| 52 | + |
| 53 | + for (int i = 0; i < sizeOfArrays[3]; i++) { |
| 54 | + _1000000_elements_array[i] = random.nextInt(sizeOfArrays[3]) + random.nextInt(sizeOfArrays[3]); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenArrayOfIntegers_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() { |
| 61 | + |
| 62 | + int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 }; |
| 63 | + int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 64 | + |
| 65 | + Arrays.sort(array); |
| 66 | + |
| 67 | + assertArrayEquals(expected, array); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void givenArrayOfIntegers_whenUsingArraysSortMethodWithRange_thenSortRangeOfArrayInAscendingOrder() { |
| 73 | + int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 }; |
| 74 | + int[] expected = { 10, 4, 1, 2, 6, 7, 8, 9, 3, 5 }; |
| 75 | + |
| 76 | + Arrays.sort(array, 2, 8); |
| 77 | + |
| 78 | + assertArrayEquals(expected, array); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenArrayOfIntegers_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() { |
| 83 | + int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 }; |
| 84 | + int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 85 | + |
| 86 | + Arrays.parallelSort(array); |
| 87 | + |
| 88 | + assertArrayEquals(expected, array); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenArrayOfIntegers_whenUsingArraysParallelSortMethodWithRange_thenSortRangeOfArrayInAscendingOrder() { |
| 93 | + int[] array = { 10, 4, 6, 2, 1, 9, 7, 8, 3, 5 }; |
| 94 | + int[] expected = { 10, 4, 1, 2, 6, 7, 8, 9, 3, 5 }; |
| 95 | + |
| 96 | + Arrays.parallelSort(array, 2, 8); |
| 97 | + |
| 98 | + assertArrayEquals(expected, array); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void givenIntegerArrayOf1000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() { |
| 103 | + int[] sequentialDataSet = Arrays.copyOf(_1000_elements_array, _1000_elements_array.length); |
| 104 | + Arrays.sort(sequentialDataSet); |
| 105 | + |
| 106 | + assertNotNull(sequentialDataSet); |
| 107 | + assertNotSame(Arrays.copyOf(_1000_elements_array, _1000_elements_array.length), sequentialDataSet); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenIntegerArrayOf1000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() { |
| 113 | + int[] parallelDataSet = Arrays.copyOf(_1000_elements_array, _1000_elements_array.length); |
| 114 | + Arrays.parallelSort(parallelDataSet); |
| 115 | + |
| 116 | + assertNotNull(parallelDataSet); |
| 117 | + assertNotSame(Arrays.copyOf(_1000_elements_array, _1000_elements_array.length), parallelDataSet); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void givenIntegerArrayOf10000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() { |
| 123 | + int[] sequentialDataSet = Arrays.copyOf(_10000_elements_array, _10000_elements_array.length); |
| 124 | + Arrays.sort(sequentialDataSet); |
| 125 | + |
| 126 | + assertNotNull(sequentialDataSet); |
| 127 | + assertNotSame(Arrays.copyOf(_10000_elements_array, _10000_elements_array.length), sequentialDataSet); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void givenIntegerArrayOf10000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() { |
| 132 | + int[] parallelDataSet = Arrays.copyOf(_10000_elements_array, _10000_elements_array.length); |
| 133 | + Arrays.parallelSort(parallelDataSet); |
| 134 | + |
| 135 | + assertNotNull(parallelDataSet); |
| 136 | + assertNotSame(Arrays.copyOf(_10000_elements_array, _10000_elements_array.length), parallelDataSet); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void givenIntegerArrayOf100000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() { |
| 141 | + int[] sequentialDataSet = Arrays.copyOf(_100000_elements_array, _100000_elements_array.length); |
| 142 | + Arrays.sort(sequentialDataSet); |
| 143 | + |
| 144 | + assertNotNull(sequentialDataSet); |
| 145 | + assertNotSame(Arrays.copyOf(_100000_elements_array, _100000_elements_array.length), sequentialDataSet); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void givenIntegerArrayOf100000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() { |
| 150 | + int[] parallelDataSet = Arrays.copyOf(_100000_elements_array, _100000_elements_array.length); |
| 151 | + Arrays.parallelSort(parallelDataSet); |
| 152 | + |
| 153 | + assertNotNull(parallelDataSet); |
| 154 | + assertNotSame(Arrays.copyOf(_100000_elements_array, _100000_elements_array.length), parallelDataSet); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void givenIntegerArrayOf1000000Elements_whenUsingArraysSortMethod_thenSortFullArrayInAscendingOrder() { |
| 159 | + int[] sequentialDataSet = Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length); |
| 160 | + Arrays.sort(sequentialDataSet); |
| 161 | + |
| 162 | + assertNotNull(sequentialDataSet); |
| 163 | + assertNotSame(Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length), sequentialDataSet); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void givenIntegerArrayOf1000000Elements_whenUsingArraysParallelSortMethod_thenSortFullArrayInAscendingOrder() { |
| 168 | + int[] parallelDataSet = Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length); |
| 169 | + Arrays.parallelSort(parallelDataSet); |
| 170 | + |
| 171 | + assertNotNull(parallelDataSet); |
| 172 | + assertNotSame(Arrays.copyOf(_1000000_elements_array, _1000000_elements_array.length), parallelDataSet); |
| 173 | + } |
| 174 | + |
| 175 | + @After |
| 176 | + public void tearDown() throws Exception { |
| 177 | + sizeOfArrays = null; |
| 178 | + _1000_elements_array = null; |
| 179 | + _10000_elements_array = null; |
| 180 | + _100000_elements_array = null; |
| 181 | + _1000000_elements_array = null; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | + |
0 commit comments