jdk

Форк
0
/z
/
test_zList.cpp 
155 строк · 3.5 Кб
1
/*
2
 * Copyright (c) 2015, 2020, 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
#include "precompiled.hpp"
25
#include "gc/z/zList.inline.hpp"
26
#include "unittest.hpp"
27

28
#ifndef PRODUCT
29

30
class ZTestEntry {
31
  friend class ZList<ZTestEntry>;
32

33
private:
34
  const int             _id;
35
  ZListNode<ZTestEntry> _node;
36

37
public:
38
  ZTestEntry(int id)
39
    : _id(id),
40
      _node() {}
41

42
  int id() const {
43
    return _id;
44
  }
45
};
46

47
class ZListTest : public ::testing::Test {
48
protected:
49
  static void assert_sorted(ZList<ZTestEntry>* list) {
50
    // Iterate forward
51
    {
52
      int count = list->first()->id();
53
      ZListIterator<ZTestEntry> iter(list);
54
      for (ZTestEntry* entry; iter.next(&entry);) {
55
        ASSERT_EQ(entry->id(), count);
56
        count++;
57
      }
58
    }
59

60
    // Iterate backward
61
    {
62
      int count = list->last()->id();
63
      ZListReverseIterator<ZTestEntry> iter(list);
64
      for (ZTestEntry* entry; iter.next(&entry);) {
65
        EXPECT_EQ(entry->id(), count);
66
        count--;
67
      }
68
    }
69
  }
70
};
71

72
TEST_F(ZListTest, test_insert) {
73
  ZList<ZTestEntry> list;
74
  ZTestEntry e0(0);
75
  ZTestEntry e1(1);
76
  ZTestEntry e2(2);
77
  ZTestEntry e3(3);
78
  ZTestEntry e4(4);
79
  ZTestEntry e5(5);
80

81
  list.insert_first(&e2);
82
  list.insert_before(&e2, &e1);
83
  list.insert_after(&e2, &e3);
84
  list.insert_last(&e4);
85
  list.insert_first(&e0);
86
  list.insert_last(&e5);
87

88
  EXPECT_EQ(list.size(), 6u);
89
  assert_sorted(&list);
90

91
  for (int i = 0; i < 6; i++) {
92
    ZTestEntry* e = list.remove_first();
93
    EXPECT_EQ(e->id(), i);
94
  }
95

96
  EXPECT_EQ(list.size(), 0u);
97
}
98

99
TEST_F(ZListTest, test_remove) {
100
  // Remove first
101
  {
102
    ZList<ZTestEntry> list;
103
    ZTestEntry e0(0);
104
    ZTestEntry e1(1);
105
    ZTestEntry e2(2);
106
    ZTestEntry e3(3);
107
    ZTestEntry e4(4);
108
    ZTestEntry e5(5);
109

110
    list.insert_last(&e0);
111
    list.insert_last(&e1);
112
    list.insert_last(&e2);
113
    list.insert_last(&e3);
114
    list.insert_last(&e4);
115
    list.insert_last(&e5);
116

117
    EXPECT_EQ(list.size(), 6u);
118

119
    for (int i = 0; i < 6; i++) {
120
      ZTestEntry* e = list.remove_first();
121
      EXPECT_EQ(e->id(), i);
122
    }
123

124
    EXPECT_EQ(list.size(), 0u);
125
  }
126

127
  // Remove last
128
  {
129
    ZList<ZTestEntry> list;
130
    ZTestEntry e0(0);
131
    ZTestEntry e1(1);
132
    ZTestEntry e2(2);
133
    ZTestEntry e3(3);
134
    ZTestEntry e4(4);
135
    ZTestEntry e5(5);
136

137
    list.insert_last(&e0);
138
    list.insert_last(&e1);
139
    list.insert_last(&e2);
140
    list.insert_last(&e3);
141
    list.insert_last(&e4);
142
    list.insert_last(&e5);
143

144
    EXPECT_EQ(list.size(), 6u);
145

146
    for (int i = 5; i >= 0; i--) {
147
      ZTestEntry* e = list.remove_last();
148
      EXPECT_EQ(e->id(), i);
149
    }
150

151
    EXPECT_EQ(list.size(), 0u);
152
  }
153
}
154

155
#endif // PRODUCT
156

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.