Home | History | Annotate | Download | only in class.directory_entry
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03
     11 
     12 // <experimental/filesystem>
     13 
     14 // class directory_entry
     15 
     16 // directory_entry& operator=(directory_entry const&) = default;
     17 // directory_entry& operator=(directory_entry&&) noexcept = default;
     18 // void assign(path const&);
     19 // void replace_filename(path const&);
     20 
     21 #include <experimental/filesystem>
     22 #include <type_traits>
     23 #include <cassert>
     24 
     25 namespace fs = std::experimental::filesystem;
     26 
     27 void test_copy_assign_operator()
     28 {
     29   using namespace fs;
     30   // Copy
     31   {
     32     static_assert(std::is_copy_assignable<directory_entry>::value,
     33                   "directory_entry must be copy assignable");
     34     static_assert(!std::is_nothrow_copy_assignable<directory_entry>::value,
     35                   "directory_entry's copy assignment cannot be noexcept");
     36     const path p("foo/bar/baz");
     37     const path p2("abc");
     38     const directory_entry e(p);
     39     directory_entry e2;
     40     assert(e.path() == p && e2.path() == path());
     41     e2 = e;
     42     assert(e.path() == p && e2.path() == p);
     43     directory_entry e3(p2);
     44     e2 = e3;
     45     assert(e2.path() == p2 && e3.path() == p2);
     46   }
     47 }
     48 
     49 
     50 void test_move_assign_operator()
     51 {
     52   using namespace fs;
     53   // Copy
     54   {
     55     static_assert(std::is_nothrow_move_assignable<directory_entry>::value,
     56                   "directory_entry is noexcept move assignable");
     57     const path p("foo/bar/baz");
     58     const path p2("abc");
     59     directory_entry e(p);
     60     directory_entry e2(p2);
     61     assert(e.path() == p && e2.path() == p2);
     62     e2 = std::move(e);
     63     assert(e2.path() == p);
     64     assert(e.path() != p); // testing moved from state
     65   }
     66 }
     67 
     68 void test_path_assign_method()
     69 {
     70   using namespace fs;
     71   const path p("foo/bar/baz");
     72   const path p2("abc");
     73   directory_entry e(p);
     74   {
     75     static_assert(std::is_same<decltype(e.assign(p)), void>::value,
     76                   "return type should be void");
     77     static_assert(noexcept(e.assign(p)) == false, "operation must not be noexcept");
     78   }
     79   {
     80     assert(e.path() == p);
     81     e.assign(p2);
     82     assert(e.path() == p2 && e.path() != p);
     83     e.assign(p);
     84     assert(e.path() == p && e.path() != p2);
     85   }
     86 }
     87 
     88 void test_replace_filename_method()
     89 {
     90   using namespace fs;
     91   const path p("/path/to/foo.exe");
     92   const path replace("bar.out");
     93   const path expect("/path/to/bar.out");
     94   directory_entry e(p);
     95   {
     96     static_assert(noexcept(e.replace_filename(replace)) == false,
     97                   "operation cannot be noexcept");
     98     static_assert(std::is_same<decltype(e.replace_filename(replace)), void>::value,
     99                   "operation must return void");
    100   }
    101   {
    102     assert(e.path() == p);
    103     e.replace_filename(replace);
    104     assert(e.path() == expect);
    105   }
    106 }
    107 
    108 int main() {
    109   test_copy_assign_operator();
    110   test_move_assign_operator();
    111   test_path_assign_method();
    112   test_replace_filename_method();
    113 }
    114