diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/folder/iterator_ut.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/folder/iterator_ut.cpp')
-rw-r--r-- | util/folder/iterator_ut.cpp | 298 |
1 files changed, 149 insertions, 149 deletions
diff --git a/util/folder/iterator_ut.cpp b/util/folder/iterator_ut.cpp index 936becd139..f774f528e2 100644 --- a/util/folder/iterator_ut.cpp +++ b/util/folder/iterator_ut.cpp @@ -1,14 +1,14 @@ -#include "dirut.h" -#include "iterator.h" - +#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> - + +#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) { @@ -17,123 +17,123 @@ static TString JoinWithNewline(const TVector<TString>& strings) { return ss.Str(); } -class TDirIteratorTest: public TTestBase { - UNIT_TEST_SUITE(TDirIteratorTest); - UNIT_TEST(TestIt) - UNIT_TEST(TestError) - UNIT_TEST(TestLocal) +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 { + UNIT_TEST(TestSort) + UNIT_TEST_SUITE_END(); + +private: + class TDirHier { + public: + struct TPath { TString Path; - int Type; - }; - + int Type; + }; + inline void AddFile(const TString& path) { - Add(path, 0); - } - + Add(path, 0); + } + inline void AddDir(const TString& path) { - Add(path, 1); - } - + 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: + 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: + break; + + case 2: ythrow yexception() << "unknown path type"; - } - - Paths_.push_back(path); - Srch_[path.Path] = path; - } - + } + + 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; - } - + + 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) { + return Type(path) == type; + } + + inline ~TDirHier() { + for (size_t i = 0; i < Paths_.size(); ++i) { NFs::Remove(Paths_[Paths_.size() - i - 1].Path); } } - private: + private: TVector<TPath> Paths_; THashMap<TString, TPath> Srch_; - }; - - inline void TestLocal() { + }; + + inline void TestLocal() { TString dirname("." LOCSLASH_S); - TDirIterator d(dirname, FTS_NOCHDIR); + TDirIterator d(dirname, FTS_NOCHDIR); for (auto it = d.begin(); it != d.end(); ++it) { - } - } - - inline void TestIt() { - TDirHier hier; - + } + } + + 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 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 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 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); - + 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)); - } - } - + UNIT_ASSERT(hier.Have(it->fts_path, it->fts_info != FTS_F)); + } + } + inline void TestSkip() { TDirHier hier; @@ -173,55 +173,55 @@ private: } } - inline void TestSort() { - TDirHier dh; + inline void TestSort() { + TDirHier dh; const TString dir("tmpdir"); - - //prepare fs - { - TMersenne<ui32> rnd; + + //prepare fs + { + TMersenne<ui32> rnd; const TString prefixes[] = { - "a", "b", "xxx", "111", ""}; - - dh.AddDir(dir); - - for (size_t i = 0; i < 100; ++i) { + "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); - } - } - + + dh.AddFile(fname); + } + } + TVector<TString> fnames; - - { - TDirIterator d(dir, TDirIterator::TOptions().SetSortByName()); - + + { + 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); - } - } - } - + 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); + 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); |