| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-require-effective-target tzdb } |
| 3 | // { dg-require-effective-target cxx11_abi } |
| 4 | |
| 5 | #include <chrono> |
| 6 | #include <fstream> |
| 7 | #include <cstdio> |
| 8 | #include <testsuite_hooks.h> |
| 9 | |
| 10 | static bool override_used = false; |
| 11 | |
| 12 | namespace __gnu_cxx |
| 13 | { |
| 14 | const char* zoneinfo_dir_override() { |
| 15 | override_used = true; |
| 16 | return "./"; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | std::string tzdata_zi = R"( |
| 21 | # version test1 |
| 22 | # Rule NAME FROM TO TYPE IN ON AT SAVE LETTER/S |
| 23 | Rule Swiss 1941 1942 - May Mon>=1 1:00 1:00 S |
| 24 | Rule Swiss 1941 1942 - Oct Mon>=1 2:00 0 - |
| 25 | Rule EU 1977 1980 - Apr Sun>=1 1:00u 1:00 S |
| 26 | Rule EU 1977 only - Sep lastSun 1:00u 0 - |
| 27 | Rule EU 1978 only - Oct 1 1:00u 0 - |
| 28 | Rule EU 1979 1995 - Sep lastSun 1:00u 0 - |
| 29 | Rule EU 1981 max - Mar lastSun 1:00u 1:00 S |
| 30 | Rule EU 1996 max - Oct lastSun 1:00u 0 - |
| 31 | |
| 32 | # Zone NAME STDOFF RULES FORMAT [UNTIL] |
| 33 | Zone Europe/Zurich 0:34:08 - LMT 1853 Jul 16 |
| 34 | 0:29:45.50 - BMT 1894 Jun |
| 35 | 1:00 Swiss CE%sT 1981 |
| 36 | 1:00 EU CE%sT |
| 37 | |
| 38 | Link Europe/Zurich Europe/Vaduz |
| 39 | |
| 40 | )"; |
| 41 | |
| 42 | using namespace std::chrono; |
| 43 | |
| 44 | void |
| 45 | test_access() |
| 46 | { |
| 47 | tzdb_list& list = get_tzdb_list(); |
| 48 | tzdb_list::const_iterator first = list.begin(); |
| 49 | tzdb_list::const_iterator last = list.end(); |
| 50 | VERIFY( list.cbegin() == first ); |
| 51 | VERIFY( list.cend() == last ); |
| 52 | VERIFY( first != last ); |
| 53 | VERIFY( &*first == &get_tzdb() ); |
| 54 | VERIFY( &*first == &list.front() ); |
| 55 | VERIFY( std::next(first) == last ); |
| 56 | first++; |
| 57 | VERIFY( first == last ); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | test_reload() |
| 62 | { |
| 63 | tzdb_list& list = get_tzdb_list(); |
| 64 | tzdb_list::const_iterator test1 = list.begin(); |
| 65 | reload_tzdb(); |
| 66 | VERIFY( list.begin() == test1 ); |
| 67 | VERIFY( std::distance(list.begin(), list.end()) == 1 ); |
| 68 | |
| 69 | std::string new_tzdata_zi = tzdata_zi; |
| 70 | auto pos = new_tzdata_zi.find("test"); |
| 71 | new_tzdata_zi[pos + 4] = '2'; |
| 72 | std::ofstream("tzdata.zi") << new_tzdata_zi; |
| 73 | VERIFY( remote_version() == "test2" ); |
| 74 | |
| 75 | // List doesn't reload until requested to. |
| 76 | VERIFY( get_tzdb_list().begin() == test1 ); |
| 77 | VERIFY( &get_tzdb() == &*test1 ); |
| 78 | reload_tzdb(); |
| 79 | VERIFY( list.begin() != test1 ); |
| 80 | VERIFY( std::distance(list.begin(), list.end()) == 2 ); |
| 81 | VERIFY( test1 == std::next(list.begin()) ); |
| 82 | VERIFY( &get_tzdb() == &*list.begin() ); |
| 83 | VERIFY( list.begin()->version == "test2" ); |
| 84 | VERIFY( test1->version == "test1" ); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | test_erase() |
| 89 | { |
| 90 | tzdb_list& list = get_tzdb_list(); |
| 91 | const int count = std::distance(list.begin(), list.end()); |
| 92 | tzdb_list::const_iterator test2 = list.begin(); |
| 93 | |
| 94 | std::string new_tzdata_zi = tzdata_zi; |
| 95 | auto pos = new_tzdata_zi.find("test"); |
| 96 | new_tzdata_zi[pos + 4] = '3'; |
| 97 | std::ofstream("tzdata.zi") << new_tzdata_zi; |
| 98 | |
| 99 | reload_tzdb(); |
| 100 | VERIFY( std::distance(list.begin(), list.end()) == count + 1 ); |
| 101 | VERIFY( list.begin()->version == "test3" ); |
| 102 | list.erase_after(list.begin()); |
| 103 | VERIFY( std::distance(list.begin(), list.end()) == count ); |
| 104 | VERIFY( list.begin()->version == "test3" ); |
| 105 | VERIFY( std::next(list.begin())->version == "test1" ); |
| 106 | |
| 107 | // As a GCC extension, the erased node is not destroyed |
| 108 | // while there are iterators referring to it. |
| 109 | VERIFY( test2->version == "test2" ); |
| 110 | VERIFY( test2->leap_seconds == list.begin()->leap_seconds ); |
| 111 | // But the iterator points to an unlinked list node now: |
| 112 | VERIFY( std::next(test2) == tzdb_list::const_iterator() ); |
| 113 | } |
| 114 | |
| 115 | int main() |
| 116 | { |
| 117 | std::ofstream("leapseconds") << '\n'; |
| 118 | std::ofstream("tzdata.zi") << tzdata_zi; |
| 119 | |
| 120 | test_access(); |
| 121 | |
| 122 | if (override_used) |
| 123 | { |
| 124 | test_reload(); |
| 125 | test_erase(); |
| 126 | } |
| 127 | else |
| 128 | std::puts("__gnu_cxx::zoneinfo_dir_override() doesn't work on this target"); |
| 129 | } |