]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/filesystem/operations/last_write_time.cc
Enable ARMv8-M atomic and synchronization support for ARMv8-M Baseline
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / last_write_time.cc
CommitLineData
fd5effb1
JW
1// Copyright (C) 2016 Free Software Foundation, Inc.
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-options "-lstdc++fs" }
19// { dg-do run { target c++11 } }
20// { dg-require-filesystem-ts "" }
21
22// 15.25 Permissions [fs.op.last_write_time]
23
24#include <experimental/filesystem>
25#include <testsuite_fs.h>
26#include <testsuite_hooks.h>
27
28#ifdef _GLIBCXX_HAVE_FCNTL_H
29# include <fcntl.h>
30#endif
31#if _GLIBCXX_HAVE_UTIME_H
32# include <utime.h>
33#endif
34
35void
36test01()
37{
7195dfe9
JW
38 // read times
39
fd5effb1
JW
40 using time_type = std::experimental::filesystem::file_time_type;
41
42 auto p = __gnu_test::nonexistent_path();
43 std::error_code ec;
44 time_type mtime = last_write_time(p, ec);
45 VERIFY( ec );
46 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
47#if __cpp_exceptions
48 bool caught = false;
49 try {
50 mtime = last_write_time(p);
51 } catch (std::system_error const& e) {
52 caught = true;
53 ec = e.code();
54 }
55 VERIFY( caught );
56 VERIFY( ec );
57 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
58#endif
59
60 __gnu_test::scoped_file file(p);
61 VERIFY( exists(p) );
62 mtime = last_write_time(p, ec);
63 VERIFY( !ec );
64 VERIFY( mtime <= time_type::clock::now() );
65 VERIFY( mtime == last_write_time(p) );
66
67 auto end_of_time = time_type::duration::max();
68 auto last_second
69 = std::chrono::duration_cast<std::chrono::seconds>(end_of_time).count();
70 if (last_second > std::numeric_limits<std::time_t>::max())
71 return; // can't test overflow
72
73#if _GLIBCXX_USE_UTIMENSAT
74 struct ::timespec ts[2];
75 ts[0].tv_sec = 0;
76 ts[0].tv_nsec = UTIME_NOW;
77 ts[1].tv_sec = std::numeric_limits<std::time_t>::max() - 1;
78 ts[1].tv_nsec = 0;
79 VERIFY( !::utimensat(AT_FDCWD, p.c_str(), ts, 0) );
80#elif _GLIBCXX_HAVE_UTIME_H
81 ::utimbuf times;
82 times.modtime = std::numeric_limits<std::time_t>::max() - 1;
83 times.actime = std::numeric_limits<std::time_t>::max() - 1;
84 VERIFY( !::utime(p.c_str(), &times) );
85#else
86 return;
87#endif
88
89 mtime = last_write_time(p, ec);
90 VERIFY( ec );
91 VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
92 VERIFY( mtime == time_type::min() );
93
94#if __cpp_exceptions
95 caught = false;
96 try {
97 mtime = last_write_time(p);
98 } catch (std::system_error const& e) {
99 caught = true;
100 ec = e.code();
101 }
102 VERIFY( caught );
103 VERIFY( ec );
104 VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
105#endif
106}
107
7195dfe9
JW
108void
109test02()
110{
111 // write times
112
113 using time_type = std::experimental::filesystem::file_time_type;
114
115 __gnu_test::scoped_file f;
116 std::error_code ec;
117 time_type time;
118
119 time = last_write_time(f.path);
120 last_write_time(f.path, time, ec);
121 VERIFY( !ec );
122 VERIFY( last_write_time(f.path) == time );
123
124 time -= std::chrono::milliseconds(1000 * 60 * 10 + 15);
125 last_write_time(f.path, time, ec);
126 VERIFY( !ec );
127 VERIFY( last_write_time(f.path) == time );
128
129 time += std::chrono::milliseconds(1000 * 60 * 20 + 15);
130 last_write_time(f.path, time, ec);
131 VERIFY( !ec );
132 VERIFY( last_write_time(f.path) == time );
133
134 time = time_type();
135 last_write_time(f.path, time, ec);
136 VERIFY( !ec );
137 VERIFY( last_write_time(f.path) == time );
138
139 time -= std::chrono::milliseconds(1000 * 60 * 10 + 15);
140 last_write_time(f.path, time, ec);
141 VERIFY( !ec );
142 VERIFY( last_write_time(f.path) == time );
143}
144
fd5effb1
JW
145int
146main()
147{
148 test01();
7195dfe9 149 test02();
fd5effb1 150}