1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#include <iostream>
#include <chrono>
#include <future>
#include <thread>
#include <condition_variable>
#include "gtest/gtest.h"
#define SRT_TEST_CIRCULAR_BUFFER
#include "api.h"
#include "common.h"
using namespace std;
using namespace srt;
// To test CircularBuffer
struct Double
{
double d;
size_t instance;
static size_t sourceid;
Double(): d(0.0)
{
instance = ++sourceid;
IF_HEAVY_LOGGING(cerr << "(Double/" << instance << ": empty costruction)\n");
}
Double(double dd): d(dd)
{
instance = ++sourceid;
IF_HEAVY_LOGGING(cerr << "(Double:/" << instance << " init construction:" << dd << ")\n");
}
Double(const Double& dd): d(dd.d)
{
instance = ++sourceid;
IF_HEAVY_LOGGING(cerr << "(Double:/" << instance << " copy construction:" << dd.d << " object/" << dd.instance << ")\n");
}
operator double() { return d; }
~Double()
{
IF_HEAVY_LOGGING(cerr << "(Double:/" << instance << " destruction:" << d << ")\n");
}
void operator=(double dd)
{
IF_HEAVY_LOGGING(cerr << "(Double:/" << instance << " copy assignment:" << d << " -> " << dd << " value)\n");
d = dd;
}
void operator=(const Double& dd)
{
IF_HEAVY_LOGGING(cerr << "(Double:/" << instance << " copy assignment:" << d << " -> " << dd.d << " object/" << dd.instance << ")\n");
d = dd.d;
}
// Required for template-based gtest :(
friend bool operator==(const Double& l, double r) { return l.d == r; }
friend bool operator==(double l, const Double r) { return l == r.d; }
bool operator == (const Double& r) { return d == r; }
};
size_t Double::sourceid = 0;
template <class Val> inline
void ShowCircularBuffer(const CircularBuffer<Val>& buf)
{
cerr << "SIZE: " << buf.size() << " FREE:" << buf.spaceleft() << " BEGIN:" << buf.m_xBegin << " END: " << buf.m_xEnd << endl;
for (int i = 0; i < buf.size(); ++i)
{
Double v;
if (buf.get(i, (v)))
cerr << "[" << i << "] = " << v << endl;
else
cerr << "[" << i << "] EMPTY!\n";
}
}
struct Add
{
Double v;
Add(const Double& vv): v(vv) {}
void operator()(Double& accessed, bool isnew)
{
if (isnew)
accessed = v;
else
accessed = Double(accessed.d + v.d);
}
};
TEST(CircularBuffer, Overall)
{
using namespace std;
// Create some odd number of elements in a circular buffer.
CircularBuffer<Double> buf(7);
cerr << dec;
// Now, add 3 elements to it and check if succeeded.
buf.push(11.2);
buf.push(12.3);
buf.push(13.4);
IF_HEAVY_LOGGING(cerr << "After adding 3 elements: size=" << buf.size() << " capacity=" << buf.capacity() << ":\n");
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
ASSERT_EQ(buf.size(), 3);
IF_HEAVY_LOGGING(cerr << "Adding element at position 5:\n");
EXPECT_TRUE(buf.set(5, 15.5));
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
ASSERT_EQ(buf.size(), 6);
IF_HEAVY_LOGGING(cerr << "Adding element at position 7 (should fail):\n");
EXPECT_FALSE(buf.set(7, 10.0));
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
ASSERT_EQ(buf.size(), 6);
IF_HEAVY_LOGGING(cerr << "Dropping first 2 elements:\n");
buf.drop(2);
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
ASSERT_EQ(buf.size(), 4);
IF_HEAVY_LOGGING(cerr << "Adding again element at position 6 (should roll):\n");
buf.set(6, 22.1);
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
IF_HEAVY_LOGGING(cerr << "Adding element at existing position 2 (overwrite):\n");
buf.set(2, 33.1);
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
IF_HEAVY_LOGGING(cerr << "Adding element at existing position 3 (no overwrite):\n");
buf.set(3, 44.4, false);
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
Double output;
// [0] = 13.4 (after dropping first 2 elements)
EXPECT_TRUE(buf.get(0, (output)));
ASSERT_EQ(output, 13.4);
// [2] = 33.1 overwriting
EXPECT_TRUE(buf.get(2, (output)));
ASSERT_EQ(output, 33.1);
// [3] = was 15.5, requested to set 44.4, but not overwriting
EXPECT_TRUE(buf.get(3, (output)));
ASSERT_EQ(output, 15.5);
// [6] = 22.1 (as set with rolling)
EXPECT_TRUE(buf.get(6, (output)));
ASSERT_EQ(output, 22.1);
IF_HEAVY_LOGGING(cerr << "Dropping first 4 positions:\n");
buf.drop(4);
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
EXPECT_TRUE(buf.get(2, (output))); // Was 6 before dropping
ASSERT_EQ(output.d, 22.1);
IF_HEAVY_LOGGING(cerr << "Pushing 1 aslong there is capacity:\n");
IF_HEAVY_LOGGING(int i = 0);
while (buf.push(1) != -1)
{
IF_HEAVY_LOGGING(cerr << "Pushed, begin=" << buf.m_xBegin << " end=" << buf.m_xEnd << endl);
IF_HEAVY_LOGGING(++i);
}
IF_HEAVY_LOGGING(cerr << "Done " << i << " operations, buffer:\n");
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
IF_HEAVY_LOGGING(cerr << "Updating value at position 5:\n");
EXPECT_TRUE(buf.update(5, Add(3.33)));
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
EXPECT_TRUE(buf.get(5, (output)));
ASSERT_EQ(output, 4.33);
int offset = 9;
IF_HEAVY_LOGGING(cerr << "Forced adding at position 9 with dropping (capacity: " << buf.capacity() << "):\n");
// State we already know it has failed. Calculate drop size.
int dropshift = offset - int(buf.capacity() - 1); // buf.capacity()-1 is the latest position
offset -= dropshift;
IF_HEAVY_LOGGING(cerr << "Need to drop: " << dropshift << " New offset:" << offset << endl);
ASSERT_GE(dropshift, 0);
if (dropshift > 0)
{
buf.drop(dropshift);
IF_HEAVY_LOGGING(cerr << "AFTER DROPPING:\n");
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
EXPECT_TRUE(buf.set(offset, 99.1, true));
// size() - 1 is the latest possible offset
ASSERT_EQ(buf.size() - 1 + dropshift, 9);
}
else
{
IF_HEAVY_LOGGING(cerr << "NEGATIVE DROP!\n");
}
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
int size = buf.size();
IF_HEAVY_LOGGING(cerr << "Dropping rest of the items (passing " << (size) << "):\n");
// NOTE: 'drop' gets the POSITION as argument, but this position
// is allowed to be past the last addressable position. When passing
// the current size, it should make the container empty.
buf.drop(size);
EXPECT_TRUE(buf.empty());
IF_HEAVY_LOGGING(ShowCircularBuffer(buf));
IF_HEAVY_LOGGING(cerr << "DONE.\n");
}
TEST(ConfigString, Setting)
{
using namespace std;
static const auto STRSIZE = 20;
StringStorage<STRSIZE> s;
EXPECT_TRUE(s.empty());
EXPECT_EQ(s.size(), 0U);
EXPECT_EQ(s.str(), std::string());
char example_ac1[] = "example_long";
char example_ac2[] = "short";
char example_ac3[] = "example_longer";
char example_acx[] = "example_long_excessively";
char example_ace[] = "";
// According to the standard, this array gets automatically
// the number of characters + 1 for terminating 0. Get sizeof()-1
// to get the number of characters.
EXPECT_TRUE(s.set(example_ac1, sizeof (example_ac1)-1));
EXPECT_EQ(s.size(), sizeof (example_ac1)-1);
EXPECT_FALSE(s.empty());
EXPECT_TRUE(s.set(example_ac2, sizeof (example_ac2)-1));
EXPECT_EQ(s.size(), sizeof (example_ac2)-1);
EXPECT_TRUE(s.set(example_ac3, sizeof (example_ac3)-1));
EXPECT_EQ(s.size(), sizeof (example_ac3)-1);
EXPECT_FALSE(s.set(example_acx, sizeof (example_acx)-1));
EXPECT_EQ(s.size(), sizeof (example_ac3)-1);
EXPECT_TRUE(s.set(example_ace, sizeof (example_ace)-1));
EXPECT_EQ(s.size(), 0U);
string example_s1 = "example_long";
string example_s2 = "short";
string example_s3 = "example_longer";
string example_sx = "example_long_excessively";
string example_se = "";
EXPECT_TRUE(s.set(example_s1));
EXPECT_EQ(s.size(), example_s1.size());
EXPECT_FALSE(s.empty());
EXPECT_TRUE(s.set(example_s2));
EXPECT_EQ(s.size(), example_s2.size());
EXPECT_TRUE(s.set(example_s3));
EXPECT_EQ(s.size(), example_s3.size());
EXPECT_FALSE(s.set(example_sx));
EXPECT_EQ(s.size(), example_s3.size());
EXPECT_TRUE(s.set(example_se));
EXPECT_EQ(s.size(), 0U);
EXPECT_TRUE(s.empty());
}
|