1 // xestiacalendar_commonfunctions.h - Xestia Calendar Common Functions Unit Tests
3 // (c) 2016 Xestia Software Development.
5 // This file is part of Xestia Calendar.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book 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.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "../common/file.h"
20 #include "../common/text.h"
21 #include "../common/colour.h"
22 #include "../common/monthviewgen.h"
24 TEST(CommonFunctions, FileTests){
26 ASSERT_EQ(false, FileExists("iCalendarEvent-Missing.vcf"));
27 ASSERT_EQ(true, FileExists("iCalendarEvent-InvalidPermissions.vcf"));
31 TEST(CommonFunctions, ProcessTextVectorsTests){
33 // Setup the objects to be used for processing.
35 vector<string> TextPropertiesExample1;
36 vector<string> TextValueExample1;
37 multimap<string, string> TextExample1TestResult;
39 TextPropertiesExample1.push_back("ATTENDEE");
40 TextValueExample1.push_back("Example Attendee 1");
42 TextPropertiesExample1.push_back("ATTENDEE");
43 TextValueExample1.push_back("Example Attendee 2");
45 TextPropertiesExample1.push_back("ATTENDEE");
46 TextValueExample1.push_back("Example Attendee 3");
48 TextPropertiesExample1.push_back("CATEGORIES");
49 TextValueExample1.push_back("CATEGORY 1");
51 TextPropertiesExample1.push_back("CATEGORIES");
52 TextValueExample1.push_back("CATEGORY 2");
54 TextPropertiesExample1.push_back("CATEGORIES");
55 TextValueExample1.push_back("CATEGORY 3");
57 TextPropertiesExample1.push_back("COMMENT");
58 TextValueExample1.push_back("This is the first comment.");
60 TextPropertiesExample1.push_back("COMMENT");
61 TextValueExample1.push_back("This is the second comment.");
63 TextPropertiesExample1.push_back("COMMENT");
64 TextValueExample1.push_back("This is the third comment.");
66 TextPropertiesExample1.push_back("CONTACT;TEST=VALUE");
67 TextValueExample1.push_back("First Contact");
69 TextPropertiesExample1.push_back("CONTACT;LAZY=NOPE");
70 TextValueExample1.push_back("Second Contact");
72 TextPropertiesExample1.push_back("CONTACT;SETUP=NO");
73 TextValueExample1.push_back("Third Contact");
75 TextPropertiesExample1.push_back("RESOURCES;ROOM=YES");
76 TextValueExample1.push_back("First Resource Widget");
78 TextPropertiesExample1.push_back("RESOURCES;ROOM=NO");
79 TextValueExample1.push_back("Second Resource Widget");
81 TextPropertiesExample1.push_back("RESOURCES;ROOM=UNKNOWN");
82 TextValueExample1.push_back("Third Resource Widget");
84 TextPropertiesExample1.push_back("RELATED;RELATION=NO");
85 TextValueExample1.push_back("First Relation");
87 TextPropertiesExample1.push_back("RELATED;RELATION=YES");
88 TextValueExample1.push_back("Second Relation");
90 TextPropertiesExample1.push_back("RELATED;RELATION=MAYBE");
91 TextValueExample1.push_back("Third Relation");
93 // Examples 1 & 2: ATTENDEE
95 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 1"));
96 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 2"));
97 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 3"));
99 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "ATTENDEE"));
101 TextExample1TestResult.clear();
102 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 1"));
104 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "ATTENDEE"));
106 // Examples 3 & 4: CATEGORIES
108 TextExample1TestResult.clear();
109 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 1"));
110 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 2"));
111 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 3"));
113 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "CATEGORIES"));
115 TextExample1TestResult.clear();
116 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 1"));
118 // Examples 5 & 6: COMMENT
120 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "CATEGORIES"));
122 TextExample1TestResult.clear();
123 TextExample1TestResult.insert(make_pair("COMMENT", "This is the first comment."));
124 TextExample1TestResult.insert(make_pair("COMMENT", "This is the second comment."));
125 TextExample1TestResult.insert(make_pair("COMMENT", "This is the third comment."));
127 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "COMMENT"));
129 TextExample1TestResult.clear();
130 TextExample1TestResult.insert(make_pair("COMMENT", "This is the first comment."));
132 // Examples 7 & 8: CONTACT;(properties)
134 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "COMMENT"));
136 TextExample1TestResult.clear();
137 TextExample1TestResult.insert(make_pair("CONTACT;TEST=VALUE", "First Contact"));
138 TextExample1TestResult.insert(make_pair("CONTACT;LAZY=NOPE", "Second Contact"));
139 TextExample1TestResult.insert(make_pair("CONTACT;SETUP=NO", "Third Contact"));
141 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "CONTACT"));
143 TextExample1TestResult.clear();
144 TextExample1TestResult.insert(make_pair("CONTACT;TEST=VALUE", "First Contact"));
146 // Examples 9 & 10: RESOURCES;(properties)
148 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "CONTACT"));
150 TextExample1TestResult.clear();
151 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=YES", "First Resource Widget"));
152 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=NO", "Second Resource Widget"));
153 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=UNKNOWN", "Third Resource Widget"));
155 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "RESOURCES"));
157 TextExample1TestResult.clear();
158 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=YES", "First Resource Widget"));
160 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "RESOURCES"));
162 // Examples 11 & 12: RELATED;(properties)
164 TextExample1TestResult.clear();
165 TextExample1TestResult.insert(make_pair("RELATED;RELATION=NO", "First Relation"));
166 TextExample1TestResult.insert(make_pair("RELATED;RELATION=YES", "Second Relation"));
167 TextExample1TestResult.insert(make_pair("RELATED;RELATION=MAYBE", "Third Relation"));
169 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "RELATED"));
171 TextExample1TestResult.clear();
172 TextExample1TestResult.insert(make_pair("RELATED;RELATION=NO", "First Relation"));
174 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "RELATED"));
178 TEST(CommonFunctions, SplitValuesTests){
180 map<string, string> TextExample1TestResult;
182 TextExample1TestResult.insert(make_pair("RELATION", "NO"));
183 TextExample1TestResult.insert(make_pair("TEST", "YES"));
184 TextExample1TestResult.insert(make_pair("DATA", "SOMEDATA"));
186 ASSERT_EQ(TextExample1TestResult, SplitValues("TEST;RELATION=NO;TEST=YES;DATA=SOMEDATA"));
190 TEST(CommonFunctions, SplitNameValueTests){
192 PropertyNameValue NameValueResult;
194 NameValueResult = SplitNameValue("TEST=OK");
196 string PropertyName = NameValueResult.Name;
197 string PropertyValue = NameValueResult.Value;
199 ASSERT_EQ(PropertyName, "TEST");
200 ASSERT_EQ(PropertyValue, "OK");
204 TEST(CommonFunctions, HexToInt){
206 string Value1 = "10"; // 16
207 string Value2 = "50"; // 80
208 string Value3 = "4F"; // 79
209 string Value4 = "FF"; // 255
210 string Value5 = "FFF"; // 4095
211 string Value6 = "FFFF"; // 65535
212 string Value7 = "75AB"; // 30123
213 string Value8 = "2AC"; // 684
214 string Value9 = "!"; // Fail
215 string Value10 = "4BZ"; // Fail
216 string Value11 = "Z?!$"; // Fail
221 Result = HexToInt(&Value1, &OutputValue);
223 ASSERT_EQ(Result, true);
224 ASSERT_EQ(OutputValue, 16);
226 Result = HexToInt(&Value2, &OutputValue);
227 ASSERT_EQ(Result, true);
228 ASSERT_EQ(OutputValue, 80);
230 Result = HexToInt(&Value3, &OutputValue);
231 ASSERT_EQ(Result, true);
232 ASSERT_EQ(OutputValue, 79);
234 Result = HexToInt(&Value4, &OutputValue);
235 ASSERT_EQ(Result, true);
236 ASSERT_EQ(OutputValue, 255);
238 Result = HexToInt(&Value5, &OutputValue);
239 ASSERT_EQ(Result, true);
240 ASSERT_EQ(OutputValue, 4095);
242 Result = HexToInt(&Value6, &OutputValue);
243 ASSERT_EQ(Result, true);
244 ASSERT_EQ(OutputValue, 65535);
246 Result = HexToInt(&Value7, &OutputValue);
247 ASSERT_EQ(Result, true);
248 ASSERT_EQ(OutputValue, 30123);
250 Result = HexToInt(&Value8, &OutputValue);
251 ASSERT_EQ(Result, true);
252 ASSERT_EQ(OutputValue, 684);
254 Result = HexToInt(&Value9, &OutputValue);
255 ASSERT_EQ(Result, false);
257 Result = HexToInt(&Value10, &OutputValue);
258 ASSERT_EQ(Result, false);
260 Result = HexToInt(&Value11, &OutputValue);
261 ASSERT_EQ(Result, false);
265 TEST(CommonFunctions, SplitPathFilename){
267 // Setup the file split.
269 string PathFilenameOriginal = "/example/file/yay.txt";
273 SplitPathFilename(&PathFilenameOriginal, &Path, &File);
275 ASSERT_EQ("/example/file/", Path);
276 ASSERT_EQ("yay.txt", File);
278 PathFilenameOriginal = "/a/path/with/lots/of/bits/in/andthenthis.html";
282 SplitPathFilename(&PathFilenameOriginal, &Path, &File);
284 ASSERT_EQ("/a/path/with/lots/of/bits/in/", Path);
285 ASSERT_EQ("andthenthis.html", File);
287 PathFilenameOriginal = "/one/more/for/a/laugh/hahaha.zip";
291 SplitPathFilename(&PathFilenameOriginal, &Path, &File);
293 ASSERT_EQ("/one/more/for/a/laugh/", Path);
294 ASSERT_EQ("hahaha.zip", File);
298 TEST(CommonFunctions, ColourStruct){
325 ASSERT_EQ("#00000000", (string)Colour1);
326 ASSERT_EQ("#FFFFFFFF", (string)Colour2);
327 ASSERT_EQ("#10101010", (string)Colour3);
328 ASSERT_EQ("#50505050", (string)Colour4);
332 TEST(CommonFunctions, OutputText){
334 string InputText1 = "This is an example text.";
335 string OutputText1 = "This is an example text.";
336 string ResultText1 = "";
338 string InputText2 = "This is the first sentence. This is the second sentence"
339 ". This is the third sentence. This is the fourth sentence. This "
340 "is the fifth sentence.";
341 string OutputText2 = "This is the first sentence. This is the second sentence"
342 ". This is the third se\n ntence. This is the fourth sentence. This "
343 "is the fifth sentence.";
344 string ResultText2 = "";
346 string InputText3 = "This is the first sentence. This is the second sentence"
347 ". This is the third sentence. This is the fourth sentence. This "
348 "is the fifth sentence. This is the sixth sentence. This is the s"
349 "eventh sentence. This is the eighth sentence. This is the ninth "
350 "sentence. This is the tenth sentence.";
351 string OutputText3 = "This is the first sentence. This is the second sentence. This is the third se\n"
352 " ntence. This is the fourth sentence. This is the fifth sentence. This is the\n"
353 " sixth sentence. This is the seventh sentence. This is the eighth sentence. \n"
354 " This is the ninth sentence. This is the tenth sentence.";
355 string ResultText3 = "";
357 string InputText4 = "This is the first sentence. This is the second sentence"
358 ". This is the third sentence. This is the fourth sentence. This "
359 "is the fifth sentence. This is the sixth sentence. This is the s"
360 "eventh sentence. This is the eighth sentence. This is the ninth "
361 "sentence. This is the tenth sentence. This is the eleventh sente"
362 "nce. This is the twelfth sentence. This is the thirteenth senten"
363 "ce. This is the fourteenth sentence. This is the fifteenth sente"
365 string OutputText4 = "This is the first sentence. This is the second sentence. This is the third se\n"
366 " ntence. This is the fourth sentence. This is the fifth sentence. This is the\n"
367 " sixth sentence. This is the seventh sentence. This is the eighth sentence. \n"
368 " This is the ninth sentence. This is the tenth sentence. This is the eleventh\n"
369 " sentence. This is the twelfth sentence. This is the thirteenth sentence. Th\n"
370 " is is the fourteenth sentence. This is the fifteenth sentence.";
371 string ResultText4 = "";
373 string InputText5 = "This is the first sentence. This is the second sentence"
374 ". This is the third sentence. This is the fourth sentence. This "
375 "is the fifth sentence. This is the sixth sentence. This is the s"
376 "eventh sentence. This is the eighth sentence. This is the ninth "
377 "sentence. This is the tenth sentence. This is the eleventh sente"
378 "nce. This is the twelfth sentence. This is the thirteenth senten"
379 "ce. This is the fourteenth sentence. This is the fifteenth sente"
380 "nce. This is the sixteenth sentence. This is the seventeenth sen"
381 "tence. This is the eighteenth sentence. This is the ninteenth se"
382 "ntence. This is the twentieth sentence.";
383 string OutputText5 = "This is the first sentence. This is the second sentence. This is the third se\n"
384 " ntence. This is the fourth sentence. This is the fifth sentence. This is the\n"
385 " sixth sentence. This is the seventh sentence. This is the eighth sentence. \n"
386 " This is the ninth sentence. This is the tenth sentence. This is the eleventh\n"
387 " sentence. This is the twelfth sentence. This is the thirteenth sentence. Th\n"
388 " is is the fourteenth sentence. This is the fifteenth sentence. This is the s\n"
389 " ixteenth sentence. This is the seventeenth sentence. This is the eighteenth \n"
390 " sentence. This is the ninteenth sentence. This is the twentieth sentence.";
393 ResultText1 = OutputText(&InputText1);
394 ResultText2 = OutputText(&InputText2);
395 ResultText3 = OutputText(&InputText3);
396 ResultText4 = OutputText(&InputText4);
397 ResultText5 = OutputText(&InputText5);
399 ASSERT_EQ(ResultText1, OutputText1);
400 ASSERT_EQ(ResultText2, OutputText2);
401 ASSERT_EQ(ResultText3, OutputText3);
402 ASSERT_EQ(ResultText4, OutputText4);
403 ASSERT_EQ(ResultText5, OutputText5);
407 TEST(CommonFunctions, PreviousMonthNumberofDays){
409 // Test the year 2015 (does not have a leap year).
411 ASSERT_EQ(30, PreviousMonthNumberofDays(12, 2015));
413 // Test the year 2016 (has a leap year).
415 ASSERT_EQ(31, PreviousMonthNumberofDays(1, 2016));
416 ASSERT_EQ(31, PreviousMonthNumberofDays(2, 2016));
417 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2016));
418 ASSERT_EQ(31, PreviousMonthNumberofDays(4, 2016));
419 ASSERT_EQ(30, PreviousMonthNumberofDays(5, 2016));
420 ASSERT_EQ(31, PreviousMonthNumberofDays(6, 2016));
421 ASSERT_EQ(30, PreviousMonthNumberofDays(7, 2016));
422 ASSERT_EQ(31, PreviousMonthNumberofDays(8, 2016));
423 ASSERT_EQ(31, PreviousMonthNumberofDays(9, 2016));
424 ASSERT_EQ(30, PreviousMonthNumberofDays(10, 2016));
425 ASSERT_EQ(31, PreviousMonthNumberofDays(11, 2016));
426 ASSERT_EQ(30, PreviousMonthNumberofDays(12, 2016));
427 ASSERT_EQ(31, PreviousMonthNumberofDays(1, 2017));
429 // Test the leap year section.
431 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2016));
432 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2012));
433 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2008));
434 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2004));
435 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2000));
437 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2015));
438 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2014));
439 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2013));
441 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2041));
442 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2042));
443 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2043));
444 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2044));
445 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2045));
446 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2046));
447 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2047));
448 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2048));
449 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2049));
450 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2050));
452 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 1900));
453 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2200));
454 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2300));
455 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2400));
456 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2500));
457 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2600));
458 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2700));
459 ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2800));
460 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2900));
461 ASSERT_EQ(28, PreviousMonthNumberofDays(3, 3000));