Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <limits.h> #include <stdio.h> #include <time.h> #include <stdlib.h> #include <darwintest.h> #include <darwintest_utils.h> //#include "../stdtime/getdate.c" extern int getdate_err; __unused static void log_tm(struct tm *tm) { T_LOG("tm = {\n\t.tm_sec = %d, tm_min = %d, tm_hour = %d,\n\t.tm_mday = %d, .tm_mon = %d, .tm_year = %d,\n\t.tm_wday = %d, tm_yday = %d\n};", tm->tm_sec, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday, tm->tm_yday); } static char * create_template_file(const char *name, const char *contents) { T_SETUPBEGIN; char *template_path; asprintf(&template_path, "%s/%s", dt_tmpdir(), name); T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(template_path, NULL); FILE *template_file = fopen(template_path, "w"); T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(template_file, NULL); fprintf(template_file, "%s", contents); fclose(template_file); T_SETUPEND; return template_path; } T_DECL(getdate3, "getdate() using \"%a\" with match") { struct tm tm = {}; char *remainder = strptime("Fri", "%a", &tm); T_EXPECT_NOTNULL(remainder, "strptime(\"Fri\", \"%%a\", &tm)"); T_EXPECT_EQ(tm.tm_wday, 5, "Returned time should be on Friday"); char *template_path = create_template_file("a3", "%a\n"); setenv("DATEMSK", template_path, 1); free(template_path); getdate_err = 0; struct tm *ptr_tm = getdate("Fri"); T_LOG("getdate(\"Fri\") -> Should indicate Friday"); T_ASSERT_NOTNULL(ptr_tm, "getdate() returned NULL, error is %d", getdate_err); T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm)); T_EXPECT_EQ(ptr_tm->tm_wday, 5, "Returned time should be on Friday"); } T_DECL(getdate4, "getdate() using \"%A\" with match") { struct tm tm = {}; char *remainder = strptime("FriDay", "%A", &tm); T_EXPECT_NOTNULL(remainder, "strptime(\"FriDay\", \"%%A\", &tm)"); T_EXPECT_EQ(tm.tm_wday, 5, "Returned time should be on Friday"); char *template_path = create_template_file("a4", "%A\n"); setenv("DATEMSK", template_path, 1); free(template_path); getdate_err = 0; struct tm *ptr_tm = getdate("FriDay"); T_LOG("getdate(\"FriDay\") -> Should indicate Friday"); T_ASSERT_NOTNULL(ptr_tm, "getdate() returned NULL, error is %d", getdate_err); T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm)); T_EXPECT_EQ(ptr_tm->tm_wday, 5, "Returned time should be on Friday"); } T_DECL(getdate15, "getdate() using \"%w\" with match") { char *template_path = create_template_file("a4", "%w\n"); setenv("DATEMSK", template_path, 1); free(template_path); getdate_err = 0; struct tm *ptr_tm = getdate("0"); T_LOG("getdate(\"0\") -> Should indicate weekday 0"); T_ASSERT_NOTNULL(ptr_tm, "getdate() returned NULL, error is %d", getdate_err); T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm)); T_EXPECT_EQ(ptr_tm->tm_wday, 0, "Returned time should be on weekday 0"); getdate_err = 0; ptr_tm = getdate("6"); T_LOG("getdate(\"6\") -> Should indicate weekday 6"); T_ASSERT_NOTNULL(ptr_tm, "getdate() returned NULL, error is %d", getdate_err); T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm)); T_EXPECT_EQ(ptr_tm->tm_wday, 6, "Returned time should be on weekday 6"); } T_DECL(getdate46, "getdate() using \"%%t\" without match") { char *template_path = create_template_file("a46", "%t\n"); setenv("DATEMSK", template_path, 1); free(template_path); getdate_err = 0; struct tm *ptr_tm = getdate("there'snotemplateforthis."); if (ptr_tm) { T_FAIL("getdate returned: %s", asctime (ptr_tm)); } /* Now check for the getdate_err */ T_EXPECT_EQ(getdate_err, 7, NULL); } T_DECL(getdate21, "getdate() using \"%D\" with match") { struct tm tm = {}; char *remainder = strptime("1/1/38", "%D", &tm); T_EXPECT_NOTNULL(remainder, "strptime(\"1/1/38\", \"%%D\", &tm)"); char *template_path = create_template_file("a21", "%D\n"); setenv("DATEMSK", template_path, 1); free(template_path); getdate_err = 0; struct tm *ptr_tm = getdate ("1/1/38"); T_LOG("getdate(\"1/1/38\") -> Should indicate 2038!"); T_ASSERT_NOTNULL(ptr_tm, "getdate() returned NULL, error is %d", getdate_err); T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm)); T_EXPECT_EQ(ptr_tm->tm_year, 2038 - 1900, NULL); T_EXPECT_EQ(ptr_tm->tm_mon, 0, NULL); T_EXPECT_EQ(ptr_tm->tm_mday, 1, NULL); } T_DECL(getdate53, "getdate() using \"%d+%m+%y %C\" with match") { struct tm tm = {}; char *remainder = strptime("15+05+20 20", "%d+%m+%y %C", &tm); T_EXPECT_NOTNULL(remainder, "strptime(\"15+05+20 20\", \"%%d+%%m+%%y %%C\", &tm)"); char *template_path = create_template_file("a53", "%d+%m+%y %C\n"); setenv("DATEMSK", template_path, 1); free(template_path); getdate_err = 0; struct tm *ptr_tm = getdate ("15+05+20 20"); T_LOG("getdate(\"15+05+20 20\")"); T_ASSERT_NOTNULL(ptr_tm, "getdate() returned NULL, error is %d", getdate_err); T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm)); T_EXPECT_EQ(ptr_tm->tm_year, 2020 - 1900, NULL); T_EXPECT_EQ(ptr_tm->tm_mon, 4, NULL); T_EXPECT_EQ(ptr_tm->tm_mday, 15, NULL); } |