annotate src/share/vm/utilities/quickSort.cpp @ 4708:3c648b9ad052

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