]>
git.saurik.com Git - apple/libc.git/blob - tests/getdate.c
6 #include <darwintest.h>
7 #include <darwintest_utils.h>
9 //#include "../stdtime/getdate.c"
11 extern int getdate_err
;
14 log_tm(struct tm
*tm
) {
15 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};",
16 tm
->tm_sec
, tm
->tm_min
, tm
->tm_hour
, tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
, tm
->tm_wday
, tm
->tm_yday
);
20 create_template_file(const char *name
, const char *contents
)
24 asprintf(&template_path
, "%s/%s", dt_tmpdir(), name
);
25 T_QUIET
; T_WITH_ERRNO
; T_ASSERT_NOTNULL(template_path
, NULL
);
27 FILE *template_file
= fopen(template_path
, "w");
28 T_QUIET
; T_WITH_ERRNO
; T_ASSERT_NOTNULL(template_file
, NULL
);
29 fprintf(template_file
, "%s", contents
);
30 fclose(template_file
);
36 T_DECL(getdate3
, "getdate() using \"%a\" with match")
39 char *remainder
= strptime("Fri", "%a", &tm
);
40 T_EXPECT_NOTNULL(remainder
, "strptime(\"Fri\", \"%%a\", &tm)");
41 T_EXPECT_EQ(tm
.tm_wday
, 5, "Returned time should be on Friday");
43 char *template_path
= create_template_file("a3", "%a\n");
44 setenv("DATEMSK", template_path
, 1);
48 struct tm
*ptr_tm
= getdate("Fri");
49 T_LOG("getdate(\"Fri\") -> Should indicate Friday");
50 T_ASSERT_NOTNULL(ptr_tm
, "getdate() returned NULL, error is %d", getdate_err
);
51 T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm
));
52 T_EXPECT_EQ(ptr_tm
->tm_wday
, 5, "Returned time should be on Friday");
55 T_DECL(getdate4
, "getdate() using \"%A\" with match")
58 char *remainder
= strptime("FriDay", "%A", &tm
);
59 T_EXPECT_NOTNULL(remainder
, "strptime(\"FriDay\", \"%%A\", &tm)");
60 T_EXPECT_EQ(tm
.tm_wday
, 5, "Returned time should be on Friday");
62 char *template_path
= create_template_file("a4", "%A\n");
63 setenv("DATEMSK", template_path
, 1);
67 struct tm
*ptr_tm
= getdate("FriDay");
68 T_LOG("getdate(\"FriDay\") -> Should indicate Friday");
69 T_ASSERT_NOTNULL(ptr_tm
, "getdate() returned NULL, error is %d", getdate_err
);
70 T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm
));
71 T_EXPECT_EQ(ptr_tm
->tm_wday
, 5, "Returned time should be on Friday");
74 T_DECL(getdate15
, "getdate() using \"%w\" with match")
76 char *template_path
= create_template_file("a4", "%w\n");
77 setenv("DATEMSK", template_path
, 1);
81 struct tm
*ptr_tm
= getdate("0");
82 T_LOG("getdate(\"0\") -> Should indicate weekday 0");
83 T_ASSERT_NOTNULL(ptr_tm
, "getdate() returned NULL, error is %d", getdate_err
);
84 T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm
));
85 T_EXPECT_EQ(ptr_tm
->tm_wday
, 0, "Returned time should be on weekday 0");
88 ptr_tm
= getdate("6");
89 T_LOG("getdate(\"6\") -> Should indicate weekday 6");
90 T_ASSERT_NOTNULL(ptr_tm
, "getdate() returned NULL, error is %d", getdate_err
);
91 T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm
));
92 T_EXPECT_EQ(ptr_tm
->tm_wday
, 6, "Returned time should be on weekday 6");
95 T_DECL(getdate46
, "getdate() using \"%%t\" without match")
97 char *template_path
= create_template_file("a46", "%t\n");
98 setenv("DATEMSK", template_path
, 1);
102 struct tm
*ptr_tm
= getdate("there'snotemplateforthis.");
104 T_FAIL("getdate returned: %s", asctime (ptr_tm
));
106 /* Now check for the getdate_err */
107 T_EXPECT_EQ(getdate_err
, 7, NULL
);
110 T_DECL(getdate21
, "getdate() using \"%D\" with match")
113 char *remainder
= strptime("1/1/38", "%D", &tm
);
114 T_EXPECT_NOTNULL(remainder
, "strptime(\"1/1/38\", \"%%D\", &tm)");
116 char *template_path
= create_template_file("a21", "%D\n");
117 setenv("DATEMSK", template_path
, 1);
121 struct tm
*ptr_tm
= getdate ("1/1/38");
122 T_LOG("getdate(\"1/1/38\") -> Should indicate 2038!");
123 T_ASSERT_NOTNULL(ptr_tm
, "getdate() returned NULL, error is %d", getdate_err
);
124 T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm
));
126 T_EXPECT_EQ(ptr_tm
->tm_year
, 2038 - 1900, NULL
);
127 T_EXPECT_EQ(ptr_tm
->tm_mon
, 0, NULL
);
128 T_EXPECT_EQ(ptr_tm
->tm_mday
, 1, NULL
);
131 T_DECL(getdate53
, "getdate() using \"%d+%m+%y %C\" with match")
134 char *remainder
= strptime("15+05+20 20", "%d+%m+%y %C", &tm
);
135 T_EXPECT_NOTNULL(remainder
, "strptime(\"15+05+20 20\", \"%%d+%%m+%%y %%C\", &tm)");
137 char *template_path
= create_template_file("a53", "%d+%m+%y %C\n");
138 setenv("DATEMSK", template_path
, 1);
142 struct tm
*ptr_tm
= getdate ("15+05+20 20");
143 T_LOG("getdate(\"15+05+20 20\")");
144 T_ASSERT_NOTNULL(ptr_tm
, "getdate() returned NULL, error is %d", getdate_err
);
145 T_PASS("getdate returned properly, with the time as: %s", asctime (ptr_tm
));
147 T_EXPECT_EQ(ptr_tm
->tm_year
, 2020 - 1900, NULL
);
148 T_EXPECT_EQ(ptr_tm
->tm_mon
, 4, NULL
);
149 T_EXPECT_EQ(ptr_tm
->tm_mday
, 15, NULL
);