/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: #ident "$Id$" /*====== This file is part of PerconaFT. Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. PerconaFT is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. PerconaFT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PerconaFT. If not, see . ---------------------------------------- PerconaFT is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. PerconaFT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with PerconaFT. If not, see . ======= */ #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." #include "test.h" /* try a reverse compare function to verify that the database always uses the application's compare function */ #include #include #include #include #include #include #include static int keycompare (const void *key1, unsigned int key1len, const void *key2, unsigned int key2len) { if (key1len==key2len) { return memcmp(key1,key2,key1len); } else if (key1lendata, a->size, b->data, b->size); } static void expect (DBC *cursor, int k, int v) { DBT key, val; int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT); CKERR(r); assert(key.size == sizeof k); int kk; memcpy(&kk, key.data, key.size); assert(val.size == sizeof v); int vv; memcpy(&vv, val.data, val.size); if (kk != k || vv != v) printf("expect key %u got %u - %u %u\n", (uint32_t)htonl(k), (uint32_t)htonl(kk), (uint32_t)htonl(v), (uint32_t)htonl(vv)); assert(kk == k); assert(vv == v); toku_free(key.data); toku_free(val.data); } static void test_reverse_compare (int n) { if (verbose) printf("test_reverse_compare:%d\n", n); DB_TXN * const null_txn = 0; const char * const fname = "reverse.compare.db"; int r; int i; toku_os_recursive_delete(TOKU_TEST_FILENAME); toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); /* create the dup database file */ DB_ENV *env; r = db_env_create(&env, 0); assert(r == 0); r = env->set_default_bt_compare(env, reverse_compare); CKERR(r); r = env->open(env, TOKU_TEST_FILENAME, DB_CREATE+DB_PRIVATE+DB_INIT_MPOOL, 0); assert(r == 0); DB *db; r = db_create(&db, env, 0); CKERR(r); r = db->set_pagesize(db, 4096); CKERR(r); r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666); CKERR(r); /* insert n unique keys {0, 1, n-1} */ for (i=0; iput(db, null_txn, &key, &val, 0); CKERR(r); } /* reopen the database to force nonleaf buffering */ r = db->close(db, 0); CKERR(r); r = db_create(&db, env, 0); CKERR(r); r = db->set_pagesize(db, 4096); CKERR(r); r = db->open(db, null_txn, fname, "main", DB_BTREE, 0, 0666); CKERR(r); /* insert n unique keys {n, n+1, 2*n-1} */ for (i=n; i<2*n; i++) { DBT key, val; int k, v; k = htonl(i); dbt_init(&key, &k, sizeof k); v = htonl(i); dbt_init(&val, &v, sizeof v); r = db->put(db, null_txn, &key, &val, 0); CKERR(r); } /* verify the sort order with a cursor */ DBC *cursor; r = db->cursor(db, null_txn, &cursor, 0); CKERR(r); //for (i=0; i<2*n; i++) for (i=2*n-1; i>=0; i--) expect(cursor, htonl(i), htonl(i)); r = cursor->c_close(cursor); CKERR(r); r = db->close(db, 0); CKERR(r); r = env->close(env, 0); CKERR(r); } int test_main(int argc, char *const argv[]) { parse_args(argc, argv); int i; for (i = 1; i <= (1<<16); i *= 2) { test_reverse_compare(i); } return 0; }