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 #define CATCH_CONFIG_MAIN
23 #include <core/utils/circular_buffer.h>
24 
25 #include <catch2/catch.hpp>
26 #include <stdexcept>
27 
28 using namespace fawkes;
29 
30 TEST_CASE("Access elements", "[circular_buffer]")
31 {
32  CircularBuffer<int> buffer(1000);
33  for (int i = 0; i < 1000; i++) {
34  buffer.push_back(i);
35  }
36  for (int i = 0; i < 1000; i++) {
37  REQUIRE(buffer[i] == i);
38  REQUIRE(buffer.at(i) == i);
39  }
40 }
41 
42 TEST_CASE("Delete elements", "[circular_buffer]")
43 {
44  CircularBuffer<int> buffer(1);
45  buffer.push_back(1);
46  buffer.push_back(2);
47  REQUIRE(buffer.size() == 1);
48  REQUIRE(buffer[0] == 2);
49 }
50 
51 TEST_CASE("Out of max range", "[circular_buffer]")
52 {
53  CircularBuffer<int> buffer(1);
54  int i;
55  CHECK_NOTHROW(i = buffer[1]);
56  REQUIRE_THROWS_AS(i = buffer.at(1), std::out_of_range);
57 }
58 
59 TEST_CASE("Out of range", "[circular_buffer]")
60 {
61  CircularBuffer<int> buffer(2);
62  buffer.push_back(1);
63  int i;
64  REQUIRE_NOTHROW(i = buffer[1]);
65  REQUIRE_THROWS_AS(i = buffer.at(1), std::out_of_range);
66 }
67 
68 TEST_CASE("Copy constructor", "[circular_buffer]")
69 {
70  CircularBuffer<int> b1(5);
71  b1.push_back(1);
72  b1.push_back(2);
73  CircularBuffer<int> b2(b1);
74  REQUIRE(b2.get_max_size() == 5);
75  REQUIRE(b2[0] == 1);
76  REQUIRE(b2[1] == 2);
77  b2.push_back(3);
78  REQUIRE(b2[2] == 3);
79  REQUIRE(b1[0] == 1);
80  REQUIRE(b1[1] == 2);
81  REQUIRE_THROWS_AS(b1.at(2), std::out_of_range);
82 }
fawkes
Fawkes library namespace.
fawkes::CircularBuffer
Circular buffer with a fixed size.
Definition: circular_buffer.h:45