is_in_ut.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "algorithm.h"
  3. #include "hash.h"
  4. #include "hash_multi_map.h"
  5. #include "hash_set.h"
  6. #include "is_in.h"
  7. #include "map.h"
  8. #include "set.h"
  9. #include "strbuf.h"
  10. #include "string.h"
  11. Y_UNIT_TEST_SUITE(TIsIn) {
  12. template <class TCont, class T>
  13. void TestIsInWithCont(const T& elem) {
  14. class TMapMock: public TCont {
  15. public:
  16. typename TCont::const_iterator find(const typename TCont::key_type& k) const {
  17. ++FindCalled;
  18. return TCont::find(k);
  19. }
  20. typename TCont::iterator find(const typename TCont::key_type& k) {
  21. ++FindCalled;
  22. return TCont::find(k);
  23. }
  24. mutable size_t FindCalled = 1;
  25. };
  26. TMapMock m;
  27. m.insert(elem);
  28. // use more effective find method
  29. UNIT_ASSERT(IsIn(m, "found"));
  30. UNIT_ASSERT(m.FindCalled);
  31. m.FindCalled = 0;
  32. UNIT_ASSERT(!IsIn(m, "not found"));
  33. UNIT_ASSERT(m.FindCalled);
  34. m.FindCalled = 0;
  35. }
  36. Y_UNIT_TEST(IsInTest) {
  37. TestIsInWithCont<TMap<TString, TString>>(std::make_pair("found", "1"));
  38. TestIsInWithCont<TMultiMap<TString, TString>>(std::make_pair("found", "1"));
  39. TestIsInWithCont<THashMap<TString, TString>>(std::make_pair("found", "1"));
  40. TestIsInWithCont<THashMultiMap<TString, TString>>(std::make_pair("found", "1"));
  41. TestIsInWithCont<TSet<TString>>("found");
  42. TestIsInWithCont<TMultiSet<TString>>("found");
  43. TestIsInWithCont<THashSet<TString>>("found");
  44. TestIsInWithCont<THashMultiSet<TString>>("found");
  45. // vector also compiles and works
  46. TVector<TString> v;
  47. v.push_back("found");
  48. UNIT_ASSERT(IsIn(v, "found"));
  49. UNIT_ASSERT(!IsIn(v, "not found"));
  50. // iterators interface
  51. UNIT_ASSERT(IsIn(v.begin(), v.end(), "found"));
  52. UNIT_ASSERT(!IsIn(v.begin(), v.end(), "not found"));
  53. // Works with TString (it has find, but find is not used)
  54. TString s = "found";
  55. UNIT_ASSERT(IsIn(s, 'f'));
  56. UNIT_ASSERT(!IsIn(s, 'z'));
  57. TStringBuf b = "found";
  58. UNIT_ASSERT(IsIn(b, 'f'));
  59. UNIT_ASSERT(!IsIn(b, 'z'));
  60. }
  61. Y_UNIT_TEST(IsInInitListTest) {
  62. const char* abc = "abc";
  63. const char* def = "def";
  64. UNIT_ASSERT(IsIn({6, 2, 12}, 6));
  65. UNIT_ASSERT(IsIn({6, 2, 12}, 2));
  66. UNIT_ASSERT(!IsIn({6, 2, 12}, 7));
  67. UNIT_ASSERT(IsIn({6}, 6));
  68. UNIT_ASSERT(!IsIn({6}, 7));
  69. UNIT_ASSERT(!IsIn(std::initializer_list<int>(), 6));
  70. UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("abc")));
  71. UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("def")));
  72. UNIT_ASSERT(IsIn({"abc", "def"}, TStringBuf("def")));
  73. UNIT_ASSERT(IsIn({abc, def}, def)); // direct pointer comparison
  74. UNIT_ASSERT(!IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("ghi")));
  75. UNIT_ASSERT(!IsIn({"abc", "def"}, TStringBuf("ghi")));
  76. UNIT_ASSERT(!IsIn({"abc", "def"}, TString("ghi")));
  77. const TStringBuf str = "abc////";
  78. UNIT_ASSERT(IsIn({"abc", "def"}, TStringBuf{str.data(), 3}));
  79. }
  80. Y_UNIT_TEST(ConfOfTest) {
  81. UNIT_ASSERT(IsIn({1, 2, 3}, 1));
  82. UNIT_ASSERT(!IsIn({1, 2, 3}, 4));
  83. const TString b = "b";
  84. UNIT_ASSERT(!IsIn({"a", "b", "c"}, b.data())); // compares pointers by value. Whether it's good or not.
  85. UNIT_ASSERT(IsIn(TVector<TStringBuf>({"a", "b", "c"}), b.data()));
  86. UNIT_ASSERT(IsIn(TVector<TStringBuf>({"a", "b", "c"}), "b"));
  87. }
  88. Y_UNIT_TEST(IsInArrayTest) {
  89. const TString array[] = {"a", "b", "d"};
  90. UNIT_ASSERT(IsIn(array, "a"));
  91. UNIT_ASSERT(IsIn(array, TString("b")));
  92. UNIT_ASSERT(!IsIn(array, "c"));
  93. UNIT_ASSERT(IsIn(array, TStringBuf("d")));
  94. }
  95. } // Y_UNIT_TEST_SUITE(TIsIn)