#!/bin/sh # # ASSIGN2.TEST # # To create a test script of your data base library put this file, # assign2.test, and your files libdb.a, db.h, dbcreate.c, dbopen.c, # dbclose.c, dbread.c, dbwrite.c, dbkeysize.c, dbdatasize.c, dbhashsize.c, # dbbeginscan.c, and dbreadnext.c together in a directory. Then run # the command "sh assign2.test > test_script". This test will take a few # minutes to complete. Then edit the test_script output to include your # name, student id, and email address at the top; if you work in a group # be sure to include this information for all members of your group. Then # submit test_script as your homework # # % sh assign2.test > test_script # % emacs test_script # add your name, email address, and id # # Your source code should be in files with the names listed above. If you # use different file names for your source files, you will have to either # modify this script below, where it says SPECIFY SOURCE FILES HERE, or # you will have to edit the output of the test script so that it includes # all your sources. # # Be sure to check the test_script file before you send it in. Be sure # it has your name, id, and email address; be sure it includes a copy # of all your source code, and be sure all the tests have run correctly. # Note some of the tests will report errors even for a completely correct # library. Don't depend on this script to test your program. # # If this test script is taking a lot of time to run, stop it and look in # the temporary directory it created. If you see files with names like # .nfsXXXX, where XXXX is some number, then there is a network error that # is NOT YOUR PROBLEM. Try running the test script on a different machine # or at a different time. # # You should compile your library on Solaris using the gcc command. # It is possible to run this script on other versions of Unix, but I # cannot guarantee it will work. If you use a compiler other than gcc, # you will have to change the line CC=gcc line below. You must run this # test script on the same sort of machine used to compile your library. # echo ========================================================================== echo echo CS 311 -- Assignment II echo echo "Name: Jacob Lundberg" echo "Student ID: 541.215.251" echo "Email: lundberg@engr.orst.edu" echo echo Name: echo Student ID: echo Email: echo echo Name: echo Student ID: echo Email: echo echo date uname -a whoami echo echo ========================================================================== echo echo echo CC=gcc # CHANGE THIS LINE IF USING SOME OTHER COMPILER LS=/bin/ls RM=rm PS=/bin/ps CP=cp OD=od MKDIR=mkdir SLEEP=sleep TESTDIR=assign2.test.$$ # SPECIFY SOURCE FILES HERE for file in db.h dbbeginscan.c dbcreate.c dbfree.c dbhashsize.c dblock.c dbread.c dbwrite.c dbclose.c dbdatasize.c dbhash.c dbkeysize.c dbopen.c dbreadnext.c do echo -------------------------------------------------------------------------- echo Source File: $file echo cat $file 2>&1 echo echo done # build test directory if test -d $TESTDIR then echo Please rename or remove $TESTDIR exit 1 fi $MKDIR $TESTDIR 2>&1 $CP libdb.a db.h $TESTDIR 2>&1 cd $TESTDIR 2>&1 echo -------------------------------------------------------------------------- echo Writing Test Program cat > assign2.c <<\___EOF___ #include "db.h" #include #include #include #include void do_create( int argc, char *argv[] ); void do_write( int argc, char *argv[] ); void do_read_one( int argc, char *argv[] ); void do_read( int argc, char *argv[] ); void do_scan( int argc, char *argv[] ); void do_multi_file( int argc, char *argv[] ); void fail( char *str ); void succeed( char *str ); void message( char *str ); int main( int argc, char *argv[] ) { if( argc <= 1 ) fail( "no command" ); if( strcmp( argv[1], "create" ) == 0 ) do_create( argc-1, argv+1 ); else if( strcmp( argv[1], "write" ) == 0 ) do_write( argc-1, argv+1 ); else if( strcmp( argv[1], "read_one" ) == 0 ) do_read_one( argc-1, argv+1 ); else if( strcmp( argv[1], "read" ) == 0 ) do_read( argc-1, argv+1 ); else if( strcmp( argv[1], "scan" ) == 0 ) do_scan( argc-1, argv+1 ); else if( strcmp( argv[1], "multi_file" ) == 0 ) do_multi_file( argc-1, argv+1 ); else fail( "unrecognized command" ); } /* create file_name key_size data_size hash_size */ void do_create( int argc, char *argv[] ) { int i,key_size,data_size,hash_size; char *file_name; file_name = argv[1]; key_size = atoi( argv[2] ); data_size = atoi( argv[3] ); hash_size = atoi( argv[4] ); if( data_size > 4000 ) fail( "create: data_size too large" ); if( db_create( file_name, key_size, data_size, hash_size ) != 0 ) fail( "create: db_create failed" ); succeed( "create: succeeded" ); } /* write file_name start_key end_key data */ void do_write( int argc, char *argv[] ) { int i,start_key,end_key; char *file_name,*data; DB_FILE *dbp; char buf[4000]; char key[DB_KEY_MAX]; file_name = argv[1]; start_key = atoi( argv[2] ); end_key = atoi( argv[3] ); data = argv[4]; if( (dbp=db_open( file_name )) == (DB_FILE *)NULL ) fail( "write: db_open failed" ); for( i=end_key; i>=start_key; i-- ) { memset( buf, 'x', sizeof(buf) ); sprintf( buf, "data %d %s", i, data ); buf[strlen(buf)] = 'x'; sprintf( key, "key %d", i ); if( db_write( dbp, key, buf, 4000 ) != 0 ) fail( "write: db_write failed" ); } if( db_close( dbp ) != 0 ) fail( "write: db_close failed" ); succeed( "write: succeeded" ); } /* read_one file_name key ... */ void do_read_one( int argc, char *argv[] ) { int i,ret; char *file_name,*key; DB_FILE *dbp; char buf[4000]; char msg[10000]; file_name = argv[1]; if( (dbp=db_open( file_name )) == (DB_FILE *)NULL ) fail( "read_one: db_open failed" ); for( i=2; i 0 ) { sprintf( msg, "read_one: db_read key %s not found, data: %s", key, buf ); message( msg ); } else { sprintf( msg, "read_one: %s, %s", key, buf ); message( msg ); } } if( db_close( dbp ) != 0 ) fail( "read_one: db_close failed" ); succeed( "read_one: succeeded" ); } /* read file_name start_key end_key */ void do_read( int argc, char *argv[] ) { int i,num,ret,start_key,end_key; char *file_name; DB_FILE *dbp; char key[DB_KEY_MAX]; char buf[4000]; char msg[10000]; file_name = argv[1]; start_key = atoi( argv[2] ); end_key = atoi( argv[3] ); if( (dbp=db_open( file_name )) == (DB_FILE *)NULL ) fail( "read: db_open failed" ); for( i=start_key; i<=end_key; i++ ) { sprintf( key, "key %d", i ); if( (ret=db_read( dbp, key, buf )) != 0 ) { sprintf( msg, "read: db_read failed on key %s, ret=%d", key, ret ); fail( msg ); } ret = sscanf( buf, "data %d", &num ); if( num != i || ret != 1 ) { sprintf( msg, "read: db_read wrong data on key %s, data is %s", key, buf ); fail( msg ); } } if( db_close( dbp ) != 0 ) fail( "read: db_close failed" ); succeed( "read: succeeded" ); } /* scan file_name */ void do_scan( int argc, char *argv[] ) { int i,ret; char *file_name; DB_FILE *dbp; char key[DB_KEY_MAX]; char buf[4000]; char msg[10000]; file_name = argv[1]; if( (dbp=db_open( file_name )) == (DB_FILE *)NULL ) fail( "scan: db_open failed" ); db_begin_scan( dbp ); while( (ret=db_read_next( dbp, key, buf )) == 0 ) { sprintf( msg, "scan: %s, %s", key, buf ); message( msg ); } if( ret < 0 ) fail( "scan: db_read_next failed" ); if( db_close( dbp ) != 0 ) fail( "scan: db_close failed" ); succeed( "scan: succeeded" ); } /* multi_file file_name1 file_name2 file_name3 key1 key2 key3 */ void do_multi_file( int argc, char *argv[] ) { int num; char *file_name1,*file_name2,*file_name3; char *key1,*key2,*key3; DB_FILE *dbp1,*dbp2,*dbp3; char buf1[1000]; char buf2[1000]; char buf3[1000]; char msg[10000]; file_name1 = argv[1]; file_name2 = argv[2]; file_name3 = argv[3]; key1 = argv[4]; key2 = argv[5]; key3 = argv[6]; if( (dbp1=db_open( file_name1 )) == (DB_FILE *)NULL ) fail( "multi_file: db_open on 1 failed" ); if( (dbp2=db_open( file_name2 )) == (DB_FILE *)NULL ) fail( "multi_file: db_open on 2 failed" ); if( (dbp3=db_open( file_name3 )) == (DB_FILE *)NULL ) fail( "multi_file: db_open on 3 failed" ); memset( buf1, '\0', sizeof(buf1) ); if( db_read( dbp1, key1, buf1 ) != 0 ) { fail( "multi_file: db_read on key 1 failed" ); } else { sprintf( msg, "multi_file: read 1, %s, %s", key1, buf1 ); message( msg ); } memset( buf2, '\0', sizeof(buf2) ); if( db_read( dbp2, key2, buf2 ) != 0 ) { fail( "multi_file: db_read on key 2 failed" ); } else { sprintf( msg, "multi_file: read 2, %s, %s", key2, buf2 ); message( msg ); } memset( buf1, '\0', sizeof(buf1) ); strcpy( buf1, "new data written on 1" ); if( db_write( dbp1, key1, buf1, 1000 ) != 0 ) fail( "multi_file: db_write on 1 failed" ); memset( buf3, '\0', sizeof(buf3) ); if( db_read( dbp3, key3, buf3 ) != 0 ) { fail( "multi_file: db_read on 3 failed" ); } else { sprintf( msg, "multi_file: read 3, %s, %s", key3, buf3 ); message( msg ); } memset( buf3, '\0', sizeof(buf3) ); strcpy( buf3, "new data written on 3" ); if( db_write( dbp3, key3, buf3, 1000 ) != 0 ) fail( "multi_file: db_write on 3 failed" ); memset( buf2, '\0', sizeof(buf2) ); strcpy( buf2, "new data written on 2" ); if( db_write( dbp2, key2, buf2, 1000 ) != 0 ) fail( "multi_file: db_write on 2 failed" ); if( db_close( dbp1 ) != 0 ) fail( "multi_file: db_close on 1 failed" ); if( db_close( dbp3 ) != 0 ) fail( "multi_file: db_close on 3 failed" ); if( db_close( dbp2 ) != 0 ) fail( "multi_file: db_close on 2 failed" ); succeed( "multi_file: succeeded" ); } void fail( char *str ) { printf( "%s\n", str ); exit( 1 ); } void succeed( char *str ) { printf( "%s\n", str ); exit( 0 ); } void message( char *str ) { printf( "%s\n", str ); } ___EOF___ echo echo echo -------------------------------------------------------------------------- echo Building Test Program echo $CC -o assign2 assign2.c libdb.a 2>&1 echo echo echo -------------------------------------------------------------------------- echo Library Test echo cat > file1.c < file2.c < file3.c <&1 $CC -o file2 file2.c libdb.a 2>&1 $CC -o file3 file3.c libdb.a 2>&1 $LS -l file[123] 2>&1 $RM -f file[123]* 2>&1 echo echo echo -------------------------------------------------------------------------- echo Create Database File echo ./assign2 create db1 10 34 5 2>&1 echo echo echo -------------------------------------------------------------------------- echo Dump Database File echo $LS -l db1 2>&1 echo $OD -c db1 2>&1 echo echo echo -------------------------------------------------------------------------- echo Write Database File echo ./assign2 write db1 1003 1005 A 2>&1 echo echo echo -------------------------------------------------------------------------- echo Dump Database File echo $LS -l db1 2>&1 echo $OD -c db1 2>&1 echo echo echo -------------------------------------------------------------------------- echo Read Database File echo ./assign2 read_one db1 "key 1003" "key 1004" "key 1005" 2>&1 echo echo echo -------------------------------------------------------------------------- echo Write Database File, Collisions echo ./assign2 write db1 1008 1008 B 2>&1 echo ./assign2 write db1 1014 1014 C 2>&1 echo ./assign2 write db1 1019 1019 D 2>&1 echo echo echo -------------------------------------------------------------------------- echo Dump Database File echo $LS -l db1 2>&1 echo $OD -c db1 2>&1 echo echo echo -------------------------------------------------------------------------- echo Read Database File echo ./assign2 read_one db1 "key 1019" "key 1014" "key 1008" "key 1003" 2>&1 echo echo echo -------------------------------------------------------------------------- echo Scan Database File echo ./assign2 scan db1 2>&1 echo echo echo -------------------------------------------------------------------------- echo Read Test echo ./assign2 create db2 20 24 100 2>&1 echo ./assign2 write db2 1 200 A 2>&1 echo ./assign2 read db2 1 200 2>&1 echo echo echo -------------------------------------------------------------------------- echo Try To Create Database File Again echo ./assign2 create db1 20 100 1000 2>&1 echo $OD -c db1 | head -1 2>&1 echo echo echo -------------------------------------------------------------------------- echo Multiple File Test echo ./assign2 create db3 10 30 100 2>&1 echo ./assign2 create db4 15 30 150 2>&1 echo ./assign2 create db5 20 30 300 2>&1 echo ./assign2 write db3 10 11 A 2>&1 echo ./assign2 write db4 11 12 B 2>&1 echo ./assign2 write db5 12 13 C 2>&1 echo ./assign2 multi_file db3 db4 db5 "key 10" "key 12" "key 12" 2>&1 echo ./assign2 read_one db3 "key 10" "key 11" 2>&1 echo ./assign2 read_one db4 "key 11" "key 12" 2>&1 echo ./assign2 read_one db5 "key 12" "key 13" 2>&1 echo echo echo -------------------------------------------------------------------------- echo Simultaneous Write Test echo ./assign2 create db6 10 24 2 2>&1 echo ./assign2 write db6 1 50 A 2>&1 & PID1=$! ./assign2 write db6 1 50 B 2>&1 & PID2=$! while ($PS | grep "^ *$PID1" > /dev/null) do echo waiting... sleep 2 done while ($PS | grep "^ *$PID2" > /dev/null) do echo waiting... sleep 2 done echo ./assign2 scan db6 | wc -l 2>&1 echo ./assign2 scan db6 2>&1 echo echo # clean up cd .. $RM -rf $TESTDIR 2>&1