Line data Source code
1 : // Copyright (c) 2011-2024 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // https://snapwebsites.org/project/edhttp 4 : // contact@m2osw.com 5 : // 6 : // This program is free software: you can redistribute it and/or modify 7 : // it under the terms of the GNU General Public License as published by 8 : // the Free Software Foundation, either version 3 of the License, or 9 : // (at your option) any later version. 10 : // 11 : // This program is distributed in the hope that it will be useful, 12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : // GNU General Public License for more details. 15 : // 16 : // You should have received a copy of the GNU General Public License 17 : // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 : 19 : /** \file 20 : * \brief Verify the mkgmtime() function. 21 : * 22 : * This file implements tests to verify that the mkgmtime() is able to 23 : * convert a struct tm back to a time_t value. 24 : */ 25 : 26 : // edhttp 27 : // 28 : #include <edhttp/mkgmtime.h> 29 : 30 : //#include <edhttp/exception.h> 31 : 32 : 33 : // self 34 : // 35 : #include "catch_main.h" 36 : 37 : 38 : // snapdev 39 : // 40 : //#include <snapdev/tokenize_string.h> 41 : 42 : 43 : // snaplogger 44 : // 45 : #include <snaplogger/message.h> 46 : 47 : 48 : // C 49 : // 50 : //#include <string.h> 51 : 52 : 53 : 54 : //extern time_t mkgmtime(struct tm * tim_p); 55 2 : CATCH_TEST_CASE("mkgmtime", "[time]") 56 : { 57 2 : CATCH_START_SECTION("mkgmtime: convert back and forth") 58 : { 59 101 : for(int i(0); i < 100; ++i) 60 : { 61 : // we limit the year to +/-10,000 so we limit time_t below for 62 : // that reason 63 : // 64 100 : time_t t(0); 65 100 : SNAP_CATCH2_NAMESPACE::random(t); 66 100 : t %= 253402329600; 67 : 68 100 : struct tm tim; 69 100 : gmtime_r(&t, &tim); 70 : 71 : // Note: at the moment, mkgmtime() is a C function 72 : // 73 100 : time_t const back(mkgmtime(&tim)); 74 : 75 100 : CATCH_REQUIRE(t == back); 76 : } 77 : } 78 2 : CATCH_END_SECTION() 79 : 80 2 : CATCH_START_SECTION("mkgmtime: test with +/- to each segment") 81 : { 82 1 : time_t const t(0x67205493); // Tue Oct 29 03:20:51 AM UTC 2024 83 : 84 1 : struct tm tim; 85 1 : gmtime_r(&t, &tim); 86 : 87 1 : struct tm const org(tim); 88 : 89 : // as is 90 : // 91 1 : time_t back(mkgmtime(&tim)); 92 1 : CATCH_REQUIRE(t == back); 93 1 : CATCH_REQUIRE(memcmp(&org, &tim, sizeof(org)) == 0); 94 : 95 1 : struct tm sec_only = {}; 96 1 : sec_only.tm_sec = static_cast<int>(t); 97 1 : sec_only.tm_mday = 1; // tm_mday cannot legally be 0 98 1 : sec_only.tm_year = 70; // tm_year is a number + 1900 and the time_t is since 1970, so we need to start with 70 here 99 1 : back = mkgmtime(&sec_only); 100 1 : CATCH_REQUIRE(t == back); 101 : 102 : // time is easy 103 : // 104 1 : int old_value(tim.tm_sec); 105 1 : tim.tm_sec += 93; 106 1 : back = mkgmtime(&tim); 107 1 : CATCH_REQUIRE(t + 93 == back); 108 1 : CATCH_REQUIRE(tim.tm_sec == (old_value + 93) % 60); 109 : 110 1 : tim = org; 111 1 : old_value = tim.tm_min; 112 1 : tim.tm_min += 93; 113 1 : back = mkgmtime(&tim); 114 1 : CATCH_REQUIRE(t + 93 * 60 == back); 115 1 : CATCH_REQUIRE(tim.tm_min == (old_value + 93) % 60); 116 : 117 1 : tim = org; 118 1 : old_value = tim.tm_hour; 119 1 : tim.tm_hour += 93; 120 1 : back = mkgmtime(&tim); 121 1 : CATCH_REQUIRE(t + 93 * 3600 == back); 122 1 : CATCH_REQUIRE(tim.tm_hour == (old_value + 93) % 24); 123 : } 124 2 : CATCH_END_SECTION() 125 2 : } 126 : 127 : 128 1 : CATCH_TEST_CASE("mkgmtime_error", "[time][error]") 129 : { 130 1 : CATCH_START_SECTION("mkgmtime: year over 10,000 do not work") 131 : { 132 1 : time_t const t(1098547031761); 133 : 134 1 : struct tm tim; 135 1 : gmtime_r(&t, &tim); 136 : 137 : // Note: at the moment, mkgmtime() is a C function 138 : // 139 1 : time_t const back(mkgmtime(&tim)); 140 : 141 1 : CATCH_REQUIRE(back - 1); 142 : } 143 1 : CATCH_END_SECTION() 144 1 : } 145 : 146 : 147 : // vim: ts=4 sw=4 et