]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/filesystem/operations/last_write_time.cc
1.cc: Remove 'test' variables.
[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{
fd5effb1
JW
38 using time_type = std::experimental::filesystem::file_time_type;
39
40 auto p = __gnu_test::nonexistent_path();
41 std::error_code ec;
42 time_type mtime = last_write_time(p, ec);
43 VERIFY( ec );
44 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
45#if __cpp_exceptions
46 bool caught = false;
47 try {
48 mtime = last_write_time(p);
49 } catch (std::system_error const& e) {
50 caught = true;
51 ec = e.code();
52 }
53 VERIFY( caught );
54 VERIFY( ec );
55 VERIFY( ec == std::make_error_code(std::errc::no_such_file_or_directory) );
56#endif
57
58 __gnu_test::scoped_file file(p);
59 VERIFY( exists(p) );
60 mtime = last_write_time(p, ec);
61 VERIFY( !ec );
62 VERIFY( mtime <= time_type::clock::now() );
63 VERIFY( mtime == last_write_time(p) );
64
65 auto end_of_time = time_type::duration::max();
66 auto last_second
67 = std::chrono::duration_cast<std::chrono::seconds>(end_of_time).count();
68 if (last_second > std::numeric_limits<std::time_t>::max())
69 return; // can't test overflow
70
71#if _GLIBCXX_USE_UTIMENSAT
72 struct ::timespec ts[2];
73 ts[0].tv_sec = 0;
74 ts[0].tv_nsec = UTIME_NOW;
75 ts[1].tv_sec = std::numeric_limits<std::time_t>::max() - 1;
76 ts[1].tv_nsec = 0;
77 VERIFY( !::utimensat(AT_FDCWD, p.c_str(), ts, 0) );
78#elif _GLIBCXX_HAVE_UTIME_H
79 ::utimbuf times;
80 times.modtime = std::numeric_limits<std::time_t>::max() - 1;
81 times.actime = std::numeric_limits<std::time_t>::max() - 1;
82 VERIFY( !::utime(p.c_str(), &times) );
83#else
84 return;
85#endif
86
87 mtime = last_write_time(p, ec);
88 VERIFY( ec );
89 VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
90 VERIFY( mtime == time_type::min() );
91
92#if __cpp_exceptions
93 caught = false;
94 try {
95 mtime = last_write_time(p);
96 } catch (std::system_error const& e) {
97 caught = true;
98 ec = e.code();
99 }
100 VERIFY( caught );
101 VERIFY( ec );
102 VERIFY( ec == std::make_error_code(std::errc::value_too_large) );
103#endif
104}
105
106int
107main()
108{
109 test01();
110}