1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/linkedlist.cpp Wed Aug 27 08:19:12 2014 -0400 1.3 @@ -0,0 +1,114 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 + 1.30 +/////////////// Unit tests /////////////// 1.31 + 1.32 +#ifndef PRODUCT 1.33 + 1.34 +#include "runtime/os.hpp" 1.35 +#include "utilities/linkedlist.hpp" 1.36 +#include "memory/allocation.hpp" 1.37 +#include "memory/allocation.inline.hpp" 1.38 + 1.39 +class Integer : public StackObj { 1.40 + private: 1.41 + int _value; 1.42 + public: 1.43 + Integer(int i) : _value(i) { } 1.44 + 1.45 + int value() const { return _value; } 1.46 + bool equals(const Integer& i) const { 1.47 + return _value == i.value(); 1.48 + } 1.49 +}; 1.50 + 1.51 +int compare_Integer(const Integer& i1, const Integer& i2) { 1.52 + return i1.value() - i2.value(); 1.53 +} 1.54 + 1.55 +void check_list_values(const int* expected, const LinkedList<Integer>* list) { 1.56 + LinkedListNode<Integer>* head = list->head(); 1.57 + int index = 0; 1.58 + while (head != NULL) { 1.59 + assert(head->peek()->value() == expected[index], "Unexpected value"); 1.60 + head = head->next(); 1.61 + index ++; 1.62 + } 1.63 +} 1.64 + 1.65 +void Test_linked_list() { 1.66 + LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll; 1.67 + 1.68 + 1.69 + // Test regular linked list 1.70 + assert(ll.is_empty(), "Start with empty list"); 1.71 + Integer one(1), two(2), three(3), four(4), five(5), six(6); 1.72 + 1.73 + ll.add(six); 1.74 + assert(!ll.is_empty(), "Should not be empty"); 1.75 + 1.76 + Integer* i = ll.find(six); 1.77 + assert(i != NULL, "Should find it"); 1.78 + 1.79 + i = ll.find(three); 1.80 + assert(i == NULL, "Not in the list"); 1.81 + 1.82 + LinkedListNode<Integer>* node = ll.find_node(six); 1.83 + assert(node != NULL, "6 is in the list"); 1.84 + 1.85 + ll.insert_after(three, node); 1.86 + ll.insert_before(one, node); 1.87 + int expected[3] = {1, 6, 3}; 1.88 + check_list_values(expected, &ll); 1.89 + 1.90 + ll.add(two); 1.91 + ll.add(four); 1.92 + ll.add(five); 1.93 + 1.94 + // Test sorted linked list 1.95 + SortedLinkedList<Integer, compare_Integer, ResourceObj::C_HEAP, mtTest> sl; 1.96 + assert(sl.is_empty(), "Start with empty list"); 1.97 + 1.98 + size_t ll_size = ll.size(); 1.99 + sl.move(&ll); 1.100 + size_t sl_size = sl.size(); 1.101 + 1.102 + assert(ll_size == sl_size, "Should be the same size"); 1.103 + assert(ll.is_empty(), "No more entires"); 1.104 + 1.105 + // sorted result 1.106 + int sorted_result[] = {1, 2, 3, 4, 5, 6}; 1.107 + check_list_values(sorted_result, &sl); 1.108 + 1.109 + node = sl.find_node(four); 1.110 + assert(node != NULL, "4 is in the list"); 1.111 + sl.remove_before(node); 1.112 + sl.remove_after(node); 1.113 + int remains[] = {1, 2, 4, 6}; 1.114 + check_list_values(remains, &sl); 1.115 +} 1.116 +#endif // PRODUCT 1.117 +