From a9d2907fa11f646f7fea665bf37e143c758eea8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kami=C5=84ski?= Date: Sun, 19 Nov 2017 18:47:18 +0100 Subject: [PATCH] Implemented test for custom clocks derived from sys_clock. Created mil_clock and s2s_clock, that are both using to_sys/from_sys function and are fully interoperable with existing clocks (including ones based on utc). Implemented an trait is used to provide direct conversion from s2s_clock to mil_clock without converting to sys_clock (conversion counter is used for this purpose). --- test/tz_test/custom_clock_casts.pass.cpp | 187 +++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 test/tz_test/custom_clock_casts.pass.cpp diff --git a/test/tz_test/custom_clock_casts.pass.cpp b/test/tz_test/custom_clock_casts.pass.cpp new file mode 100644 index 0000000..fd6853a --- /dev/null +++ b/test/tz_test/custom_clock_casts.pass.cpp @@ -0,0 +1,187 @@ +// The MIT License (MIT) +// +// Copyright (c) 2017 Tomasz KamiƄski +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "tz.h" +#include +#include + +//used to count number of conversion +int conversions = 0; + +//to/from impl +struct mil_clock +{ + using duration = std::common_type_t; + using rep = duration::rep; + using period = duration::period; + using time_point = std::chrono::time_point; + + static constexpr date::sys_days epoch = date::year{2000}/date::month{0}/date::day{1}; + + template + static + std::chrono::time_point> + to_sys(std::chrono::time_point const& tp) + { + ++conversions; + return epoch + tp.time_since_epoch(); + } + + template + static + std::chrono::time_point> + from_sys(std::chrono::time_point const& tp) + { + ++conversions; + using res = std::chrono::time_point>; + return res(tp - epoch); + } + + static time_point now() + { + return from_sys(std::chrono::system_clock::now()); + } +}; + + +date::sys_days const mil_clock::epoch; + +// traits example +struct s2s_clock +{ + using duration = std::chrono::system_clock::duration; + using rep = duration::rep; + using period = duration::period; + using time_point = std::chrono::time_point; + + template + static + std::chrono::time_point + to_sys(std::chrono::time_point const& tp) + { + ++conversions; + return std::chrono::time_point(tp.time_since_epoch()); + } + + template + static + std::chrono::time_point + from_sys(std::chrono::time_point const& tp) + { + ++conversions; + return std::chrono::time_point(tp.time_since_epoch()); + } + + static time_point now() + { + return from_sys(std::chrono::system_clock::now()); + } +}; + +namespace date +{ + template<> + struct clock_time_conversion + { + template + std::chrono::time_point> + operator()(std::chrono::time_point const& tp) + { + ++conversions; + using res = std::chrono::time_point>; + return res(tp.time_since_epoch() - mil_clock::epoch.time_since_epoch()); + } + }; +} + +int +main() +{ + using namespace date; + using sys_clock = std::chrono::system_clock; + + // self + { + sys_days st(1997_y/dec/12); + auto mt = mil_clock::from_sys(st); + + assert(clock_cast(mt) == mt); + } + + // mil <-> sys + { + sys_days st(1997_y/dec/12); + auto mt = mil_clock::from_sys(st); + + assert(clock_cast(st) == mt); + assert(clock_cast(mt) == st); + } + + // mil <-> utc + { + sys_days st(1997_y/dec/12); + auto mt = mil_clock::from_sys(st); + auto ut = utc_clock::from_sys(st); + + assert(clock_cast(ut) == mt); + assert(clock_cast(mt) == ut); + } + + // mil <-> tai + { + sys_days st(1997_y/dec/12); + auto mt = mil_clock::from_sys(st); + auto ut = utc_clock::from_sys(st); + auto tt = tai_clock::from_utc(ut); + + assert(clock_cast(mt) == tt); + assert(clock_cast(tt) == mt); + } + + // mil <-> gps + { + sys_days st(1997_y/dec/12); + auto mt = mil_clock::from_sys(st); + auto ut = utc_clock::from_sys(st); + auto gt = gps_clock::from_utc(ut); + + assert(clock_cast(mt) == gt); + assert(clock_cast(gt) == mt); + } + + // s2s -> mil + { + sys_days st(1997_y/dec/12); + auto mt = mil_clock::from_sys(st); + auto s2t = s2s_clock::from_sys(st); + + //direct trait conversion + conversions = 0; + assert(clock_cast(s2t) == mt); + assert(conversions == 1); + + //uses sys_clock + conversions = 0; + assert(clock_cast(mt) == s2t); + assert(conversions == 2); + } +}