hotspot/test/native/utilities/test_linkedlist.cpp
author alanb
Thu, 01 Dec 2016 15:54:39 +0000
changeset 42309 21f9f3fbc302
parent 41305 6d3c585464c0
child 42900 ff634f19e82c
permissions -rw-r--r--
8170599: compiler/** tests using ToolProvider.getSystemClassLoader failing Reviewed-by: hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     1
/*
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
     2
 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     4
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     8
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    13
 * accompanied this code).
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    14
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    18
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    21
 * questions.
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    22
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    23
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    24
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    25
#include "precompiled.hpp"
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    26
#include "unittest.hpp"
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    27
#include "utilities/linkedlist.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    28
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    29
class Integer : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    30
 private:
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    31
  int _value;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    32
 public:
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    33
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    34
  Integer(int i) : _value(i) {
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    35
  }
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    36
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    37
  int value() const {
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    38
    return _value;
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    39
  }
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    40
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    41
  bool equals(const Integer& i) const {
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    42
    return _value == i.value();
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    43
  }
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    44
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    45
  static int compare(const Integer& i1, const Integer& i2) {
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    46
    return i1.value() - i2.value();
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    47
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    48
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    49
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    50
static void check_list_values(const int* expected, const LinkedList<Integer>* list) {
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    51
  LinkedListNode<Integer>* head = list->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    52
  int index = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    53
  while (head != NULL) {
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    54
    ASSERT_EQ(expected[index], head->peek()->value())
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    55
            << "Unexpected value at index " << index;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    56
    head = head->next();
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    57
    index++;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    58
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    59
}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    60
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    61
const Integer one(1), two(2), three(3), four(4), five(5), six(6);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    62
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    63
// Test regular linked list
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    64
TEST(LinkedList, simple) {
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    65
  LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    66
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    67
  ASSERT_TRUE(ll.is_empty()) << "Start with empty list";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    68
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    69
  ll.add(six);
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    70
  ASSERT_TRUE(!ll.is_empty()) << "Should not be empty";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    71
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    72
  Integer* i = ll.find(six);
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    73
  ASSERT_TRUE(i != NULL) << "Should find it";
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    74
  ASSERT_EQ(six.value(), i->value()) << "Should be 6";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    75
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    76
  i = ll.find(three);
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    77
  ASSERT_EQ(NULL, i) << "Not in the list";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    78
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    79
  LinkedListNode<Integer>* node = ll.find_node(six);
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    80
  ASSERT_TRUE(node != NULL) << "6 is in the list";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    81
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    82
  ll.insert_after(three, node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    83
  ll.insert_before(one, node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    84
  int expected[3] = {1, 6, 3};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    85
  check_list_values(expected, &ll);
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    86
}
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    87
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    88
// Test sorted linked list
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    89
TEST(SortedLinkedList, simple) {
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    90
  LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    91
  ll.add(one);
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    92
  ll.add(six);
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    93
  ll.add(three);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    94
  ll.add(two);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    95
  ll.add(four);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    96
  ll.add(five);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    97
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    98
  SortedLinkedList<Integer, Integer::compare, ResourceObj::C_HEAP, mtTest> sl;
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
    99
  ASSERT_TRUE(sl.is_empty()) << "Start with empty list";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   100
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   101
  size_t ll_size = ll.size();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   102
  sl.move(&ll);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   103
  size_t sl_size = sl.size();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   104
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
   105
  ASSERT_EQ(ll_size, sl_size) << "Should be the same size";
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
   106
  ASSERT_TRUE(ll.is_empty()) << "No more entries";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   107
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   108
  // sorted result
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   109
  int sorted_result[] = {1, 2, 3, 4, 5, 6};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   110
  check_list_values(sorted_result, &sl);
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
   111
  if (HasFatalFailure()) {
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
   112
    return;
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
   113
  }
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   114
41305
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
   115
  LinkedListNode<Integer>* node = sl.find_node(four);
6d3c585464c0 8165433: Convert Test_linked_list to Gtest
kzhaldyb
parents: 25946
diff changeset
   116
  ASSERT_TRUE(node != NULL) << "4 is in the list";
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   117
  sl.remove_before(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   118
  sl.remove_after(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   119
  int remains[] = {1, 2, 4, 6};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   120
  check_list_values(remains, &sl);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   121
}