20360
|
1 /*
|
|
2 * Copyright (c) 2011, 2014, 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 */
|
|
24
|
|
25 #include "precompiled.hpp"
|
|
26
|
|
27 /////////////// Unit tests ///////////////
|
|
28
|
|
29 #ifndef PRODUCT
|
|
30
|
|
31 #include "runtime/os.hpp"
|
|
32 #include "utilities/linkedlist.hpp"
|
|
33 #include "memory/allocation.hpp"
|
|
34 #include "memory/allocation.inline.hpp"
|
|
35
|
|
36 class Integer : public StackObj {
|
|
37 private:
|
|
38 int _value;
|
|
39 public:
|
|
40 Integer(int i) : _value(i) { }
|
|
41
|
|
42 int value() const { return _value; }
|
|
43 bool equals(const Integer& i) const {
|
|
44 return _value == i.value();
|
|
45 }
|
|
46 };
|
|
47
|
|
48 int compare_Integer(const Integer& i1, const Integer& i2) {
|
|
49 return i1.value() - i2.value();
|
|
50 }
|
|
51
|
|
52 void check_list_values(const int* expected, const LinkedList<Integer>* list) {
|
|
53 LinkedListNode<Integer>* head = list->head();
|
|
54 int index = 0;
|
|
55 while (head != NULL) {
|
|
56 assert(head->peek()->value() == expected[index], "Unexpected value");
|
|
57 head = head->next();
|
|
58 index ++;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 void Test_linked_list() {
|
|
63 LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
|
|
64
|
|
65
|
|
66 // Test regular linked list
|
|
67 assert(ll.is_empty(), "Start with empty list");
|
|
68 Integer one(1), two(2), three(3), four(4), five(5), six(6);
|
|
69
|
|
70 ll.add(six);
|
|
71 assert(!ll.is_empty(), "Should not be empty");
|
|
72
|
|
73 Integer* i = ll.find(six);
|
|
74 assert(i != NULL, "Should find it");
|
|
75
|
|
76 i = ll.find(three);
|
|
77 assert(i == NULL, "Not in the list");
|
|
78
|
|
79 LinkedListNode<Integer>* node = ll.find_node(six);
|
|
80 assert(node != NULL, "6 is in the list");
|
|
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);
|
|
86
|
|
87 ll.add(two);
|
|
88 ll.add(four);
|
|
89 ll.add(five);
|
|
90
|
|
91 // Test sorted linked list
|
|
92 SortedLinkedList<Integer, compare_Integer, ResourceObj::C_HEAP, mtTest> sl;
|
|
93 assert(sl.is_empty(), "Start with empty list");
|
|
94
|
|
95 size_t ll_size = ll.size();
|
|
96 sl.move(&ll);
|
|
97 size_t sl_size = sl.size();
|
|
98
|
|
99 assert(ll_size == sl_size, "Should be the same size");
|
|
100 assert(ll.is_empty(), "No more entires");
|
|
101
|
|
102 // sorted result
|
|
103 int sorted_result[] = {1, 2, 3, 4, 5, 6};
|
|
104 check_list_values(sorted_result, &sl);
|
|
105
|
|
106 node = sl.find_node(four);
|
|
107 assert(node != NULL, "4 is in the list");
|
|
108 sl.remove_before(node);
|
|
109 sl.remove_after(node);
|
|
110 int remains[] = {1, 2, 4, 6};
|
|
111 check_list_values(remains, &sl);
|
|
112 }
|
|
113 #endif // PRODUCT
|
|
114
|