Line data Source code
1 : /*
2 : Zipios++ - a small C++ library that provides easy access to .zip files.
3 :
4 : Copyright (C) 2000-2007 Thomas Sondergaard
5 : Copyright (C) 2015 Made to Order Software Corporation
6 :
7 : This library is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU Lesser General Public
9 : License as published by the Free Software Foundation; either
10 : version 2 of the License, or (at your option) any later version.
11 :
12 : This library is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public
18 : License along with this library; if not, write to the Free Software
19 : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 : */
21 :
22 : /** \file
23 : *
24 : * Zipios++ unit tests verify the dostime.c/h private implementations.
25 : *
26 : * \warning
27 : * This directly accesses dostime.h which is a private header file.
28 : */
29 :
30 : #include "catch_tests.hpp"
31 :
32 : #include "src/dostime.h"
33 :
34 : #include <fstream>
35 :
36 : #include <sys/stat.h>
37 : #include <unistd.h>
38 :
39 : #include <iostream>
40 :
41 2 : TEST_CASE("Unix to DOS time conversions and vice versa", "[dostime]")
42 : {
43 1 : REQUIRE(mindostime() == dostime(1980, 1, 1, 0, 0, 0));
44 1 : REQUIRE(maxdostime() == dostime(2107, 12, 31, 23, 59, 59));
45 :
46 : if(sizeof(time_t) < sizeof(uint64_t))
47 : {
48 : std::cerr << "warning: Unix to DOS time conversion is ignored on platform with a 32 bit time_t definition." << std::endl;
49 1 : return;
50 : }
51 :
52 1 : time_t const clock = time(NULL);
53 1 : struct tm *mm_time = localtime(&clock);
54 :
55 1 : mm_time->tm_isdst = -1; /* let mktime() determine if DST is in effect */
56 1 : mm_time->tm_sec = 0; // min = Jan 1, 1980 at 00:00:00
57 1 : mm_time->tm_min = 0;
58 1 : mm_time->tm_hour = 0;
59 1 : mm_time->tm_mday = 1;
60 1 : mm_time->tm_mon = 0;
61 1 : mm_time->tm_year = 80;
62 1 : time_t const minimum_unix = mktime(mm_time);
63 :
64 1 : mm_time->tm_isdst = -1; /* let mktime() determine if DST is in effect */
65 1 : mm_time->tm_sec = 59; // max = Dec 3, 2107 at 23:59:59
66 1 : mm_time->tm_min = 59;
67 1 : mm_time->tm_hour = 23;
68 1 : mm_time->tm_mday = 31;
69 1 : mm_time->tm_mon = 11;
70 1 : mm_time->tm_year = 207;
71 1 : time_t const maximum_unix = mktime(mm_time);
72 :
73 : // make sure the minimum limit is checked properly
74 42 : for(time_t t(minimum_unix - 20); t <= minimum_unix + 20; ++t)
75 : {
76 41 : time_t et((t + 1) & ~1);
77 41 : dostime_t const d(unix2dostime(t));
78 41 : time_t const u(dos2unixtime(d));
79 41 : if(et < minimum_unix)
80 : {
81 19 : REQUIRE(d == 0);
82 19 : REQUIRE(u == -1);
83 : }
84 : else
85 : {
86 : // TODO: add necessary code to verify DST?
87 22 : long long int const diff(llabs(u - et));
88 22 : bool const valid(diff == 0 || diff == 3600);
89 22 : REQUIRE(valid);
90 : }
91 : }
92 :
93 : // make sure the maximum limit is checked properly
94 42 : for(time_t t(maximum_unix - 20); t <= maximum_unix + 20; ++t)
95 : {
96 41 : time_t et((t + 1) & ~1);
97 41 : dostime_t const d(unix2dostime(t));
98 41 : time_t const u(dos2unixtime(d));
99 41 : if(et > maximum_unix)
100 : {
101 21 : REQUIRE(d == 0);
102 21 : REQUIRE(u == -1);
103 : }
104 : else
105 : {
106 : // TODO: add necessary code to verify DST?
107 20 : long long int const diff(llabs(u - et));
108 20 : bool const valid(diff == 0 || diff == 3600);
109 20 : REQUIRE(valid);
110 : }
111 : }
112 :
113 : // we verified that time_t is at least a 64 bit type
114 : #if defined(_ILP32)
115 : for(time_t t(0); t <= 0x7FFFFFFF; t += rand() & 0x7FFF)
116 : #else
117 133512 : for(time_t t(0); t <= 0x104000000; t += rand() & 0xFFFF)
118 : #endif
119 : {
120 133511 : time_t et((t + 1) & ~1);
121 133511 : dostime_t const d(unix2dostime(t));
122 133511 : time_t const u(dos2unixtime(d));
123 133511 : if(et < minimum_unix)
124 : {
125 9528 : REQUIRE(d == 0);
126 9528 : REQUIRE(u == -1);
127 : }
128 123983 : else if(et > maximum_unix)
129 : {
130 214 : REQUIRE(d == 0);
131 214 : REQUIRE(u == -1);
132 : }
133 : else
134 : {
135 : // TODO: add necessary code to verify DST?
136 123769 : long long int const diff(llabs(u - et));
137 123769 : bool const valid(diff == 0 || diff == 3600);
138 123769 : REQUIRE(valid);
139 : }
140 : }
141 3 : }
142 :
143 :
144 :
145 : // vim: ts=4 sw=4 et
146 :
147 : // Local Variables:
148 : // mode: cpp
149 : // indent-tabs-mode: nil
150 : // c-basic-offset: 4
151 : // tab-width: 4
152 : // End:
|