test/hotspot/gtest/utilities/test_quicksort.cpp
author manc
Mon, 14 Oct 2019 18:48:10 -0700
changeset 58652 9b67dd88a931
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232232: G1RemSetSummary::_rs_threads_vtimes is not initialized to zero Summary: Fix error in "Concurrent refinement threads times" in GC log and cleanup. Reviewed-by: tschatzl, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     1
/*
46768
58f648e29a26 8185757: QuickSort array size should be size_t
kbarrett
parents: 42054
diff changeset
     2
 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     4
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     8
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    14
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    18
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    21
 * questions.
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    22
 *
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    23
 */
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    24
39394
044a0eb96215 8159364: Gtest unit tests does not support PCH
ehelin
parents: 38255
diff changeset
    25
#include "precompiled.hpp"
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    26
#include "memory/allocation.inline.hpp"
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    27
#include "runtime/os.hpp"
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    28
#include "utilities/quickSort.hpp"
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    29
#include "unittest.hpp"
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    30
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    31
static int test_comparator(int a, int b) {
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    32
  if (a == b) {
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    33
    return 0;
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    34
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    35
  if (a < b) {
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    36
    return -1;
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    37
  }
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    38
  return 1;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    39
}
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    40
46768
58f648e29a26 8185757: QuickSort array size should be size_t
kbarrett
parents: 42054
diff changeset
    41
static bool compare_arrays(int* actual, int* expected, size_t length) {
58f648e29a26 8185757: QuickSort array size should be size_t
kbarrett
parents: 42054
diff changeset
    42
  for (size_t i = 0; i < length; i++) {
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    43
    if (actual[i] != expected[i]) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    44
      return false;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    45
    }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    46
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    47
  return true;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    48
}
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    49
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    50
template <class C>
46768
58f648e29a26 8185757: QuickSort array size should be size_t
kbarrett
parents: 42054
diff changeset
    51
static bool sort_and_compare(int* arrayToSort, int* expectedResult, size_t length, C comparator, bool idempotent = false) {
58f648e29a26 8185757: QuickSort array size should be size_t
kbarrett
parents: 42054
diff changeset
    52
  QuickSort::sort(arrayToSort, length, comparator, idempotent);
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    53
  return compare_arrays(arrayToSort, expectedResult, length);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    54
}
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    55
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    56
static int test_even_odd_comparator(int a, int b) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    57
  bool a_is_odd = ((a % 2) == 1);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    58
  bool b_is_odd = ((b % 2) == 1);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    59
  if (a_is_odd == b_is_odd) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    60
    return 0;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    61
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    62
  if (a_is_odd) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    63
    return -1;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    64
  }
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    65
  return 1;
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    66
}
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    67
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    68
extern "C" {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    69
  static int test_stdlib_comparator(const void* a, const void* b) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    70
    int ai = *(int*)a;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    71
    int bi = *(int*)b;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    72
    if (ai == bi) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    73
      return 0;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    74
    }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    75
    if (ai < bi) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    76
      return -1;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    77
    }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    78
    return 1;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    79
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    80
}
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    81
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    82
TEST(QuickSort, quicksort) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    83
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    84
    int* test_array = NULL;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    85
    int* expected_array = NULL;
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    86
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 0, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    87
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    88
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    89
    int test_array[] = {3};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    90
    int expected_array[] = {3};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    91
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 1, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    92
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    93
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    94
    int test_array[] = {3,2};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    95
    int expected_array[] = {2,3};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    96
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 2, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    97
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    98
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
    99
    int test_array[] = {3,2,1};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   100
    int expected_array[] = {1,2,3};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   101
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 3, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   102
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   103
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   104
    int test_array[] = {4,3,2,1};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   105
    int expected_array[] = {1,2,3,4};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   106
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   107
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   108
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   109
    int test_array[] = {7,1,5,3,6,9,8,2,4,0};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   110
    int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   111
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 10, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   112
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   113
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   114
    int test_array[] = {4,4,1,4};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   115
    int expected_array[] = {1,4,4,4};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   116
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   117
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   118
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   119
    int test_array[] = {0,1,2,3,4,5,6,7,8,9};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   120
    int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   121
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 10, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   122
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   123
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   124
    // one of the random arrays that found an issue in the partition method.
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   125
    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};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   126
    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};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   127
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 42, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   128
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   129
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   130
    int test_array[] = {2,8,1,4};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   131
    int expected_array[] = {1,4,2,8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   132
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   133
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   134
}
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   135
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   136
TEST(QuickSort, idempotent) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   137
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   138
    // An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent.
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   139
    int test_array[] = {1, 4, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   140
    int expected_array[] = {1, 4, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   141
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   142
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   143
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   144
    int test_array[] = {1, 7, 9, 4, 8, 2};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   145
    int expected_array[] = {1, 7, 9, 4, 8, 2};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   146
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   147
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   148
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   149
    int test_array[] = {1, 9, 7, 4, 2, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   150
    int expected_array[] = {1, 9, 7, 4, 2, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   151
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   152
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   153
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   154
    int test_array[] = {7, 9, 1, 2, 8, 4};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   155
    int expected_array[] = {7, 9, 1, 2, 8, 4};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   156
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   157
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   158
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   159
    int test_array[] = {7, 1, 9, 2, 4, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   160
    int expected_array[] = {7, 1, 9, 2, 4, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   161
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   162
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   163
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   164
    int test_array[] = {9, 1, 7, 4, 8, 2};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   165
    int expected_array[] = {9, 1, 7, 4, 8, 2};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   166
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   167
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   168
  {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   169
    int test_array[] = {9, 7, 1, 4, 2, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   170
    int expected_array[] = {9, 7, 1, 4, 2, 8};
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   171
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   172
  }
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   173
}
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   174
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   175
TEST(QuickSort, random) {
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   176
  for (int i = 0; i < 1000; i++) {
46768
58f648e29a26 8185757: QuickSort array size should be size_t
kbarrett
parents: 42054
diff changeset
   177
    size_t length = os::random() % 100;
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   178
    int* test_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   179
    int* expected_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);
46768
58f648e29a26 8185757: QuickSort array size should be size_t
kbarrett
parents: 42054
diff changeset
   180
    for (size_t j = 0; j < length; j++) {
42054
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   181
        // Choose random values, but get a chance of getting duplicates
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   182
        test_array[j] = os::random() % (length * 2);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   183
        expected_array[j] = test_array[j];
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   184
    }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   185
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   186
    // Compare sorting to stdlib::qsort()
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   187
    qsort(expected_array, length, sizeof(int), test_stdlib_comparator);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   188
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, length, test_comparator));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   189
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   190
    // Make sure sorting is idempotent.
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   191
    // Both test_array and expected_array are sorted by the test_comparator.
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   192
    // Now sort them once with the test_even_odd_comparator. Then sort the
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   193
    // test_array one more time with test_even_odd_comparator and verify that
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   194
    // it is idempotent.
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   195
    QuickSort::sort(expected_array, length, test_even_odd_comparator, true);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   196
    QuickSort::sort(test_array, length, test_even_odd_comparator, true);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   197
    EXPECT_TRUE(compare_arrays(test_array, expected_array, length));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   198
    QuickSort::sort(test_array, length, test_even_odd_comparator, true);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   199
    EXPECT_TRUE(compare_arrays(test_array, expected_array, length));
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   200
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   201
    FREE_C_HEAP_ARRAY(int, test_array);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   202
    FREE_C_HEAP_ARRAY(int, expected_array);
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   203
  }
498d02c046db 8156800: Convert QuickSort_test to GTest
jwilhelm
parents: 41705
diff changeset
   204
}