writeread.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Tim Brecht
  2. * Added :Sat 5 Jan 2013 15:19:15 EST
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include "../lib/testutils.h"
  11. #define NUM_TIMES (1)
  12. #define NUM_INTS (4*1024)
  13. int
  14. main()
  15. {
  16. int i, rc, fd;
  17. int write_array[NUM_INTS];
  18. int read_array[NUM_INTS];
  19. /* Uncomment this when having failures and for debugging */
  20. // TEST_VERBOSE_ON();
  21. /* Initialize the array */
  22. for (i=0; i<NUM_INTS; i++) {
  23. write_array[i] = i;
  24. }
  25. /* Open the file for writing */
  26. fd = open("WRITE_READ_FILE", O_WRONLY | O_CREAT);
  27. TEST_POSITIVE(fd, "Open file named WRITE_READ_FILE failed\n");
  28. for (i=0; i<NUM_TIMES; i++) {
  29. rc = write(fd, write_array, sizeof(write_array));
  30. TEST_EQUAL(rc, sizeof(write_array), "Failed to write all of the array");
  31. }
  32. close(fd);
  33. /* Open the file */
  34. fd = open("WRITE_READ_FILE", O_RDONLY);
  35. TEST_POSITIVE(fd, "Open file named WRITE_READ_FILE failed\n");
  36. for (i=0; i<NUM_TIMES; i++) {
  37. rc = read(fd, read_array, sizeof(read_array));
  38. TEST_EQUAL(rc, sizeof(read_array), "Failed to read all of the array");
  39. for (i=0; i<NUM_INTS; i++) {
  40. TEST_EQUAL(read_array[i], write_array[i], "Value read not equal to value written");
  41. }
  42. }
  43. TEST_STATS();
  44. exit(0);
  45. }