freopen.c 833 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <wchar.h>
  4. #include "test_helpers.h"
  5. int test_reopen_opens_file(void) {
  6. FILE *f = freopen("stdio/stdio.in", "r", stdin);
  7. ERROR_IF(freopen, f, == NULL);
  8. char in[6];
  9. fgets(in, 6, stdin);
  10. printf("%s\n", in); // should print Hello
  11. fclose(f);
  12. return 0;
  13. }
  14. int test_reopen_resets_orientation(void) {
  15. FILE *f = freopen("stdio/stdio.in", "r", stdin);
  16. assert(fwide(f, 0) == 0);
  17. assert(fwide(f, -1) == -1);
  18. f = freopen("stdio/stdio.in", "r", stdin);
  19. assert(fwide(f, 0) == 0);
  20. fclose(f);
  21. return 0;
  22. }
  23. int main(void) {
  24. int(*tests[])(void) = {
  25. &test_reopen_opens_file,
  26. &test_reopen_resets_orientation,
  27. };
  28. for(int i=0; i<sizeof(tests)/sizeof(int(*)(void)); i++) {
  29. printf("%d\n", (*tests[i])());
  30. }
  31. }