1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "MoveOnly.h"
29#include <wtf/Deque.h>
30
31namespace TestWebKitAPI {
32
33TEST(WTF_Deque, Iterator)
34{
35 Deque<int> deque;
36 deque.append(11);
37 deque.prepend(10);
38 deque.append(12);
39 deque.append(13);
40
41 Deque<int>::iterator it = deque.begin();
42 Deque<int>::iterator end = deque.end();
43 EXPECT_TRUE(end != it);
44
45 EXPECT_EQ(10, *it);
46 ++it;
47 EXPECT_EQ(11, *it);
48 ++it;
49 EXPECT_EQ(12, *it);
50 ++it;
51 EXPECT_EQ(13, *it);
52 ++it;
53
54 EXPECT_TRUE(end == it);
55}
56
57TEST(WTF_Deque, InitializerList)
58{
59 Deque<int> deque = { 1, 2, 3, 4 };
60
61 EXPECT_EQ(4u, deque.size());
62
63 auto it = deque.begin();
64 auto end = deque.end();
65 EXPECT_TRUE(end != it);
66
67 EXPECT_EQ(1, *it);
68 ++it;
69 EXPECT_EQ(2, *it);
70 ++it;
71 EXPECT_EQ(3, *it);
72 ++it;
73 EXPECT_EQ(4, *it);
74 ++it;
75
76 EXPECT_TRUE(end == it);
77}
78
79TEST(WTF, DequeReverseIterator)
80{
81 Deque<int> deque;
82 deque.append(11);
83 deque.prepend(10);
84 deque.append(12);
85 deque.append(13);
86
87 Deque<int>::reverse_iterator it = deque.rbegin();
88 Deque<int>::reverse_iterator end = deque.rend();
89 EXPECT_TRUE(end != it);
90
91 EXPECT_EQ(13, *it);
92 ++it;
93 EXPECT_EQ(12, *it);
94 ++it;
95 EXPECT_EQ(11, *it);
96 ++it;
97 EXPECT_EQ(10, *it);
98 ++it;
99
100 EXPECT_TRUE(end == it);
101}
102
103TEST(WTF_Deque, Remove)
104{
105 Deque<int> deque;
106 deque.append(11);
107 deque.prepend(10);
108 deque.append(12);
109 deque.append(13);
110
111 EXPECT_EQ(10, deque.first());
112 EXPECT_EQ(13, deque.last());
113
114 deque.removeLast();
115 EXPECT_EQ(10, deque.first());
116 EXPECT_EQ(12, deque.last());
117
118 deque.removeFirst();
119 EXPECT_EQ(11, deque.first());
120 EXPECT_EQ(12, deque.last());
121
122 deque.removeFirst();
123 EXPECT_EQ(12, deque.first());
124 EXPECT_EQ(12, deque.last());
125
126 deque.removeLast();
127 EXPECT_TRUE(deque.isEmpty());
128}
129
130TEST(WTF_Deque, MoveOnly)
131{
132 Deque<MoveOnly> deque;
133
134 deque.append(MoveOnly(1));
135 deque.prepend(MoveOnly(0));
136
137 EXPECT_EQ(0U, deque.first().value());
138 EXPECT_EQ(1U, deque.last().value());
139
140 auto first = deque.takeFirst();
141 EXPECT_EQ(0U, first.value());
142
143 auto last = deque.takeLast();
144 EXPECT_EQ(1U, last.value());
145}
146
147TEST(WTF_Deque, MoveConstructor)
148{
149 Deque<MoveOnly, 4> deque;
150
151 for (unsigned i = 0; i < 10; ++i)
152 deque.append(MoveOnly(i));
153
154 EXPECT_EQ(10u, deque.size());
155
156 Deque<MoveOnly, 4> deque2 = WTFMove(deque);
157
158 EXPECT_EQ(10u, deque2.size());
159
160 unsigned i = 0;
161 for (auto& element : deque2) {
162 EXPECT_EQ(i, element.value());
163 ++i;
164 }
165}
166
167TEST(WTF_Deque, MoveAssignmentOperator)
168{
169 Deque<MoveOnly, 4> deque1;
170
171 for (unsigned i = 0; i < 10; ++i)
172 deque1.append(MoveOnly(i));
173
174 EXPECT_EQ(10u, deque1.size());
175
176 Deque<MoveOnly, 4> deque2;
177 for (unsigned i = 0; i < 10; ++i)
178 deque2.append(MoveOnly(i * 2));
179
180 deque1 = WTFMove(deque2);
181
182 EXPECT_EQ(10u, deque2.size());
183
184 unsigned i = 0;
185 for (auto& element : deque1) {
186 EXPECT_EQ(i * 2, element.value());
187 ++i;
188 }
189}
190
191} // namespace TestWebKitAPI
192