[libcxx-commits] [libcxx] [libc++][chrono] Loads tzdata.zi in tzdb. (PR #74928)
Mark de Wever via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Feb 11 05:01:18 PST 2024
================
@@ -0,0 +1,571 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: no-filesystem, no-localization, no-tzdb
+
+// XFAIL: libcpp-has-no-incomplete-tzdb
+// XFAIL: availability-tzdb-missing
+
+// <chrono>
+
+// Tests the IANA database rules parsing and operations.
+// This is not part of the public tzdb interface.
+
+// TODO TZDB Enable the disabled parts of the tests once we can test them using the public interface.
+
+#include <chrono>
+#include <fstream>
+#include <string>
+#include <string_view>
+#include <variant>
+
+#include "assert_macros.h"
+#include "concat_macros.h"
+#include "filesystem_test_helper.h"
+#include "test_tzdb.h"
+
+scoped_test_env env;
+[[maybe_unused]] const std::filesystem::path dir = env.create_dir("zoneinfo");
+const std::filesystem::path file = env.create_file("zoneinfo/tzdata.zi");
+
+std::string_view std::chrono::__libcpp_tzdb_directory() {
+ static std::string result = dir.string();
+ return result;
+}
+
+void write(std::string_view input) {
+ static int version = 0;
+
+ std::ofstream f{file};
+ f << "# version " << version++ << '\n';
+ f.write(input.data(), input.size());
+}
+#if 0
+static const std::chrono::tzdb& parse(std::string_view input) {
+ write(input);
+ return std::chrono::reload_tzdb();
+}
+#endif
+static void test_exception(std::string_view input, [[maybe_unused]] std::string_view what) {
+ write(input);
+
+ TEST_VALIDATE_EXCEPTION(
+ std::runtime_error,
+ [&]([[maybe_unused]] const std::runtime_error& e) {
+ TEST_LIBCPP_REQUIRE(
+ e.what() == what,
+ TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
+ },
+ TEST_IGNORE_NODISCARD std::chrono::reload_tzdb());
+}
+
+static void test_invalid() {
+ test_exception("R", "corrupt tzdb: expected whitespace");
+
+ test_exception("R ", "corrupt tzdb: expected a string");
+
+ test_exception("R r", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r x", "corrupt tzdb: expected a digit");
+ test_exception("R r +", "corrupt tzdb: expected a digit");
+ test_exception("R r mx", "corrupt tzdb year: expected 'min' or 'max'");
+ test_exception("R r -32768", "corrupt tzdb year: year is less than the minimum");
+
+ test_exception("R r mix", "corrupt tzdb: expected whitespace");
+ test_exception("R r 0", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r 0 x", "corrupt tzdb: expected a digit");
+ test_exception("R r 0 +", "corrupt tzdb: expected a digit");
+ test_exception("R r 0 mx", "corrupt tzdb year: expected 'min' or 'max'");
+
+ test_exception("R r 0 mix", "corrupt tzdb: expected whitespace");
+ test_exception("R r 0 1", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r 0 1 X", "corrupt tzdb: expected character '-'");
+
+ test_exception("R r 0 1 -", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r 0 1 - j", "corrupt tzdb month: invalid name");
+
+ test_exception("R r 0 1 - Ja", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r 0 1 - Ja +", "corrupt tzdb weekday: invalid name");
+ test_exception("R r 0 1 - Ja 32", "corrupt tzdb day: value too large");
+ test_exception("R r 0 1 - Ja l", "corrupt tzdb: expected string 'last'");
+ test_exception("R r 0 1 - Ja last", "corrupt tzdb weekday: invalid name");
+ test_exception("R r 0 1 - Ja lastS", "corrupt tzdb weekday: invalid name");
+ test_exception("R r 0 1 - Ja S", "corrupt tzdb weekday: invalid name");
+ test_exception("R r 0 1 - Ja Su", "corrupt tzdb on: expected '>=' or '<='");
+ test_exception("R r 0 1 - Ja Su>", "corrupt tzdb: expected character '='");
+ test_exception("R r 0 1 - Ja Su<", "corrupt tzdb: expected character '='");
+ test_exception("R r 0 1 - Ja Su>=+", "corrupt tzdb: expected a non-zero digit");
+ test_exception("R r 0 1 - Ja Su>=0", "corrupt tzdb: expected a non-zero digit");
+ test_exception("R r 0 1 - Ja Su>=32", "corrupt tzdb day: value too large");
+
+ test_exception("R r 0 1 - Ja Su>=31", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r 0 1 - Ja Su>=31 ", "corrupt tzdb: expected a digit");
+ test_exception("R r 0 1 - Ja Su>=31 +", "corrupt tzdb: expected a digit");
+
+ test_exception("R r 0 1 - Ja Su>=31 1", "corrupt tzdb: expected whitespace");
+ test_exception("R r 0 1 - Ja Su>=31 1a", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r 0 1 - Ja Su>=31 1w 2", "corrupt tzdb: expected whitespace");
+ test_exception("R r 0 1 - Ja Su>=31 1w 2a", "corrupt tzdb: expected whitespace");
+
+ test_exception("R r 0 1 - Ja Su>=31 1w 2s", "corrupt tzdb: expected whitespace");
+ test_exception("R r 0 1 - Ja Su>=31 1w 2s ", "corrupt tzdb: expected a string");
+}
+
+#if 0
----------------
mordante wrote:
I was not aware we did this. This indeed works as wanted. I did the same for the `zones` test. (The `links` test was not partly commented out since it only uses public interfaces.)
https://github.com/llvm/llvm-project/pull/74928
More information about the libcxx-commits
mailing list