xref: /aosp_15_r20/external/cronet/third_party/libc++/src/test/libcxx/time/time.zone/time.zone.db/links.pass.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-filesystem, no-localization, no-tzdb
11 
12 // XFAIL: libcpp-has-no-incomplete-tzdb
13 // XFAIL: availability-tzdb-missing
14 
15 // <chrono>
16 
17 // Tests the IANA database rules links and operations.
18 // This is not part of the public tzdb interface.
19 
20 #include <chrono>
21 
22 #include <cassert>
23 #include <chrono>
24 #include <fstream>
25 #include <string>
26 #include <string_view>
27 
28 #include "assert_macros.h"
29 #include "concat_macros.h"
30 #include "filesystem_test_helper.h"
31 #include "test_tzdb.h"
32 
33 scoped_test_env env;
34 [[maybe_unused]] const std::filesystem::path dir = env.create_dir("zoneinfo");
35 const std::filesystem::path file                 = env.create_file("zoneinfo/tzdata.zi");
36 
__libcpp_tzdb_directory()37 std::string_view std::chrono::__libcpp_tzdb_directory() {
38   static std::string result = dir.string();
39   return result;
40 }
41 
write(std::string_view input)42 void write(std::string_view input) {
43   static int version = 0;
44 
45   std::ofstream f{file};
46   f << "# version " << version++ << '\n';
47   f.write(input.data(), input.size());
48 }
49 
parse(std::string_view input)50 static const std::chrono::tzdb& parse(std::string_view input) {
51   write(input);
52   return std::chrono::reload_tzdb();
53 }
54 
test_exception(std::string_view input,std::string_view what)55 static void test_exception(std::string_view input, [[maybe_unused]] std::string_view what) {
56   write(input);
57 
58   TEST_VALIDATE_EXCEPTION(
59       std::runtime_error,
60       [&]([[maybe_unused]] const std::runtime_error& e) {
61         TEST_LIBCPP_REQUIRE(
62             e.what() == what,
63             TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception   ", e.what(), '\n'));
64       },
65       TEST_IGNORE_NODISCARD std::chrono::reload_tzdb());
66 }
67 
test_invalid()68 static void test_invalid() {
69   test_exception("L", "corrupt tzdb: expected whitespace");
70 
71   test_exception("L ", "corrupt tzdb: expected a string");
72 
73   test_exception("L n", "corrupt tzdb: expected whitespace");
74 
75   test_exception("L n ", "corrupt tzdb: expected a string");
76 }
77 
test_link()78 static void test_link() {
79   const std::chrono::tzdb& result = parse(
80       R"(
81 L z d
82 l b a
83 lInK b b
84 )");
85   assert(result.links.size() == 3);
86 
87   assert(result.links[0].name() == "a");
88   assert(result.links[0].target() == "b");
89 
90   assert(result.links[1].name() == "b");
91   assert(result.links[1].target() == "b");
92 
93   assert(result.links[2].name() == "d");
94   assert(result.links[2].target() == "z");
95 }
96 
main(int,const char **)97 int main(int, const char**) {
98   test_invalid();
99   test_link();
100 
101   return 0;
102 }
103