/* Test bread by writing random data and then reading it using bread_backwards() to see if it gives the right answer. * See test_1305 for another bread test (testing to see if it can read 1GB files) */ #include "test.h" #include "toku_portability.h" #include #include #include #include #include #include #include #include "../brttypes.h" #include "../bread.h" #define FNAME "bread-test.data" #define RECORDS 20 #define RECORDLEN 100 char buf[RECORDS][RECORDLEN]; int sizes[RECORDS]; int sizesn[RECORDS]; int nwrote=0; char wrotedata[RECORDS*RECORDLEN]; static void test (int seed) { srandom(seed); unlink_file_and_bit(FNAME); int i; { int fd = open(FNAME, O_CREAT+O_RDWR+O_BINARY, 0777); assert(fd>=0); for (i=0; i=0); // Now read it all backward BREAD br = create_bread_from_fd_initialize_at(fd); while (bread_has_more(br)) { assert(nwrote>0); int to_read = 1+(random()%RECORDLEN); // read from 1 to 100 (if RECORDLEN is 100) if (to_read>nwrote) to_read=nwrote; char rbuf[to_read]; int r = bread_backwards(br, rbuf, to_read); assert(r==to_read); assert(memcmp(rbuf, &wrotedata[nwrote-to_read], to_read)==0); nwrote-=to_read; } assert(nwrote==0); { int r=close_bread_without_closing_fd(br); assert(r==0); } { int r=close(fd); assert(r==0); } unlink_file_and_bit(FNAME); } int test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute__((__unused__))) { int i; for (i=0; i<10; i++) test(i); return 0; }