multiset.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #if __GNUC__
  5. // We need auto_ptr for compatibility reasons
  6. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  7. # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  8. #endif
  9. #include <string>
  10. #include <cstdio>
  11. #include <memory>
  12. #include <gtest/gtest.h>
  13. #include <uavcan/util/multiset.hpp>
  14. static std::string toString(long x)
  15. {
  16. char buf[80];
  17. std::snprintf(buf, sizeof(buf), "%li", x);
  18. return std::string(buf);
  19. }
  20. static bool oddValuePredicate(const std::string& value)
  21. {
  22. EXPECT_FALSE(value.empty());
  23. const int num = atoi(value.c_str());
  24. return num & 1;
  25. }
  26. struct FindPredicate
  27. {
  28. const std::string target;
  29. FindPredicate(const std::string& target) : target(target) { }
  30. bool operator()(const std::string& value) const { return value == target; }
  31. };
  32. struct NoncopyableWithCounter : uavcan::Noncopyable
  33. {
  34. static int num_objects;
  35. long long value;
  36. NoncopyableWithCounter() : value(0) { num_objects++; }
  37. NoncopyableWithCounter(long long x) : value(x) { num_objects++; }
  38. ~NoncopyableWithCounter() { num_objects--; }
  39. static bool isNegative(const NoncopyableWithCounter& val)
  40. {
  41. return val.value < 0;
  42. }
  43. bool operator==(const NoncopyableWithCounter& ref) const { return ref.value == value; }
  44. };
  45. int NoncopyableWithCounter::num_objects = 0;
  46. template <typename T>
  47. struct SummationOperator : uavcan::Noncopyable
  48. {
  49. T accumulator;
  50. SummationOperator() : accumulator() { }
  51. void operator()(const T& x) { accumulator += x; }
  52. };
  53. struct ClearingOperator
  54. {
  55. template <typename T>
  56. void operator()(T& x) const { x = T(); }
  57. };
  58. TEST(Multiset, Basic)
  59. {
  60. using uavcan::Multiset;
  61. static const int POOL_BLOCKS = 4;
  62. uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
  63. typedef Multiset<std::string> MultisetType;
  64. std::auto_ptr<MultisetType> mset(new MultisetType(pool));
  65. typedef SummationOperator<std::string> StringConcatenationOperator;
  66. // Empty
  67. mset->removeFirst("foo");
  68. ASSERT_EQ(0, pool.getNumUsedBlocks());
  69. ASSERT_FALSE(mset->getByIndex(0));
  70. ASSERT_FALSE(mset->getByIndex(1));
  71. ASSERT_FALSE(mset->getByIndex(10000));
  72. // Static addion
  73. ASSERT_EQ("1", *mset->emplace("1"));
  74. ASSERT_EQ("2", *mset->emplace("2"));
  75. ASSERT_LE(1, pool.getNumUsedBlocks()); // One or more
  76. ASSERT_EQ(2, mset->getSize());
  77. {
  78. StringConcatenationOperator op;
  79. mset->forEach<StringConcatenationOperator&>(op);
  80. ASSERT_EQ(2, op.accumulator.size());
  81. }
  82. // Dynamic addition
  83. ASSERT_EQ("3", *mset->emplace("3"));
  84. ASSERT_LE(1, pool.getNumUsedBlocks()); // One or more
  85. ASSERT_EQ("4", *mset->emplace("4"));
  86. ASSERT_LE(1, pool.getNumUsedBlocks()); // One or more
  87. ASSERT_EQ(4, mset->getSize());
  88. ASSERT_FALSE(mset->getByIndex(100));
  89. ASSERT_FALSE(mset->getByIndex(4));
  90. // Finding some items
  91. ASSERT_EQ("1", *mset->find(FindPredicate("1")));
  92. ASSERT_EQ("2", *mset->find(FindPredicate("2")));
  93. ASSERT_EQ("3", *mset->find(FindPredicate("3")));
  94. ASSERT_EQ("4", *mset->find(FindPredicate("4")));
  95. ASSERT_FALSE(mset->find(FindPredicate("nonexistent")));
  96. {
  97. StringConcatenationOperator op;
  98. mset->forEach<StringConcatenationOperator&>(op);
  99. std::cout << "Accumulator: " << op.accumulator << std::endl;
  100. ASSERT_EQ(4, op.accumulator.size());
  101. }
  102. // Removing some
  103. mset->removeFirst("1");
  104. mset->removeFirst("foo"); // There's no such thing anyway
  105. mset->removeFirst("2");
  106. // Adding some new items
  107. unsigned max_value_integer = 0;
  108. for (int i = 0; i < 100; i++)
  109. {
  110. const std::string value = toString(i);
  111. std::string* res = mset->emplace(value); // Will NOT override above
  112. if (res == UAVCAN_NULLPTR)
  113. {
  114. ASSERT_LT(1, i);
  115. break;
  116. }
  117. else
  118. {
  119. ASSERT_EQ(value, *res);
  120. }
  121. max_value_integer = unsigned(i);
  122. }
  123. std::cout << "Max value: " << max_value_integer << std::endl;
  124. // Making sure there is true OOM
  125. ASSERT_EQ(0, pool.getNumFreeBlocks());
  126. ASSERT_FALSE(mset->emplace("nonexistent"));
  127. // Removing odd values - nearly half of them
  128. mset->removeAllWhere(oddValuePredicate);
  129. // Making sure there's no odd values left
  130. for (unsigned kv_int = 0; kv_int <= max_value_integer; kv_int++)
  131. {
  132. const std::string* val = mset->find(FindPredicate(toString(kv_int)));
  133. if (val)
  134. {
  135. ASSERT_FALSE(kv_int & 1);
  136. }
  137. else
  138. {
  139. ASSERT_TRUE(kv_int & 1);
  140. }
  141. }
  142. // Clearing all strings
  143. {
  144. StringConcatenationOperator op;
  145. mset->forEach<StringConcatenationOperator&>(op);
  146. std::cout << "Accumulator before clearing: " << op.accumulator << std::endl;
  147. }
  148. mset->forEach(ClearingOperator());
  149. {
  150. StringConcatenationOperator op;
  151. mset->forEach<StringConcatenationOperator&>(op);
  152. std::cout << "Accumulator after clearing: " << op.accumulator << std::endl;
  153. ASSERT_TRUE(op.accumulator.empty());
  154. }
  155. // Making sure the memory will be released
  156. mset.reset();
  157. ASSERT_EQ(0, pool.getNumUsedBlocks());
  158. }
  159. TEST(Multiset, PrimitiveKey)
  160. {
  161. using uavcan::Multiset;
  162. static const int POOL_BLOCKS = 3;
  163. uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
  164. typedef Multiset<int> MultisetType;
  165. std::auto_ptr<MultisetType> mset(new MultisetType(pool));
  166. // Empty
  167. mset->removeFirst(8);
  168. ASSERT_EQ(0, pool.getNumUsedBlocks());
  169. ASSERT_EQ(0, mset->getSize());
  170. ASSERT_FALSE(mset->getByIndex(0));
  171. // Insertion
  172. ASSERT_EQ(1, *mset->emplace(1));
  173. ASSERT_EQ(1, mset->getSize());
  174. ASSERT_EQ(2, *mset->emplace(2));
  175. ASSERT_EQ(2, mset->getSize());
  176. ASSERT_EQ(3, *mset->emplace(3));
  177. ASSERT_EQ(4, *mset->emplace(4));
  178. ASSERT_EQ(4, mset->getSize());
  179. // Summation and clearing
  180. {
  181. SummationOperator<int> summation_operator;
  182. mset->forEach<SummationOperator<int>&>(summation_operator);
  183. ASSERT_EQ(1 + 2 + 3 + 4, summation_operator.accumulator);
  184. }
  185. mset->forEach(ClearingOperator());
  186. {
  187. SummationOperator<int> summation_operator;
  188. mset->forEach<SummationOperator<int>&>(summation_operator);
  189. ASSERT_EQ(0, summation_operator.accumulator);
  190. }
  191. }
  192. TEST(Multiset, NoncopyableWithCounter)
  193. {
  194. using uavcan::Multiset;
  195. static const int POOL_BLOCKS = 3;
  196. uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
  197. typedef Multiset<NoncopyableWithCounter> MultisetType;
  198. std::auto_ptr<MultisetType> mset(new MultisetType(pool));
  199. ASSERT_EQ(0, NoncopyableWithCounter::num_objects);
  200. ASSERT_EQ(0, mset->emplace()->value);
  201. ASSERT_EQ(1, NoncopyableWithCounter::num_objects);
  202. ASSERT_EQ(123, mset->emplace(123)->value);
  203. ASSERT_EQ(2, NoncopyableWithCounter::num_objects);
  204. ASSERT_EQ(-456, mset->emplace(-456)->value);
  205. ASSERT_EQ(3, NoncopyableWithCounter::num_objects);
  206. ASSERT_EQ(456, mset->emplace(456)->value);
  207. ASSERT_EQ(4, NoncopyableWithCounter::num_objects);
  208. ASSERT_EQ(-789, mset->emplace(-789)->value);
  209. ASSERT_EQ(5, NoncopyableWithCounter::num_objects);
  210. mset->removeFirst(NoncopyableWithCounter(0));
  211. ASSERT_EQ(4, NoncopyableWithCounter::num_objects);
  212. mset->removeFirstWhere(&NoncopyableWithCounter::isNegative);
  213. ASSERT_EQ(3, NoncopyableWithCounter::num_objects);
  214. mset->removeAllWhere(&NoncopyableWithCounter::isNegative);
  215. ASSERT_EQ(2, NoncopyableWithCounter::num_objects); // Only 1 and 2 are left
  216. mset.reset();
  217. ASSERT_EQ(0, pool.getNumUsedBlocks());
  218. ASSERT_EQ(0, NoncopyableWithCounter::num_objects); // All destroyed
  219. }