File: test_quicksort.cpp

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (204 lines) | stat: -rw-r--r-- 7,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "memory/allocation.inline.hpp"
#include "runtime/os.hpp"
#include "utilities/quickSort.hpp"
#include "unittest.hpp"

static int test_comparator(int a, int b) {
  if (a == b) {
    return 0;
  }
  if (a < b) {
    return -1;
  }
  return 1;
}

static bool compare_arrays(int* actual, int* expected, size_t length) {
  for (size_t i = 0; i < length; i++) {
    if (actual[i] != expected[i]) {
      return false;
    }
  }
  return true;
}

template <class C>
static bool sort_and_compare(int* arrayToSort, int* expectedResult, size_t length, C comparator, bool idempotent = false) {
  QuickSort::sort(arrayToSort, length, comparator, idempotent);
  return compare_arrays(arrayToSort, expectedResult, length);
}

static int test_even_odd_comparator(int a, int b) {
  bool a_is_odd = ((a % 2) == 1);
  bool b_is_odd = ((b % 2) == 1);
  if (a_is_odd == b_is_odd) {
    return 0;
  }
  if (a_is_odd) {
    return -1;
  }
  return 1;
}

extern "C" {
  static int test_stdlib_comparator(const void* a, const void* b) {
    int ai = *(int*)a;
    int bi = *(int*)b;
    if (ai == bi) {
      return 0;
    }
    if (ai < bi) {
      return -1;
    }
    return 1;
  }
}

TEST(QuickSort, quicksort) {
  {
    int* test_array = nullptr;
    int* expected_array = nullptr;
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 0, test_comparator));
  }
  {
    int test_array[] = {3};
    int expected_array[] = {3};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 1, test_comparator));
  }
  {
    int test_array[] = {3,2};
    int expected_array[] = {2,3};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 2, test_comparator));
  }
  {
    int test_array[] = {3,2,1};
    int expected_array[] = {1,2,3};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 3, test_comparator));
  }
  {
    int test_array[] = {4,3,2,1};
    int expected_array[] = {1,2,3,4};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_comparator));
  }
  {
    int test_array[] = {7,1,5,3,6,9,8,2,4,0};
    int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 10, test_comparator));
  }
  {
    int test_array[] = {4,4,1,4};
    int expected_array[] = {1,4,4,4};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_comparator));
  }
  {
    int test_array[] = {0,1,2,3,4,5,6,7,8,9};
    int expected_array[] = {0,1,2,3,4,5,6,7,8,9};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 10, test_comparator));
  }
  {
    // one of the random arrays that found an issue in the partition method.
    int test_array[] = {76,46,81,8,64,56,75,11,51,55,11,71,59,27,9,64,69,75,21,25,39,40,44,32,7,8,40,41,24,78,24,74,9,65,28,6,40,31,22,13,27,82};
    int expected_array[] = {6,7,8,8,9,9,11,11,13,21,22,24,24,25,27,27,28,31,32,39,40,40,40,41,44,46,51,55,56,59,64,64,65,69,71,74,75,75,76,78,81,82};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 42, test_comparator));
  }
  {
    int test_array[] = {2,8,1,4};
    int expected_array[] = {1,4,2,8};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 4, test_even_odd_comparator));
  }
}

TEST(QuickSort, idempotent) {
  {
    // An array of lenght 3 is only sorted by find_pivot. Make sure that it is idempotent.
    int test_array[] = {1, 4, 8};
    int expected_array[] = {1, 4, 8};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 3, test_even_odd_comparator, true));
  }
  {
    int test_array[] = {1, 7, 9, 4, 8, 2};
    int expected_array[] = {1, 7, 9, 4, 8, 2};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
  }
  {
    int test_array[] = {1, 9, 7, 4, 2, 8};
    int expected_array[] = {1, 9, 7, 4, 2, 8};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
  }
  {
    int test_array[] = {7, 9, 1, 2, 8, 4};
    int expected_array[] = {7, 9, 1, 2, 8, 4};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
  }
  {
    int test_array[] = {7, 1, 9, 2, 4, 8};
    int expected_array[] = {7, 1, 9, 2, 4, 8};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
  }
  {
    int test_array[] = {9, 1, 7, 4, 8, 2};
    int expected_array[] = {9, 1, 7, 4, 8, 2};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
  }
  {
    int test_array[] = {9, 7, 1, 4, 2, 8};
    int expected_array[] = {9, 7, 1, 4, 2, 8};
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, 6, test_even_odd_comparator, true));
  }
}

TEST(QuickSort, random) {
  for (int i = 0; i < 1000; i++) {
    size_t length = os::random() % 100;
    int* test_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);
    int* expected_array = NEW_C_HEAP_ARRAY(int, length, mtInternal);
    for (size_t j = 0; j < length; j++) {
        // Choose random values, but get a chance of getting duplicates
        test_array[j] = os::random() % (length * 2);
        expected_array[j] = test_array[j];
    }

    // Compare sorting to stdlib::qsort()
    qsort(expected_array, length, sizeof(int), test_stdlib_comparator);
    EXPECT_TRUE(sort_and_compare(test_array, expected_array, length, test_comparator));

    // Make sure sorting is idempotent.
    // Both test_array and expected_array are sorted by the test_comparator.
    // Now sort them once with the test_even_odd_comparator. Then sort the
    // test_array one more time with test_even_odd_comparator and verify that
    // it is idempotent.
    QuickSort::sort(expected_array, length, test_even_odd_comparator, true);
    QuickSort::sort(test_array, length, test_even_odd_comparator, true);
    EXPECT_TRUE(compare_arrays(test_array, expected_array, length));
    QuickSort::sort(test_array, length, test_even_odd_comparator, true);
    EXPECT_TRUE(compare_arrays(test_array, expected_array, length));

    FREE_C_HEAP_ARRAY(int, test_array);
    FREE_C_HEAP_ARRAY(int, expected_array);
  }
}