mariadb/newbrt/tests/bread-test.c
Rich Prohaska a12ca96fba use toku_htonl. closes #1247
git-svn-id: file:///svn/toku/tokudb.1032b@7850 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:29 -04:00

73 lines
1.7 KiB
C

#include "portability.h"
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "../brttypes.h"
#include "../bread.h"
#define FNAME "bread-test.data"
#define RECORDS 2
static void
test (int seed) {
srandom(seed);
unlink(FNAME);
int i;
char buf[RECORDS][100];
int sizes[RECORDS];
int sizesn[RECORDS];
toku_off_t off = 0;
{
int fd = open(FNAME, O_CREAT+O_RDWR+O_BINARY, 0777);
assert(fd>=0);
for (i=0; i<RECORDS; i++) {
sizes[i] = random()%100;
sizesn[i] = toku_htonl(sizes[i]);
int j;
for (j=0; j<sizes[i]; j++) {
buf[i][j]=(char)random();
}
int r = write(fd, buf[i], sizes[i]);
assert(r==sizes[i]);
off+=r;
r = write(fd, &sizesn[i], 4);
assert(r==4);
off+=4;
}
{ int r=close(fd); assert(r==0); }
}
int fd = open(FNAME, O_RDONLY+O_BINARY); assert(fd>=0);
// Now read it all backward
BREAD br = create_bread_from_fd_initialize_at(fd, off, 50);
while (bread_has_more(br)) {
assert(i>0);
i--;
int sizen;
{ int r = bread_backwards(br, &sizen, 4); assert(r==4); }
int sizeh=toku_ntohl(sizen);
assert(sizeh==sizes[i]);
assert(0<=sizeh && sizeh<100);
{
char rbuf[100];
int r = bread_backwards(br, rbuf,sizeh);
assert(r==sizeh);
assert(memcmp(rbuf, &buf[i][0], sizes[i])==0);
}
}
assert(i==0);
{ int r=close_bread_without_closing_fd(br); assert(r==0); }
{ int r=close(fd); assert(r==0); }
unlink(FNAME);
}
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
int i;
for (i=0; i<10; i++) test(i);
return 0;
}