Mon, 02 Jul 2012 13:11:28 -0400
Merge
brutisso@2976 | 1 | /* |
brutisso@2976 | 2 | * Copyright (c) 2011, 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" |
brutisso@2976 | 33 | #include <stdlib.h> |
brutisso@2976 | 34 | |
brutisso@2976 | 35 | static int test_comparator(int a, int b) { |
brutisso@2976 | 36 | if (a == b) { |
brutisso@2976 | 37 | return 0; |
brutisso@2976 | 38 | } |
brutisso@2976 | 39 | if (a < b) { |
brutisso@2976 | 40 | return -1; |
brutisso@2976 | 41 | } |
brutisso@2976 | 42 | return 1; |
brutisso@2976 | 43 | } |
brutisso@2976 | 44 | |
brutisso@2976 | 45 | static int test_even_odd_comparator(int a, int b) { |
brutisso@2976 | 46 | bool a_is_odd = (a % 2) == 1; |
brutisso@2976 | 47 | bool b_is_odd = (b % 2) == 1; |
brutisso@2976 | 48 | if (a_is_odd == b_is_odd) { |
brutisso@2976 | 49 | return 0; |
brutisso@2976 | 50 | } |
brutisso@2976 | 51 | if (a_is_odd) { |
brutisso@2976 | 52 | return -1; |
brutisso@2976 | 53 | } |
brutisso@2976 | 54 | return 1; |
brutisso@2976 | 55 | } |
brutisso@2976 | 56 | |
brutisso@3177 | 57 | extern "C" { |
brutisso@3177 | 58 | static int test_stdlib_comparator(const void* a, const void* b) { |
brutisso@3177 | 59 | int ai = *(int*)a; |
brutisso@3177 | 60 | int bi = *(int*)b; |
brutisso@3177 | 61 | if (ai == bi) { |
brutisso@3177 | 62 | return 0; |
brutisso@3177 | 63 | } |
brutisso@3177 | 64 | if (ai < bi) { |
brutisso@3177 | 65 | return -1; |
brutisso@3177 | 66 | } |
brutisso@3177 | 67 | return 1; |
brutisso@2976 | 68 | } |
brutisso@2976 | 69 | } |
brutisso@2976 | 70 | |
brutisso@2976 | 71 | void QuickSort::print_array(const char* prefix, int* array, int length) { |
brutisso@2976 | 72 | tty->print("%s:", prefix); |
brutisso@2976 | 73 | for (int i = 0; i < length; i++) { |
brutisso@2976 | 74 | tty->print(" %d", array[i]); |
brutisso@2976 | 75 | } |
brutisso@2976 | 76 | tty->print_cr(""); |
brutisso@2976 | 77 | } |
brutisso@2976 | 78 | |
brutisso@2976 | 79 | bool QuickSort::compare_arrays(int* actual, int* expected, int length) { |
brutisso@2976 | 80 | for (int i = 0; i < length; i++) { |
brutisso@2976 | 81 | if (actual[i] != expected[i]) { |
brutisso@2976 | 82 | print_array("Sorted array ", actual, length); |
brutisso@2976 | 83 | print_array("Expected array", expected, length); |
brutisso@2976 | 84 | return false; |
brutisso@2976 | 85 | } |
brutisso@2976 | 86 | } |
brutisso@2976 | 87 | return true; |
brutisso@2976 | 88 | } |
brutisso@2976 | 89 | |
brutisso@2976 | 90 | template <class C> |
brutisso@2976 | 91 | bool QuickSort::sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent) { |
brutisso@2976 | 92 | sort<int, C>(arrayToSort, length, comparator, idempotent); |
brutisso@2976 | 93 | return compare_arrays(arrayToSort, expectedResult, length); |
brutisso@2976 | 94 | } |
brutisso@2976 | 95 | |
stefank@3335 | 96 | void QuickSort::test_quick_sort() { |
brutisso@2976 | 97 | { |
brutisso@2976 | 98 | int* test_array = NULL; |
brutisso@2976 | 99 | int* expected_array = NULL; |
brutisso@2976 | 100 | assert(sort_and_compare(test_array, expected_array, 0, test_comparator), "Empty array not handled"); |
brutisso@2976 | 101 | } |
brutisso@2976 | 102 | { |
brutisso@2976 | 103 | int test_array[] = {3}; |
brutisso@2976 | 104 | int expected_array[] = {3}; |
brutisso@2976 | 105 | assert(sort_and_compare(test_array, expected_array, 1, test_comparator), "Single value array not handled"); |
brutisso@2976 | 106 | } |
brutisso@2976 | 107 | { |
brutisso@2976 | 108 | int test_array[] = {3,2}; |
brutisso@2976 | 109 | int expected_array[] = {2,3}; |
brutisso@2976 | 110 | assert(sort_and_compare(test_array, expected_array, 2, test_comparator), "Array with 2 values not correctly sorted"); |
brutisso@2976 | 111 | } |
brutisso@2976 | 112 | { |
brutisso@2976 | 113 | int test_array[] = {3,2,1}; |
brutisso@2976 | 114 | int expected_array[] = {1,2,3}; |
brutisso@2976 | 115 | assert(sort_and_compare(test_array, expected_array, 3, test_comparator), "Array with 3 values not correctly sorted"); |
brutisso@2976 | 116 | } |
brutisso@2976 | 117 | { |
brutisso@2976 | 118 | int test_array[] = {4,3,2,1}; |
brutisso@2976 | 119 | int expected_array[] = {1,2,3,4}; |
brutisso@2976 | 120 | assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "Array with 4 values not correctly sorted"); |
brutisso@2976 | 121 | } |
brutisso@2976 | 122 | { |
brutisso@2976 | 123 | int test_array[] = {7,1,5,3,6,9,8,2,4,0}; |
brutisso@2976 | 124 | int expected_array[] = {0,1,2,3,4,5,6,7,8,9}; |
brutisso@2976 | 125 | assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Array with 10 values not correctly sorted"); |
brutisso@2976 | 126 | } |
brutisso@2976 | 127 | { |
brutisso@2976 | 128 | int test_array[] = {4,4,1,4}; |
brutisso@2976 | 129 | int expected_array[] = {1,4,4,4}; |
brutisso@2976 | 130 | assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "3 duplicates not sorted correctly"); |
brutisso@2976 | 131 | } |
brutisso@2976 | 132 | { |
brutisso@2976 | 133 | int test_array[] = {0,1,2,3,4,5,6,7,8,9}; |
brutisso@2976 | 134 | int expected_array[] = {0,1,2,3,4,5,6,7,8,9}; |
brutisso@2976 | 135 | assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Already sorted array not correctly sorted"); |
brutisso@2976 | 136 | } |
brutisso@2976 | 137 | { |
brutisso@2976 | 138 | // one of the random arrays that found an issue in the partion method. |
brutisso@2976 | 139 | 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 | 140 | 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 | 141 | assert(sort_and_compare(test_array, expected_array, 42, test_comparator), "Not correctly sorted"); |
brutisso@2976 | 142 | } |
brutisso@2976 | 143 | { |
brutisso@2976 | 144 | int test_array[] = {2,8,1,4}; |
brutisso@2976 | 145 | int expected_array[] = {1,4,2,8}; |
brutisso@2976 | 146 | assert(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator), "Even/odd not sorted correctly"); |
brutisso@2976 | 147 | } |
brutisso@2976 | 148 | { // Some idempotent tests |
brutisso@2976 | 149 | { |
brutisso@2976 | 150 | // An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent. |
brutisso@2976 | 151 | int test_array[] = {1,4,8}; |
brutisso@2976 | 152 | int expected_array[] = {1,4,8}; |
brutisso@2976 | 153 | assert(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 154 | } |
brutisso@2976 | 155 | { |
brutisso@2976 | 156 | int test_array[] = {1,7,9,4,8,2}; |
brutisso@2976 | 157 | int expected_array[] = {1,7,9,4,8,2}; |
brutisso@2976 | 158 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 159 | } |
brutisso@2976 | 160 | { |
brutisso@2976 | 161 | int test_array[] = {1,9,7,4,2,8}; |
brutisso@2976 | 162 | int expected_array[] = {1,9,7,4,2,8}; |
brutisso@2976 | 163 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 164 | } |
brutisso@2976 | 165 | { |
brutisso@2976 | 166 | int test_array[] = {7,9,1,2,8,4}; |
brutisso@2976 | 167 | int expected_array[] = {7,9,1,2,8,4}; |
brutisso@2976 | 168 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 169 | } |
brutisso@2976 | 170 | { |
brutisso@2976 | 171 | int test_array[] = {7,1,9,2,4,8}; |
brutisso@2976 | 172 | int expected_array[] = {7,1,9,2,4,8}; |
brutisso@2976 | 173 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 174 | } |
brutisso@2976 | 175 | { |
brutisso@2976 | 176 | int test_array[] = {9,1,7,4,8,2}; |
brutisso@2976 | 177 | int expected_array[] = {9,1,7,4,8,2}; |
brutisso@2976 | 178 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 179 | } |
brutisso@2976 | 180 | { |
brutisso@2976 | 181 | int test_array[] = {9,7,1,4,2,8}; |
brutisso@2976 | 182 | int expected_array[] = {9,7,1,4,2,8}; |
brutisso@2976 | 183 | assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); |
brutisso@2976 | 184 | } |
brutisso@2976 | 185 | } |
brutisso@2976 | 186 | |
brutisso@2976 | 187 | // test sorting random arrays |
brutisso@2976 | 188 | for (int i = 0; i < 1000; i++) { |
brutisso@2976 | 189 | int length = os::random() % 100; |
brutisso@2976 | 190 | int* test_array = new int[length]; |
brutisso@2976 | 191 | int* expected_array = new int[length]; |
brutisso@2976 | 192 | for (int j = 0; j < length; j++) { |
brutisso@2976 | 193 | // Choose random values, but get a chance of getting duplicates |
brutisso@2976 | 194 | test_array[j] = os::random() % (length * 2); |
brutisso@2976 | 195 | expected_array[j] = test_array[j]; |
brutisso@2976 | 196 | } |
brutisso@2976 | 197 | |
brutisso@2976 | 198 | // Compare sorting to stdlib::qsort() |
brutisso@2976 | 199 | qsort(expected_array, length, sizeof(int), test_stdlib_comparator); |
brutisso@2976 | 200 | assert(sort_and_compare(test_array, expected_array, length, test_comparator), "Random array not correctly sorted"); |
brutisso@2976 | 201 | |
brutisso@2976 | 202 | // Make sure sorting is idempotent. |
brutisso@2976 | 203 | // Both test_array and expected_array are sorted by the test_comparator. |
brutisso@2976 | 204 | // Now sort them once with the test_even_odd_comparator. Then sort the |
brutisso@2976 | 205 | // test_array one more time with test_even_odd_comparator and verify that |
brutisso@2976 | 206 | // it is idempotent. |
brutisso@2976 | 207 | sort(expected_array, length, test_even_odd_comparator, true); |
brutisso@2976 | 208 | sort(test_array, length, test_even_odd_comparator, true); |
brutisso@2976 | 209 | assert(compare_arrays(test_array, expected_array, length), "Sorting identical arrays rendered different results"); |
brutisso@2976 | 210 | sort(test_array, length, test_even_odd_comparator, true); |
brutisso@2976 | 211 | assert(compare_arrays(test_array, expected_array, length), "Sorting already sorted array changed order of elements - not idempotent"); |
brutisso@2976 | 212 | |
brutisso@2976 | 213 | delete[] test_array; |
brutisso@2976 | 214 | delete[] expected_array; |
brutisso@2976 | 215 | } |
brutisso@2976 | 216 | } |
brutisso@2976 | 217 | |
brutisso@2976 | 218 | #endif |