1
1
/*
2
- * Copyright (c) 2011, 2016 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2011, 2020 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -58,7 +58,7 @@ static void check_list_values(const int* expected, const LinkedList<Integer>* li
58
58
}
59
59
}
60
60
61
- const Integer one (1 ), two(2 ), three(3 ), four(4 ), five(5 ), six(6 );
61
+ const Integer one (1 ), two(2 ), three(3 ), four(4 ), five(5 ), six(6 ), notfound( 404 ) ;
62
62
63
63
// Test regular linked list
64
64
TEST (LinkedList, simple) {
@@ -85,6 +85,82 @@ TEST(LinkedList, simple) {
85
85
check_list_values (expected, &ll);
86
86
}
87
87
88
+ TEST (LinkedList, generic) {
89
+ LinkedListImpl<int > il;
90
+ const int N = 100 ;
91
+ for (int i=0 ; i<N; ++i) {
92
+ il.add (i);
93
+ }
94
+ EXPECT_EQ (il.size (), (size_t )N);
95
+
96
+ const LinkedListIterator<int > cit (il.head ());
97
+ for (int i=N-1 ; i>=0 ; --i) {
98
+ const int * e = cit.next ();
99
+ EXPECT_EQ (*e, i);
100
+ }
101
+ EXPECT_TRUE (cit.is_empty ());
102
+ EXPECT_EQ (il.size (), (size_t )N);
103
+ EXPECT_EQ (*(il.head ()->peek ()), N-1 );
104
+
105
+ typedef LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> list_t ;
106
+ LinkedList<Integer>* list = new (ResourceObj::C_HEAP, mtTest) list_t ();
107
+ list->add (Integer (1 ));
108
+ list->add (Integer (2 ));
109
+ EXPECT_EQ (list->size (), (size_t )2 );
110
+ list->~LinkedList<Integer>();
111
+ EXPECT_EQ (list->size (), (size_t )0 );
112
+
113
+ // copyable
114
+ // list_t a;
115
+ // a.add(Integer(1));
116
+ // list_t b(a);
117
+ // EXPECT_EQ(b.size(), (size_t)1);
118
+ // EXPECT_TRUE(b.head()->peek()->equals(Integer(1)));
119
+
120
+ list_t lifo, dummy;
121
+ const Integer* e;
122
+ lifo.add (one);
123
+ lifo.add (two);
124
+ LinkedListIterator<Integer> it (lifo.head ());
125
+
126
+ EXPECT_FALSE (it.is_empty ());
127
+ // pop 2
128
+ e = it.next ();
129
+ EXPECT_TRUE (e->equals (two));
130
+ EXPECT_FALSE (it.is_empty ());
131
+ // pop 1
132
+ e = it.next ();
133
+ EXPECT_TRUE (e->equals (one));
134
+ // empty
135
+ EXPECT_TRUE (it.is_empty ());
136
+
137
+ LinkedListIterator<Integer> it2 (dummy.head ());
138
+ EXPECT_TRUE (it2.is_empty ());
139
+ EXPECT_EQ (it2.next (), (Integer* )NULL );
140
+ }
141
+
142
+ TEST (LinkedList, algorithm) {
143
+ LinkedListImpl<int > il;
144
+ il.add (1 );
145
+ il.add (2 );
146
+ il.add (3 );
147
+ EXPECT_EQ (*il.find (1 ), 1 );
148
+ EXPECT_EQ (il.find (404 ), (int * )NULL );
149
+ EXPECT_TRUE (il.remove (1 ));
150
+ EXPECT_FALSE (il.remove (404 ));
151
+
152
+ LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
153
+ ll.add (one);
154
+
155
+ EXPECT_TRUE (ll.find (one));
156
+ EXPECT_FALSE (ll.find (notfound));
157
+
158
+ EXPECT_TRUE (ll.remove (one));
159
+ EXPECT_FALSE (ll.find (one));
160
+ EXPECT_FALSE (ll.remove (notfound));
161
+ EXPECT_FALSE (ll.find (notfound));
162
+ }
163
+
88
164
// Test sorted linked list
89
165
TEST (SortedLinkedList, simple) {
90
166
LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
0 commit comments