test_util.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/time/internal/test_util.h"
00016 
00017 #include <algorithm>
00018 #include <cstddef>
00019 #include <cstring>
00020 
00021 #include "absl/base/internal/raw_logging.h"
00022 #include "absl/time/internal/cctz/include/cctz/zone_info_source.h"
00023 
00024 namespace cctz = absl::time_internal::cctz;
00025 
00026 namespace absl {
00027 namespace time_internal {
00028 
00029 TimeZone LoadTimeZone(const std::string& name) {
00030   TimeZone tz;
00031   ABSL_RAW_CHECK(LoadTimeZone(name, &tz), name.c_str());
00032   return tz;
00033 }
00034 
00035 }  // namespace time_internal
00036 }  // namespace absl
00037 
00038 namespace absl {
00039 namespace time_internal {
00040 namespace cctz_extension {
00041 namespace {
00042 
00043 // Embed the zoneinfo data for time zones used during tests and benchmarks.
00044 // The data was generated using "xxd -i zoneinfo-file".  There is no need
00045 // to update the data as long as the tests do not depend on recent changes
00046 // (and the past rules remain the same).
00047 #include "absl/time/internal/zoneinfo.inc"
00048 
00049 const struct ZoneInfo {
00050   const char* name;
00051   const char* data;
00052   std::size_t length;
00053 } kZoneInfo[] = {
00054     // The three real time zones used by :time_test and :time_benchmark.
00055     {"America/Los_Angeles",  //
00056      reinterpret_cast<char*>(America_Los_Angeles), America_Los_Angeles_len},
00057     {"America/New_York",  //
00058      reinterpret_cast<char*>(America_New_York), America_New_York_len},
00059     {"Australia/Sydney",  //
00060      reinterpret_cast<char*>(Australia_Sydney), Australia_Sydney_len},
00061 
00062     // Other zones named in tests but which should fail to load.
00063     {"Invalid/TimeZone", nullptr, 0},
00064     {"", nullptr, 0},
00065 
00066     // Also allow for loading the local time zone under TZ=US/Pacific.
00067     {"US/Pacific",  //
00068      reinterpret_cast<char*>(America_Los_Angeles), America_Los_Angeles_len},
00069 
00070     // Allows use of the local time zone from a system-specific location.
00071 #ifdef _MSC_VER
00072     {"localtime",  //
00073      reinterpret_cast<char*>(America_Los_Angeles), America_Los_Angeles_len},
00074 #else
00075     {"/etc/localtime",  //
00076      reinterpret_cast<char*>(America_Los_Angeles), America_Los_Angeles_len},
00077 #endif
00078 };
00079 
00080 class TestZoneInfoSource : public cctz::ZoneInfoSource {
00081  public:
00082   TestZoneInfoSource(const char* data, std::size_t size)
00083       : data_(data), end_(data + size) {}
00084 
00085   std::size_t Read(void* ptr, std::size_t size) override {
00086     const std::size_t len = std::min<std::size_t>(size, end_ - data_);
00087     memcpy(ptr, data_, len);
00088     data_ += len;
00089     return len;
00090   }
00091 
00092   int Skip(std::size_t offset) override {
00093     data_ += std::min<std::size_t>(offset, end_ - data_);
00094     return 0;
00095   }
00096 
00097  private:
00098   const char* data_;
00099   const char* const end_;
00100 };
00101 
00102 std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
00103     const std::string& name,
00104     const std::function<std::unique_ptr<cctz::ZoneInfoSource>(
00105         const std::string& name)>& /*fallback_factory*/) {
00106   for (const ZoneInfo& zoneinfo : kZoneInfo) {
00107     if (name == zoneinfo.name) {
00108       if (zoneinfo.data == nullptr) return nullptr;
00109       return std::unique_ptr<cctz::ZoneInfoSource>(
00110           new TestZoneInfoSource(zoneinfo.data, zoneinfo.length));
00111     }
00112   }
00113   ABSL_RAW_LOG(FATAL, "Unexpected time zone \"%s\" in test", name.c_str());
00114   return nullptr;
00115 }
00116 
00117 }  // namespace
00118 
00119 ZoneInfoSourceFactory zone_info_source_factory = TestFactory;
00120 
00121 }  // namespace cctz_extension
00122 }  // namespace time_internal
00123 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15