Files
RedBear-OS/tests/time/mktime.c
T
Tibor Nagy a92be000fb tests: Even more work on error handling
realpath: Fixing undefined behaviour in the second test. If the call fails the resolved_name argument cannot be used for error checking because its state is undefined by SUSv2.
pipe: Changing the order of close and write error handling code. Errors in close could overwrite errno after write errors, returning incorrect error messages.
gmtime: Removed duplicate checks
Other fixes for fseek, rename, mktime, putwchar
2019-02-24 22:02:11 +01:00

61 lines
1.4 KiB
C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "test_helpers.h"
int check(time_t input) {
struct tm *t = localtime(&input);
ERROR_IF(localtime, t, == NULL);
time_t output = mktime(t);
ERROR_IF(mktime, output, == (time_t)-1);
printf("%ld = %ld\n", input, output);
if (input != output) {
printf(
"Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n",
t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec
);
puts("Failed!");
return 1;
}
return 0;
}
int main(void) {
struct tm t = { 0 };
t.tm_year = 71;
t.tm_mday = 1;
printf("%ld\n", mktime(&t));
int day = 60 * 60 * 24;
time_t inputs[] = { -(day * 33), -day, -500, 0, 1531454950 };
for (int i = 0; i < 5; i += 1) {
if (check(inputs[i])) {
exit(EXIT_FAILURE);
}
}
srand(time(NULL));
for (int i = 0; i < 10; i += 1) {
time_t input = (time_t) rand();
struct tm *time = localtime(&input);
ERROR_IF(localtime, time, == NULL);
time_t output = mktime(time);
ERROR_IF(mktime, output, == (time_t)-1);
if (input != output) {
// asctime has newline
printf("Comparison %ld == %ld failed. Time: %s", input, output, asctime(time));
}
}
}