6 # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" 10 #include <gtest/gtest.h> 15 "It was like this: I asked myself one day this question - what if Napoleon, for instance, had happened to be in my " 16 "place, and if he had not had Toulon nor Egypt nor the passage of Mont Blanc to begin his career with, but " 17 "instead of all those picturesque and monumental things, there had simply been some ridiculous old hag, a " 18 "pawnbroker, who had to be murdered too to get money from her trunk (for his career, you understand). " 19 "Well, would he have brought himself to that if there had been no other means?";
21 template <
typename T,
unsigned Size>
25 while ((--n > 0) && (a[n] == a[0])) { }
29 template <
typename T,
unsigned Size,
typename R>
30 static void fill(T (&a)[Size], R value)
32 for (
unsigned i = 0; i < Size; i++)
39 unsigned offset = 0,
int len = -1)
42 fill(local_buffer, 0);
43 assert((len < 0) || (
sizeof(local_buffer) >= static_cast<unsigned>(len)));
47 const int res = tbb.
read(offset, local_buffer,
sizeof(local_buffer));
50 std::cout <<
"matchAgainst(): res " << res << std::endl;
57 const int res = tbb.
read(offset, local_buffer,
unsigned(len));
60 std::cout <<
"matchAgainst(): res " << res <<
" expected " << len << std::endl;
64 const bool equals =
std::equal(local_buffer, local_buffer + len, data.begin() + offset);
67 std::cout <<
"local_buffer:\n\t" << local_buffer << std::endl;
68 std::cout <<
"test_data:\n\t" << std::string(data.begin() + offset, data.begin() + offset + len) << std::endl;
78 TEST(TransferBuffer, TestDataValidation)
83 ASSERT_FALSE(
allEqual(local_buffer));
88 TEST(StaticTransferBuffer, Basic)
91 StaticTransferBuffer<TEST_BUFFER_SIZE> buf;
93 uint8_t local_buffer[TEST_BUFFER_SIZE * 2];
97 fill(local_buffer, 0xA5);
98 ASSERT_EQ(0, buf.read(0, local_buffer, 999));
99 ASSERT_EQ(0, buf.read(0, local_buffer, 0));
100 ASSERT_EQ(0, buf.read(999, local_buffer, 0));
101 ASSERT_TRUE(
allEqual(local_buffer));
104 ASSERT_EQ(TEST_BUFFER_SIZE, buf.write(0, test_data_ptr,
unsigned(
TEST_DATA.length())));
113 fill(local_buffer, 0xA5);
115 ASSERT_EQ(0, buf.read(0, local_buffer, 0));
116 ASSERT_EQ(0, buf.read(0, local_buffer, 999));
117 ASSERT_TRUE(
allEqual(local_buffer));
120 ASSERT_EQ(21, buf.write(12, test_data_ptr + 12, 21));
123 ASSERT_EQ(12, buf.write(0, test_data_ptr, 12));
126 ASSERT_EQ(0, buf.write(21, test_data_ptr + 21, 0));
127 ASSERT_EQ(TEST_BUFFER_SIZE - 21, buf.write(21, test_data_ptr + 21, 999));
133 TEST(TransferBufferManagerEntry, Basic)
138 static const int POOL_BLOCKS = 8;
141 TransferBufferManagerEntry buf(pool, MAX_SIZE);
143 uint8_t local_buffer[TEST_BUFFER_SIZE * 2];
147 fill(local_buffer, 0xA5);
148 ASSERT_EQ(0, buf.read(0, local_buffer, 999));
149 ASSERT_EQ(0, buf.read(0, local_buffer, 0));
150 ASSERT_EQ(0, buf.read(999, local_buffer, 0));
151 ASSERT_TRUE(
allEqual(local_buffer));
154 ASSERT_EQ(MAX_SIZE, buf.write(0, test_data_ptr,
unsigned(
TEST_DATA.length())));
166 fill(local_buffer, 0xA5);
168 ASSERT_EQ(0, buf.read(0, local_buffer, 0));
169 ASSERT_EQ(0, buf.read(0, local_buffer, 999));
170 ASSERT_TRUE(
allEqual(local_buffer));
174 ASSERT_EQ(21, buf.write(12, test_data_ptr + 12, 21));
177 ASSERT_EQ(60, buf.write(TEST_BUFFER_SIZE - 60, test_data_ptr + TEST_BUFFER_SIZE - 60, 60));
182 ASSERT_EQ(0, buf.write(0, test_data_ptr, 0));
183 ASSERT_EQ(TEST_BUFFER_SIZE - 21, buf.write(21, test_data_ptr + 21, TEST_BUFFER_SIZE - 21));
188 ASSERT_EQ(21, buf.write(0, test_data_ptr, 21));
193 buf.~TransferBufferManagerEntry();
200 "I thought you would cry out again \'don\'t speak of it, leave off.\'\" Raskolnikov gave a laugh, but rather a " 201 "forced one. \"What, silence again?\" he asked a minute later. \"We must talk about something, you know. ",
203 "It would be interesting for me to know how you would decide a certain \'problem\' as Lebeziatnikov would say.\" " 204 "(He was beginning to lose the thread.) \"No, really, I am serious. Imagine, Sonia, that you had known all ",
206 "Luzhin\'s intentions beforehand. Known, that is, for a fact, that they would be the ruin of Katerina Ivanovna " 207 "and the children and yourself thrown in--since you don\'t count yourself for anything--Polenka too... for ",
209 "she\'ll go the same way. Well, if suddenly it all depended on your decision whether he or they should go on " 210 "living, that is whether Luzhin should go on living and doing wicked things, or Katerina Ivanovna should die? " 211 "How would you decide which of them was to die? I ask you?" 216 TEST(TransferBufferManager, TestDataValidation)
218 for (
unsigned i = 0; i <
sizeof(
MGR_TEST_DATA) /
sizeof(MGR_TEST_DATA[0]); i++)
220 ASSERT_LT(MGR_MAX_BUFFER_SIZE, MGR_TEST_DATA[i].length());
227 return tbb->
write(0, reinterpret_cast<const uint8_t*>(data.c_str()),
unsigned(data.length()));
230 TEST(TransferBufferManager, Basic)
236 static const int POOL_BLOCKS = 100;
239 std::unique_ptr<TransferBufferManager> mgr(
new TransferBufferManager(MGR_MAX_BUFFER_SIZE, pool));
247 const TransferBufferManagerKey keys[5] =
256 ASSERT_TRUE((tbb = mgr->create(keys[0])));
257 ASSERT_EQ(MGR_MAX_BUFFER_SIZE,
fillTestData(MGR_TEST_DATA[0], tbb));
258 ASSERT_EQ(1, mgr->getNumBuffers());
260 ASSERT_TRUE((tbb = mgr->create(keys[1])));
261 ASSERT_EQ(MGR_MAX_BUFFER_SIZE,
fillTestData(MGR_TEST_DATA[1], tbb));
262 ASSERT_EQ(2, mgr->getNumBuffers());
265 ASSERT_TRUE((tbb = mgr->create(keys[2])));
266 ASSERT_EQ(MGR_MAX_BUFFER_SIZE,
fillTestData(MGR_TEST_DATA[2], tbb));
267 ASSERT_EQ(3, mgr->getNumBuffers());
269 std::cout <<
"TransferBufferManager - Basic: Pool usage: " << pool.
getNumUsedBlocks() << std::endl;
271 ASSERT_TRUE((tbb = mgr->create(keys[3])));
274 ASSERT_EQ(4, mgr->getNumBuffers());
277 ASSERT_TRUE((tbb = mgr->access(keys[0])));
280 ASSERT_TRUE((tbb = mgr->access(keys[1])));
283 ASSERT_TRUE((tbb = mgr->access(keys[2])));
286 ASSERT_TRUE((tbb = mgr->access(keys[3])));
289 mgr->remove(keys[1]);
290 ASSERT_FALSE(mgr->access(keys[1]));
291 ASSERT_EQ(3, mgr->getNumBuffers());
294 mgr->remove(keys[0]);
295 ASSERT_FALSE(mgr->access(keys[0]));
296 ASSERT_EQ(2, mgr->getNumBuffers());
299 ASSERT_TRUE((tbb = mgr->access(keys[2])));
302 ASSERT_TRUE((tbb = mgr->access(keys[3])));
306 ASSERT_FALSE(mgr->access(keys[1]));
307 ASSERT_FALSE(mgr->access(keys[0]));
308 ASSERT_TRUE(mgr->access(keys[3]));
311 ASSERT_TRUE((tbb = mgr->create(keys[1])));
TEST(TransferBuffer, TestDataValidation)
UAVCAN_EXPORT bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
static bool allEqual(const T(&a)[Size])
static const unsigned MemPoolBlockSize
Safe default that should be OK for any platform.
static const std::string MGR_TEST_DATA[4]
static const std::string TEST_DATA
virtual int write(unsigned offset, const uint8_t *data, unsigned len)=0
UAVCAN_EXPORT OutputIt copy(InputIt first, InputIt last, OutputIt result)
uint16_t getNumFreeBlocks() const
virtual int read(unsigned offset, uint8_t *data, unsigned len) const =0
static bool matchAgainst(const std::string &data, const uavcan::ITransferBuffer &tbb, unsigned offset=0, int len=-1)
static const int TEST_BUFFER_SIZE
static void fill(T(&a)[Size], R value)
static const int MGR_MAX_BUFFER_SIZE
static bool matchAgainstTestData(const uavcan::ITransferBuffer &tbb, unsigned offset, int len=-1)
static int fillTestData(const std::string &data, uavcan::ITransferBuffer *tbb)
uint16_t getNumUsedBlocks() const