hotspot/src/share/vm/utilities/quickSort.cpp
author roland
Wed, 24 Apr 2013 14:26:09 +0200
changeset 17379 fec16b38217a
parent 17031 9a576749065b
child 17388 8193ee82efbe
permissions -rw-r--r--
8012292: optimized build with GCC broken Summary: Some #ifndef PRODUCT should be #ifdef ASSERT Reviewed-by: kvn, twisti Contributed-by: gdub <duboscq@ssw.jku.at>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     1
/*
17031
9a576749065b 8012907: anti-delta fix for 8010992
dcubed
parents: 17026
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     4
 *
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     8
 *
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    13
 * accompanied this code).
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    14
 *
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    18
 *
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    21
 * questions.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    22
 *
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    23
 */
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    24
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    25
#include "precompiled.hpp"
10994
fc93bca9c720 7102044: G1: VM crashes with assert(old_end != new_end) failed: don't call this otherwise
brutisso
parents: 10672
diff changeset
    26
fc93bca9c720 7102044: G1: VM crashes with assert(old_end != new_end) failed: don't call this otherwise
brutisso
parents: 10672
diff changeset
    27
/////////////// Unit tests ///////////////
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    28
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    29
#ifndef PRODUCT
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    30
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    31
#include "runtime/os.hpp"
10994
fc93bca9c720 7102044: G1: VM crashes with assert(old_end != new_end) failed: don't call this otherwise
brutisso
parents: 10672
diff changeset
    32
#include "utilities/quickSort.hpp"
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    33
#include <stdlib.h>
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    34
17379
fec16b38217a 8012292: optimized build with GCC broken
roland
parents: 17031
diff changeset
    35
#ifdef ASSERT
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    36
static int test_comparator(int a, int b) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    37
  if (a == b) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    38
    return 0;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    39
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    40
  if (a < b) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    41
    return -1;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    42
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    43
  return 1;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    44
}
17379
fec16b38217a 8012292: optimized build with GCC broken
roland
parents: 17031
diff changeset
    45
#endif // ASSERT
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    46
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    47
static int test_even_odd_comparator(int a, int b) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    48
  bool a_is_odd = (a % 2) == 1;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    49
  bool b_is_odd = (b % 2) == 1;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    50
  if (a_is_odd == b_is_odd) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    51
    return 0;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    52
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    53
  if (a_is_odd) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    54
    return -1;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    55
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    56
  return 1;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    57
}
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    58
10672
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    59
extern "C" {
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    60
  static int test_stdlib_comparator(const void* a, const void* b) {
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    61
    int ai = *(int*)a;
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    62
    int bi = *(int*)b;
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    63
    if (ai == bi) {
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    64
      return 0;
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    65
    }
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    66
    if (ai < bi) {
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    67
      return -1;
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    68
    }
446554f77598 7091366: re-enable quicksort tests
brutisso
parents: 10555
diff changeset
    69
    return 1;
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    70
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    71
}
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    72
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    73
void QuickSort::print_array(const char* prefix, int* array, int length) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    74
  tty->print("%s:", prefix);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    75
  for (int i = 0; i < length; i++) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    76
    tty->print(" %d", array[i]);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    77
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    78
  tty->print_cr("");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    79
}
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    80
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    81
bool QuickSort::compare_arrays(int* actual, int* expected, int length) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    82
  for (int i = 0; i < length; i++) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    83
    if (actual[i] != expected[i]) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    84
      print_array("Sorted array  ", actual, length);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    85
      print_array("Expected array", expected, length);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    86
      return false;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    87
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    88
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    89
  return true;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    90
}
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    91
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    92
template <class C>
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    93
bool QuickSort::sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    94
  sort<int, C>(arrayToSort, length, comparator, idempotent);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    95
  return compare_arrays(arrayToSort, expectedResult, length);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    96
}
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    97
11247
d6faa02b3802 7121373: Clean up CollectedHeap::is_in
stefank
parents: 10994
diff changeset
    98
void QuickSort::test_quick_sort() {
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
    99
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   100
    int* test_array = NULL;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   101
    int* expected_array = NULL;
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   102
    assert(sort_and_compare(test_array, expected_array, 0, test_comparator), "Empty array not handled");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   103
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   104
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   105
    int test_array[] = {3};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   106
    int expected_array[] = {3};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   107
    assert(sort_and_compare(test_array, expected_array, 1, test_comparator), "Single value array not handled");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   108
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   109
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   110
    int test_array[] = {3,2};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   111
    int expected_array[] = {2,3};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   112
    assert(sort_and_compare(test_array, expected_array, 2, test_comparator), "Array with 2 values not correctly sorted");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   113
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   114
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   115
    int test_array[] = {3,2,1};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   116
    int expected_array[] = {1,2,3};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   117
    assert(sort_and_compare(test_array, expected_array, 3, test_comparator), "Array with 3 values not correctly sorted");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   118
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   119
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   120
    int test_array[] = {4,3,2,1};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   121
    int expected_array[] = {1,2,3,4};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   122
    assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "Array with 4 values not correctly sorted");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   123
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   124
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   125
    int test_array[] = {7,1,5,3,6,9,8,2,4,0};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   126
    int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   127
    assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Array with 10 values not correctly sorted");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   128
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   129
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   130
    int test_array[] = {4,4,1,4};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   131
    int expected_array[] = {1,4,4,4};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   132
    assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "3 duplicates not sorted correctly");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   133
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   134
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   135
    int test_array[] = {0,1,2,3,4,5,6,7,8,9};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   136
    int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   137
    assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Already sorted array not correctly sorted");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   138
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   139
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   140
    // one of the random arrays that found an issue in the partion method.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   141
    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};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   142
    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};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   143
    assert(sort_and_compare(test_array, expected_array, 42, test_comparator), "Not correctly sorted");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   144
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   145
  {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   146
    int test_array[] = {2,8,1,4};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   147
    int expected_array[] = {1,4,2,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   148
    assert(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator), "Even/odd not sorted correctly");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   149
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   150
  {  // Some idempotent tests
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   151
    {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   152
      // An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   153
      int test_array[] = {1,4,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   154
      int expected_array[] = {1,4,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   155
      assert(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true), "Even/odd not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   156
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   157
    {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   158
      int test_array[] = {1,7,9,4,8,2};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   159
      int expected_array[] = {1,7,9,4,8,2};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   160
      assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   161
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   162
    {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   163
      int test_array[] = {1,9,7,4,2,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   164
      int expected_array[] = {1,9,7,4,2,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   165
      assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   166
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   167
    {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   168
      int test_array[] = {7,9,1,2,8,4};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   169
      int expected_array[] = {7,9,1,2,8,4};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   170
      assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   171
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   172
    {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   173
      int test_array[] = {7,1,9,2,4,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   174
      int expected_array[] = {7,1,9,2,4,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   175
      assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   176
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   177
    {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   178
      int test_array[] = {9,1,7,4,8,2};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   179
      int expected_array[] = {9,1,7,4,8,2};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   180
      assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   181
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   182
    {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   183
      int test_array[] = {9,7,1,4,2,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   184
      int expected_array[] = {9,7,1,4,2,8};
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   185
      assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   186
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   187
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   188
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   189
  // test sorting random arrays
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   190
  for (int i = 0; i < 1000; i++) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   191
    int length = os::random() % 100;
17031
9a576749065b 8012907: anti-delta fix for 8010992
dcubed
parents: 17026
diff changeset
   192
    int* test_array = new int[length];
9a576749065b 8012907: anti-delta fix for 8010992
dcubed
parents: 17026
diff changeset
   193
    int* expected_array = new int[length];
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   194
    for (int j = 0; j < length; j++) {
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   195
        // Choose random values, but get a chance of getting duplicates
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   196
        test_array[j] = os::random() % (length * 2);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   197
        expected_array[j] = test_array[j];
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   198
    }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   199
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   200
    // Compare sorting to stdlib::qsort()
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   201
    qsort(expected_array, length, sizeof(int), test_stdlib_comparator);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   202
    assert(sort_and_compare(test_array, expected_array, length, test_comparator), "Random array not correctly sorted");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   203
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   204
    // Make sure sorting is idempotent.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   205
    // Both test_array and expected_array are sorted by the test_comparator.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   206
    // Now sort them once with the test_even_odd_comparator. Then sort the
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   207
    // test_array one more time with test_even_odd_comparator and verify that
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   208
    // it is idempotent.
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   209
    sort(expected_array, length, test_even_odd_comparator, true);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   210
    sort(test_array, length, test_even_odd_comparator, true);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   211
    assert(compare_arrays(test_array, expected_array, length), "Sorting identical arrays rendered different results");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   212
    sort(test_array, length, test_even_odd_comparator, true);
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   213
    assert(compare_arrays(test_array, expected_array, length), "Sorting already sorted array changed order of elements - not idempotent");
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   214
17031
9a576749065b 8012907: anti-delta fix for 8010992
dcubed
parents: 17026
diff changeset
   215
    delete[] test_array;
9a576749065b 8012907: anti-delta fix for 8010992
dcubed
parents: 17026
diff changeset
   216
    delete[] expected_array;
10002
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   217
  }
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   218
}
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   219
2d83be3a0927 7016112: CMS: crash during promotion testing
brutisso
parents:
diff changeset
   220
#endif