aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder/iterator_ut.cpp
blob: f774f528e2d27a340f3d68a70b494354f3077bea (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "dirut.h" 
#include "iterator.h" 
 
#include <library/cpp/testing/unittest/registar.h>
 
#include <util/system/fs.h> 
#include <util/system/file.h> 
#include <util/generic/hash.h> 
#include <util/generic/algorithm.h> 
#include <util/random/mersenne.h> 
 
static TString JoinWithNewline(const TVector<TString>& strings) {
    TStringStream ss;
    for (const auto& string : strings) {
        ss << string << "\n";
    }
    return ss.Str();
}

class TDirIteratorTest: public TTestBase { 
    UNIT_TEST_SUITE(TDirIteratorTest); 
    UNIT_TEST(TestIt) 
    UNIT_TEST(TestError) 
    UNIT_TEST(TestLocal) 
    UNIT_TEST(TestSkip)
    UNIT_TEST(TestSort) 
    UNIT_TEST_SUITE_END(); 
 
private: 
    class TDirHier { 
    public: 
        struct TPath { 
            TString Path;
            int Type; 
        }; 
 
        inline void AddFile(const TString& path) {
            Add(path, 0); 
        } 
 
        inline void AddDir(const TString& path) {
            Add(path, 1); 
        } 
 
        inline void Add(const TString& path, int type) {
            const TPath p = { 
                path, type}; 
 
            Add(p); 
        } 
 
        inline void Add(const TPath& path) { 
            switch (path.Type) { 
                case 0: 
                    TFile(path.Path, CreateAlways | RdWr); 
                    break; 
 
                case 1: 
                    MakeDirIfNotExist(path.Path.data());
                    break; 
 
                case 2: 
                    ythrow yexception() << "unknown path type";
            } 
 
            Paths_.push_back(path); 
            Srch_[path.Path] = path; 
        } 
 
        inline int Type(const TString& path) {
            THashMap<TString, TPath>::const_iterator it = Srch_.find(path);
 
            UNIT_ASSERT(it != Srch_.end()); 
 
            return it->second.Type; 
        } 
 
        inline bool Have(const TString& path, int type) {
            return Type(path) == type; 
        } 
 
        inline ~TDirHier() { 
            for (size_t i = 0; i < Paths_.size(); ++i) { 
                NFs::Remove(Paths_[Paths_.size() - i - 1].Path);
            }
        }

    private: 
        TVector<TPath> Paths_;
        THashMap<TString, TPath> Srch_;
    }; 
 
    inline void TestLocal() { 
        TString dirname("." LOCSLASH_S);
        TDirIterator d(dirname, FTS_NOCHDIR); 
        for (auto it = d.begin(); it != d.end(); ++it) {
        } 
    } 
 
    inline void TestIt() { 
        TDirHier hier; 
 
        const TString dir = "tmpdir";
        const TDirHier::TPath path = {dir, 1}; 
 
        hier.Add(path); 
 
        for (size_t i = 0; i < 10; ++i) { 
            const TString dir1 = dir + LOCSLASH_C + ToString(i);
            const TDirHier::TPath path1 = {dir1, 1}; 
 
            hier.Add(path1); 
 
            for (size_t j = 0; j < 10; ++j) { 
                const TString subdir2 = ToString(j);
                const TString dir2 = dir1 + LOCSLASH_C + subdir2;
                const TDirHier::TPath path2 = {dir2, 1}; 
 
                hier.Add(path2); 
 
                for (size_t k = 0; k < 3; ++k) { 
                    const TString file = dir2 + LOCSLASH_C + "file" + ToString(k);
                    const TDirHier::TPath fpath = {file, 0}; 
 
                    hier.Add(fpath); 
                } 
            } 
        } 
 
        TDirIterator d(dir); 
 
        for (auto it = d.begin(); it != d.end(); ++it) {
            UNIT_ASSERT(hier.Have(it->fts_path, it->fts_info != FTS_F)); 
        } 
    } 
 
    inline void TestSkip() {
        TDirHier hier;

        const TString dir = "tmpdir";
        const TDirHier::TPath path = {dir, 1};

        hier.Add(path);
        hier.AddDir(dir + LOCSLASH_C + "dir1");
        hier.AddDir(dir + LOCSLASH_C + "dir1" + LOCSLASH_C + "dir2");
        //
        // Without skip
        //
        {
            TDirIterator di(dir);

            UNIT_ASSERT(di.Next());
            UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir1");
            UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir2");
            UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir2");
            UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir1");
            UNIT_ASSERT(di.Next());
            UNIT_ASSERT_EQUAL(di.Next(), nullptr);
        }
        //
        // With skip
        //
        {
            TDirIterator di(dir);

            UNIT_ASSERT(di.Next());
            auto ent = di.Next();
            UNIT_ASSERT_EQUAL(TStringBuf(ent->fts_name), "dir1");
            di.Skip(ent);
            UNIT_ASSERT_EQUAL(TStringBuf(di.Next()->fts_name), "dir1");
            UNIT_ASSERT(di.Next());
            UNIT_ASSERT_EQUAL(di.Next(), nullptr);
        }
    }

    inline void TestSort() { 
        TDirHier dh; 
        const TString dir("tmpdir");
 
        //prepare fs 
        { 
            TMersenne<ui32> rnd; 
            const TString prefixes[] = {
                "a", "b", "xxx", "111", ""}; 
 
            dh.AddDir(dir); 
 
            for (size_t i = 0; i < 100; ++i) { 
                const TString fname = dir + LOCSLASH_C + prefixes[i % Y_ARRAY_SIZE(prefixes)] + ToString(rnd.GenRand());
 
                dh.AddFile(fname); 
            } 
        } 
 
        TVector<TString> fnames;
 
        { 
            TDirIterator d(dir, TDirIterator::TOptions().SetSortByName()); 
 
            for (auto it = d.begin(); it != d.end(); ++it) {
                if (it->fts_info == FTS_F) { 
                    fnames.push_back(it->fts_name); 
                } 
            } 
        } 
 
        TVector<TString> sorted(fnames);
        Sort(sorted.begin(), sorted.end()); 
 
        UNIT_ASSERT_VALUES_EQUAL(JoinWithNewline(fnames), JoinWithNewline(sorted)); 
    } 
 
    inline void TestError() { 
        try { 
            TDirIterator d("./notexistingfilename"); 
 
            UNIT_ASSERT(false); 
        } catch (const TDirIterator::TError&) { 
        } catch (...) { 
            UNIT_ASSERT(false); 
        } 
 
        UNIT_ASSERT(true); 
    } 
}; 
 
UNIT_TEST_SUITE_REGISTRATION(TDirIteratorTest);