Path: blob/master/test/hotspot/gtest/utilities/test_quicksort.cpp
41144 views
/*1* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.inline.hpp"26#include "runtime/os.hpp"27#include "utilities/quickSort.hpp"28#include "unittest.hpp"2930static int test_comparator(int a, int b) {31if (a == b) {32return 0;33}34if (a < b) {35return -1;36}37return 1;38}3940static bool compare_arrays(int* actual, int* expected, size_t length) {41for (size_t i = 0; i < length; i++) {42if (actual[i] != expected[i]) {43return false;44}45}46return true;47}4849template <class C>50static bool sort_and_compare(int* arrayToSort, int* expectedResult, size_t length, C comparator, bool idempotent = false) {51QuickSort::sort(arrayToSort, length, comparator, idempotent);52return compare_arrays(arrayToSort, expectedResult, length);53}5455static int test_even_odd_comparator(int a, int b) {56bool a_is_odd = ((a % 2) == 1);57bool b_is_odd = ((b % 2) == 1);58if (a_is_odd == b_is_odd) {59return 0;60}61if (a_is_odd) {62return -1;63}64return 1;65}6667extern "C" {68static int test_stdlib_comparator(const void* a, const void* b) {69int ai = *(int*)a;70int bi = *(int*)b;71if (ai == bi) {72return 0;73}74if (ai < bi) {75return -1;76}77return 1;78}79}8081TEST(QuickSort, quicksort) {82{83int* test_array = NULL;84int* expected_array = NULL;85EXPECT_TRUE(sort_and_compare(test_array, expected_array, 0, test_comparator));86}87{88int test_array[] = {3};89int expected_array[] = {3};90EXPECT_TRUE(sort_and_compare(test_array, expected_array, 1, test_comparator));91}92{93int test_array[] = {3,2};94int expected_array[] = {2,3};95EXPECT_TRUE(sort_and_compare(test_array, expected_array, 2, test_comparator));96}97{98int test_array[] = {3,2,1};99int expected_array[] = {1,2,3};100EXPECT_TRUE(sort_and_compare(test_array, expected_array, 3, test_comparator));101}102{103int test_array[] = {4,3,2,1};104int expected_array[] = {1,2,3,4};105EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_comparator));106}107{108int test_array[] = {7,1,5,3,6,9,8,2,4,0};109int expected_array[] = {0,1,2,3,4,5,6,7,8,9};110EXPECT_TRUE(sort_and_compare(test_array, expected_array, 10, test_comparator));111}112{113int test_array[] = {4,4,1,4};114int expected_array[] = {1,4,4,4};115EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_comparator));116}117{118int test_array[] = {0,1,2,3,4,5,6,7,8,9};119int expected_array[] = {0,1,2,3,4,5,6,7,8,9};120EXPECT_TRUE(sort_and_compare(test_array, expected_array, 10, test_comparator));121}122{123// one of the random arrays that found an issue in the partition method.124int 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};125int 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};126EXPECT_TRUE(sort_and_compare(test_array, expected_array, 42, test_comparator));127}128{129int test_array[] = {2,8,1,4};130int expected_array[] = {1,4,2,8};131EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator));132}133}134135TEST(QuickSort, idempotent) {136{137// An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent.138int test_array[] = {1, 4, 8};139int expected_array[] = {1, 4, 8};140EXPECT_TRUE(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true));141}142{143int test_array[] = {1, 7, 9, 4, 8, 2};144int expected_array[] = {1, 7, 9, 4, 8, 2};145EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));146}147{148int test_array[] = {1, 9, 7, 4, 2, 8};149int expected_array[] = {1, 9, 7, 4, 2, 8};150EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));151}152{153int test_array[] = {7, 9, 1, 2, 8, 4};154int expected_array[] = {7, 9, 1, 2, 8, 4};155EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));156}157{158int test_array[] = {7, 1, 9, 2, 4, 8};159int expected_array[] = {7, 1, 9, 2, 4, 8};160EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));161}162{163int test_array[] = {9, 1, 7, 4, 8, 2};164int expected_array[] = {9, 1, 7, 4, 8, 2};165EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));166}167{168int test_array[] = {9, 7, 1, 4, 2, 8};169int expected_array[] = {9, 7, 1, 4, 2, 8};170EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));171}172}173174TEST(QuickSort, random) {175for (int i = 0; i < 1000; i++) {176size_t length = os::random() % 100;177int* test_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);178int* expected_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);179for (size_t j = 0; j < length; j++) {180// Choose random values, but get a chance of getting duplicates181test_array[j] = os::random() % (length * 2);182expected_array[j] = test_array[j];183}184185// Compare sorting to stdlib::qsort()186qsort(expected_array, length, sizeof(int), test_stdlib_comparator);187EXPECT_TRUE(sort_and_compare(test_array, expected_array, length, test_comparator));188189// Make sure sorting is idempotent.190// Both test_array and expected_array are sorted by the test_comparator.191// Now sort them once with the test_even_odd_comparator. Then sort the192// test_array one more time with test_even_odd_comparator and verify that193// it is idempotent.194QuickSort::sort(expected_array, length, test_even_odd_comparator, true);195QuickSort::sort(test_array, length, test_even_odd_comparator, true);196EXPECT_TRUE(compare_arrays(test_array, expected_array, length));197QuickSort::sort(test_array, length, test_even_odd_comparator, true);198EXPECT_TRUE(compare_arrays(test_array, expected_array, length));199200FREE_C_HEAP_ARRAY(int, test_array);201FREE_C_HEAP_ARRAY(int, expected_array);202}203}204205206