2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* Deque.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
#include "flow/Deque.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2018-10-06 13:09:58 +08:00
|
|
|
TEST_CASE("/flow/Deque/12345") {
|
2017-05-26 04:48:44 +08:00
|
|
|
Deque<int> q;
|
|
|
|
q.push_back(1);
|
|
|
|
q.push_back(2);
|
|
|
|
q.push_back(3);
|
|
|
|
q.pop_front();
|
|
|
|
q.push_back(4);
|
|
|
|
q.pop_back();
|
|
|
|
q.push_back(5);
|
|
|
|
q.push_back(6);
|
|
|
|
q.pop_front();
|
|
|
|
q.pop_back();
|
|
|
|
ASSERT(q.size() == 2 && q[0] == 3 && q[1] == 5);
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:09:58 +08:00
|
|
|
TEST_CASE("/flow/Deque/queue") {
|
2017-05-26 04:48:44 +08:00
|
|
|
std::queue<int, Deque<int>> q;
|
|
|
|
|
|
|
|
int to_push = 0, to_pop = 0;
|
|
|
|
while (to_pop != 1000) {
|
2019-05-11 05:01:52 +08:00
|
|
|
if (to_push != 1000 && (q.empty() || deterministicRandom()->random01() < 0.55)) {
|
2017-05-26 04:48:44 +08:00
|
|
|
q.push(to_push++);
|
|
|
|
} else {
|
|
|
|
ASSERT(q.front() == to_pop++);
|
|
|
|
q.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(q.empty());
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:09:58 +08:00
|
|
|
TEST_CASE("/flow/Deque/max_size") {
|
2017-05-26 04:48:44 +08:00
|
|
|
Deque<uint8_t> q;
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
q.push_back(i);
|
|
|
|
q.pop_front();
|
|
|
|
for (int64_t i = 10; i <= q.max_size(); i++)
|
|
|
|
q.push_back(i);
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
q.pop_front();
|
|
|
|
q.push_back(1);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
ASSERT(q[q.size() - 100 + i] == 1);
|
|
|
|
for (int64_t i = 101; i <= q.max_size(); i++) {
|
|
|
|
ASSERT(q[i - 101] == uint8_t(i));
|
|
|
|
}
|
|
|
|
ASSERT(&q.back() + 1 == &q.front());
|
|
|
|
try {
|
|
|
|
q.push_back(1);
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
catch (std::bad_alloc&) {}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:16:45 +08:00
|
|
|
void forceLinkDequeTests() {}
|