#include "nbbio.h" #include #include #include #include #include #include #include #include #include #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif #define NVECTORS 3 void check(int d, int e, int line) { if (d != e) { printf("check: %d != %d at line %d\n", d, e, line); exit(1); } } #define CHECK(d, e) check(d, e, __LINE__) static void dumpBuf(const void *p, int len) { int i; for (i=0; i for finding that one */ /* 1. Create a file with a single line of C's, len nbbio_BUFLEN/2 (inc CRLF) */ wfd = open("nbbiotest.dat", O_CREAT|O_TRUNC|O_RDWR, 0664); if (wfd < 0) { printf("Failed at line %d; can't create nbbiotest.dat\n", __LINE__); perror("open"); exit(1); } memset(buf, 'C', nbbio_BUFLEN/2 - 2); buf[nbbio_BUFLEN/2 - 2] = '\r'; buf[nbbio_BUFLEN/2 - 1] = '\n'; int nwrite = write(wfd, buf, nbbio_BUFLEN/2); CHECK(nbbio_BUFLEN/2, nwrite); err = close(wfd); CHECK(err, 0); /* 2. Rotate buf so there's two bytes of free space at the beginning, * then fill buf with a line of B's until there's exactly nbbio_BUFLEN/2 contigFree */ nb.init(); CHECK(nb.contigBytesFree(), nbbio_BUFLEN-1); CHECK(nb.bytesFree(), nbbio_BUFLEN-1); nb.put("AZ", 2); c = nb.readc(); CHECK(c, 'A'); c = nb.readc(); CHECK(c, 'Z'); CHECK(nb.contigBytesFree(), nbbio_BUFLEN-2); /* because putTo is 2 */ CHECK(nb.bytesFree(), nbbio_BUFLEN-1); while (nb.contigBytesFree() > nbbio_BUFLEN/2 + 2) { nb.put("B", 1); CHECK(nb.contigBytesFree(), nb.bytesFree()-1); } nb.put("\r\n", 2); CHECK(nb.contigBytesFree(), nbbio_BUFLEN/2); CHECK(nb.bytesFree(), nbbio_BUFLEN/2+1); /* 3. Fill buf from the file. Make sure it doesn't barf because * we hit EOF. */ rfd = open("nbbiotest.dat", O_RDONLY); if (rfd < 0) { printf("Failed at line %d; can't open nbbiotest.dat\n", __LINE__); perror("open"); exit(1); } err = nb.fillFrom(rfd); CHECK(err, 0); close(rfd); /* 4. Make sure we can read both lines. */ memset(buf2, 255, sizeof(buf2)); err = nb.readline(buf2, sizeof(buf2)); CHECK(err, 0); len = strlen(buf2); CHECK(len, nbbio_BUFLEN/2 - 2 - 2); CHECK(buf2[0], 'B'); /* first line all B's */ err = nb.readline(buf2, sizeof(buf2)); CHECK(err, 0); CHECK(buf2[0], 'C'); /* 2nd line all C's */ err = memcmp(buf, buf2, nbbio_BUFLEN/2 - 2); if (err) { printf("got:"); dumpBuf(buf2, nbbio_BUFLEN/2-2); printf("wanted:"); dumpBuf(buf, nbbio_BUFLEN/2-2); } CHECK(err, 0); len = strlen(buf2); if (len != nbbio_BUFLEN/2 - 2) { printf("got:"); dumpBuf(buf2, len); printf("wanted:"); dumpBuf(buf, nbbio_BUFLEN/2-2); } CHECK(len, nbbio_BUFLEN/2 - 2); /* Whew. */ unlink("nbbiotest.dat"); printf("Test passed\n"); exit(0); }