Tue, 14 May 2013 09:41:12 -0700
8012902: remove use of global operator new - take 2
Summary: The fix of 8010992, disable use of global operator new and new[] which caused failure on some tests. This takes two of the bugs also add ALLOW_OPERATOR_NEW_USAGE to prevent crash for third party code calling operator new of jvm on certain platforms.
Reviewed-by: coleenp, dholmes, zgu
Contributed-by: yumin.qi@oracle.com
1 /*
2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
27 /////////////// Unit tests ///////////////
29 #ifndef PRODUCT
31 #include "runtime/os.hpp"
32 #include "utilities/quickSort.hpp"
33 #include "memory/allocation.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include <stdlib.h>
37 static int test_comparator(int a, int b) {
38 if (a == b) {
39 return 0;
40 }
41 if (a < b) {
42 return -1;
43 }
44 return 1;
45 }
47 static int test_even_odd_comparator(int a, int b) {
48 bool a_is_odd = (a % 2) == 1;
49 bool b_is_odd = (b % 2) == 1;
50 if (a_is_odd == b_is_odd) {
51 return 0;
52 }
53 if (a_is_odd) {
54 return -1;
55 }
56 return 1;
57 }
59 extern "C" {
60 static int test_stdlib_comparator(const void* a, const void* b) {
61 int ai = *(int*)a;
62 int bi = *(int*)b;
63 if (ai == bi) {
64 return 0;
65 }
66 if (ai < bi) {
67 return -1;
68 }
69 return 1;
70 }
71 }
73 void QuickSort::print_array(const char* prefix, int* array, int length) {
74 tty->print("%s:", prefix);
75 for (int i = 0; i < length; i++) {
76 tty->print(" %d", array[i]);
77 }
78 tty->print_cr("");
79 }
81 bool QuickSort::compare_arrays(int* actual, int* expected, int length) {
82 for (int i = 0; i < length; i++) {
83 if (actual[i] != expected[i]) {
84 print_array("Sorted array ", actual, length);
85 print_array("Expected array", expected, length);
86 return false;
87 }
88 }
89 return true;
90 }
92 template <class C>
93 bool QuickSort::sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent) {
94 sort<int, C>(arrayToSort, length, comparator, idempotent);
95 return compare_arrays(arrayToSort, expectedResult, length);
96 }
98 void QuickSort::test_quick_sort() {
99 {
100 int* test_array = NULL;
101 int* expected_array = NULL;
102 assert(sort_and_compare(test_array, expected_array, 0, test_comparator), "Empty array not handled");
103 }
104 {
105 int test_array[] = {3};
106 int expected_array[] = {3};
107 assert(sort_and_compare(test_array, expected_array, 1, test_comparator), "Single value array not handled");
108 }
109 {
110 int test_array[] = {3,2};
111 int expected_array[] = {2,3};
112 assert(sort_and_compare(test_array, expected_array, 2, test_comparator), "Array with 2 values not correctly sorted");
113 }
114 {
115 int test_array[] = {3,2,1};
116 int expected_array[] = {1,2,3};
117 assert(sort_and_compare(test_array, expected_array, 3, test_comparator), "Array with 3 values not correctly sorted");
118 }
119 {
120 int test_array[] = {4,3,2,1};
121 int expected_array[] = {1,2,3,4};
122 assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "Array with 4 values not correctly sorted");
123 }
124 {
125 int test_array[] = {7,1,5,3,6,9,8,2,4,0};
126 int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
127 assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Array with 10 values not correctly sorted");
128 }
129 {
130 int test_array[] = {4,4,1,4};
131 int expected_array[] = {1,4,4,4};
132 assert(sort_and_compare(test_array, expected_array, 4, test_comparator), "3 duplicates not sorted correctly");
133 }
134 {
135 int test_array[] = {0,1,2,3,4,5,6,7,8,9};
136 int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
137 assert(sort_and_compare(test_array, expected_array, 10, test_comparator), "Already sorted array not correctly sorted");
138 }
139 {
140 // one of the random arrays that found an issue in the partion method.
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};
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};
143 assert(sort_and_compare(test_array, expected_array, 42, test_comparator), "Not correctly sorted");
144 }
145 {
146 int test_array[] = {2,8,1,4};
147 int expected_array[] = {1,4,2,8};
148 assert(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator), "Even/odd not sorted correctly");
149 }
150 { // Some idempotent tests
151 {
152 // An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent.
153 int test_array[] = {1,4,8};
154 int expected_array[] = {1,4,8};
155 assert(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true), "Even/odd not idempotent");
156 }
157 {
158 int test_array[] = {1,7,9,4,8,2};
159 int expected_array[] = {1,7,9,4,8,2};
160 assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
161 }
162 {
163 int test_array[] = {1,9,7,4,2,8};
164 int expected_array[] = {1,9,7,4,2,8};
165 assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
166 }
167 {
168 int test_array[] = {7,9,1,2,8,4};
169 int expected_array[] = {7,9,1,2,8,4};
170 assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
171 }
172 {
173 int test_array[] = {7,1,9,2,4,8};
174 int expected_array[] = {7,1,9,2,4,8};
175 assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
176 }
177 {
178 int test_array[] = {9,1,7,4,8,2};
179 int expected_array[] = {9,1,7,4,8,2};
180 assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
181 }
182 {
183 int test_array[] = {9,7,1,4,2,8};
184 int expected_array[] = {9,7,1,4,2,8};
185 assert(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true), "Even/odd not idempotent");
186 }
187 }
189 // test sorting random arrays
190 for (int i = 0; i < 1000; i++) {
191 int length = os::random() % 100;
192 int* test_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);
193 int* expected_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);
194 for (int j = 0; j < length; j++) {
195 // Choose random values, but get a chance of getting duplicates
196 test_array[j] = os::random() % (length * 2);
197 expected_array[j] = test_array[j];
198 }
200 // Compare sorting to stdlib::qsort()
201 qsort(expected_array, length, sizeof(int), test_stdlib_comparator);
202 assert(sort_and_compare(test_array, expected_array, length, test_comparator), "Random array not correctly sorted");
204 // Make sure sorting is idempotent.
205 // Both test_array and expected_array are sorted by the test_comparator.
206 // Now sort them once with the test_even_odd_comparator. Then sort the
207 // test_array one more time with test_even_odd_comparator and verify that
208 // it is idempotent.
209 sort(expected_array, length, test_even_odd_comparator, true);
210 sort(test_array, length, test_even_odd_comparator, true);
211 assert(compare_arrays(test_array, expected_array, length), "Sorting identical arrays rendered different results");
212 sort(test_array, length, test_even_odd_comparator, true);
213 assert(compare_arrays(test_array, expected_array, length), "Sorting already sorted array changed order of elements - not idempotent");
215 FREE_C_HEAP_ARRAY(int, test_array, mtInternal);
216 FREE_C_HEAP_ARRAY(int, expected_array, mtInternal);
217 }
218 }
220 #endif