1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2013 - 2021, Göteborg Bit Factory.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 #include <cmake.h>
28 #include <test.h>
29 #include <Datetime.h>
30 
31 ////////////////////////////////////////////////////////////////////////////////
testInit(UnitTest & t,const std::string & value,Datetime & var)32 void testInit (UnitTest& t, const std::string& value, Datetime& var)
33 {
34   try
35   {
36     var = Datetime (value);
37     t.pass (value + " --> valid");
38   }
39 
40   catch (const std::string& e)
41   {
42     t.fail (value + " --> valid");
43     t.diag (e);
44   }
45 
46   catch (...)
47   {
48     t.fail (value + " --> valid");
49   }
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
main(int,char **)53 int main (int, char**)
54 {
55   UnitTest t (164);
56 
57   Datetime sunday;    testInit (t, "sunday",    sunday);
58   Datetime monday;    testInit (t, "monday",    monday);
59   Datetime tuesday;   testInit (t, "tuesday",   tuesday);
60   Datetime wednesday; testInit (t, "wednesday", wednesday);
61   Datetime thursday;  testInit (t, "thursday",  thursday);
62   Datetime friday;    testInit (t, "friday",    friday);
63   Datetime saturday;  testInit (t, "saturday",  saturday);
64 
65   Datetime sun; testInit (t, "sun", sun);
66   Datetime mon; testInit (t, "mon", mon);
67   Datetime tue; testInit (t, "tue", tue);
68   Datetime wed; testInit (t, "wed", wed);
69   Datetime thu; testInit (t, "thu", thu);
70   Datetime fri; testInit (t, "fri", fri);
71   Datetime sat; testInit (t, "sat", sat);
72 
73   t.ok (sunday    == sun, "sunday == sun");
74   t.ok (monday    == mon, "monday == mon");
75   t.ok (tuesday   == tue, "tuesday == tue");
76   t.ok (wednesday == wed, "wednesday == wed");
77   t.ok (thursday  == thu, "thursday == thu");
78   t.ok (friday    == fri, "friday == fri");
79   t.ok (saturday  == sat, "saturday == sat");
80 
81   Datetime january;   testInit (t, "january",   january);
82   Datetime february;  testInit (t, "february",  february);
83   Datetime march;     testInit (t, "march",     march);
84   Datetime april;     testInit (t, "april",     april);
85   Datetime may;       testInit (t, "may",       may);
86   Datetime june;      testInit (t, "june",      june);
87   Datetime july;      testInit (t, "july",      july);
88   Datetime august;    testInit (t, "august",    august);
89   Datetime september; testInit (t, "september", september);
90   Datetime october;   testInit (t, "october",   october);
91   Datetime november;  testInit (t, "november",  november);
92   Datetime december;  testInit (t, "december",  december);
93 
94   Datetime jan; testInit (t, "jan", jan);
95   Datetime feb; testInit (t, "feb", feb);
96   Datetime mar; testInit (t, "mar", mar);
97   Datetime apr; testInit (t, "apr", apr);
98   Datetime jun; testInit (t, "jun", jun);
99   Datetime jul; testInit (t, "jul", jul);
100   Datetime aug; testInit (t, "aug", aug);
101   Datetime sep; testInit (t, "sep", sep);
102   Datetime oct; testInit (t, "oct", oct);
103   Datetime nov; testInit (t, "nov", nov);
104   Datetime dec; testInit (t, "dec", dec);
105 
106   t.ok (january   == jan, "january == jan");
107   t.ok (february  == feb, "february == feb");
108   t.ok (march     == mar, "march == mar");
109   t.ok (april     == apr, "april == apr");
110   // May has only three letters.
111   t.ok (june      == jun, "june == jun");
112   t.ok (july      == jul, "july == jul");
113   t.ok (august    == aug, "august == aug");
114   t.ok (september == sep, "september == sep");
115   t.ok (october   == oct, "october == oct");
116   t.ok (november  == nov, "november == nov");
117   t.ok (december  == dec, "december == dec");
118 
119   // Simply instantiate these for now.  Test later.
120   Datetime now;            testInit (t, "now", now);
121   Datetime yesterday;      testInit (t, "yesterday", yesterday);
122   Datetime today;          testInit (t, "today", today);
123   Datetime tomorrow;       testInit (t, "tomorrow", tomorrow);
124   Datetime first;          testInit (t, "1st", first);
125   Datetime second;         testInit (t, "2nd", second);
126   Datetime third;          testInit (t, "3rd", third);
127   Datetime fourth;         testInit (t, "4th", fourth);
128   Datetime later;          testInit (t, "later", later);
129   Datetime someday;        testInit (t, "someday", someday);
130   Datetime sopd;           testInit (t, "sopd", sopd);
131   Datetime sond;           testInit (t, "sond", sond);
132   Datetime sod;            testInit (t, "sod", sod);
133   Datetime eopd;           testInit (t, "eopd", eopd);
134   Datetime eond;           testInit (t, "eond", eond);
135   Datetime eod;            testInit (t, "eod", eod);
136   Datetime sopw;           testInit (t, "sopw", sopw);
137   Datetime sonw;           testInit (t, "sonw", sonw);
138   Datetime sow;            testInit (t, "sow", sow);
139   Datetime eopw;           testInit (t, "eopw", eopw);
140   Datetime eonw;           testInit (t, "eonw", eonw);
141   Datetime eow;            testInit (t, "eow", eow);
142   Datetime sopww;          testInit (t, "sopww", sopww);
143   Datetime sonww;          testInit (t, "sonww", sonww);
144   Datetime soww;           testInit (t, "soww", soww);
145   Datetime eopww;          testInit (t, "eopww", eopww);
146   Datetime eonww;          testInit (t, "eonww", eonww);
147   Datetime eoww;           testInit (t, "eoww", eoww);
148   Datetime sopm;           testInit (t, "sopm", sopm);
149   Datetime sonm;           testInit (t, "sonm", sonm);
150   Datetime som;            testInit (t, "som", som);
151   Datetime eopm;           testInit (t, "eopm", eopm);
152   Datetime eonm;           testInit (t, "eonm", eonm);
153   Datetime eom;            testInit (t, "eom", eom);
154   Datetime sopq;           testInit (t, "sopq", sopq);
155   Datetime sonq;           testInit (t, "sonq", sonq);
156   Datetime soq;            testInit (t, "soq", soq);
157   Datetime eopq;           testInit (t, "eopq", eopq);
158   Datetime eonq;           testInit (t, "eonq", eonq);
159   Datetime eoq;            testInit (t, "eoq", eoq);
160   Datetime sopy;           testInit (t, "sopy", sopy);
161   Datetime sony;           testInit (t, "sony", sony);
162   Datetime soy;            testInit (t, "soy", soy);
163   Datetime eopy;           testInit (t, "eopy", eopy);
164   Datetime eony;           testInit (t, "eony", eony);
165   Datetime eoy;            testInit (t, "eoy", eoy);
166   Datetime easter;         testInit (t, "easter", easter);
167   Datetime eastermonday;   testInit (t, "eastermonday", eastermonday);
168   Datetime ascension;      testInit (t, "ascension", ascension);
169   Datetime pentecost;      testInit (t, "pentecost", pentecost);
170   Datetime goodfriday;     testInit (t, "goodfriday", goodfriday);
171 
172   Datetime midsommar;      testInit (t, "midsommar", midsommar);
173   Datetime midsommarafton; testInit (t, "midsommarafton", midsommarafton);
174   Datetime juhannus;       testInit (t, "juhannus", juhannus);
175 
176   // Check abbreviations.
177   // TW-1515: abbreviation.minimum does not apply to date recognition
178   Datetime yesterday2;      testInit (t, "yesterday", yesterday2);
179   Datetime yesterday3;      testInit (t, "yesterda",  yesterday3);
180   Datetime yesterday4;      testInit (t, "yesterd",   yesterday4);
181   Datetime yesterday5;      testInit (t, "yester",    yesterday5);
182   Datetime yesterday6;      testInit (t, "yeste",     yesterday6);
183   Datetime yesterday7;      testInit (t, "yest",      yesterday7);
184   Datetime yesterday8;      testInit (t, "yes",       yesterday8);
185 
186   t.ok (eod == tomorrow,            "eod == tomorrow");
187   t.ok (eom == sonm,                "eom == sonm");
188   t.ok (eond > tomorrow,            "eond > tomorrow");
189   t.ok (eonm > sonm,                "eonm > sonm");
190   t.ok (eonq > sonq,                "eonq > sonq");
191   t.ok (eonw > sonw,                "eonw > sonw");
192   t.ok (eonww > sonww,              "eonww > sonww");
193   t.ok (eony > sony,                "eony > sony");
194   t.ok (eopd == today,              "eopd == today");
195   t.ok (eopm == som,                "eopm == som");
196   t.ok (eopq == soq,                "eopq == soq");
197   t.ok (eopw == sow,                "eopw == sow");
198   t.ok (eopww < soww,               "eopww < soww");
199   t.ok (eopy == soy,                "eopy == soy");
200   t.ok (eoq == eoq,                 "eoq == eoq");
201   t.ok (eoq == sonq,                "eoq == sonq");
202   t.ok (eow == eow,                 "eow == eow");
203   t.ok (eow == sonw,                "eow == sonw");
204   t.ok (eoww >= eopww,              "eoww >= eopww");
205   t.ok (eoy == eoy,                 "eoy == eoy");
206   t.ok (eoy == sony,                "eoy == sony");
207   t.ok (later == someday,           "later == someday");
208   t.ok (now < later,                "now < later");
209   t.ok (now < someday,              "now < someday");
210   t.ok (now >= today,               "now >= today");
211   t.ok (sod == today,               "sod == today");
212   t.ok (som < eom,                  "som < eom");
213   t.ok (sond == tomorrow,           "sond == tomorrow");
214   t.ok (sonq == eoq,                "sonq == eoq");
215   t.ok (sonq > soq,                 "sonq > soq");
216   t.ok (sonw == eow,                "sonw == eow");
217   t.ok (sony == eoy,                "sony == eoy");
218   t.ok (sony > soy,                 "sony > soy");
219   t.ok (sopd == yesterday,          "sopd == yesterday");
220   t.ok (sopm < som,                 "sopm < som");
221   t.ok (sopq < soq,                 "sopq < soq");
222   t.ok (sopw < sow,                 "sopw < sow");
223   t.ok (sopww < soww,               "sopww < soww");
224   t.ok (sopy < soy,                 "sopy < soy");
225   t.ok (sow < sonw,                 "sow < sonw");
226   t.ok (soww == sonww,              "soww == sonww");
227   t.ok (soy > sopy,                 "soy > sopy");
228   t.ok (today < tomorrow,           "today < tomorrow");
229   t.ok (yesterday < today,          "yesterday < today");
230   t.ok (goodfriday < easter,        "goodfriday < easter");
231   t.ok (easter < eastermonday,      "easter < eastermonday");
232   t.ok (midsommarafton < midsommar, "midsommarafton < midsommar");
233   t.ok (juhannus == midsommarafton, "juhannus == midsommarafton");
234 
235   return 0;
236 }
237 
238 ////////////////////////////////////////////////////////////////////////////////
239