2020-08-25 14:57:02 +02:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-08-25 14:57:02 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-25 14:57:02 +02:00
|
|
|
*/
|
|
|
|
|
|
2021-04-24 23:53:23 -06:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-08-25 14:57:02 +02:00
|
|
|
|
|
|
|
|
#include <AK/CircularDuplexStream.h>
|
|
|
|
|
|
|
|
|
|
TEST_CASE(works_like_a_queue)
|
|
|
|
|
{
|
|
|
|
|
constexpr size_t capacity = 32;
|
|
|
|
|
|
|
|
|
|
CircularQueue<u8, capacity> queue;
|
|
|
|
|
CircularDuplexStream<capacity> stream;
|
|
|
|
|
|
|
|
|
|
for (size_t idx = 0; idx < capacity; ++idx) {
|
|
|
|
|
queue.enqueue(static_cast<u8>(idx % 256));
|
|
|
|
|
stream << static_cast<u8>(idx % 256);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t idx = 0; idx < capacity; ++idx) {
|
2021-01-17 14:25:12 -07:00
|
|
|
u8 byte = 0;
|
2020-08-25 14:57:02 +02:00
|
|
|
stream >> byte;
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(queue.dequeue(), byte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT(stream.eof());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(overwritting_is_well_defined)
|
|
|
|
|
{
|
|
|
|
|
constexpr size_t half_capacity = 16;
|
|
|
|
|
constexpr size_t capacity = 2 * half_capacity;
|
|
|
|
|
|
|
|
|
|
CircularDuplexStream<capacity> stream;
|
|
|
|
|
|
|
|
|
|
for (size_t idx = 0; idx < capacity; ++idx)
|
|
|
|
|
stream << static_cast<u8>(idx % 256);
|
|
|
|
|
|
2020-12-19 21:19:59 +01:00
|
|
|
Array<u8, half_capacity> buffer;
|
|
|
|
|
stream >> buffer;
|
2020-08-25 14:57:02 +02:00
|
|
|
|
2020-09-05 17:38:46 +02:00
|
|
|
for (size_t idx = 0; idx < half_capacity; ++idx)
|
2020-12-19 21:19:59 +01:00
|
|
|
EXPECT_EQ(buffer[idx], idx % 256);
|
2020-08-25 14:57:02 +02:00
|
|
|
|
|
|
|
|
for (size_t idx = 0; idx < half_capacity; ++idx)
|
|
|
|
|
stream << static_cast<u8>(idx % 256);
|
|
|
|
|
|
|
|
|
|
for (size_t idx = 0; idx < capacity; ++idx) {
|
2021-01-17 14:25:12 -07:00
|
|
|
u8 byte = 0;
|
2020-08-25 14:57:02 +02:00
|
|
|
stream >> byte;
|
|
|
|
|
|
|
|
|
|
if (idx < half_capacity)
|
|
|
|
|
EXPECT_EQ(byte, half_capacity + idx % 256);
|
|
|
|
|
else
|
|
|
|
|
EXPECT_EQ(byte, idx % 256 - half_capacity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT(stream.eof());
|
|
|
|
|
}
|