mirror of
https://gitlab.com/80486DX2-66/gists
synced 2024-12-27 05:40:17 +05:30
freadln.*: add testing against unexpected STDOUT flushing
This commit is contained in:
parent
53d0b8908d
commit
06c5330a67
@ -77,6 +77,24 @@ int freadln(FILE* f, char** output, size_t* length_out) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TEST
|
#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) {
|
int main(void) {
|
||||||
// stdin test
|
// stdin test
|
||||||
printf("Type something> ");
|
printf("Type something> ");
|
||||||
@ -101,7 +119,16 @@ int main(void) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
f = new_f;
|
f = new_f;
|
||||||
|
|
||||||
|
printf("Waiting for 4 seconds...\n");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
clock_t start;
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
|
#if DO_SLEEP
|
||||||
|
start = clock();
|
||||||
|
#endif
|
||||||
|
|
||||||
size_t line_length;
|
size_t line_length;
|
||||||
int result = freadln(f, &line, &line_length);
|
int result = freadln(f, &line, &line_length);
|
||||||
if (result == freadln_ERROR) {
|
if (result == freadln_ERROR) {
|
||||||
@ -114,8 +141,12 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line,
|
printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line,
|
||||||
line_length);
|
line_length);
|
||||||
|
|
||||||
|
SLEEP_FN(1000 - ((long double) (clock() - start) * 1000.l) /
|
||||||
|
CLOCKS_PER_SEC);
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user