summaryrefslogtreecommitdiffstats
path: root/libcxx/test/libcxx/experimental/filesystem/class.directory_entry/directory_entry.mods/last_write_time.sh.cpp
blob: 2305f54f5cb33a672ad98be885113965c6209085 (plain)
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
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03

// <experimental/filesystem>

// class directory_entry

// RUN: %build -I%libcxx_src_root/src/experimental/filesystem
// RUN: %run

#include "filesystem_include.hpp"
#include <type_traits>
#include <cassert>

#include "test_macros.h"
#include "rapid-cxx-test.hpp"
#include "filesystem_test_helper.hpp"

#include "filesystem_common.h"

using namespace fs::detail;

TEST_SUITE(directory_entry_mods_suite)

TEST_CASE(last_write_time_not_representable_error) {
  using namespace fs;
  using namespace std::chrono;
  scoped_test_env env;
  const path dir = env.create_dir("dir");
  const path file = env.create_file("dir/file", 42);

  TimeSpec ToTime;
  ToTime.tv_sec = std::numeric_limits<decltype(ToTime.tv_sec)>::max();
  ToTime.tv_nsec = duration_cast<nanoseconds>(seconds(1)).count() - 1;

  TimeStructArray TS;
  SetTimeStructTo(TS[0], ToTime);
  SetTimeStructTo(TS[1], ToTime);

  file_time_type old_time = last_write_time(file);
  directory_entry ent(file);

  file_time_type start_time = file_time_type::clock::now() - hours(1);
  last_write_time(file, start_time);

  TEST_CHECK(ent.last_write_time() == old_time);

  bool IsRepresentable = true;
  file_time_type rep_value;
  {
    std::error_code ec;
    if (SetFileTimes(file, TS, ec)) {
      TEST_REQUIRE(false && "unsupported");
    }
    ec.clear();
    rep_value = last_write_time(file, ec);
    IsRepresentable = !bool(ec);
  }

  if (!IsRepresentable) {
    std::error_code rec = GetTestEC();
    ent.refresh(rec);
    TEST_CHECK(!rec);

    const std::errc expected_err = std::errc::value_too_large;

    std::error_code ec = GetTestEC();
    TEST_CHECK(ent.last_write_time(ec) == file_time_type::min());
    TEST_CHECK(ErrorIs(ec, expected_err));

    ec = GetTestEC();
    TEST_CHECK(last_write_time(file, ec) == file_time_type::min());
    TEST_CHECK(ErrorIs(ec, expected_err));

    ExceptionChecker CheckExcept(file, expected_err,
                                 "directory_entry::last_write_time");
    TEST_CHECK_THROW_RESULT(fs::filesystem_error, CheckExcept,
                            ent.last_write_time());

  } else {
    ent.refresh();

    std::error_code ec = GetTestEC();
    TEST_CHECK(ent.last_write_time(ec) == rep_value);
    TEST_CHECK(!ec);
  }
}

TEST_SUITE_END()
OpenPOWER on IntegriCloud