src/share/vm/utilities/quickSort.cpp

Tue, 24 Jul 2018 13:22:11 +0800

author
aoqi
date
Tue, 24 Jul 2018 13:22:11 +0800
changeset 9137
dc1769738300
parent 6876
710a3c8b516e
permissions
-rw-r--r--

#7048 added Loongson release info to hs_err crash files

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

mercurial