// The MIT License (MIT) // // Copyright (c) 2018 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 "date.h" #include #include void test_SI() { using namespace std::chrono; using namespace date; std::ostringstream os; // atto { duration d(13); date::operator<<(os, d); assert(os.str() == "13as"); os.str(""); } // femto { duration d(13); date::operator<<(os, d); assert(os.str() == "13fs"); os.str(""); } // pico { duration d(13); date::operator<<(os, d); assert(os.str() == "13ps"); os.str(""); } // nano { duration d(13); date::operator<<(os, d); assert(os.str() == "13ns"); os.str(""); } // mikro { duration d(13); date::operator<<(os, d); assert(os.str() == "13\xC2\xB5s"); os.str(""); } // milli { duration d(13); date::operator<<(os, d); assert(os.str() == "13ms"); os.str(""); } // centi { duration d(13); date::operator<<(os, d); assert(os.str() == "13cs"); os.str(""); } // deci { duration d(13); date::operator<<(os, d); assert(os.str() == "13ds"); os.str(""); } // seconds { duration d(13); date::operator<<(os, d); assert(os.str() == "13s"); os.str(""); } // deca { duration d(13); date::operator<<(os, d); assert(os.str() == "13das"); os.str(""); } // hecto { duration d(13); date::operator<<(os, d); assert(os.str() == "13hs"); os.str(""); } // kilo { duration d(13); date::operator<<(os, d); assert(os.str() == "13ks"); os.str(""); } // mega { duration d(13); date::operator<<(os, d); assert(os.str() == "13Ms"); os.str(""); } // giga { duration d(13); date::operator<<(os, d); assert(os.str() == "13Gs"); os.str(""); } // tera { duration d(13); date::operator<<(os, d); assert(os.str() == "13Ts"); os.str(""); } // peta { duration d(13); date::operator<<(os, d); assert(os.str() == "13Ps"); os.str(""); } // femto { duration d(13); date::operator<<(os, d); assert(os.str() == "13Es"); os.str(""); } } void test_calendar() { using namespace std::chrono; using namespace date; std::ostringstream os; // minutes { minutes d(13); date::operator<<(os, d); assert(os.str() == "13min"); os.str(""); } // hours { hours d(13); date::operator<<(os, d); assert(os.str() == "13h"); os.str(""); } // days { days d(13); date::operator<<(os, d); assert(os.str() == "13d"); os.str(""); } } void test_integral_scale() { using namespace std::chrono; using namespace date; std::ostringstream os; // ratio 123 / 1 { duration> d(13); date::operator<<(os, d); assert(os.str() == "13[123]s"); os.str(""); } // ratio 100 / 4 = ratio 25 / 1 { duration> d(13); date::operator<<(os, d); assert(os.str() == "13[25]s"); os.str(""); } // weeks = ratio 7 * 24 * 60 * 60 / 1 = ratio 604800 / 1 { weeks d(13); date::operator<<(os, d); assert(os.str() == "13[604800]s"); os.str(""); } // years = 146097/400 days = ratio 146097/400 * 24 * 60 * 60 = ratio 31556952 / 1 { years d(13); date::operator<<(os, d); assert(os.str() == "13[31556952]s"); os.str(""); } // months = 1/12 years = ratio 1/12 * 31556952 = ratio 2629746 / 1 { months d(13); date::operator<<(os, d); assert(os.str() == "13[2629746]s"); os.str(""); } } void test_ratio_scale() { using namespace std::chrono; using namespace date; std::ostringstream os; // ratio 1 / 2 { duration> d(13); date::operator<<(os, d); assert(os.str() == "13[1/2]s"); os.str(""); } // ratio 100 / 3 { duration> d(13); date::operator<<(os, d); assert(os.str() == "13[100/3]s"); os.str(""); } // ratio 100 / 6 = ratio 50 / 3 { duration> d(13); date::operator<<(os, d); assert(os.str() == "13[50/3]s"); os.str(""); } } void test_constexpr() { using date::detail::get_units; CONSTCD11 auto as = get_units(std::atto{}); CONSTCD11 auto fs = get_units(std::femto{}); CONSTCD11 auto ps = get_units(std::pico{}); CONSTCD11 auto ns = get_units(std::nano{}); CONSTCD11 auto us = get_units(std::micro{}); CONSTCD11 auto usw = get_units(std::micro{}); CONSTCD11 auto ms = get_units(std::milli{}); CONSTCD11 auto cs = get_units(std::centi{}); CONSTCD11 auto ds = get_units(std::deci{}); CONSTCD11 auto s = get_units(std::ratio<1>{}); CONSTCD11 auto das = get_units(std::deca{}); CONSTCD11 auto hs = get_units(std::hecto{}); CONSTCD11 auto ks = get_units(std::kilo{}); CONSTCD11 auto Ms = get_units(std::mega{}); CONSTCD11 auto Gs = get_units(std::giga{}); CONSTCD11 auto Ts = get_units(std::tera{}); CONSTCD11 auto Ps = get_units(std::peta{}); CONSTCD11 auto Es = get_units(std::exa{}); (void)as, (void)fs, (void)ps, (void)ns, (void)usw, (void)us, (void)ms, (void)cs, (void)ds, (void)s, (void)das, (void)hs, (void)ks, (void)Ms, (void)Gs, (void)Ts, (void)Ps, (void)Es; CONSTCD11 auto min = get_units(std::ratio<60>{}); CONSTCD11 auto h = get_units(std::ratio<3600>{}); CONSTCD11 auto d = get_units(std::ratio<86400>{}); (void)min, (void)h, (void)d; CONSTCD14 auto integer = get_units(std::ratio<123>{}); CONSTCD14 auto ratio = get_units(std::ratio<123, 3>{}); (void)integer, (void)ratio; } int main() { test_SI(); test_calendar(); test_integral_scale(); test_ratio_scale(); }