aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/is_in_ut.cpp
blob: 49c14bff12c65fb09c6d8ff1f055ca39425f6dcb (plain) (blame)
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
#include <library/cpp/testing/unittest/registar.h>
 
#include "algorithm.h"
#include "hash.h"
#include "hash_set.h"
#include "is_in.h"
#include "map.h"
#include "set.h"
#include "strbuf.h"
#include "string.h"
 
Y_UNIT_TEST_SUITE(TIsIn) {
    template <class TCont, class T>
    void TestIsInWithCont(const T& elem) { 
        class TMapMock: public TCont {
        public: 
            typename TCont::const_iterator find(const typename TCont::key_type& k) const { 
                ++FindCalled; 
                return TCont::find(k); 
            } 
 
            typename TCont::iterator find(const typename TCont::key_type& k) { 
                ++FindCalled; 
                return TCont::find(k); 
            } 
 
            mutable size_t FindCalled = 1; 
        }; 
 
        TMapMock m; 
        m.insert(elem); 
 
        // use more effective find method 
        UNIT_ASSERT(IsIn(m, "found")); 
        UNIT_ASSERT(m.FindCalled); 
        m.FindCalled = 0; 
 
        UNIT_ASSERT(!IsIn(m, "not found")); 
        UNIT_ASSERT(m.FindCalled); 
        m.FindCalled = 0; 
    } 
 
    Y_UNIT_TEST(IsInTest) {
        TestIsInWithCont<TMap<TString, TString>>(std::make_pair("found", "1"));
        TestIsInWithCont<TMultiMap<TString, TString>>(std::make_pair("found", "1"));
        TestIsInWithCont<THashMap<TString, TString>>(std::make_pair("found", "1"));
        TestIsInWithCont<THashMultiMap<TString, TString>>(std::make_pair("found", "1"));
 
        TestIsInWithCont<TSet<TString>>("found");
        TestIsInWithCont<TMultiSet<TString>>("found");
        TestIsInWithCont<THashSet<TString>>("found");
        TestIsInWithCont<THashMultiSet<TString>>("found");
 
        // vector also compiles and works 
        TVector<TString> v;
        v.push_back("found"); 
        UNIT_ASSERT(IsIn(v, "found")); 
        UNIT_ASSERT(!IsIn(v, "not found")); 
 
        // iterators interface 
        UNIT_ASSERT(IsIn(v.begin(), v.end(), "found")); 
        UNIT_ASSERT(!IsIn(v.begin(), v.end(), "not found")); 
 
        // Works with TString (it has find, but find is not used)
        TString s = "found";
        UNIT_ASSERT(IsIn(s, 'f')); 
        UNIT_ASSERT(!IsIn(s, 'z')); 
 
        TStringBuf b = "found"; 
        UNIT_ASSERT(IsIn(b, 'f')); 
        UNIT_ASSERT(!IsIn(b, 'z')); 
    } 

    Y_UNIT_TEST(IsInInitListTest) {
        const char* abc = "abc";
        const char* def = "def";

        UNIT_ASSERT(IsIn({6, 2, 12}, 6));
        UNIT_ASSERT(IsIn({6, 2, 12}, 2));
        UNIT_ASSERT(!IsIn({6, 2, 12}, 7));
        UNIT_ASSERT(IsIn({6}, 6));
        UNIT_ASSERT(!IsIn({6}, 7));
        UNIT_ASSERT(!IsIn(std::initializer_list<int>(), 6));
        UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("abc")));
        UNIT_ASSERT(IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("def")));
        UNIT_ASSERT(IsIn({"abc", "def"}, TStringBuf("def")));
        UNIT_ASSERT(IsIn({abc, def}, def)); // direct pointer comparison
        UNIT_ASSERT(!IsIn({TStringBuf("abc"), TStringBuf("def")}, TStringBuf("ghi")));
        UNIT_ASSERT(!IsIn({"abc", "def"}, TStringBuf("ghi")));
        UNIT_ASSERT(!IsIn({"abc", "def"}, TString("ghi")));

        const TStringBuf str = "abc////";

        UNIT_ASSERT(IsIn({"abc", "def"}, TStringBuf{str.data(), 3}));
    }

    Y_UNIT_TEST(ConfOfTest) {
        UNIT_ASSERT(IsIn({1, 2, 3}, 1));
        UNIT_ASSERT(!IsIn({1, 2, 3}, 4));

        const TString b = "b";

        UNIT_ASSERT(!IsIn({"a", "b", "c"}, b.data())); // compares pointers by value. Whether it's good or not.
        UNIT_ASSERT(IsIn(TVector<TStringBuf>({"a", "b", "c"}), b.data()));
        UNIT_ASSERT(IsIn(TVector<TStringBuf>({"a", "b", "c"}), "b"));
    }

    Y_UNIT_TEST(IsInArrayTest) {
        const TString array[] = {"a", "b", "d"};

        UNIT_ASSERT(IsIn(array, "a"));
        UNIT_ASSERT(IsIn(array, TString("b")));
        UNIT_ASSERT(!IsIn(array, "c"));
        UNIT_ASSERT(IsIn(array, TStringBuf("d")));
    }
}