]> git.saurik.com Git - apple/libc.git/blob - tests/getdate.c
Libc-1158.50.2.tar.gz
[apple/libc.git] / tests / getdate.c
1 #include <limits.h>
2 #include <stdio.h>
3 #include <time.h>
4 #include <stdlib.h>
5
6 #include <darwintest.h>
7 #include <darwintest_utils.h>
8
9 //#include "../stdtime/getdate.c"
10
11 extern int getdate_err;
12
13 __unused static void
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);
17 }
18
19 static char *
20 create_template_file(const char *name, const char *contents)
21 {
22 T_SETUPBEGIN;
23 char *template_path;
24 asprintf(&template_path, "%s/%s", dt_tmpdir(), name);
25 T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(template_path, NULL);
26
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);
31 T_SETUPEND;
32
33 return template_path;
34 }
35
36 T_DECL(getdate3, "getdate() using \"%a\" with match")
37 {
38 struct tm tm = {};
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");
42
43 char *template_path = create_template_file("a3", "%a\n");
44 setenv("DATEMSK", template_path, 1);
45 free(template_path);
46
47 getdate_err = 0;
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");
53 }
54
55 T_DECL(getdate4, "getdate() using \"%A\" with match")
56 {
57 struct tm tm = {};
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");
61
62 char *template_path = create_template_file("a4", "%A\n");
63 setenv("DATEMSK", template_path, 1);
64 free(template_path);
65
66 getdate_err = 0;
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");
72 }
73
74 T_DECL(getdate15, "getdate() using \"%w\" with match")
75 {
76 char *template_path = create_template_file("a4", "%w\n");
77 setenv("DATEMSK", template_path, 1);
78 free(template_path);
79
80 getdate_err = 0;
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");
86
87 getdate_err = 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");
93 }
94
95 T_DECL(getdate46, "getdate() using \"%%t\" without match")
96 {
97 char *template_path = create_template_file("a46", "%t\n");
98 setenv("DATEMSK", template_path, 1);
99 free(template_path);
100
101 getdate_err = 0;
102 struct tm *ptr_tm = getdate("there'snotemplateforthis.");
103 if (ptr_tm) {
104 T_FAIL("getdate returned: %s", asctime (ptr_tm));
105 }
106 /* Now check for the getdate_err */
107 T_EXPECT_EQ(getdate_err, 7, NULL);
108 }
109
110 T_DECL(getdate21, "getdate() using \"%D\" with match")
111 {
112 struct tm tm = {};
113 char *remainder = strptime("1/1/38", "%D", &tm);
114 T_EXPECT_NOTNULL(remainder, "strptime(\"1/1/38\", \"%%D\", &tm)");
115
116 char *template_path = create_template_file("a21", "%D\n");
117 setenv("DATEMSK", template_path, 1);
118 free(template_path);
119
120 getdate_err = 0;
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));
125
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);
129 }
130
131 T_DECL(getdate53, "getdate() using \"%d+%m+%y %C\" with match")
132 {
133 struct tm tm = {};
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)");
136
137 char *template_path = create_template_file("a53", "%d+%m+%y %C\n");
138 setenv("DATEMSK", template_path, 1);
139 free(template_path);
140
141 getdate_err = 0;
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));
146
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);
150 }