Wed, 03 Jul 2019 20:42:37 +0800
Merge
zgu@7074 | 1 | /* |
zgu@7074 | 2 | * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
zgu@7074 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
zgu@7074 | 4 | * |
zgu@7074 | 5 | * This code is free software; you can redistribute it and/or modify it |
zgu@7074 | 6 | * under the terms of the GNU General Public License version 2 only, as |
zgu@7074 | 7 | * published by the Free Software Foundation. |
zgu@7074 | 8 | * |
zgu@7074 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
zgu@7074 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
zgu@7074 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
zgu@7074 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
zgu@7074 | 13 | * accompanied this code). |
zgu@7074 | 14 | * |
zgu@7074 | 15 | * You should have received a copy of the GNU General Public License version |
zgu@7074 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
zgu@7074 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
zgu@7074 | 18 | * |
zgu@7074 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
zgu@7074 | 20 | * or visit www.oracle.com if you need additional information or have any |
zgu@7074 | 21 | * questions. |
zgu@7074 | 22 | * |
zgu@7074 | 23 | */ |
zgu@7074 | 24 | |
zgu@7074 | 25 | #include "precompiled.hpp" |
zgu@7074 | 26 | |
zgu@7074 | 27 | /////////////// Unit tests /////////////// |
zgu@7074 | 28 | |
zgu@7074 | 29 | #ifndef PRODUCT |
zgu@7074 | 30 | |
zgu@7074 | 31 | #include "runtime/os.hpp" |
zgu@7074 | 32 | #include "utilities/linkedlist.hpp" |
zgu@7074 | 33 | #include "memory/allocation.hpp" |
zgu@7074 | 34 | #include "memory/allocation.inline.hpp" |
zgu@7074 | 35 | |
zgu@7074 | 36 | class Integer : public StackObj { |
zgu@7074 | 37 | private: |
zgu@7074 | 38 | int _value; |
zgu@7074 | 39 | public: |
zgu@7074 | 40 | Integer(int i) : _value(i) { } |
zgu@7074 | 41 | |
zgu@7074 | 42 | int value() const { return _value; } |
zgu@7074 | 43 | bool equals(const Integer& i) const { |
zgu@7074 | 44 | return _value == i.value(); |
zgu@7074 | 45 | } |
zgu@7074 | 46 | }; |
zgu@7074 | 47 | |
zgu@7074 | 48 | int compare_Integer(const Integer& i1, const Integer& i2) { |
zgu@7074 | 49 | return i1.value() - i2.value(); |
zgu@7074 | 50 | } |
zgu@7074 | 51 | |
zgu@7074 | 52 | void check_list_values(const int* expected, const LinkedList<Integer>* list) { |
zgu@7074 | 53 | LinkedListNode<Integer>* head = list->head(); |
zgu@7074 | 54 | int index = 0; |
zgu@7074 | 55 | while (head != NULL) { |
zgu@7074 | 56 | assert(head->peek()->value() == expected[index], "Unexpected value"); |
zgu@7074 | 57 | head = head->next(); |
zgu@7074 | 58 | index ++; |
zgu@7074 | 59 | } |
zgu@7074 | 60 | } |
zgu@7074 | 61 | |
zgu@7074 | 62 | void Test_linked_list() { |
zgu@7074 | 63 | LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll; |
zgu@7074 | 64 | |
zgu@7074 | 65 | |
zgu@7074 | 66 | // Test regular linked list |
zgu@7074 | 67 | assert(ll.is_empty(), "Start with empty list"); |
zgu@7074 | 68 | Integer one(1), two(2), three(3), four(4), five(5), six(6); |
zgu@7074 | 69 | |
zgu@7074 | 70 | ll.add(six); |
zgu@7074 | 71 | assert(!ll.is_empty(), "Should not be empty"); |
zgu@7074 | 72 | |
zgu@7074 | 73 | Integer* i = ll.find(six); |
zgu@7074 | 74 | assert(i != NULL, "Should find it"); |
zgu@7074 | 75 | |
zgu@7074 | 76 | i = ll.find(three); |
zgu@7074 | 77 | assert(i == NULL, "Not in the list"); |
zgu@7074 | 78 | |
zgu@7074 | 79 | LinkedListNode<Integer>* node = ll.find_node(six); |
zgu@7074 | 80 | assert(node != NULL, "6 is in the list"); |
zgu@7074 | 81 | |
zgu@7074 | 82 | ll.insert_after(three, node); |
zgu@7074 | 83 | ll.insert_before(one, node); |
zgu@7074 | 84 | int expected[3] = {1, 6, 3}; |
zgu@7074 | 85 | check_list_values(expected, &ll); |
zgu@7074 | 86 | |
zgu@7074 | 87 | ll.add(two); |
zgu@7074 | 88 | ll.add(four); |
zgu@7074 | 89 | ll.add(five); |
zgu@7074 | 90 | |
zgu@7074 | 91 | // Test sorted linked list |
zgu@7074 | 92 | SortedLinkedList<Integer, compare_Integer, ResourceObj::C_HEAP, mtTest> sl; |
zgu@7074 | 93 | assert(sl.is_empty(), "Start with empty list"); |
zgu@7074 | 94 | |
zgu@7074 | 95 | size_t ll_size = ll.size(); |
zgu@7074 | 96 | sl.move(&ll); |
zgu@7074 | 97 | size_t sl_size = sl.size(); |
zgu@7074 | 98 | |
zgu@7074 | 99 | assert(ll_size == sl_size, "Should be the same size"); |
zgu@7074 | 100 | assert(ll.is_empty(), "No more entires"); |
zgu@7074 | 101 | |
zgu@7074 | 102 | // sorted result |
zgu@7074 | 103 | int sorted_result[] = {1, 2, 3, 4, 5, 6}; |
zgu@7074 | 104 | check_list_values(sorted_result, &sl); |
zgu@7074 | 105 | |
zgu@7074 | 106 | node = sl.find_node(four); |
zgu@7074 | 107 | assert(node != NULL, "4 is in the list"); |
zgu@7074 | 108 | sl.remove_before(node); |
zgu@7074 | 109 | sl.remove_after(node); |
zgu@7074 | 110 | int remains[] = {1, 2, 4, 6}; |
zgu@7074 | 111 | check_list_values(remains, &sl); |
zgu@7074 | 112 | } |
zgu@7074 | 113 | #endif // PRODUCT |
zgu@7074 | 114 |