/* -*- mode: C; c-basic-offset: 4 -*- */ #ident "$Id$" #ident "Copyright (c) 2010 Tokutek Inc. All rights reserved." #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." #include "test.h" #include #include #include #include #include "brtloader-internal.h" #include "memory.h" #if defined(__cplusplus) extern "C" { #endif static int qsort_compare_ints (const void *a, const void *b) { int avalue = *(int*)a; int bvalue = *(int*)b; if (avaluebvalue) return +1; return 0; } static int compare_ints (DB *dest_db, const DBT *akey, const DBT *bkey) { assert(dest_db==NULL); assert(akey->size==sizeof(int)); assert(bkey->size==sizeof(int)); return qsort_compare_ints(akey->data, bkey->data); } static void err_cb(DB *db UU(), int dbn UU(), int err UU(), DBT *key UU(), DBT *val UU(), void *extra UU()) { fprintf(stderr, "error in test"); abort(); } BOOL founddup; static void expect_dups_cb(DB *db UU(), int dbn UU(), int err UU(), DBT *key UU(), DBT *val UU(), void *extra UU()) { founddup=TRUE; } static void test_merge_internal (int a[], int na, int b[], int nb, BOOL dups) { int *MALLOC_N(na+nb, ab); // the combined array a and b for (int i=0; ib[j]); j++; } else { assert(0); } } } } toku_free(cr); toku_free(ar); toku_free(br); toku_free(ab); brt_loader_destroy_error_callback(&bl.error_callback); } /* Test the basic merger. */ static void test_merge (void) { { int avals[]={1,2,3,4,5}; int *bvals = NULL; //icc won't let us use a zero-sized array explicitly or by [] = {} construction. test_merge_internal(avals, 5, bvals, 0, FALSE); test_merge_internal(bvals, 0, avals, 5, FALSE); } { int avals[]={1,3,5,7}; int bvals[]={2,4}; test_merge_internal(avals, 4, bvals, 2, FALSE); test_merge_internal(bvals, 2, avals, 4, FALSE); } { int avals[]={1,2,3,5,6,7}; int bvals[]={2,4,5,6,8}; test_merge_internal(avals, 6, bvals, 5, TRUE); test_merge_internal(bvals, 5, avals, 6, TRUE); } } static void test_internal_mergesort_row_array (int a[], int n) { struct row *MALLOC_N(n, ar); for (int i=0; i= fs.n_temp_files); // destroy_rowset(&aset); // destroy_rowset(&bset); for (int i=0; i<2; i++) assert(fs.data_fidxs[i].idx != -1); brtloader_fi_close_all(&bl.file_infos); QUEUE q; r = queue_create(&q, 0xFFFFFFFF); // infinite queue. assert(r==0); r = merge_files(&fs, &bl, 0, dest_db, compare_ints, 0, q); CKERR(r); assert(fs.n_temp_files==0); DESCRIPTOR_S desc = {.dbt = (DBT){.size = 4, .data="abcd"}}; int fd = open(output_name, O_RDWR | O_CREAT | O_BINARY, S_IRWXU|S_IRWXG|S_IRWXO); assert(fd>=0); r = toku_loader_write_brt_from_q_in_C(&bl, &desc, fd, 1000, q, size_est, 0, 0); assert(r==0); destroy_merge_fileset(&fs); brtloader_fi_destroy(&bl.file_infos, FALSE); brt_loader_lock_destroy(&bl); // verify the dbfile verify_dbfile(10, sorted_keys, sorted_vals, output_name); r = queue_destroy(q); assert(r==0); } /* Test to see if we can open temporary files. */ int test_main (int argc, const char *argv[]) { const char *progname=argv[0]; argc--; argv++; while (argc>0) { if (strcmp(argv[0],"-v")==0) { verbose=1; } else if (strcmp(argv[0],"-q")==0) { verbose=0; } else if (argc!=1) { fprintf(stderr, "Usage:\n %s [-v] [-q] directory\n", progname); exit(1); } else { break; } argc--; argv++; } assert(argc==1); // argv[1] is the directory in which to do the test. const char* directory = argv[0]; int r = toku_os_mkdir(directory, 0755); if (r!=0) CKERR2(errno, EEXIST); int templen = strlen(directory)+15; char template[templen]; { int n = snprintf(template, templen, "%s/tempXXXXXX", directory); assert (n>0 && n0 && n0 && n