Fawkes API Fawkes Development Version
test_circular_buffer.cpp
1/***************************************************************************
2 * test_circular_buffer.cpp - CircularBuffer Unit Test
3 *
4 * Created: Fri Aug 15 16:27:42 2014
5 * Copyright 2014 Till Hofmann
6 *
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include <core/utils/circular_buffer.h>
23
24#include <catch2/catch.hpp>
25#include <stdexcept>
26
27using namespace fawkes;
28
29TEST_CASE("Access elements", "[circular_buffer]")
30{
31 CircularBuffer<int> buffer(1000);
32 for (int i = 0; i < 1000; i++) {
33 buffer.push_back(i);
34 }
35 for (int i = 0; i < 1000; i++) {
36 REQUIRE(buffer[i] == i);
37 REQUIRE(buffer.at(i) == i);
38 }
39}
40
41TEST_CASE("Delete elements", "[circular_buffer]")
42{
43 CircularBuffer<int> buffer(1);
44 buffer.push_back(1);
45 buffer.push_back(2);
46 REQUIRE(buffer.size() == 1);
47 REQUIRE(buffer[0] == 2);
48}
49
50TEST_CASE("Out of max range", "[circular_buffer]")
51{
52 CircularBuffer<int> buffer(1);
53 int i;
54 CHECK_NOTHROW(i = buffer[1]);
55 REQUIRE_THROWS_AS(i = buffer.at(1), std::out_of_range);
56}
57
58TEST_CASE("Out of range", "[circular_buffer]")
59{
60 CircularBuffer<int> buffer(2);
61 buffer.push_back(1);
62 int i;
63 REQUIRE_NOTHROW(i = buffer[1]);
64 REQUIRE_THROWS_AS(i = buffer.at(1), std::out_of_range);
65}
66
67TEST_CASE("Copy constructor", "[circular_buffer]")
68{
70 b1.push_back(1);
71 b1.push_back(2);
73 REQUIRE(b2.get_max_size() == 5);
74 REQUIRE(b2[0] == 1);
75 REQUIRE(b2[1] == 2);
76 b2.push_back(3);
77 REQUIRE(b2[2] == 3);
78 REQUIRE(b1[0] == 1);
79 REQUIRE(b1[1] == 2);
80 REQUIRE_THROWS_AS(b1.at(2), std::out_of_range);
81}
82
83TEST_CASE("Iterator", "[circular_buffer]")
84{
85 CircularBuffer<int> buffer(5);
86 for (int i = 0; i < 10; i++) {
87 buffer.push_back(i);
88 }
89 int i = 5;
90 for (auto it = buffer.begin(); it != buffer.end(); it++) {
91 REQUIRE(*it == i++);
92 }
93}
94
95TEST_CASE("Reverse Iterator", "[circular_buffer]")
96{
97 CircularBuffer<int> buffer(5);
98 for (int i = 0; i < 10; i++) {
99 buffer.push_back(i);
100 }
101 int i = 9;
102 for (auto it = buffer.rbegin(); it != buffer.rend(); it++) {
103 REQUIRE(*it == i--);
104 }
105}
Circular buffer with a fixed size.
Fawkes library namespace.