os161-1.99
 All Data Structures
files1.c
00001 /*
00002  * Title   : files1
00003  * Author  : Tim Brecht
00004  * Date    : Sat Oct  2 08:19:57 EST 1999
00005  *
00006  * Some example tests using open, and close.
00007  * Assumes that files named FILE1 and FILE2 do not exist in current directory
00008  *
00009  * Modified: Thu Dec 23 17:05:19 EST 2004
00010  *   TBB - updated for Winter 2005 term.
00011  *   TBB - cleaned up and moved into uw-testbin for Winter 2013
00012  */
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <errno.h>
00018 #include "../lib/testutils.h"
00019 
00020 int
00021 main()
00022 {
00023   int f1, f2;
00024   int i = 42;
00025   int j = -999;
00026   int intbuf = 0;
00027   int rc = 0;      /* return code */
00028         int save_errno = 0;
00029 
00030   /* Useful for debugging, if failures occur turn verbose on by uncommenting */
00031   // TEST_VERBOSE_ON();
00032 
00033   /* Check that open succeeds */
00034   f1 = open("FILE1", O_RDWR | O_CREAT | O_TRUNC);
00035   TEST_POSITIVE(f1, "Unable to open FILE1");
00036 
00037   /* Check that open succeeds */
00038   f2 = open("FILE2", O_RDWR | O_CREAT | O_TRUNC);
00039   TEST_POSITIVE(f2, "Unable to open FILE2");
00040 
00041   TEST_NOT_EQUAL(f1, f2, "fd f1 == f2");
00042 
00043   /* Write something simple to file 1 */
00044   rc = write(f1, (char *) &i, sizeof(i));
00045   TEST_EQUAL(rc, sizeof(i), "write to f1 does not write/return proper value");
00046 
00047   /* Write something simple to file 2 */
00048   rc = write(f2, (char *) &j, sizeof(j));
00049   TEST_EQUAL(rc, sizeof(j), "write to f2 does not write/return proper value");
00050 
00051   rc = close(f1);
00052   TEST_EQUAL(rc, SUCCESS, "close f1 failed");
00053 
00054   rc = close(f1);
00055         save_errno = errno;
00056   /* closing a second time should fail - it's already closed */
00057   TEST_NEGATIVE(rc, "close f1 second time didn't fail");
00058 
00059   rc = close(f2);
00060   TEST_EQUAL(rc, SUCCESS, "close f2 failed");
00061 
00062   f1 = open("FILE1", O_RDONLY);
00063   TEST_POSITIVE(f1, "Unable to open FILE1, after Close");
00064 
00065   f2 = open("FILE2", O_RDONLY);
00066   TEST_POSITIVE(f1, "Unable to open FILE2, after Close");
00067 
00068   TEST_NOT_EQUAL(f1, f2, "fd f1 == f2");
00069 
00070   rc = read(f1, (char *) &intbuf, sizeof(intbuf));
00071   TEST_EQUAL(rc, sizeof(intbuf), 
00072      "read from f1 does not read/return proper value");
00073   TEST_EQUAL(intbuf, i,
00074      "read from f1 did not get correct value");
00075 
00076   rc = read(f2, (char *) &intbuf, sizeof(intbuf));
00077   TEST_EQUAL(rc, sizeof(j), "read from f2 does not read/return proper value");
00078   TEST_EQUAL(intbuf, j, "read from f2 did not get correct value");
00079 
00080   TEST_STATS();
00081 
00082   exit(0);
00083 }
00084 
 All Data Structures