📄 pblkftst.c
字号:
/* pblkftst.c - interactive PBL key file test frame Copyright (C) 2002 Peter Graf This file is part of PBL - The Program Base Library. PBL is free software. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For more information on the Program Base Library or Peter Graf, please see: http://mission.base.com/. $Log: pblkftst.c,v $ Revision 1.1 2002/09/12 20:47:08 peter Initial revision------------------------------------------------------------------------------*//* * make sure "strings <exe> | grep Id | sort -u" shows the source file versions */static char * rcsid = "$Id: pblkftst.c,v 1.1 2002/09/12 20:47:08 peter Exp $";static int rcsid_fkt() { return( rcsid ? 0 : rcsid_fkt() ); }#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>#include "pbl.h"static FILE * log;static FILE * infile;static void putChar( int c ){ static int last = 0; if( last == '\n' && c == '\n' ) return; last = c; ( void ) putc( last, log );}static int getChar( void ){ int c; c = getc( infile ); /* * a '#' starts a comment for the rest of the line */ if( c == '#') { while( c != '\n' && c != EOF ) { c = getc( infile ); } } if( c != EOF ) putChar( c ); return( c );} static void getWord( char * buffer ){ int c; /* * skip preceeding blanks */ c = ' '; while( c== '\t' || c == ' ' || c == '\n' ) c = getChar(); /* * read one word */ for(;; c = getChar()) { if( c == EOF ) { exit( 0 ); } if( c== '\t' || c == ' ' || c == '\n' ) { *buffer = '\0'; return; } *buffer++ = c; }}static int commastrlen( char * s ){ char *ptr = s; while( ptr && *ptr && *ptr != ',' ) { ptr++; } return( ptr - s );}static void commatolength( char * s ){ while( s && *s ) { if( *s == ',' ) { *s = 0xff & commastrlen( s + 1 ); } s++; }} /** * test frame for the key file library * * This test frame calls the key file library, * it is an interactive test frame capable of regression tests. * * <B>Interactive mode:</B> * <UL> * Call the program pblkftst from a UNIX or DOS shell. * <BR> * The following commands to test the PBL Key File Library are supplied: * <UL> * <PRE> q FOR QUIT create <filename> open <filename> <update> close | flush | delete insert <key> <data> ninsert <n> <key> <data> find <key> < LT | LE | FI | EQ | LA | GE | GT > update <data> ndelete <n> first | last | next | prev | this | read list <n> </PRE> * </UL> * </UL> * <B>Interactive mode example:</B> * <UL> * The following short examples demonstrates how to use the interactive * test frame. Lines starting with a # are printed by the program * lines starting with other characters are user input. * <UL> * <PRE>pblkftst##command: create /tmp/keytest# create filename # pblKfCreate( /tmp/keytest )# ok!##command: insert testkey testdata# insert key, data# pblKfInsert( 1, testkey, 8, testdata, 9 )# rc 0, pbl_errno 0, errno 0##command: first# pblKfFirst( 1 )# datalen 9, key testkey, keylen 8##command: close# pblKfClose( 1 )# rc 0, pbl_errno 0, errno 0##command: quit </PRE> * </UL> * </UL> * <B>Regression mode:</B> * <UL> * Put a sequence of test commands into a test batch file. * <P> * Example: * <UL> * Open a new file KEY0001.TST and add the following lines * <PRE>create /tmp/keytest2insert testkey testdatafirstclosequit</PRE></UL> * Then run pblkftst KEY0001.TST * * The program creates a log file called <B>pblkftst.log</B> when run. * This log file can be used as regression input file for further * tests. * <UL><PRE>##command:create # create filename/tmp/key0001# pblKfCreate( /tmp/key0001 )# ok!##command:insert # insert key, datatestkey testdata# pblKfInsert( 1, testkey, 8, testdata, 9 )# rc 0, pbl_errno 0, errno 0##command:first# pblKfFirst( 1 )# datalen 9, key testkey, keylen 8##command:close# pblKfClose( 1 )# rc 0, pbl_errno 0, errno 0##command:quit</PRE></UL> * Keep the contents of this file and diff it with the outout of * the KEY0001.TST testcase you created whenever you change * the code of the library. * * See \Ref{pblISAMFILE_TestFrame} for an example of regression * tests with a test frame. The regression test cases given with * \Ref{pblISAMFILE_TestFrame} of course also test the PBL Key File * library. * </UL> */int pblKEYFILE_TestFrame( int argc, char * argv[] ){ char command[ 2048 ]; char filename [ 2048 ]; char buffer [ 2048 ]; int update; int ival; int dowork = 1; long rc; int i; int len; pblKeyFile_t * kf = ( pblKeyFile_t *) 0; infile = stdin; if( argc > 1 ) { infile = fopen( argv[ 1 ], "r" ); if( !infile ) { fprintf( stderr, "Failed to open %s, %s\n", argv[ 1 ], strerror( errno )); exit( -1 ); } } log = fopen( "./pblkftst.log", "w" ); if( !log ) { fprintf( stderr, "cant open logfile, .\\pblkftst.log\n" ); exit( 1 ); } while( dowork ) { memset( command, 0, sizeof( command )); memset( filename, 0, sizeof( filename )); memset( buffer, 0, sizeof( buffer )); errno = 0; printf( "\n##command: \n" ); fprintf( log, "\n##command: \n" ); getWord( command ); if( command[0] == 'q' || command[0] == 'Q' ) { dowork = 0; } else if( !strcmp( command, "create" )) { printf( "# create filename \n" ); fprintf( log, "# create filename \n" ); getWord( filename ); printf( "# pblKfCreate( %s )\n", filename ); fprintf( log, "# pblKfCreate( %s )\n", filename ); if( kf ) ( void ) pblKfClose( kf ); kf = pblKfCreate( filename, NULL ); if( kf ) { printf( "# ok!\n" ); fprintf( log, "# ok!\n" ); } else { printf( "# pbl_errno %d, errno %d\n", pbl_errno, errno ); fprintf( log, "# pbl_errno %d, errno %d\n", pbl_errno, errno ); } } else if( !strcmp( command, "open" )) { printf( "# open filename, update \n" ); fprintf( log, "# open filename, update \n" ); getWord( filename ); getWord( buffer ); update = atoi( buffer ); printf( "# pblKfOpen( %s, %d )\n", filename, update ); fprintf( log, "# pblKfOpen( %s, %d )\n", filename, update ); if( kf ) ( void ) pblKfClose( kf ); kf = pblKfOpen( filename, update, NULL ); if( kf ) { printf( "# ok!\n" ); fprintf( log, "# ok!\n" ); } else { printf( "# pbl_errno %d, errno %d\n", pbl_errno, errno ); fprintf( log, "# pbl_errno %d, errno %d\n", pbl_errno, errno ); } } else if( !strcmp( command, "close" )) { if( !kf ) continue; printf( "# pblKfClose( %d )\n", 1 ); fprintf( log, "# pblKfClose( %d )\n", 1 ); rc = pblKfClose( kf ); kf = ( pblKeyFile_t * ) 0; printf( "# rc %ld, pbl_errno %d, errno %d\n", rc, pbl_errno, errno ); fprintf( log, "# rc %ld, pbl_errno %d, errno %d\n", rc, pbl_errno, errno ); } else if( !strcmp( command, "flush" )) { if( !kf ) continue; printf( "# pblKfFlush( %d )\n", 1 ); fprintf( log, "# pblKfFlush( %d )\n", 1 ); rc = pblKfFlush( kf ); printf( "# rc %ld, pbl_errno %d, errno %d\n", rc, pbl_errno, errno ); fprintf( log, "# rc %ld, pbl_errno %d, errno %d\n", rc, pbl_errno, errno ); } else if( !strcmp( command, "delete" )) { if( !kf ) continue; printf( "# pblKfDelete( %d )\n", 1 ); fprintf( log, "# pblKfDelete( %d )\n", 1 ); rc = pblKfDelete( kf );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -