1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-27 05:40:17 +05:30
gists/c-programming/experiments/clock-malfunction-imitation.c

196 lines
4.2 KiB
C
Raw Normal View History

2024-01-25 22:53:51 +05:30
/*
* clock-malfunction-imitation.c
*
* The imitations include:
* - time change
* - clock overflow
* - clock hang
* - clock reverse
* - exponential growth of time
*
2024-01-25 22:53:51 +05:30
* Author: Intel A80486DX2-66
2024-04-26 01:46:39 +05:30
* License: Unlicense
2024-01-25 22:53:51 +05:30
*/
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define EXIT_IN 2U
#define SECOND_PRECISION 1L
#define TIME_BEFORE_ACCIDENT 2UL
#define TEST_LABEL(s) do { \
printf("Test: %s\n", s); \
fflush(stdout); \
} while (0)
2024-01-25 22:53:51 +05:30
#define CLOCK_FUN_BEGIN \
static unsigned ticks_before_accident = TIME_BEFORE_ACCIDENT; \
time_t y
2024-01-25 22:53:51 +05:30
#define CLOCK_FUN_END \
if (x != NULL) \
*x = y; \
\
return y
2024-01-25 22:53:51 +05:30
time_t clock_change_time(time_t* x);
time_t clock_exponential_growth(time_t* x);
time_t clock_hang(time_t* x);
time_t clock_overflow(time_t* x);
time_t clock_reverse(time_t* x);
void time_flow_test(time_t (*time_function)(time_t*));
time_t clock_change_time(time_t* x) {
CLOCK_FUN_BEGIN;
2024-01-25 22:53:51 +05:30
time(&y);
if (ticks_before_accident == 0) {
y = 0;
size_t max = sizeof(time_t) / sizeof(int),
intmax_plus_1 = (size_t) INT_MAX + 1;
2024-01-25 22:53:51 +05:30
for (size_t i = 0; i < max; i++)
y |= rand() % intmax_plus_1;
2024-01-25 22:53:51 +05:30
} else
ticks_before_accident--;
CLOCK_FUN_END;
2024-01-25 22:53:51 +05:30
}
time_t clock_exponential_growth(time_t* x) {
CLOCK_FUN_BEGIN;
2024-01-25 22:53:51 +05:30
static time_t counter = 1;
time(&y);
if (ticks_before_accident == 0) {
y += counter++;
counter *= counter;
} else
2024-01-25 22:53:51 +05:30
ticks_before_accident--;
CLOCK_FUN_END;
2024-01-25 22:53:51 +05:30
}
time_t clock_hang(time_t* x) {
CLOCK_FUN_BEGIN;
2024-01-25 22:53:51 +05:30
static time_t last_time;
if (ticks_before_accident == 0)
y = last_time;
else {
time(&y);
last_time = y;
ticks_before_accident--;
}
CLOCK_FUN_END;
2024-01-25 22:53:51 +05:30
}
time_t clock_overflow(time_t* x) {
CLOCK_FUN_BEGIN;
2024-01-25 22:53:51 +05:30
time(&y);
if (ticks_before_accident == 0)
y = LONG_MAX - y;
else
ticks_before_accident--;
CLOCK_FUN_END;
2024-01-25 22:53:51 +05:30
}
time_t clock_reverse(time_t* x) {
CLOCK_FUN_BEGIN;
2024-01-25 22:53:51 +05:30
static time_t counter = 1, old_time;
if (ticks_before_accident == 0)
2024-01-25 22:53:51 +05:30
y = old_time - counter++;
else {
2024-01-25 22:53:51 +05:30
time(&y);
old_time = y;
ticks_before_accident--;
}
CLOCK_FUN_END;
2024-01-25 22:53:51 +05:30
}
void time_flow_test(time_t (*time_function)(time_t*)) {
time_t current_time, previous_time;
double time_diff;
// Get the current time
time(&current_time);
previous_time = current_time;
bool first_time_passed = false;
unsigned i = 0;
for (;;) {
// Get the current time
time_function(&current_time);
// Calculate the time difference in seconds
time_diff = difftime(current_time, previous_time);
if (!first_time_passed) {
time_diff = (double) SECOND_PRECISION;
first_time_passed = true;
}
printf("time difference: %f", time_diff);
fflush(stdout);
// Check if the time difference is not normal
2024-01-25 22:53:51 +05:30
if (time_diff != (double) SECOND_PRECISION) {
printf(" -> Abnormal time flow detected!");
fflush(stdout);
if (++i >= EXIT_IN) {
puts("\nAborting");
2024-01-25 22:53:51 +05:30
fflush(stdout);
break;
}
}
putchar('\n');
2024-01-25 22:53:51 +05:30
fflush(stdout);
// Update the previous time
previous_time = current_time;
usleep(SECOND_PRECISION * 1000000L);
first_time_passed = true;
}
putchar('\n');
2024-01-25 22:53:51 +05:30
fflush(stdout);
}
int main(void) {
srand(time(NULL));
TEST_LABEL("clock_change_time");
2024-01-25 22:53:51 +05:30
time_flow_test(&clock_change_time);
TEST_LABEL("clock_exponential_growth");
2024-01-25 22:53:51 +05:30
time_flow_test(&clock_exponential_growth);
TEST_LABEL("clock_hang");
2024-01-25 22:53:51 +05:30
time_flow_test(&clock_hang);
TEST_LABEL("clock_overflow");
2024-01-25 22:53:51 +05:30
time_flow_test(&clock_overflow);
TEST_LABEL("clock_reverse");
2024-01-25 22:53:51 +05:30
time_flow_test(&clock_reverse);
return 0;
}