files1.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Title : files1
  3. * Author : Tim Brecht
  4. * Date : Sat Oct 2 08:19:57 EST 1999
  5. *
  6. * Some example tests using open, and close.
  7. * Assumes that files named FILE1 and FILE2 do not exist in current directory
  8. *
  9. * Modified: Thu Dec 23 17:05:19 EST 2004
  10. * TBB - updated for Winter 2005 term.
  11. * TBB - cleaned up and moved into uw-testbin for Winter 2013
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include "../lib/testutils.h"
  18. int
  19. main()
  20. {
  21. int f1, f2;
  22. int i = 42;
  23. int j = -999;
  24. int intbuf = 0;
  25. int rc = 0; /* return code */
  26. int save_errno = 0;
  27. /* Useful for debugging, if failures occur turn verbose on by uncommenting */
  28. // TEST_VERBOSE_ON();
  29. /* Check that open succeeds */
  30. f1 = open("FILE1", O_RDWR | O_CREAT | O_TRUNC);
  31. TEST_POSITIVE(f1, "Unable to open FILE1");
  32. /* Check that open succeeds */
  33. f2 = open("FILE2", O_RDWR | O_CREAT | O_TRUNC);
  34. TEST_POSITIVE(f2, "Unable to open FILE2");
  35. TEST_NOT_EQUAL(f1, f2, "fd f1 == f2");
  36. /* Write something simple to file 1 */
  37. rc = write(f1, (char *) &i, sizeof(i));
  38. TEST_EQUAL(rc, sizeof(i), "write to f1 does not write/return proper value");
  39. /* Write something simple to file 2 */
  40. rc = write(f2, (char *) &j, sizeof(j));
  41. TEST_EQUAL(rc, sizeof(j), "write to f2 does not write/return proper value");
  42. rc = close(f1);
  43. TEST_EQUAL(rc, SUCCESS, "close f1 failed");
  44. rc = close(f1);
  45. save_errno = errno;
  46. /* closing a second time should fail - it's already closed */
  47. TEST_NEGATIVE(rc, "close f1 second time didn't fail");
  48. rc = close(f2);
  49. TEST_EQUAL(rc, SUCCESS, "close f2 failed");
  50. f1 = open("FILE1", O_RDONLY);
  51. TEST_POSITIVE(f1, "Unable to open FILE1, after Close");
  52. f2 = open("FILE2", O_RDONLY);
  53. TEST_POSITIVE(f1, "Unable to open FILE2, after Close");
  54. TEST_NOT_EQUAL(f1, f2, "fd f1 == f2");
  55. rc = read(f1, (char *) &intbuf, sizeof(intbuf));
  56. TEST_EQUAL(rc, sizeof(intbuf),
  57. "read from f1 does not read/return proper value");
  58. TEST_EQUAL(intbuf, i,
  59. "read from f1 did not get correct value");
  60. rc = read(f2, (char *) &intbuf, sizeof(intbuf));
  61. TEST_EQUAL(rc, sizeof(j), "read from f2 does not read/return proper value");
  62. TEST_EQUAL(intbuf, j, "read from f2 did not get correct value");
  63. TEST_STATS();
  64. exit(0);
  65. }