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