set_ut.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #include "set.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <utility>
  4. #include <algorithm>
  5. Y_UNIT_TEST_SUITE(YSetTest) {
  6. Y_UNIT_TEST(TestSet1) {
  7. TSet<int, TLess<int>> s;
  8. UNIT_ASSERT(!s);
  9. UNIT_ASSERT(s.count(42) == 0);
  10. s.insert(42);
  11. UNIT_ASSERT(s);
  12. UNIT_ASSERT(s.count(42) == 1);
  13. s.insert(42);
  14. UNIT_ASSERT(s.count(42) == 1);
  15. size_t count = s.erase(42);
  16. UNIT_ASSERT(count == 1);
  17. }
  18. Y_UNIT_TEST(TestSet2) {
  19. using int_set = TSet<int, TLess<int>>;
  20. int_set s;
  21. std::pair<int_set::iterator, bool> p = s.insert(42);
  22. UNIT_ASSERT(p.second == true);
  23. p = s.insert(42);
  24. UNIT_ASSERT(p.second == false);
  25. int array1[] = {1, 3, 6, 7};
  26. s.insert(array1, array1 + 4);
  27. UNIT_ASSERT(distance(s.begin(), s.end()) == 5);
  28. int_set s2;
  29. s2.swap(s);
  30. UNIT_ASSERT(distance(s2.begin(), s2.end()) == 5);
  31. UNIT_ASSERT(distance(s.begin(), s.end()) == 0);
  32. int_set s3;
  33. s3.swap(s);
  34. s3.swap(s2);
  35. UNIT_ASSERT(distance(s.begin(), s.end()) == 0);
  36. UNIT_ASSERT(distance(s2.begin(), s2.end()) == 0);
  37. UNIT_ASSERT(distance(s3.begin(), s3.end()) == 5);
  38. }
  39. Y_UNIT_TEST(TestErase) {
  40. TSet<int, TLess<int>> s;
  41. s.insert(1);
  42. s.erase(s.begin());
  43. UNIT_ASSERT(s.empty());
  44. size_t nb = s.erase(1);
  45. UNIT_ASSERT(nb == 0);
  46. }
  47. Y_UNIT_TEST(TestInsert) {
  48. TSet<int> s;
  49. TSet<int>::iterator i = s.insert(s.end(), 0);
  50. UNIT_ASSERT(*i == 0);
  51. }
  52. Y_UNIT_TEST(TestFind) {
  53. TSet<int> s;
  54. UNIT_ASSERT(s.find(0) == s.end());
  55. TSet<int> const& crs = s;
  56. UNIT_ASSERT(crs.find(0) == crs.end());
  57. }
  58. Y_UNIT_TEST(TestHas) {
  59. TSet<int> s;
  60. UNIT_ASSERT(!s.contains(0));
  61. TSet<int> const& crs = s;
  62. UNIT_ASSERT(!crs.contains(0));
  63. s.insert(1);
  64. s.insert(42);
  65. s.insert(100);
  66. s.insert(2);
  67. UNIT_ASSERT(s.contains(1));
  68. UNIT_ASSERT(s.contains(2));
  69. UNIT_ASSERT(s.contains(42));
  70. UNIT_ASSERT(s.contains(100));
  71. }
  72. Y_UNIT_TEST(TestBounds) {
  73. int array1[] = {1, 3, 6, 7};
  74. TSet<int> s(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  75. TSet<int> const& crs = s;
  76. TSet<int>::iterator sit;
  77. TSet<int>::const_iterator scit;
  78. std::pair<TSet<int>::iterator, TSet<int>::iterator> pit;
  79. std::pair<TSet<int>::const_iterator, TSet<int>::const_iterator> pcit;
  80. //Check iterator on mutable set
  81. sit = s.lower_bound(2);
  82. UNIT_ASSERT(sit != s.end());
  83. UNIT_ASSERT(*sit == 3);
  84. sit = s.upper_bound(5);
  85. UNIT_ASSERT(sit != s.end());
  86. UNIT_ASSERT(*sit == 6);
  87. pit = s.equal_range(6);
  88. UNIT_ASSERT(pit.first != pit.second);
  89. UNIT_ASSERT(pit.first != s.end());
  90. UNIT_ASSERT(*pit.first == 6);
  91. UNIT_ASSERT(pit.second != s.end());
  92. UNIT_ASSERT(*pit.second == 7);
  93. pit = s.equal_range(4);
  94. UNIT_ASSERT(pit.first == pit.second);
  95. UNIT_ASSERT(pit.first != s.end());
  96. UNIT_ASSERT(*pit.first == 6);
  97. UNIT_ASSERT(pit.second != s.end());
  98. UNIT_ASSERT(*pit.second == 6);
  99. //Check const_iterator on mutable set
  100. scit = s.lower_bound(2);
  101. UNIT_ASSERT(scit != s.end());
  102. UNIT_ASSERT(*scit == 3);
  103. scit = s.upper_bound(5);
  104. UNIT_ASSERT(scit != s.end());
  105. UNIT_ASSERT(*scit == 6);
  106. pcit = s.equal_range(6);
  107. UNIT_ASSERT(pcit.first != pcit.second);
  108. UNIT_ASSERT(pcit.first != s.end());
  109. UNIT_ASSERT(*pcit.first == 6);
  110. UNIT_ASSERT(pcit.second != s.end());
  111. UNIT_ASSERT(*pcit.second == 7);
  112. //Check const_iterator on const set
  113. scit = crs.lower_bound(2);
  114. UNIT_ASSERT(scit != crs.end());
  115. UNIT_ASSERT(*scit == 3);
  116. scit = crs.upper_bound(5);
  117. UNIT_ASSERT(scit != crs.end());
  118. UNIT_ASSERT(*scit == 6);
  119. pcit = crs.equal_range(6);
  120. UNIT_ASSERT(pcit.first != pcit.second);
  121. UNIT_ASSERT(pcit.first != crs.end());
  122. UNIT_ASSERT(*pcit.first == 6);
  123. UNIT_ASSERT(pcit.second != crs.end());
  124. UNIT_ASSERT(*pcit.second == 7);
  125. }
  126. Y_UNIT_TEST(TestImplementationCheck) {
  127. TSet<int> tree;
  128. tree.insert(1);
  129. TSet<int>::iterator it = tree.begin();
  130. int const& int_ref = *it++;
  131. UNIT_ASSERT(int_ref == 1);
  132. UNIT_ASSERT(it == tree.end());
  133. UNIT_ASSERT(it != tree.begin());
  134. TSet<int>::const_iterator cit = tree.begin();
  135. int const& int_cref = *cit++;
  136. UNIT_ASSERT(int_cref == 1);
  137. }
  138. Y_UNIT_TEST(TestReverseIteratorTest) {
  139. TSet<int> tree;
  140. tree.insert(1);
  141. tree.insert(2);
  142. {
  143. TSet<int>::reverse_iterator rit(tree.rbegin());
  144. UNIT_ASSERT(*(rit++) == 2);
  145. UNIT_ASSERT(*(rit++) == 1);
  146. UNIT_ASSERT(rit == tree.rend());
  147. }
  148. {
  149. TSet<int> const& ctree = tree;
  150. TSet<int>::const_reverse_iterator rit(ctree.rbegin());
  151. UNIT_ASSERT(*(rit++) == 2);
  152. UNIT_ASSERT(*(rit++) == 1);
  153. UNIT_ASSERT(rit == ctree.rend());
  154. }
  155. }
  156. Y_UNIT_TEST(TestConstructorsAndAssignments) {
  157. {
  158. using container = TSet<int>;
  159. container c1;
  160. c1.insert(100);
  161. c1.insert(200);
  162. container c2(c1);
  163. UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
  164. UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
  165. UNIT_ASSERT(c1.contains(100));
  166. UNIT_ASSERT(c2.contains(200));
  167. container c3(std::move(c1));
  168. UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
  169. UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
  170. UNIT_ASSERT(c3.contains(100));
  171. c2.insert(300);
  172. c3 = c2;
  173. UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
  174. UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
  175. UNIT_ASSERT(c3.contains(300));
  176. c2.insert(400);
  177. c3 = std::move(c2);
  178. UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
  179. UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
  180. UNIT_ASSERT(c3.contains(400));
  181. }
  182. {
  183. using container = TMultiSet<int>;
  184. container c1;
  185. c1.insert(100);
  186. c1.insert(200);
  187. container c2(c1);
  188. UNIT_ASSERT_VALUES_EQUAL(2, c1.size());
  189. UNIT_ASSERT_VALUES_EQUAL(2, c2.size());
  190. UNIT_ASSERT(c1.find(100) != c1.end());
  191. UNIT_ASSERT(c2.find(200) != c2.end());
  192. container c3(std::move(c1));
  193. UNIT_ASSERT_VALUES_EQUAL(0, c1.size());
  194. UNIT_ASSERT_VALUES_EQUAL(2, c3.size());
  195. UNIT_ASSERT(c3.find(100) != c3.end());
  196. c2.insert(300);
  197. c3 = c2;
  198. UNIT_ASSERT_VALUES_EQUAL(3, c2.size());
  199. UNIT_ASSERT_VALUES_EQUAL(3, c3.size());
  200. UNIT_ASSERT(c3.find(300) != c3.end());
  201. c2.insert(400);
  202. c3 = std::move(c2);
  203. UNIT_ASSERT_VALUES_EQUAL(0, c2.size());
  204. UNIT_ASSERT_VALUES_EQUAL(4, c3.size());
  205. UNIT_ASSERT(c3.find(400) != c3.end());
  206. }
  207. }
  208. struct TKey {
  209. TKey()
  210. : m_data(0)
  211. {
  212. }
  213. explicit TKey(int data)
  214. : m_data(data)
  215. {
  216. }
  217. int m_data;
  218. };
  219. struct TKeyCmp {
  220. bool operator()(TKey lhs, TKey rhs) const {
  221. return lhs.m_data < rhs.m_data;
  222. }
  223. bool operator()(TKey lhs, int rhs) const {
  224. return lhs.m_data < rhs;
  225. }
  226. bool operator()(int lhs, TKey rhs) const {
  227. return lhs < rhs.m_data;
  228. }
  229. using is_transparent = void;
  230. };
  231. struct TKeyCmpPtr {
  232. bool operator()(TKey const volatile* lhs, TKey const volatile* rhs) const {
  233. return (*lhs).m_data < (*rhs).m_data;
  234. }
  235. bool operator()(TKey const volatile* lhs, int rhs) const {
  236. return (*lhs).m_data < rhs;
  237. }
  238. bool operator()(int lhs, TKey const volatile* rhs) const {
  239. return lhs < (*rhs).m_data;
  240. }
  241. using is_transparent = void;
  242. };
  243. Y_UNIT_TEST(TestTemplateMethods) {
  244. {
  245. using KeySet = TSet<TKey, TKeyCmp>;
  246. KeySet keySet;
  247. keySet.insert(TKey(1));
  248. keySet.insert(TKey(2));
  249. keySet.insert(TKey(3));
  250. keySet.insert(TKey(4));
  251. UNIT_ASSERT(keySet.count(TKey(1)) == 1);
  252. UNIT_ASSERT(keySet.count(1) == 1);
  253. UNIT_ASSERT(keySet.count(5) == 0);
  254. UNIT_ASSERT(keySet.find(2) != keySet.end());
  255. UNIT_ASSERT(keySet.lower_bound(2) != keySet.end());
  256. UNIT_ASSERT(keySet.upper_bound(2) != keySet.end());
  257. UNIT_ASSERT(keySet.equal_range(2) != std::make_pair(keySet.begin(), keySet.end()));
  258. KeySet const& ckeySet = keySet;
  259. UNIT_ASSERT(ckeySet.find(2) != ckeySet.end());
  260. UNIT_ASSERT(ckeySet.lower_bound(2) != ckeySet.end());
  261. UNIT_ASSERT(ckeySet.upper_bound(2) != ckeySet.end());
  262. UNIT_ASSERT(ckeySet.equal_range(2) != std::make_pair(ckeySet.begin(), ckeySet.end()));
  263. }
  264. {
  265. using KeySet = TSet<TKey*, TKeyCmpPtr>;
  266. KeySet keySet;
  267. TKey key1(1), key2(2), key3(3), key4(4);
  268. keySet.insert(&key1);
  269. keySet.insert(&key2);
  270. keySet.insert(&key3);
  271. keySet.insert(&key4);
  272. UNIT_ASSERT(keySet.count(1) == 1);
  273. UNIT_ASSERT(keySet.count(5) == 0);
  274. UNIT_ASSERT(keySet.find(2) != keySet.end());
  275. UNIT_ASSERT(keySet.lower_bound(2) != keySet.end());
  276. UNIT_ASSERT(keySet.upper_bound(2) != keySet.end());
  277. UNIT_ASSERT(keySet.equal_range(2) != std::make_pair(keySet.begin(), keySet.end()));
  278. KeySet const& ckeySet = keySet;
  279. UNIT_ASSERT(ckeySet.find(2) != ckeySet.end());
  280. UNIT_ASSERT(ckeySet.lower_bound(2) != ckeySet.end());
  281. UNIT_ASSERT(ckeySet.upper_bound(2) != ckeySet.end());
  282. UNIT_ASSERT(ckeySet.equal_range(2) != std::make_pair(ckeySet.begin(), ckeySet.end()));
  283. }
  284. {
  285. using KeySet = TMultiSet<TKey, TKeyCmp>;
  286. KeySet keySet;
  287. keySet.insert(TKey(1));
  288. keySet.insert(TKey(2));
  289. keySet.insert(TKey(3));
  290. keySet.insert(TKey(4));
  291. UNIT_ASSERT(keySet.count(TKey(1)) == 1);
  292. UNIT_ASSERT(keySet.count(1) == 1);
  293. UNIT_ASSERT(keySet.count(5) == 0);
  294. UNIT_ASSERT(keySet.find(2) != keySet.end());
  295. UNIT_ASSERT(keySet.lower_bound(2) != keySet.end());
  296. UNIT_ASSERT(keySet.upper_bound(2) != keySet.end());
  297. UNIT_ASSERT(keySet.equal_range(2) != std::make_pair(keySet.begin(), keySet.end()));
  298. KeySet const& ckeySet = keySet;
  299. UNIT_ASSERT(ckeySet.find(2) != ckeySet.end());
  300. UNIT_ASSERT(ckeySet.lower_bound(2) != ckeySet.end());
  301. UNIT_ASSERT(ckeySet.upper_bound(2) != ckeySet.end());
  302. UNIT_ASSERT(ckeySet.equal_range(2) != std::make_pair(ckeySet.begin(), ckeySet.end()));
  303. }
  304. {
  305. using KeySet = TMultiSet<TKey const volatile*, TKeyCmpPtr>;
  306. KeySet keySet;
  307. TKey key1(1), key2(2), key3(3), key4(4);
  308. keySet.insert(&key1);
  309. keySet.insert(&key2);
  310. keySet.insert(&key3);
  311. keySet.insert(&key4);
  312. UNIT_ASSERT(keySet.count(1) == 1);
  313. UNIT_ASSERT(keySet.count(5) == 0);
  314. UNIT_ASSERT(keySet.find(2) != keySet.end());
  315. UNIT_ASSERT(keySet.lower_bound(2) != keySet.end());
  316. UNIT_ASSERT(keySet.upper_bound(2) != keySet.end());
  317. UNIT_ASSERT(keySet.equal_range(2) != std::make_pair(keySet.begin(), keySet.end()));
  318. KeySet const& ckeySet = keySet;
  319. UNIT_ASSERT(ckeySet.find(2) != ckeySet.end());
  320. UNIT_ASSERT(ckeySet.lower_bound(2) != ckeySet.end());
  321. UNIT_ASSERT(ckeySet.upper_bound(2) != ckeySet.end());
  322. UNIT_ASSERT(ckeySet.equal_range(2) != std::make_pair(ckeySet.begin(), ckeySet.end()));
  323. }
  324. }
  325. }