zgu@7074: /* zgu@7074: * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. zgu@7074: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@7074: * zgu@7074: * This code is free software; you can redistribute it and/or modify it zgu@7074: * under the terms of the GNU General Public License version 2 only, as zgu@7074: * published by the Free Software Foundation. zgu@7074: * zgu@7074: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@7074: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@7074: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@7074: * version 2 for more details (a copy is included in the LICENSE file that zgu@7074: * accompanied this code). zgu@7074: * zgu@7074: * You should have received a copy of the GNU General Public License version zgu@7074: * 2 along with this work; if not, write to the Free Software Foundation, zgu@7074: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@7074: * zgu@7074: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@7074: * or visit www.oracle.com if you need additional information or have any zgu@7074: * questions. zgu@7074: * zgu@7074: */ zgu@7074: zgu@7074: #include "precompiled.hpp" zgu@7074: zgu@7074: /////////////// Unit tests /////////////// zgu@7074: zgu@7074: #ifndef PRODUCT zgu@7074: zgu@7074: #include "runtime/os.hpp" zgu@7074: #include "utilities/linkedlist.hpp" zgu@7074: #include "memory/allocation.hpp" zgu@7074: #include "memory/allocation.inline.hpp" zgu@7074: zgu@7074: class Integer : public StackObj { zgu@7074: private: zgu@7074: int _value; zgu@7074: public: zgu@7074: Integer(int i) : _value(i) { } zgu@7074: zgu@7074: int value() const { return _value; } zgu@7074: bool equals(const Integer& i) const { zgu@7074: return _value == i.value(); zgu@7074: } zgu@7074: }; zgu@7074: zgu@7074: int compare_Integer(const Integer& i1, const Integer& i2) { zgu@7074: return i1.value() - i2.value(); zgu@7074: } zgu@7074: zgu@7074: void check_list_values(const int* expected, const LinkedList* list) { zgu@7074: LinkedListNode* head = list->head(); zgu@7074: int index = 0; zgu@7074: while (head != NULL) { zgu@7074: assert(head->peek()->value() == expected[index], "Unexpected value"); zgu@7074: head = head->next(); zgu@7074: index ++; zgu@7074: } zgu@7074: } zgu@7074: zgu@7074: void Test_linked_list() { zgu@7074: LinkedListImpl ll; zgu@7074: zgu@7074: zgu@7074: // Test regular linked list zgu@7074: assert(ll.is_empty(), "Start with empty list"); zgu@7074: Integer one(1), two(2), three(3), four(4), five(5), six(6); zgu@7074: zgu@7074: ll.add(six); zgu@7074: assert(!ll.is_empty(), "Should not be empty"); zgu@7074: zgu@7074: Integer* i = ll.find(six); zgu@7074: assert(i != NULL, "Should find it"); zgu@7074: zgu@7074: i = ll.find(three); zgu@7074: assert(i == NULL, "Not in the list"); zgu@7074: zgu@7074: LinkedListNode* node = ll.find_node(six); zgu@7074: assert(node != NULL, "6 is in the list"); zgu@7074: zgu@7074: ll.insert_after(three, node); zgu@7074: ll.insert_before(one, node); zgu@7074: int expected[3] = {1, 6, 3}; zgu@7074: check_list_values(expected, &ll); zgu@7074: zgu@7074: ll.add(two); zgu@7074: ll.add(four); zgu@7074: ll.add(five); zgu@7074: zgu@7074: // Test sorted linked list zgu@7074: SortedLinkedList sl; zgu@7074: assert(sl.is_empty(), "Start with empty list"); zgu@7074: zgu@7074: size_t ll_size = ll.size(); zgu@7074: sl.move(&ll); zgu@7074: size_t sl_size = sl.size(); zgu@7074: zgu@7074: assert(ll_size == sl_size, "Should be the same size"); zgu@7074: assert(ll.is_empty(), "No more entires"); zgu@7074: zgu@7074: // sorted result zgu@7074: int sorted_result[] = {1, 2, 3, 4, 5, 6}; zgu@7074: check_list_values(sorted_result, &sl); zgu@7074: zgu@7074: node = sl.find_node(four); zgu@7074: assert(node != NULL, "4 is in the list"); zgu@7074: sl.remove_before(node); zgu@7074: sl.remove_after(node); zgu@7074: int remains[] = {1, 2, 4, 6}; zgu@7074: check_list_values(remains, &sl); zgu@7074: } zgu@7074: #endif // PRODUCT zgu@7074: