tst-fmemopen.c 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. static char *text_input = "1 23 43";
  5. static const char *good_answer = "1 529 1849 ";
  6. static int
  7. do_test (void)
  8. {
  9. FILE *out, *in;
  10. int v, s;
  11. size_t size;
  12. char *ptr;
  13. in = fmemopen(text_input, strlen(text_input), "r");
  14. if (in == NULL) {
  15. perror("fmemopen");
  16. return 1;
  17. }
  18. out = open_memstream(&ptr, &size);
  19. if (out == NULL) {
  20. perror("open_memstream");
  21. return 1;
  22. }
  23. for (;;) {
  24. s = fscanf(in, "%d", &v);
  25. if (s <= 0)
  26. break;
  27. s = fprintf(out, "%d ", v * v);
  28. if (s == -1) {
  29. puts("fprintf failed");
  30. exit(1);
  31. }
  32. }
  33. fclose(in);
  34. fclose(out);
  35. if (size != strlen(good_answer) || strcmp(good_answer, ptr) != 0) {
  36. printf("failed: size=%zu; ptr=%s\n", size, ptr);
  37. exit(1);
  38. }
  39. free(ptr);
  40. exit(0);
  41. }
  42. #define TEST_FUNCTION do_test ()
  43. #include "../test-skeleton.c"