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