| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
#include "config.h" |
| 4 |
|
| 5 |
#include <memory> |
| 6 |
|
| 7 |
#include <dune/common/test/testsuite.hh> |
| 8 |
|
| 9 |
#include "../fifo.h" |
| 10 |
|
| 11 |
using namespace Dune; |
| 12 |
|
| 13 |
TestSuite test_fifo() |
| 14 |
{ |
| 15 |
TestSuite test; |
| 16 |
|
| 17 |
using namespace UG; |
| 18 |
|
| 19 |
FIFO fifo; |
| 20 |
const INT size = 32; |
| 21 |
int items[size]; |
| 22 |
for (int i = 0; i < size; ++i) |
| 23 |
items[i] = i; |
| 24 |
|
| 25 |
auto buffer = std::make_unique<void*[]>(size); |
| 26 |
{ |
| 27 |
const auto result = fifo_init(&fifo, buffer.get(), size * sizeof(void*)); |
| 28 |
test.require(result, "require that fifo_init() succeeds"); |
| 29 |
} |
| 30 |
|
| 31 |
test.check(fifo_empty(&fifo), "New FIFO must be empty"); |
| 32 |
test.check(!fifo_full(&fifo), "New FIFO must not be full"); |
| 33 |
test.check(fifo_out(&fifo) == nullptr, "New FIFO must not return an element"); |
| 34 |
|
| 35 |
{ |
| 36 |
const auto result = fifo_in(&fifo, &items[0]); |
| 37 |
test.check(!result, "Inserting an element must succeed"); |
| 38 |
} |
| 39 |
|
| 40 |
test.check(!fifo_empty(&fifo), "FIFO must not be empty after inserting an element"); |
| 41 |
test.check(!fifo_full(&fifo), "FIFO must not be full after inserting an element"); |
| 42 |
|
| 43 |
{ |
| 44 |
const auto v = static_cast<int*>(fifo_out(&fifo)); |
| 45 |
test.check(v == &items[0], "FIFO must return the item inserted before"); |
| 46 |
} |
| 47 |
|
| 48 |
test.check(fifo_empty(&fifo), "FIFO must be empty after removing the element again"); |
| 49 |
test.check(!fifo_full(&fifo), "FIFO must not be full after removing the element again"); |
| 50 |
|
| 51 |
for (auto& item : items) { |
| 52 |
bool result = fifo_in(&fifo, &item); |
| 53 |
test.check(!result, "Inserting elements must succeed"); |
| 54 |
} |
| 55 |
|
| 56 |
test.check(!fifo_empty(&fifo), "FIFO must not be empty after filling it"); |
| 57 |
test.check(fifo_full(&fifo), "FIFO must be full after filling it"); |
| 58 |
|
| 59 |
{ |
| 60 |
bool result = fifo_in(&fifo, &items[0]); |
| 61 |
test.check(result, "Inserting into a full FIFO must fail"); |
| 62 |
} |
| 63 |
|
| 64 |
for (auto& item : items) { |
| 65 |
const auto v = static_cast<int*>(fifo_out(&fifo)); |
| 66 |
test.check(v == &item, "FIFO must return correct item"); |
| 67 |
} |
| 68 |
|
| 69 |
test.check(fifo_empty(&fifo), "FIFO must be empty after removing all elements"); |
| 70 |
test.check(!fifo_full(&fifo), "FIFO must not be full after removing all elements"); |
| 71 |
test.check(fifo_out(&fifo) == nullptr, "fifo_out() must return nullptr after removing all elements"); |
| 72 |
|
| 73 |
fifo_clear(&fifo); |
| 74 |
|
| 75 |
return test; |
| 76 |
} |
| 77 |
|
| 78 |
int main() |
| 79 |
{ |
| 80 |
TestSuite test; |
| 81 |
|
| 82 |
test.subTest(test_fifo()); |
| 83 |
|
| 84 |
return test.exit(); |
| 85 |
} |