2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-08-21 16:51:25 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2019-10-23 10:20:45 +00:00
|
|
|
#include <AK/CircularQueue.h>
|
|
|
|
|
|
|
|
TEST_CASE(basic)
|
|
|
|
{
|
|
|
|
CircularQueue<int, 3> ints;
|
|
|
|
EXPECT(ints.is_empty());
|
|
|
|
ints.enqueue(1);
|
|
|
|
ints.enqueue(2);
|
|
|
|
ints.enqueue(3);
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(ints.size(), 3u);
|
2019-10-23 10:20:45 +00:00
|
|
|
|
|
|
|
ints.enqueue(4);
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(ints.size(), 3u);
|
2019-10-23 10:20:45 +00:00
|
|
|
EXPECT_EQ(ints.dequeue(), 2);
|
|
|
|
EXPECT_EQ(ints.dequeue(), 3);
|
|
|
|
EXPECT_EQ(ints.dequeue(), 4);
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(ints.size(), 0u);
|
2019-10-23 10:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(complex_type)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
CircularQueue<ByteString, 2> strings;
|
2019-10-23 10:20:45 +00:00
|
|
|
|
|
|
|
strings.enqueue("ABC");
|
|
|
|
strings.enqueue("DEF");
|
|
|
|
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(strings.size(), 2u);
|
2019-10-23 10:20:45 +00:00
|
|
|
|
|
|
|
strings.enqueue("abc");
|
|
|
|
strings.enqueue("def");
|
|
|
|
|
|
|
|
EXPECT_EQ(strings.dequeue(), "abc");
|
|
|
|
EXPECT_EQ(strings.dequeue(), "def");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(complex_type_clear)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
CircularQueue<ByteString, 5> strings;
|
2019-10-23 10:20:45 +00:00
|
|
|
strings.enqueue("xxx");
|
|
|
|
strings.enqueue("xxx");
|
|
|
|
strings.enqueue("xxx");
|
|
|
|
strings.enqueue("xxx");
|
|
|
|
strings.enqueue("xxx");
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(strings.size(), 5u);
|
2019-10-23 10:20:45 +00:00
|
|
|
strings.clear();
|
2020-03-06 10:21:23 +00:00
|
|
|
EXPECT_EQ(strings.size(), 0u);
|
2019-10-23 10:20:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 13:07:44 +00:00
|
|
|
struct ConstructorCounter {
|
|
|
|
static unsigned s_num_constructor_calls;
|
|
|
|
ConstructorCounter() { ++s_num_constructor_calls; }
|
|
|
|
};
|
|
|
|
unsigned ConstructorCounter::s_num_constructor_calls = 0;
|
|
|
|
|
|
|
|
TEST_CASE(should_not_call_value_type_constructor_when_created)
|
|
|
|
{
|
|
|
|
CircularQueue<ConstructorCounter, 10> queue;
|
|
|
|
EXPECT_EQ(0u, ConstructorCounter::s_num_constructor_calls);
|
|
|
|
}
|