1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-26 11:30:03 +05:30

freadln.*: add testing against unexpected STDOUT flushing

This commit is contained in:
Intel A80486DX2-66 2024-03-10 15:40:51 +03:00
parent 53d0b8908d
commit 06c5330a67
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -77,6 +77,24 @@ int freadln(FILE* f, char** output, size_t* length_out) {
}
#ifdef TEST
# if defined(_WIN32) || defined(WIN32)
# include <windows.h>
# define SLEEP_FN Sleep
# define DO_SLEEP 1
# elif defined(__CYGWIN__) || defined(__unix__) || defined(__APPLE__) && \
defined(__MACH__) || defined(__linux__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || \
defined(__DragonFly__) || defined(__MINGW32__) || defined(__MINGW64__)
# include <unistd.h>
# define SLEEP_FN usleep
# define DO_SLEEP 1
# else
# define DO_SLEEP 0
# endif
# if DO_SLEEP
# include <time.h>
# endif
int main(void) {
// stdin test
printf("Type something> ");
@ -101,7 +119,16 @@ int main(void) {
exit(EXIT_FAILURE);
}
f = new_f;
printf("Waiting for 4 seconds...\n");
fflush(stdout);
clock_t start;
for (int i = 0; i < 4; i++) {
#if DO_SLEEP
start = clock();
#endif
size_t line_length;
int result = freadln(f, &line, &line_length);
if (result == freadln_ERROR) {
@ -114,8 +141,12 @@ int main(void) {
}
printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line,
line_length);
SLEEP_FN(1000 - ((long double) (clock() - start) * 1000.l) /
CLOCKS_PER_SEC);
}
fclose(f);
fflush(stdout);
return 0;
}