25946
|
1 |
/*
|
41305
|
2 |
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
|
25946
|
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.
|
41305
|
22 |
|
25946
|
23 |
*/
|
|
24 |
|
|
25 |
#include "precompiled.hpp"
|
41305
|
26 |
#include "unittest.hpp"
|
25946
|
27 |
#include "utilities/linkedlist.hpp"
|
|
28 |
|
|
29 |
class Integer : public StackObj {
|
|
30 |
private:
|
41305
|
31 |
int _value;
|
25946
|
32 |
public:
|
41305
|
33 |
|
|
34 |
Integer(int i) : _value(i) {
|
|
35 |
}
|
25946
|
36 |
|
41305
|
37 |
int value() const {
|
|
38 |
return _value;
|
|
39 |
}
|
|
40 |
|
|
41 |
bool equals(const Integer& i) const {
|
|
42 |
return _value == i.value();
|
|
43 |
}
|
|
44 |
|
|
45 |
static int compare(const Integer& i1, const Integer& i2) {
|
|
46 |
return i1.value() - i2.value();
|
25946
|
47 |
}
|
|
48 |
};
|
|
49 |
|
41305
|
50 |
static void check_list_values(const int* expected, const LinkedList<Integer>* list) {
|
25946
|
51 |
LinkedListNode<Integer>* head = list->head();
|
|
52 |
int index = 0;
|
|
53 |
while (head != NULL) {
|
41305
|
54 |
ASSERT_EQ(expected[index], head->peek()->value())
|
|
55 |
<< "Unexpected value at index " << index;
|
25946
|
56 |
head = head->next();
|
41305
|
57 |
index++;
|
25946
|
58 |
}
|
|
59 |
}
|
|
60 |
|
41305
|
61 |
const Integer one(1), two(2), three(3), four(4), five(5), six(6);
|
25946
|
62 |
|
41305
|
63 |
// Test regular linked list
|
|
64 |
TEST(LinkedList, simple) {
|
|
65 |
LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
|
|
66 |
|
|
67 |
ASSERT_TRUE(ll.is_empty()) << "Start with empty list";
|
25946
|
68 |
|
|
69 |
ll.add(six);
|
41305
|
70 |
ASSERT_TRUE(!ll.is_empty()) << "Should not be empty";
|
25946
|
71 |
|
|
72 |
Integer* i = ll.find(six);
|
41305
|
73 |
ASSERT_TRUE(i != NULL) << "Should find it";
|
|
74 |
ASSERT_EQ(six.value(), i->value()) << "Should be 6";
|
25946
|
75 |
|
|
76 |
i = ll.find(three);
|
41305
|
77 |
ASSERT_EQ(NULL, i) << "Not in the list";
|
25946
|
78 |
|
|
79 |
LinkedListNode<Integer>* node = ll.find_node(six);
|
41305
|
80 |
ASSERT_TRUE(node != NULL) << "6 is in the list";
|
25946
|
81 |
|
|
82 |
ll.insert_after(three, node);
|
|
83 |
ll.insert_before(one, node);
|
|
84 |
int expected[3] = {1, 6, 3};
|
|
85 |
check_list_values(expected, &ll);
|
41305
|
86 |
}
|
25946
|
87 |
|
41305
|
88 |
// Test sorted linked list
|
|
89 |
TEST(SortedLinkedList, simple) {
|
|
90 |
LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
|
|
91 |
ll.add(one);
|
|
92 |
ll.add(six);
|
|
93 |
ll.add(three);
|
25946
|
94 |
ll.add(two);
|
|
95 |
ll.add(four);
|
|
96 |
ll.add(five);
|
|
97 |
|
41305
|
98 |
SortedLinkedList<Integer, Integer::compare, ResourceObj::C_HEAP, mtTest> sl;
|
|
99 |
ASSERT_TRUE(sl.is_empty()) << "Start with empty list";
|
25946
|
100 |
|
|
101 |
size_t ll_size = ll.size();
|
|
102 |
sl.move(&ll);
|
|
103 |
size_t sl_size = sl.size();
|
|
104 |
|
41305
|
105 |
ASSERT_EQ(ll_size, sl_size) << "Should be the same size";
|
|
106 |
ASSERT_TRUE(ll.is_empty()) << "No more entries";
|
25946
|
107 |
|
|
108 |
// sorted result
|
|
109 |
int sorted_result[] = {1, 2, 3, 4, 5, 6};
|
|
110 |
check_list_values(sorted_result, &sl);
|
41305
|
111 |
if (HasFatalFailure()) {
|
|
112 |
return;
|
|
113 |
}
|
25946
|
114 |
|
41305
|
115 |
LinkedListNode<Integer>* node = sl.find_node(four);
|
|
116 |
ASSERT_TRUE(node != NULL) << "4 is in the list";
|
25946
|
117 |
sl.remove_before(node);
|
|
118 |
sl.remove_after(node);
|
|
119 |
int remains[] = {1, 2, 4, 6};
|
|
120 |
check_list_values(remains, &sl);
|
|
121 |
}
|