Thu, 16 May 2013 13:47:55 -0700
Merge
brutisso@2976 | 1 | /* |
minqi@5103 | 2 | * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
brutisso@2976 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
brutisso@2976 | 4 | * |
brutisso@2976 | 5 | * This code is free software; you can redistribute it and/or modify it |
brutisso@2976 | 6 | * under the terms of the GNU General Public License version 2 only, as |
brutisso@2976 | 7 | * published by the Free Software Foundation. |
brutisso@2976 | 8 | * |
brutisso@2976 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
brutisso@2976 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
brutisso@2976 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
brutisso@2976 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
brutisso@2976 | 13 | * accompanied this code). |
brutisso@2976 | 14 | * |
brutisso@2976 | 15 | * You should have received a copy of the GNU General Public License version |
brutisso@2976 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
brutisso@2976 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
brutisso@2976 | 18 | * |
brutisso@2976 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
brutisso@2976 | 20 | * or visit www.oracle.com if you need additional information or have any |
brutisso@2976 | 21 | * questions. |
brutisso@2976 | 22 | * |
brutisso@2976 | 23 | */ |
brutisso@2976 | 24 | |
brutisso@2976 | 25 | #include "precompiled.hpp" |
brutisso@3266 | 26 | |
brutisso@3266 | 27 | /////////////// Unit tests /////////////// |
brutisso@2976 | 28 | |
brutisso@2976 | 29 | #ifndef PRODUCT |
brutisso@2976 | 30 | |
brutisso@2976 | 31 | #include "runtime/os.hpp" |
brutisso@3266 | 32 | #include "utilities/quickSort.hpp" |
minqi@5103 | 33 | #include "memory/allocation.hpp" |
minqi@5103 | 34 | #include "memory/allocation.inline.hpp" |
brutisso@2976 | 35 | #include <stdlib.h> |
brutisso@2976 | 36 | |
roland@5106 | 37 | #ifdef ASSERT |
brutisso@2976 | 38 | static int test_comparator(int a, int b) { |
brutisso@2976 | 39 | if (a == b) { |
brutisso@2976 | 40 | return 0; |
brutisso@2976 | 41 | } |
brutisso@2976 | 42 | if (a < b) { |
brutisso@2976 | 43 | return -1; |
brutisso@2976 | 44 | } |
brutisso@2976 | 45 | return 1; |
brutisso@2976 | 46 | } |
roland@5106 | 47 | #endif // ASSERT |
brutisso@2976 | 48 | |
brutisso@2976 | 49 | static int test_even_odd_comparator(int a, int b) { |
brutisso@2976 | 50 | bool a_is_odd = (a % 2) == 1; |
brutisso@2976 | 51 | bool b_is_odd = (b % 2) == 1; |
brutisso@2976 | 52 | if (a_is_odd == b_is_odd) { |
brutisso@2976 | 53 | return 0; |
brutisso@2976 | 54 | } |
brutisso@2976 | 55 | if (a_is_odd) { |
brutisso@2976 | 56 | return -1; |
brutisso@2976 | 57 | } |
brutisso@2976 | 58 | return 1; |
brutisso@2976 | 59 | } |
brutisso@2976 | 60 | |
brutisso@3177 | 61 | extern "C" { |
brutisso@3177 | 62 | static int test_stdlib_comparator(const void* a, const void* b) { |
brutisso@3177 | 63 | int ai = *(int*)a; |
brutisso@3177 | 64 | int bi = *(int*)b; |
brutisso@3177 | 65 | if (ai == bi) { |
brutisso@3177 | 66 | return 0; |
brutisso@3177 | 67 | } |
brutisso@3177 | 68 | if (ai < bi) { |
brutisso@3177 | 69 | return -1; |
brutisso@3177 | 70 | } |
brutisso@3177 | 71 | return 1; |
brutisso@2976 | 72 | } |
brutisso@2976 | 73 | } |
brutisso@2976 | 74 | |
brutisso@2976 | 75 | void QuickSort::print_array(const char* prefix, int* array, int length) { |
brutisso@2976 | 76 | tty->print("%s:", prefix); |
brutisso@2976 | 77 | for (int i = 0; i < length; i++) { |
brutisso@2976 | 78 | tty->print(" %d", array[i]); |
brutisso@2976 | 79 | } |
brutisso@2976 | 80 | tty->print_cr(""); |
brutisso@2976 | 81 | } |
brutisso@2976 | 82 | |
brutisso@2976 | 83 | bool QuickSort::compare_arrays(int* actual, int* expected, int length) { |
brutisso@2976 | 84 | for (int i = 0; i < length; i++) { |
brutisso@2976 | 85 | if (actual[i] != expected[i]) { |
brutisso@2976 | 86 | print_array("Sorted array ", actual, length); |
brutisso@2976 | 87 | print_array("Expected array", expected, length); |
brutisso@2976 | 88 | return false; |
brutisso@2976 | 89 | } |
brutisso@2976 | 90 | } |
brutisso@2976 | 91 | return true; |
brutisso@2976 | 92 | } |
brutisso@2976 | 93 | |
brutisso@2976 | 94 | template <class C> |
brutisso@2976 | 95 | bool QuickSort::sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent) { |
brutisso@2976 | 96 | sort<int, C>(arrayToSort, length, comparator, idempotent); |
brutisso@2976 | 97 | return compare_arrays(arrayToSort, expectedResult, length); |
brutisso@2976 | 98 | } |
brutisso@2976 | 99 | |
stefank@3335 | 100 | void QuickSort::test_quick_sort() { |
brutisso@2976 | 101 | { |
brutisso@2976 | 102 | int* test_array = NULL; |
brutisso@2976 | 103 | int* expected_array = NULL; |
brutisso@2976 | 104 | assert(sort_and_compare(test_array, expected_array, 0, test_comparator), "Empty array not handled"); |
brutisso@2976 | 105 | } |
brutisso@2976 | 106 | { |
brutisso@2976 | 107 | int test_array[] = {3}; |
brutisso@2976 | 108 | int expected_array[] = {3}; |
brutisso@2976 | 109 | assert(sort_and_compare(test_array, expected_array, 1, test_comparator), "Single value array not handled"); |
brutisso@2976 | 110 | } |
brutisso@2976 | 111 | { |
brutisso@2976 | 112 | int test_array[] = {3,2}; |
brutisso@2976 | 113 | int expected_array[] = {2,3}; |
brutisso@2976 | 114 | assert(sort_and_compare(test_array, expected_array, 2, test_comparator), "Array with 2 values not correctly sorted"); |
brutisso@2976 | 115 | } |
brutisso@2976 | 116 | { |
brutisso@2976 | 117 | int test_array[] = {3,2,1}; |
brutisso@2976 | 118 | int expected_array[] = {1,2,3}; |
brutisso@2976 | 119 | assert(sort_and_compare(test_array, expected_array, 3, test_comparator), "Array with 3 values not correctly sorted"); |
brutisso@2976 | 120 | } |
brutisso@2976 | 121 | { |
brutisso@2976 | 122 | int test_array[] = {4,3,2,1}; |
brutisso@2976 | 123 | int expected_array[] = {1,2,3,4}; |
brutisso@2976 | 124 | assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "Array with 4 values not correctly sorted"); |
brutisso@2976 | 125 | } |
brutisso@2976 | 126 | { |
brutisso@2976 | 127 | int test_array[] = {7,1,5,3,6,9,8,2,4,0}; |
brutisso@2976 | 128 | int expected_array[] = {0,1,2,3,4,5,6,7,8,9}; |
brutisso@2976 | 129 | assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Array with 10 values not correctly sorted"); |
brutisso@2976 | 130 | } |
brutisso@2976 | 131 | { |
brutisso@2976 | 132 | int test_array[] = {4,4,1,4}; |
brutisso@2976 | 133 | int expected_array[] = {1,4,4,4}; |
brutisso@2976 | 134 | assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "3 duplicates not sorted correctly"); |
brutisso@2976 | 135 | } |
brutisso@2976 | 136 | { |
brutisso@2976 | 137 | int test_array[] = {0,1,2,3,4,5,6,7,8,9}; |
brutisso@2976 | 138 | int expected_array[] = {0,1,2,3,4,5,6,7,8,9}; |
brutisso@2976 | 139 | assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Already sorted array not correctly sorted"); |
brutisso@2976 | 140 | } |
brutisso@2976 | 141 | { |
brutisso@2976 | 142 | // one of the random arrays that found an issue in the partion method. |
brutisso@2976 | 143 | int test_array[] = {76,46,81,8,64,56,75,11,51,55,11,71,59,27,9,64,69,75,21,25,39,40,44,32,7,8,40,41,24,78,24,74,9,65,28,6,40,31,22,13,27,82}; |
brutisso@2976 | 144 | int expected_array[] = {6,7,8,8,9,9,11,11,13,21,22,24,24,25,27,27,28,31,32,39,40,40,40,41,44,46,51,55,56,59,64,64,65,69,71,74,75,75,76,78,81,82}; |
brutisso@2976 | 145 | assert(sort_and_compare(test_array, expected_array, 42, test_comparator), "Not correctly sorted"); |
brutisso@2976 | 146 | } |
brutisso@2976 | 147 | { |
brutisso@2976 | 148 | int test_array[] = {2,8,1,4}; |
brutisso@2976 | 149 | int expected_array[] = {1,4,2,8}; |
brutisso@2976 | 150 | assert(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator), "Even/odd not sorted correctly"); |
brutisso@2976 | 151 | } |
brutisso@2976 | 152 | { // Some idempotent tests |
brutisso@2976 | 153 | { |
brutisso@2976 | 154 | // An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent. |
brutisso@2976 | 155 | int test_array[] = {1,4,8}; |
brutisso@2976 | 156 | int expected_array[] = {1,4,8}; |
brutisso@2976 | 157 | assert(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 158 | } |
brutisso@2976 | 159 | { |
brutisso@2976 | 160 | int test_array[] = {1,7,9,4,8,2}; |
brutisso@2976 | 161 | int expected_array[] = {1,7,9,4,8,2}; |
brutisso@2976 | 162 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 163 | } |
brutisso@2976 | 164 | { |
brutisso@2976 | 165 | int test_array[] = {1,9,7,4,2,8}; |
brutisso@2976 | 166 | int expected_array[] = {1,9,7,4,2,8}; |
brutisso@2976 | 167 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 168 | } |
brutisso@2976 | 169 | { |
brutisso@2976 | 170 | int test_array[] = {7,9,1,2,8,4}; |
brutisso@2976 | 171 | int expected_array[] = {7,9,1,2,8,4}; |
brutisso@2976 | 172 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 173 | } |
brutisso@2976 | 174 | { |
brutisso@2976 | 175 | int test_array[] = {7,1,9,2,4,8}; |
brutisso@2976 | 176 | int expected_array[] = {7,1,9,2,4,8}; |
brutisso@2976 | 177 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 178 | } |
brutisso@2976 | 179 | { |
brutisso@2976 | 180 | int test_array[] = {9,1,7,4,8,2}; |
brutisso@2976 | 181 | int expected_array[] = {9,1,7,4,8,2}; |
brutisso@2976 | 182 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 183 | } |
brutisso@2976 | 184 | { |
brutisso@2976 | 185 | int test_array[] = {9,7,1,4,2,8}; |
brutisso@2976 | 186 | int expected_array[] = {9,7,1,4,2,8}; |
brutisso@2976 | 187 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 188 | } |
brutisso@2976 | 189 | } |
brutisso@2976 | 190 | |
brutisso@2976 | 191 | // test sorting random arrays |
brutisso@2976 | 192 | for (int i = 0; i < 1000; i++) { |
brutisso@2976 | 193 | int length = os::random() % 100; |
minqi@5103 | 194 | int* test_array = NEW_C_HEAP_ARRAY(int, length, mtInternal); |
minqi@5103 | 195 | int* expected_array = NEW_C_HEAP_ARRAY(int, length, mtInternal); |
brutisso@2976 | 196 | for (int j = 0; j < length; j++) { |
brutisso@2976 | 197 | // Choose random values, but get a chance of getting duplicates |
brutisso@2976 | 198 | test_array[j] = os::random() % (length * 2); |
brutisso@2976 | 199 | expected_array[j] = test_array[j]; |
brutisso@2976 | 200 | } |
brutisso@2976 | 201 | |
brutisso@2976 | 202 | // Compare sorting to stdlib::qsort() |
brutisso@2976 | 203 | qsort(expected_array, length, sizeof(int), test_stdlib_comparator); |
brutisso@2976 | 204 | assert(sort_and_compare(test_array, expected_array, length, test_comparator), "Random array not correctly sorted"); |
brutisso@2976 | 205 | |
brutisso@2976 | 206 | // Make sure sorting is idempotent. |
brutisso@2976 | 207 | // Both test_array and expected_array are sorted by the test_comparator. |
brutisso@2976 | 208 | // Now sort them once with the test_even_odd_comparator. Then sort the |
brutisso@2976 | 209 | // test_array one more time with test_even_odd_comparator and verify that |
brutisso@2976 | 210 | // it is idempotent. |
brutisso@2976 | 211 | sort(expected_array, length, test_even_odd_comparator, true); |
brutisso@2976 | 212 | sort(test_array, length, test_even_odd_comparator, true); |
brutisso@2976 | 213 | assert(compare_arrays(test_array, expected_array, length), "Sorting identical arrays rendered different results"); |
brutisso@2976 | 214 | sort(test_array, length, test_even_odd_comparator, true); |
brutisso@2976 | 215 | assert(compare_arrays(test_array, expected_array, length), "Sorting already sorted array changed order of elements - not idempotent"); |
brutisso@2976 | 216 | |
minqi@5103 | 217 | FREE_C_HEAP_ARRAY(int, test_array, mtInternal); |
minqi@5103 | 218 | FREE_C_HEAP_ARRAY(int, expected_array, mtInternal); |
brutisso@2976 | 219 | } |
brutisso@2976 | 220 | } |
brutisso@2976 | 221 | |
brutisso@2976 | 222 | #endif |