fwide.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <wchar.h>
  4. int test_initial_orientation(void) {
  5. FILE *f = tmpfile();
  6. assert(fwide(f, 0) == 0);
  7. return 0;
  8. }
  9. int test_manual_byte_orientation(void) {
  10. FILE *f = tmpfile();
  11. // set manually to byte orientation
  12. assert(fwide(f, -483) == -1);
  13. // Cannot change to wchar orientation
  14. assert(fwide(f, 1) == -1);
  15. fclose(f);
  16. return 0;
  17. }
  18. int test_manual_wchar_orientation(void) {
  19. FILE *f = tmpfile();
  20. // set manually to wchar orientation
  21. assert(fwide(f, 483) == 1);
  22. // Cannot change to byte orientation
  23. assert(fwide(f, -1) == 1);
  24. fclose(f);
  25. return 0;
  26. }
  27. int test_orientation_after_fprintf(void) {
  28. // open file and write bytes; implicitly setting the bytes orientation
  29. FILE *f = tmpfile();
  30. assert(fprintf(f, "blah\n") == 5);
  31. // Check that bytes orientation is set
  32. assert(fwide(f, 0) == -1);
  33. fclose(f);
  34. return 0;
  35. }
  36. int main() {
  37. int(*tests[])(void) = {
  38. &test_initial_orientation,
  39. &test_manual_byte_orientation,
  40. &test_manual_wchar_orientation,
  41. &test_orientation_after_fprintf,
  42. };
  43. for(int i=0; i<sizeof(tests)/sizeof(int(*)(void)); i++) {
  44. printf("%d\n", (*tests[i])());
  45. }
  46. }