MAIN() { char *s = "1234567890\n"; Sfoff_t n, i; Sfio_t *f; char buf[1024]; char* addr; if(sfopen(sfstdout,tstfile(0),"w+") != sfstdout) terror("Opening output file\n"); for(i = 0; i < 10000; ++i) if(sfputr(sfstdout,s,-1) < 0) terror("Writing data\n"); if(!(f = sfopen((Sfio_t*)0,tstfile(1),"w"))) terror("Opening output file \n"); sfseek(sfstdout,(Sfoff_t)0,0); if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i) terror("Move %d lines, Expect %d\n",n,i); sfseek(sfstdout,(Sfoff_t)0,0); sfseek(f,(Sfoff_t)0,0); sfsetbuf(sfstdout,buf,sizeof(buf)); if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i) terror("Move %d lines, Expect %d\n",n,i); sfopen(sfstdin,tstfile(0),"r"); sfopen(sfstdout,tstfile(1),"w"); sfmove(sfstdin,sfstdout,(Sfoff_t)SF_UNBOUND,-1); if(!sfeof(sfstdin)) terror("Sfstdin is not eof\n"); if(sferror(sfstdin)) terror("Sfstdin is in error\n"); if(sferror(sfstdout)) terror("Sfstdout is in error\n"); sfseek(sfstdin,(Sfoff_t)0,0); sfseek(sfstdout,(Sfoff_t)0,0); sfsetbuf(sfstdin,buf,sizeof(buf)); addr = (char*)sbrk(0); sfmove(sfstdin,sfstdout,(Sfoff_t)((unsigned long)(~0L)>>1),-1); if((ssize_t)((char*)sbrk(0)-addr) > 256*1024) terror("Too much space allocated in sfmove\n"); if(!sfeof(sfstdin)) terror("Sfstdin is not eof2\n"); if(sferror(sfstdin)) terror("Sfstdin is in error2\n"); if(sferror(sfstdout)) terror("Sfstdout is in error2\n"); TSTEXIT(0); }
MAIN() { Sfio_t* f; Sfio_t sf; if(sfopen(sfstdout,"abc","s") != sfstdout) terror("Bad reopening of sfstdout\n"); if(sfopen(sfstdin,"123","s") != sfstdin) terror("Bad reopening of sfstdin\n"); sfclose(sfstdin); if(!(f = sfopen(NIL(Sfio_t*),"123","s")) ) terror("Opening a stream\n"); sfclose(f); if(sfopen(f,"123","s") != NIL(Sfio_t*)) terror("can't reopen a closed stream!\n"); if(sfnew(&sf,NIL(char*),(size_t)SF_UNBOUND,0,SF_EOF|SF_READ) != &sf) terror("Did not open sf\n"); sfset(&sf,SF_STATIC,1); if(sfclose(&sf) < 0 || !(sfset(&sf,0,0)&SF_STATIC)) terror("Did not close sf\n"); /* test for exclusive opens */ unlink(tstfile(0)); if(!(f = sfopen(NIL(Sfio_t*),tstfile(0),"wx") ) ) terror("sfopen failed\n"); if((f = sfopen(f,tstfile(0),"wx") ) ) terror("sfopen should not succeed here\n"); TSTEXIT(0); }
tmain() { Sfio_t* f; Sfio_t sf; if(sfopen(sfstdout,"abc","s") != sfstdout) terror("Bad reopening of sfstdout"); if(sfopen(sfstdin,"123","s") != sfstdin) terror("Bad reopening of sfstdin"); sfclose(sfstdin); if(!(f = sfopen(NIL(Sfio_t*),"123","s")) ) terror("Opening a stream"); sfclose(f); if(sfopen(f,"123","s") != NIL(Sfio_t*)) terror("can't reopen a closed stream!"); if(sfnew(&sf,NIL(char*),(size_t)SF_UNBOUND,0,SF_EOF|SF_READ) != &sf) terror("Did not open sf"); sfset(&sf,SF_STATIC,1); if(!sfclose(&sf) || errno != EBADF) terror("sfclose(sf) should fail with EBADF"); if(!(sfset(&sf,0,0)&SF_STATIC)) terror("Did not close sf"); /* test for exclusive opens */ unlink(tstfile("sf", 0)); if(!(f = sfopen(NIL(Sfio_t*),tstfile("sf", 0),"wx") ) ) terror("sfopen failed"); if((f = sfopen(f,tstfile("sf", 0),"wx") ) ) terror("sfopen should not succeed here"); texit(0); }
MAIN() { int i; char wbuf[1023]; char rbuf[1023]; FILE *fp; for(i = 0; i < sizeof(wbuf); ++i) wbuf[i] = (i%26)+'a'; wbuf[sizeof(wbuf)-1] = '\0'; if(!(fp = fopen(tstfile(0), "w+")) ) terror("Opening temp file\n"); for(i = 0; i < 256; ++i) if(fwrite(wbuf,sizeof(wbuf),1,fp) != 1) terror("Writing\n"); fseek(fp,(long)0,0); for(i = 0; i < 256; ++i) { if(fread(rbuf,sizeof(rbuf),1,fp) != 1) terror("Reading\n"); if(strcmp(rbuf,wbuf) != 0) terror("Unmatched record\n"); } TSTEXIT(0); }
MAIN() { FILE *fp; int i, c, e; if(!(fp = fopen(tstfile(0), "w+")) ) terror("Can't open temp file\n"); for(i = 0; i < 256; ++i) if(putc(i, fp) < 0) terror("Bad putc\n"); for(i = 1; i < 255; ++i) { if(fseek(fp, (long)(-i), SEEK_END) < 0) terror("fseek seek_end failed\n"); if((c = getc(fp)) != (e = 256-i) ) terror("Bad getc: expect %d, get %d\n", e, c); if(fseek(fp, (long)(i), SEEK_SET) < 0) terror("fseek seek_set failed\n"); if((c = getc(fp)) != i) terror("Bad getc: expect %d, get %d\n", i, c); if(fseek(fp, (long)(-1), SEEK_CUR) < 0) terror("fseek seek_cur failed\n"); if((c = getc(fp)) != i ) terror("Bad getc: expect %d, get %d\n", i, c); } TSTEXIT(0); }
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f1, *f2; char *s; Sfoff_t p; char buf[1024]; int r, w; if (!(f1 = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Can't open f1"); if (!(f1 = sfopen(f1, tstfile("sf", 0), "a+"))) terror("Can't open f1"); if (!(f2 = sfopen(NULL, tstfile("sf", 0), "a+"))) terror("Can't open f2"); if (sfwrite(f1, "012345678\n", 10) != 10 || sfsync(f1) < 0) terror("Writing to f1"); if ((p = sftell(f1)) != 10) terror("Bad sftell1 %ld", p); if (sfwrite(f2, "abcdefghi\n", 10) != 10 || sfsync(f2) < 0) terror("Writing to f2"); if ((p = sftell(f2)) != 20) terror("Bad sftell2"); if ((p = sfseek(f1, (Sfoff_t)0, 0)) != 0) terror("Bad seek"); if (!(s = sfgetr(f1, '\n', 1))) terror("Bad getr1"); if (strcmp(s, "012345678") != 0) terror("Bad input1"); if ((p = sftell(f1)) != 10) terror("Bad sftell3"); if (sfwrite(f1, "012345678\n", 10) != 10 || sfsync(f1) < 0) terror("Writing to f1"); if ((p = sftell(f1)) != 30) terror("Bad sftell4"); if ((p = sfseek(f2, (Sfoff_t)10, 0)) != 10) terror("Bad seek"); if (!(s = sfgetr(f2, '\n', 1))) terror("Bad getr2"); if (strcmp(s, "abcdefghi") != 0) terror("Bad input2"); if (!(s = sfgetr(f2, '\n', 1))) terror("Bad getr3"); if (strcmp(s, "012345678") != 0) terror("Bad input3"); if (!(f1 = sfopen(f1, tstfile("sf", 0), "w"))) terror("Can't open file to write"); for (r = 0; r < 1024; ++r) buf[r] = 'a'; if ((w = sfwrite(f1, buf, 1024)) != 1024) terror("writing w=%d", w); if (!(f1 = sfopen(f1, tstfile("sf", 0), "a"))) terror("Can't open file to append"); sfseek(f1, (Sfoff_t)0, 0); if ((w = sfwrite(f1, buf, 64)) != 64) terror("writing w=%d", w); if ((r = (int)sftell(f1)) != (1024 + 64)) terror("seek position wrong s=%d", r); texit(0); }
MAIN() { Sfio_t *f; int fd; off_t sk; if(!(f = sfopen((Sfio_t*)0,tstfile(0),"w+"))) terror("Opening file\n"); fd = sffileno(f); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd1\n"); if(sfputc(f,'a') >= 0) terror("sfputc\n"); if(sfsetfd(f,fd) != fd) terror("setfd2\n"); if(sfwrite(f,"123456789\n",10) != 10) terror("sfwrite\n"); sfseek(f,(Sfoff_t)0,0); if(sfgetc(f) != '1') terror("sfgetc1\n"); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd2\n"); if((sk = lseek(fd, (off_t)0, 1)) != (off_t)1) terror("Bad seek address %lld\n", (Sfoff_t)sk ); if(sfgetc(f) >= 0) terror("sfgetc2\n"); if(sfsetfd(f,fd) != fd) terror("setfd2\n"); if(sfgetc(f) != '2') terror("sfgetc3\n"); TSTEXIT(0); }
tmain() { Sfio_t *f; int fd; off_t sk; if(!(f = sfopen((Sfio_t*)0,tstfile("sf", 0),"w+"))) terror("Opening file"); fd = sffileno(f); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd1"); if(sfputc(f,'a') >= 0) terror("sfputc"); if(sfsetfd(f,fd) != fd) terror("setfd2"); if(sfwrite(f,"123456789\n",10) != 10) terror("sfwrite"); sfseek(f,(Sfoff_t)0,0); if(sfgetc(f) != '1') terror("sfgetc1"); if(sfsetfd(f,-1) != -1 || sffileno(f) != -1) terror("setfd2"); if((sk = lseek(fd, (off_t)0, 1)) != (off_t)1) terror("Bad seek address %lld", (Sfoff_t)sk ); if(sfgetc(f) >= 0) terror("sfgetc2"); if(sfsetfd(f,fd) != fd) terror("setfd2"); if(sfgetc(f) != '2') terror("sfgetc3"); texit(0); }
// Perform validation of an MLP using MSE criterion. This should probably be moved into the MLP class. // Maybe next version after moving matops to BOOST. double validate(MLP* X,string val_fname) { double E=0; int N=X->N,Nh=X->Nh,M=X->M,i,num_patterns=0; valarray<double> x(N+1),O(Nh),net(Nh),y(M),e(M),t(M); ifstream tstfile(val_fname.c_str()); while (!tstfile.eof()) { for (i=0;i<N;i++) tstfile >> x[i]; x[N]=1.; if (!tstfile.eof()) for (i = 0; i < M; i++) tstfile >> t[i]; else break; num_patterns++; y=X->process_pattern(x,net,O); e=t-y; E += (e*e).sum(); }
tmain() { Sfio_t *f; if (argc > 1) { if (sfopen(sfstdin, argv[1], "r") != sfstdin) terror("Can't reopen stdin"); sfmove(sfstdin, sfstdout, (Sfoff_t)(-1), -1); return 0; } if (!(f = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Opening to write"); if (sfputc(f, 'a') != 'a') terror("sfputc"); if (sfgetc(f) >= 0) terror("sfgetc"); if (!(f = sfopen(f, tstfile("sf", 0), "r"))) terror("Opening to read"); if (sfgetc(f) != 'a') terror("sfgetc2"); if (sfputc(f, 'b') >= 0) terror("sfputc2"); if (!(f = sfopen(f, tstfile("sf", 0), "r+"))) terror("Opening to read/write"); if (sfgetc(f) != 'a') terror("sfgetc3"); if (sfputc(f, 'b') != 'b') terror("sfputc3"); if (sfclose(f) < 0) terror("sfclose"); if (!(f = sfpopen(NULL, sfprints("%s %s", argv[0], tstfile("sf", 0)), "r"))) terror("sfpopen"); if (sfgetc(f) != 'a') terror("sfgetc4"); if (sfgetc(f) != 'b') terror("sfgetc5"); if (sfgetc(f) >= 0) terror("sfgetc6"); if (!(f = sfopen(f, tstfile("sf", 0), "w"))) terror("sfopen"); if (sfputc(f, 'a') != 'a') terror("sfputc1"); sfsetfd(f, -1); if (sfputc(f, 'b') >= 0) terror("sfputc2"); if (sfclose(f) < 0) terror("sfclose"); if (!(f = sfopen(NULL, tstfile("sf", 0), "a+"))) terror("sfopen2"); sfset(f, SF_READ, 0); if (!sfreserve(f, 0, -1)) terror("Failed on buffer getting"); if (sfvalue(f) <= 0) terror("There is no buffer?"); texit(0); }
/* test compliance of certain stdio behaviors */ tmain() { FILE *f, *f2; long s1, s2; int i, k, fd; char buf[128*1024], rbuf[1024], *sp; if(argc > 1) { if(sfwrite(sfstdout,argv[1],strlen(argv[1])) != strlen(argv[1])) terror("Can't write to stdout"); sfsync(sfstdout); return 0; } /* test for shared streams and seek behavior */ if(!(f = fopen(tstfile("sf", 0),"w+")) ) terror("Opening file to read&write"); /* change stdout to a dup of fileno(f) */ fd = dup(1); close(1); dup(fileno(f)); /* write something to the dup file descriptor */ system(sfprints("%s 0123456789", argv[0])); /* change stdout back */ close(1); dup(fd); close(fd); /* this fseek should reset the stream back to where we can read */ fseek(f, 0L, SEEK_SET); /* see if data is any good */ fread(buf, sizeof(buf), 1, f); for(i = 0; i < 10; ++i) if(buf[i] != '0'+i) terror("Bad data0"); /* construct a bunch of lines and out put to f */ sp = buf; for(k = 0; k < sizeof(buf)/10; ++k) { for(i = 0; i < 9; ++i) *sp++ = '0' + i; *sp++ = '\n'; } /* write out a bunch of thing */ fseek(f, 0L, SEEK_SET); if(fwrite(buf, sizeof(buf), 1, f) != 1) terror("Writing data"); if((fd = dup(fileno(f))) < 0) terror("Can't dup file descriptor"); if(!(f2 = fdopen(fd, "r")) ) terror("Can't create stream"); /* read a few bytes from this dup stream */ fseek(f2, 0L, SEEK_SET); rbuf[0] = 0; if(fread(rbuf, 1, 7, f2) != 7) terror("Bad read"); for(i = 0; i < 7; ++i) if(rbuf[i] != '0'+i) terror("Bad data1"); if((s2 = ftell(f2)) != 7) terror("Bad tell location in f2"); /* now seek way off on f */ fseek(f, 1005L, SEEK_SET); rbuf[0] = 0; fread(rbuf, 5, 1, f); for(i = 5; i < 9; ++i) if(rbuf[i-5] != '0'+i) terror("Bad data2"); if(rbuf[i-5] != '\n') terror("Bad data: did not get new-line"); if((s1 = ftell(f)) != 1010) terror("Bad location in f: s1=%lld", (Sflong_t)s1); fseek(f, 0L, SEEK_CUR); /* switch mode so we can write */ if(fputc('x',f) < 0) terror("fputc failed"); if(fflush(f) < 0) terror("fflush failed"); if((s1 = ftell(f)) != 1011) terror("Bad location in f: s1=%lld", (Sflong_t)s1); fseek(f, -1L, SEEK_CUR); /* set the seek location in the file descriptor */ fflush(f2); /* assuming POSIX conformance and to set seek location to 1010 */ if((s2 = ftell(f2)) != 1010) terror("Bad location in f2: s2=%lld", (Sflong_t)s2); fread(rbuf, 10, 1, f2); if(rbuf[0] != 'x') terror("Didn't get x"); for(i = 1; i < 9; ++i) if(rbuf[i] != '0'+i) terror("Bad data3"); if(rbuf[i] != '\n') terror("Did not get new-line"); texit(0); }
tmain() { int k, code = 0; Vmalloc_t *shm, *map; char *arg[5]; pid_t ppid, cpid; if(k = tchild()) { cpid = getpid(); shmfile = argv[k]; mapfile = argv[k+1]; tinfo("Child[pid=%d]: allocating heap memory before opening shm region", cpid); for(k = 0; k < 1024; ++k) if(!malloc(32*1024) ) terror("Child[pid=%d]: Can't allocate segment %d", cpid, k); if(*shmfile) { tinfo("Child[pid=%d]: opening shm region", cpid); if(!(shm = vmmopen(shmfile, 1, MAPSIZE)) ) terror("Child[pid=%d]: Can't open shm region in child process", cpid); tinfo("Child[pid=%d]: shm region opened", cpid); } tinfo("Child[pid=%d]: allocating heap memory before opening map region", cpid); for(k = 0; k < 1024; ++k) if(!malloc(32*1024) ) terror("Child[pid=%d]: Can't allocate segment %d", cpid, k); if(*mapfile) { tinfo("Child[pid=%d]: opening map region", cpid); if(!(map = vmmopen(mapfile, -1, MAPSIZE)) ) terror("Child[pid=%d]: Can't open map region in child process", cpid); tinfo("Child[pid=%d]: map region opened", cpid); } } else { ppid = getpid(); shmfile = tstfile("shm", -1); mapfile = tstfile("map", -1); (void)unlink(shmfile); (void)unlink(mapfile); tinfo("Parent[pid=%d]: %s: opening shm region", ppid, shmfile); if(shm = vmmopen(shmfile, 1, MAPSIZE) ) tinfo("Parent[pid=%d]: %s: shm region opened", ppid, shmfile); else { tnote("shm not supported"); shmfile = ""; } tinfo("Parent[pid=%d]: %s: opening map region", ppid, mapfile); if(map = vmmopen(mapfile, -1, MAPSIZE) ) tinfo("Parent[pid=%d]: %s: map region opened", ppid, mapfile); else { tnote("map not supported"); mapfile = ""; } switch((cpid = fork()) ) /* make a child process */ { default : code = twait(&cpid, 1); break; case 0 : arg[0] = argv[0]; arg[1] = "--child"; arg[2] = shmfile; arg[3] = mapfile; arg[4] = 0; if(execv(argv[0], arg) < 0 ) terror("Could not exec child process"); case -1: terror("Could not fork a child process"); } } vmmrelease(shm, 1); vmclose(shm); vmmrelease(map, 1); vmclose(map); texit(code); }
/* test compliance of certain stdio behaviors */ MAIN() { FILE *f, *f2; long s1, s2; int i, k, fd; char buf[128*1024], rbuf[1024], *sp; /* test for shared streams and seek behavior */ if(!(f = fopen(tstfile(0),"w+")) ) terror("Opening file to read&write"); /* write something to a dup file descriptor */ fd = dup(fileno(f)); if(write(fd, "0123456789", 10) != 10) terror("write failed"); close(fd); /* this fseek should reset the stream back to where we can read */ if(fseek(f, 0L, SEEK_SET) < 0) terror("fseek failed"); /* see if data is any good */ if((k = fread(buf, 1, 10, f)) != 10) terror("fread failed"); for(i = 0; i < 10; ++i) if(buf[i] != '0'+i) terror("Bad data0"); /* construct a bunch of lines and out put to f */ sp = buf; for(k = 0; k < sizeof(buf)/10; ++k) { for(i = 0; i < 9; ++i) *sp++ = '0' + i; *sp++ = '\n'; } /* write out a bunch of thing */ fseek(f, 0L, SEEK_SET); if(fwrite(buf, sizeof(buf), 1, f) != 1) terror("Writing data"); if((fd = dup(fileno(f))) < 0) terror("Can't dup file descriptor"); if(!(f2 = fdopen(fd, "r")) ) terror("Can't create stream"); /* read a few bytes from this dup stream */ fseek(f2, 0L, SEEK_SET); rbuf[0] = 0; if(fread(rbuf, 1, 7, f2) != 7) terror("Bad read\n"); for(i = 0; i < 7; ++i) if(rbuf[i] != '0'+i) terror("Bad data1"); if((s2 = ftell(f2)) != 7) terror("Bad tell location in f2"); /* now seek way off on f */ fseek(f, 1005L, SEEK_SET); rbuf[0] = 0; fread(rbuf, 5, 1, f); for(i = 5; i < 9; ++i) if(rbuf[i-5] != '0'+i) terror("Bad data2"); if(rbuf[i-5] != '\n') terror("Bad data: did not get new-line"); if((s1 = ftell(f)) != 1010) terror("Bad location in f, s1=%ld", (long)s1); fseek(f, 0L, SEEK_CUR); /* switch mode so we can write */ if(fputc('x',f) < 0) terror("fputc failed"); if(fflush(f) < 0) terror("fflush failed"); if((s1 = ftell(f)) != 1011) terror("Bad tell location in f"); fseek(f, -1L, SEEK_CUR); /* set the seek location in the file descriptor */ fflush(f2); /* assuming POSIX conformance and to set seek location to 1010 */ if((s2 = ftell(f2)) != 1010) terror("Bad tell2 location=%ld in f2, expecting=%ld", s2, 1010); fread(rbuf, 10, 1, f2); if(rbuf[0] != 'x') terror("Didn't get x"); for(i = 1; i < 9; ++i) if(rbuf[i] != '0'+i) terror("Bad data3"); if(rbuf[i] != '\n') terror("Did not get new-line"); TSTEXIT(0); }
tmain() { int i, k, m; ssize_t size; Vmdisc_t *dc; Region_t region[N_REGION]; char *shmfile, *mapfile, *warn; warn = (char*)0; size = 0; m = sizeof(char*) == 4 ? 2 : 16; for(k = 0; k < N_REGION; ++k) { region[k].size = (trandom()%m + m)*m; region[k].size *= 1024*1024; if(k%(N_SHMREG+1) != 0 ) /* do a bunch of shm memory */ { shmfile = tstfile("shm", k); if(!(dc = vmdcshare(shmfile, 1, region[k].size, -1)) || !(region[k].vm = vmopen(dc, Vmbest, 0)) ) { warn = "shmem"; break; } tinfo("Region-shmem[%d] size=%lu addr=%p", k, region[k].size/(1024*1024), region[k].vm->data); size += region[k].size; } else /* interspersed the above with mmap */ { mapfile = tstfile("map", k); if(!(dc = vmdcshare(mapfile, -1, region[k].size, -1)) || !(region[k].vm = vmopen(dc, Vmbest, 0)) ) { warn = "mmap"; break; } tinfo("Region-mmap[%d] size=%lu addr=%p", k, region[k].size/(1024*1024), region[k].vm->data); size += region[k].size; } for(i = 0; i < k; ++i) { if((char*)region[i].vm->data >= (char*)region[k].vm->data && (char*)region[i].vm->data <= ((char*)region[k].vm->data + region[k].size) ) terror("Region[%d] and Region[%d] overlap", i, k); } } if(size > 0 ) tinfo("#regions to try=%d #regions actually opened=%d total memory=%luM", N_REGION, k, size/(1024*1024) ); for(i = 0; i < k; ++i) vmclose(region[i].vm); if(warn && k == 0) terror("Region-%s[%d] size=%luM failed", warn, k, region[k].size/(1024*1024) ); texit(0); }
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f, *f2; char *s; int i, n; char buf[16 * 1024]; if (!(f = sfopen(NULL, tstfile("sf", 0), "w+"))) terror("Can't open file"); if (sfnputc(f, 'a', 1000) != 1000) terror("Writing"); if (sfseek(f, (Sfoff_t)0, 0) != 0) terror("Seeking"); if ((n = (int)sfsize(f)) != 1000) terror("Wrong size %d", n); if (!(f2 = sfnew(NULL, NULL, (size_t)SF_UNBOUND, sffileno(f), SF_WRITE))) { terror("Can't open stream"); } if (sfseek(f2, (Sfoff_t)1999, 0) != (Sfoff_t)1999) terror("Seeking2"); sfputc(f2, 'b'); sfsync(f2); if ((n = (int)sfsize(f2)) != 2000) terror("Wrong size2 %d", n); if ((n = (int)sfsize(f)) != 1000) terror("Wrong size3 %d", n); sfputc(f, 'a'); sfset(f, SF_SHARE, 1); if ((n = (int)sfsize(f)) != 2000) terror("Wrong size4 %d", n); if (!(f = sfopen(f, NULL, "srw"))) terror("Can't open string stream"); sfwrite(f, "0123456789", 10); if (sfsize(f) != 10) terror("String size is wrong1"); sfseek(f, (Sfoff_t)19, 0); if (sfsize(f) != 10) terror("String size is wrong2"); sfputc(f, 'a'); if (sfsize(f) != 20) terror("String size is wrong3"); sfseek(f, (Sfoff_t)0, 0); if (sfsize(f) != 20) terror("String size is wrong4"); sfseek(f, (Sfoff_t)0, 0); if (!(s = sfreserve(f, SF_UNBOUND, SF_LOCKR)) && sfvalue(f) != 20) { terror("String size is wrong5"); } sfread(f, s, 5); if (sfsize(f) != 20) terror("String size is wrong6"); sfwrite(f, "01234567890123456789", 20); if (sfsize(f) != 25) terror("String size is wrong7"); strcpy(buf, "0123456789"); if (!(f = sfopen(f, buf, "s+"))) terror("Can't open string stream2"); if (sfset(f, 0, 0) & SF_MALLOC) terror("SF_MALLOC should not have been set"); if (sfsize(f) != 10) terror("String size is wrong8"); sfread(f, buf, 5); if ((n = (int)sfwrite(f, "0123456789", 10)) != 5) terror("Write wrong amount %d", n); if (sfsize(f) != 10) terror("String size is wrong9"); if (!(f = sfopen(f, tstfile("sf", 0), "w"))) terror("Reopening file1"); for (i = 0; i < 10000; ++i) { if (sfputc(f, '0' + (i % 10)) != '0' + (i % 10)) terror("sfputc failed"); } if (!(f = sfopen(f, tstfile("sf", 0), "r+"))) terror("Reopening file2"); if (sfsize(f) != 10000) terror("Bad size of file1"); sfsetbuf(f, buf, 1024); for (i = 0; i < 20; ++i) { if (!sfreserve(f, 100, 0)) terror("Reserve failed"); } s = buf + 1024; for (i = 0; i < 20; ++i) s[i] = '0' + i % 10; sfseek(f, (Sfoff_t)(10000 - 10), 0); if (sfwrite(f, s, 20) != 20) terror("Write failed"); if (sfsize(f) != 10010) terror("Bad size of file2"); sfseek(f, (Sfoff_t)0, 0); for (i = 0; i < 10; ++i) { if (!(s = sfreserve(f, 1001, 0))) terror("Reserve failed2"); if (s[0] != '0' + i) terror("Bad data1"); } for (n = 0; n < 1001; ++n) { if (s[n] != ((n + i - 1) % 10 + '0')) terror("Bad data"); } /* test to see if a string stream extends ok during writes */ s = malloc(5); f = sfnew(NULL, (void *)s, 5, -1, SF_STRING | SF_READ | SF_WRITE | SF_MALLOC); if (!f) terror("Can't create string stream"); if (sfwrite(f, "01", 2) != 2) terror("Bad write to string stream"); if (sfwrite(f, "2345678", 7) != 7) terror("Bad write to string stream2"); if (sfputc(f, 0) != 0) terror("sfputc failed"); if (sfseek(f, (Sfoff_t)0, 0) != 0) terror("sfseek failed"); if ((n = (int)sfread(f, buf, 100)) != 10) terror("sfread gets wrong amount of data %d", n); if (strcmp(buf, "012345678") != 0) terror("Get wrong data"); texit(0); }
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f1, *f2, *f3, *f; char *s, *s1, *s2, *s3, *s4; int n; int fd[2]; if (!(f1 = sfopen(NULL, tstfile("sf", 0), "w+"))) terror("Opening file1"); if (!(f2 = sfopen(NULL, tstfile("sf", 0), "w+"))) terror("Opening file2"); Fclose = f2; sfdisc(f1, &Disc); sfdisc(f2, &Disc); sfstack(f1, f2); if ((n = sfgetc(f1)) >= 0 || !sfeof(f1)) terror("There should be no data n=%d", n); if (sfstacked(f1)) terror("There should be no stack"); Fclose = f1; if (sfclose(f1) < 0) terror("Can't close f1"); tcleanup(); s1 = "1234567890"; s2 = "abcdefghijklmnopqrstuvwxyz"; s3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; s4 = "!@#$%^&*()_-+={}[]~`':;?/><,|"; if (!(f1 = sfopen(NULL, s1, "s")) || !(f2 = sfopen(NULL, s2, "s")) || !(f3 = sfopen(NULL, s3, "s"))) { terror("Opening strings"); } sfdisc(sfstdin, &Disc); sfclose(sfstdin); if (sffileno(sfstdin) != 0) terror("Bad fd for stdin"); if (!(f = sfopen(NULL, tstfile("sf", 0), "w+"))) terror("Opening file"); if (sfwrite(f, "0123456789", 10) != 10) terror("Write file"); if (sfseek(f, (Sfoff_t)0, 0) != 0) terror("Seek file"); if (sfstack(sfstdin, f) != sfstdin) terror("Stacking on stdin2"); if (sfopen(sfstdout, "/dev/null", "w") != sfstdout) terror("Opening sfstdout"); if (sfmove(sfstdin, sfstdout, (Sfoff_t)SF_UNBOUND, -1) != 10 || !sfeof(sfstdin) || sferror(sfstdout)) { terror("Bad sfmove"); } tcleanup(); if (!(f = sftmp(0))) terror("Opening temp file"); if (sfputr(f, s4, -1) != (ssize_t)strlen(s4)) terror("Writing s4"); sfseek(f, (Sfoff_t)0, 0); #if FIX_THIS_TEST_2008_08_11 if (sfstack(f, f3) != f) terror("Stacking s3"); if (sfstack(f, f2) != f) terror("Stacking s2"); if (sfstack(f, f1) != f) terror("Stacking s1"); sfsprintf(str, sizeof(str), "%s%s%s%s", s1, s2, s3, s4); if ((ss = sfgetr(f, '\n', 1))) terror("There shouldn't have been any new-line"); else { if (!(ss = sfgetr(f, '\n', -1))) terror("Reading streams"); n = sfvalue(f); if (ss[n]) ss[n] = 0; } if (strcmp(ss, str) != 0) terror("Expect=%s Got=%s", str, ss); #endif if (!(f1 = sfopen(NULL, s1, "s")) || !(f2 = sfopen(NULL, s2, "s")) || !(f3 = sfopen(NULL, s3, "s"))) { terror("Opening strings2"); } sfseek(f, (Sfoff_t)0, 0); if (sfstack(f, f3) != f || sfstack(f, f2) != f || sfstack(f, f1) != f) { terror("Stacking streams2"); } if (!(s = sfreserve(f, SF_UNBOUND, 0)) || s != s1) terror("Sfpeek1"); if (!(s = sfreserve(f, SF_UNBOUND, 0)) || s != s2) terror("Sfpeek2"); if (!(s = sfreserve(f, SF_UNBOUND, 0)) || s != s3) terror("Sfpeek3"); if (!(s = sfreserve(f, SF_UNBOUND, 0)) || strncmp(s, s4, strlen(s4)) != 0) terror("Sfpeek4"); /* test to see if hidden read data still accessible */ if (pipe(fd) < 0) terror("Can't create pipe"); if (!(f1 = sfnew(0, NULL, (size_t)SF_UNBOUND, fd[0], SF_READ | SF_WRITE))) { terror("Can't create stream"); } if (write(fd[1], "0123", 4) != 4) terror("Can't write to pipe"); if (sfgetc(f1) != '0') terror("sfgetc failed"); /* hack to create hidden reserved buffer */ f1->file = fd[1]; if (sfwrite(f1, "4", 1) != 1) terror("Can't write to stream"); sfsync(f1); f1->file = fd[0]; close(fd[1]); /* now stack stream */ if (!(f2 = sfopen(0, "abcd\n", "s"))) terror("Can't open string stream"); sfstack(f2, f1); if (!(s = sfgetr(f2, '\n', 1))) terror("sfgetr failed"); if (strcmp(s, "1234abcd") != 0) terror("sfgetr got wrong data"); texit(0); }
MAIN() { int i, n, k; Sfoff_t o; char buf[1024], *s; char bigbuf[1024*8]; int fd[2]; Sfio_t* f; if(!(f = sfopen(0, tstfile(0), "w")) ) terror("Opening file to write"); if(sfwrite(f,"0123456789",10) != 10 || sfwrite(f,"abcdefgh",8) != 8) terror("Writing data"); if(!(f = sfopen(f, tstfile(0), "r")) ) terror("Opening file to read"); sfsetbuf(f, buf, sizeof(buf)); if(!(s = (char*)sfreserve(f,10,0)) ) terror("sfreserve failed"); if(strncmp(s,"0123456789",10) != 0) terror("Did not get correct data"); if((s = (char*)sfreserve(f,10,0)) ) terror("sfreserve should not have succeeded"); if(sfvalue(f) != 8) terror("sfreserve should have left the right unread record length"); if(!(s = (char*)sfreserve(f,4,0)) ) terror("sfreserve should return 4 bytes"); if(strncmp(s,"abcd",4) != 0) terror("Got wrong data"); if((s = (char*)sfreserve(f,10,0)) ) terror("sfreserve should not have succeeded2"); if(sfvalue(f) != 4) terror("sfreserve should have left 4 bytes length"); if(!(s = (char*)sfreserve(f,0,SF_LASTR)) ) terror("sfreserve should have returned last unread record"); if(strncmp(s,"efgh",4) != 0) terror("Record has wrong data"); sfclose(f); sfsetbuf(sfstdout,buf,sizeof(buf)); sfset(sfstdout,SF_SHARE|SF_PUBLIC,0); if((s = (char*)sfreserve(sfstdout,0,0)) != buf) terror("Wrong buffer\n"); if((n = sfwrite(sfstdout,"foobar",6)) != 6) terror("Write failed\n"); if((char*)sfreserve(sfstdout,0,0) != s+6) terror("Wrong reserved pointer\n"); sfpurge(sfstdout); if(sfopen(sfstdout, tstfile(0),"w") != sfstdout) terror("Opening file\n"); sfsetbuf(sfstdout,NIL(char*),0); if(!(s = sfreserve(sfstdout,0,1)) ) terror("Could not lock stdout\n"); if(sfputc(sfstdout,'1') >= 0) terror("stdout wasn't locked\n"); if(sfwrite(sfstdout,s,0) != 0) terror("stdout can't be unlocked\n"); sfsetbuf(sfstdout,NIL(char*),sizeof(buf)/2); for(i = 0; i < sizeof(buf); ++i) buf[i] = (i%26) + 'a'; n = 0; for(i = 0; i < 33; ++i) { if(!(s = sfreserve(sfstdout,sizeof(buf),1)) ) terror("Can't reserve write buffer\n"); memcpy(s,buf,sizeof(buf)); if(sfwrite(sfstdout,s,sizeof(buf)) != sizeof(buf) ) terror("Writing to file\n"); else n += sizeof(buf); } sfsync(sfstdout); if(sfopen(sfstdin, tstfile(0),"r") != sfstdin) terror("Opening file2\n"); sfsetbuf(sfstdin,NIL(char*),8*sizeof(buf)); if(sfsize(sfstdin) != n) terror("Wrong size for file\n"); i = 0; for(;;) { if(!(s = sfreserve(sfstdin,16*sizeof(buf),0)) ) break; else i += 16*sizeof(buf); } if(sfvalue(sfstdin) > 0) i += sfvalue(sfstdin); if(i != n) terror("Did not read data\n"); if(sfseek(sfstdin,(Sfoff_t)0,0) != 0) terror("sfseek failed0\n"); sfsetbuf(sfstdin,bigbuf,sizeof(bigbuf)); i = 0; for(;;) { if(!(s = sfreserve(sfstdin,16*sizeof(buf),0)) ) break; else i += 16*sizeof(buf); } if(sfvalue(sfstdin) > 0) i += sfvalue(sfstdin); if(i != n) terror("Did not read data2\n"); sfsetbuf(sfstdin,NIL(Void_t*),(size_t)SF_UNBOUND); if(sfopen(sfstdout, tstfile(0), "w") != sfstdout) terror("Can't open to write\n"); for(i = 0; i < 32; ++i) { for(k = 0; k < sizeof(bigbuf); ++k) bigbuf[k] = '0' + (k+i)%10; if(sfwrite(sfstdout,bigbuf,sizeof(bigbuf)) != sizeof(bigbuf)) terror("Writing to %s\n", tstfile(0)); } sfclose(sfstdout); if(sfopen(sfstdin, tstfile(0), "r") != sfstdin) terror("Opening to read\n"); sfsetbuf(sfstdin,NIL(Void_t*),8*1024); if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),0)) ) terror("sfreserve failed\n"); for(i = 0; i < 16; ++i) { for(k = 0; k < sizeof(bigbuf); ++k) if(*s++ != ('0' + (k+i)%10)) terror("Wrong data i=%d k=%d\n",i,k); } if((o = sfseek(sfstdin,-15*((Sfoff_t)sizeof(bigbuf)),1)) != sizeof(bigbuf)) terror("sfseek failed o=%lld\n", (Sflong_t)o); if(sfread(sfstdin,bigbuf,sizeof(bigbuf)) != sizeof(bigbuf) ) terror("sfread failed\n"); s = bigbuf; for(i = 1; i < 2; ++i) { for(k = 0; k < sizeof(bigbuf); ++k) if(*s++ != ('0' + (k+i)%10)) terror("Wrong data2 i=%d k=%d\n",i,k); } if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),1)) ) { sfsetbuf(sfstdin,NIL(Void_t*),16*sizeof(bigbuf)); if(!(s = sfreserve(sfstdin,16*sizeof(bigbuf),1)) ) terror("sfreserve failed2\n"); }
tmain() { UNUSED(argc); UNUSED(argv); Sfio_t *f, *f2; char buf[1024]; char rbuf[4 * 1024]; off_t o; int i; if (!(f = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Can't open file"); sfset(f, SF_IOCHECK, 1); Disc.exceptf = except; if (!sfdisc(f, &Disc)) terror("Pushing discipline failed"); sfdisc(f, &Disc); if (Type != SF_DPUSH) terror("Did not get push event"); /* this is to test sfraise(NULL,...) */ if (!(f2 = sfopen(NULL, tstfile("sf", 0), "w"))) terror("Can't open file"); sfdisc(f2, &Disc); Sfn = 0; if (sfraise(0, SF_WRITE, 0) < 0) terror("sfraise failed"); if (Sfn != 2) terror("Didn't get right event count"); sfdisc(f, NULL); if (Type != SF_DPOP) terror("Did not get pop event"); sfwrite(f, "123", 3); sfsync(f); if (Type != SF_SYNC) terror("Did not get sync event"); sfwrite(f, "123", 3); sfpurge(f); if (Type != SF_PURGE) terror("Did not get purge event"); sfclose(f); if (Type != SF_CLOSING) terror("Did not get close event"); sfclose(f); if (Type != SF_FINAL) terror("Did not get final event"); if (!(f = sfopen(NULL, tstfile("sf", 0), "r"))) terror("Can't open file"); Disc2.readf = readfunc; Disc2.exceptf = except3; sfdisc(f, &Disc2); if (sfgetc(f) >= 0) terror("There should be no data here"); if (Type != SF_LOCKED) terror("Did not get lock event"); /* test to see if sfclose() preserves seek location */ if (!(f = sftmp(0))) terror("Can't create temp file"); sfsetbuf(f, buf, sizeof(buf)); for (i = 0; i < sizeof(rbuf); ++i) rbuf[i] = i; sfwrite(f, rbuf, sizeof(rbuf)); sfset(f, SF_WRITE, 0); Disc.exceptf = except2; sfdisc(f, &Disc); sfseek(f, (Sfoff_t)0, 0); if (sfread(f, rbuf, 4) != 4) terror("reading 4 bytes"); for (i = 0; i < 4; ++i) { if (rbuf[i] != i) terror("wrong 4 bytes"); } sfsync(f); if ((o = lseek(sffileno(f), (off_t)0, SEEK_CUR)) != 4) { terror("Wrong seek location %lld", (Sfoff_t)o); } if ((i = dup(sffileno(f))) < 0) terror("Can't dup file descriptor"); if ((o = lseek(i, (off_t)0, SEEK_CUR)) != 4) terror("Wrong seek location %lld", (Sfoff_t)o); sfclose(f); if ((o = lseek(i, (off_t)0, SEEK_CUR)) != 4) terror("Wrong seek location %lld", (Sfoff_t)o); texit(0); }
MAIN() { Sfio_t *f; if(argc > 1) { if(sfopen(sfstdin,argv[1],"r") != sfstdin) terror("Can't reopen stdin"); sfmove(sfstdin,sfstdout,(Sfoff_t)(-1),-1); return 0; } if(!(f = sfopen((Sfio_t*)0,tstfile(0),"w"))) terror("Opening to write\n"); if(sfputc(f,'a') != 'a') terror("sfputc\n"); if(sfgetc(f) >= 0) terror("sfgetc\n"); if(!(f = sfopen(f,tstfile(0),"r"))) terror("Opening to read\n"); if(sfgetc(f) != 'a') terror("sfgetc2\n"); if(sfputc(f,'b') >= 0) terror("sfputc2\n"); if(!(f = sfopen(f,tstfile(0),"r+"))) terror("Opening to read/write\n"); if(sfgetc(f) != 'a') terror("sfgetc3\n"); if(sfputc(f,'b') != 'b') terror("sfputc3\n"); if(sfclose(f) < 0) terror("sfclose\n"); if(!(f = sfpopen(NIL(Sfio_t*),sfprints("%s %s", argv[0], tstfile(0)),"r"))) terror("sfpopen\n"); if(sfgetc(f) != 'a') terror("sfgetc4\n"); if(sfgetc(f) != 'b') terror("sfgetc5\n"); if(sfgetc(f) >= 0) terror("sfgetc6\n"); if(!(f = sfopen(f,tstfile(0),"w")) ) terror("sfopen\n"); if(sfputc(f,'a') != 'a') terror("sfputc1\n"); sfsetfd(f,-1); if(sfputc(f,'b') >= 0) terror("sfputc2\n"); if(sfclose(f) < 0) terror("sfclose\n"); if(!(f = sfopen(NIL(Sfio_t*),tstfile(0),"a+")) ) terror("sfopen2\n"); sfset(f,SF_READ,0); if(!sfreserve(f,0,-1) ) terror("Failed on buffer getting\n"); if(sfvalue(f) <= 0) terror("There is no buffer?\n"); TSTEXIT(0); }
/* Test multiple processes reading/writing from same file ** descriptor. */ MAIN() { char* s; if(argc > 1) { if(strcmp(argv[1],"-r") == 0) /* doing sfgetr */ { if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line2") != 0) terror("Coprocess getr did not get Line2\n"); if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line3") != 0) terror("Coprocess getr did not get Line3\n"); } else /* doing sfmove */ { Sfio_t* f = sfopen(NIL(Sfio_t*),NIL(char*),"swr"); if(!f) terror("Can't open string stream\n"); if(sfmove(sfstdin,f,(Sfoff_t)2,'\n') != 2) terror("Coprocess sfmove failed\n"); sfseek(f,(Sfoff_t)0,0); if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"Line2") != 0) terror("Coprocess move did not get Line2\n"); if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"Line3") != 0) terror("Coprocess move did not get Line3\n"); } TSTEXIT(0); } if(sfopen(sfstdout, tstfile(0), "w") != sfstdout ) terror("Opening file\n"); if(sfputr(sfstdout,"Line1",'\n') < 0 || sfputr(sfstdout,"Line2",'\n') < 0 || sfputr(sfstdout,"Line3",'\n') < 0 || sfputr(sfstdout,"Line4",'\n') < 0) terror("Writing data\n"); sfopen(sfstdout,"/dev/null","w"); /* testing coprocess calling sfgetr */ if(sfopen(sfstdin, tstfile(0),"r") != sfstdin) terror("Opening to read\n"); if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0) terror("Did not get Line1 for sfgetr\n"); sfsync(sfstdin); system(sfprints("%s -r",argv[0])); sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0); if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0) terror("Did not get Line4 for sfgetr\n"); /* testing coprocess calling sfmove */ if(sfopen(sfstdin, tstfile(0), "r") != sfstdin) terror("Opening to read\n"); if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0) terror("Did not get Line1 for sfmove\n"); sfsync(sfstdin); system(sfprints("%s -m",argv[0])); sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0); if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0) terror("Did not get Line4 for sfmove\n"); /* testing the head program */ #ifdef HEAD if(sfopen(sfstdin, tstfile(0), "r") != sfstdin) terror("Opening to read\n"); if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0) terror("Did not get Line1 for head\n"); sfsync(sfstdin); system("head -2 > /dev/null"); /* for testing the head program */ sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0); if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0) terror("Did not get Line4 for head\n"); #endif TSTEXIT(0); }