aoqi@0: /* aoqi@0: * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: aoqi@0: /////////////// Unit tests /////////////// aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "utilities/quickSort.hpp" aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: static int test_comparator(int a, int b) { aoqi@0: if (a == b) { aoqi@0: return 0; aoqi@0: } aoqi@0: if (a < b) { aoqi@0: return -1; aoqi@0: } aoqi@0: return 1; aoqi@0: } aoqi@0: #endif // ASSERT aoqi@0: aoqi@0: static int test_even_odd_comparator(int a, int b) { aoqi@0: bool a_is_odd = (a % 2) == 1; aoqi@0: bool b_is_odd = (b % 2) == 1; aoqi@0: if (a_is_odd == b_is_odd) { aoqi@0: return 0; aoqi@0: } aoqi@0: if (a_is_odd) { aoqi@0: return -1; aoqi@0: } aoqi@0: return 1; aoqi@0: } aoqi@0: aoqi@0: extern "C" { aoqi@0: static int test_stdlib_comparator(const void* a, const void* b) { aoqi@0: int ai = *(int*)a; aoqi@0: int bi = *(int*)b; aoqi@0: if (ai == bi) { aoqi@0: return 0; aoqi@0: } aoqi@0: if (ai < bi) { aoqi@0: return -1; aoqi@0: } aoqi@0: return 1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void QuickSort::print_array(const char* prefix, int* array, int length) { aoqi@0: tty->print("%s:", prefix); aoqi@0: for (int i = 0; i < length; i++) { aoqi@0: tty->print(" %d", array[i]); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: bool QuickSort::compare_arrays(int* actual, int* expected, int length) { aoqi@0: for (int i = 0; i < length; i++) { aoqi@0: if (actual[i] != expected[i]) { aoqi@0: print_array("Sorted array ", actual, length); aoqi@0: print_array("Expected array", expected, length); aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: bool QuickSort::sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent) { aoqi@0: sort(arrayToSort, length, comparator, idempotent); aoqi@0: return compare_arrays(arrayToSort, expectedResult, length); aoqi@0: } aoqi@0: aoqi@0: void QuickSort::test_quick_sort() { aoqi@0: { aoqi@0: int* test_array = NULL; aoqi@0: int* expected_array = NULL; aoqi@0: assert(sort_and_compare(test_array, expected_array, 0, test_comparator), "Empty array not handled"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {3}; aoqi@0: int expected_array[] = {3}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 1, test_comparator), "Single value array not handled"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {3,2}; aoqi@0: int expected_array[] = {2,3}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 2, test_comparator), "Array with 2 values not correctly sorted"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {3,2,1}; aoqi@0: int expected_array[] = {1,2,3}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 3, test_comparator), "Array with 3 values not correctly sorted"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {4,3,2,1}; aoqi@0: int expected_array[] = {1,2,3,4}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "Array with 4 values not correctly sorted"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {7,1,5,3,6,9,8,2,4,0}; aoqi@0: int expected_array[] = {0,1,2,3,4,5,6,7,8,9}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Array with 10 values not correctly sorted"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {4,4,1,4}; aoqi@0: int expected_array[] = {1,4,4,4}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "3 duplicates not sorted correctly"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {0,1,2,3,4,5,6,7,8,9}; aoqi@0: int expected_array[] = {0,1,2,3,4,5,6,7,8,9}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Already sorted array not correctly sorted"); aoqi@0: } aoqi@0: { aoqi@0: // one of the random arrays that found an issue in the partion method. aoqi@0: 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: 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: assert(sort_and_compare(test_array, expected_array, 42, test_comparator), "Not correctly sorted"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {2,8,1,4}; aoqi@0: int expected_array[] = {1,4,2,8}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator), "Even/odd not sorted correctly"); aoqi@0: } aoqi@0: { // Some idempotent tests aoqi@0: { aoqi@0: // An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent. aoqi@0: int test_array[] = {1,4,8}; aoqi@0: int expected_array[] = {1,4,8}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true), "Even/odd not idempotent"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {1,7,9,4,8,2}; aoqi@0: int expected_array[] = {1,7,9,4,8,2}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {1,9,7,4,2,8}; aoqi@0: int expected_array[] = {1,9,7,4,2,8}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {7,9,1,2,8,4}; aoqi@0: int expected_array[] = {7,9,1,2,8,4}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {7,1,9,2,4,8}; aoqi@0: int expected_array[] = {7,1,9,2,4,8}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {9,1,7,4,8,2}; aoqi@0: int expected_array[] = {9,1,7,4,8,2}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); aoqi@0: } aoqi@0: { aoqi@0: int test_array[] = {9,7,1,4,2,8}; aoqi@0: int expected_array[] = {9,7,1,4,2,8}; aoqi@0: assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // test sorting random arrays aoqi@0: for (int i = 0; i < 1000; i++) { aoqi@0: int length = os::random() % 100; aoqi@0: int* test_array = NEW_C_HEAP_ARRAY(int, length, mtInternal); aoqi@0: int* expected_array = NEW_C_HEAP_ARRAY(int, length, mtInternal); aoqi@0: for (int j = 0; j < length; j++) { aoqi@0: // Choose random values, but get a chance of getting duplicates aoqi@0: test_array[j] = os::random() % (length * 2); aoqi@0: expected_array[j] = test_array[j]; aoqi@0: } aoqi@0: aoqi@0: // Compare sorting to stdlib::qsort() aoqi@0: qsort(expected_array, length, sizeof(int), test_stdlib_comparator); aoqi@0: assert(sort_and_compare(test_array, expected_array, length, test_comparator), "Random array not correctly sorted"); aoqi@0: aoqi@0: // Make sure sorting is idempotent. aoqi@0: // Both test_array and expected_array are sorted by the test_comparator. aoqi@0: // Now sort them once with the test_even_odd_comparator. Then sort the aoqi@0: // test_array one more time with test_even_odd_comparator and verify that aoqi@0: // it is idempotent. aoqi@0: sort(expected_array, length, test_even_odd_comparator, true); aoqi@0: sort(test_array, length, test_even_odd_comparator, true); aoqi@0: assert(compare_arrays(test_array, expected_array, length), "Sorting identical arrays rendered different results"); aoqi@0: sort(test_array, length, test_even_odd_comparator, true); aoqi@0: assert(compare_arrays(test_array, expected_array, length), "Sorting already sorted array changed order of elements - not idempotent"); aoqi@0: aoqi@0: FREE_C_HEAP_ARRAY(int, test_array, mtInternal); aoqi@0: FREE_C_HEAP_ARRAY(int, expected_array, mtInternal); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #endif