1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include <stdlib.h>
40 #include <ft/comparator.h>
41 
42 static int MAGIC = 49;
43 static DBT dbt_a;
44 static DBT dbt_b;
45 static DESCRIPTOR expected_desc;
46 
magic_compare(DB * db,const DBT * a,const DBT * b)47 static int magic_compare(DB *db, const DBT *a, const DBT *b) {
48     invariant(db && a && b);
49     invariant(db->cmp_descriptor == expected_desc);
50     invariant(a == &dbt_a);
51     invariant(b == &dbt_b);
52     return MAGIC;
53 }
54 
test_desc(void)55 static void test_desc(void) {
56     int c;
57     toku::comparator cmp;
58     DESCRIPTOR_S d1, d2;
59 
60     // create with d1, make sure it gets used
61     cmp.create(magic_compare, &d1);
62     expected_desc = &d1;
63     c = cmp(&dbt_a, &dbt_b);
64     invariant(c == MAGIC);
65 
66     // set desc to d2, make sure it gets used
67     toku::comparator cmp2;
68     cmp2.create(magic_compare, &d2);
69     cmp.inherit(cmp2);
70     expected_desc = &d2;
71     c = cmp(&dbt_a, &dbt_b);
72     invariant(c == MAGIC);
73     cmp2.destroy();
74 
75     // go back to using d1, but using the create_from API
76     toku::comparator cmp3, cmp4;
77     cmp3.create(magic_compare, &d1); // cmp3 has d1
78     cmp4.create_from(cmp3); // cmp4 should get d1 from cmp3
79     expected_desc = &d1;
80     c = cmp3(&dbt_a, &dbt_b);
81     invariant(c == MAGIC);
82     c = cmp4(&dbt_a, &dbt_b);
83     invariant(c == MAGIC);
84     cmp3.destroy();
85     cmp4.destroy();
86 
87     cmp.destroy();
88 }
89 
dont_compare_me_bro(DB * db,const DBT * a,const DBT * b)90 static int dont_compare_me_bro(DB *db, const DBT *a, const DBT *b) {
91     abort();
92     return db && a && b;
93 }
94 
test_infinity(void)95 static void test_infinity(void) {
96     int c;
97     toku::comparator cmp;
98     cmp.create(dont_compare_me_bro, nullptr);
99 
100     // make sure infinity-valued end points compare as expected
101     // to an arbitrary (uninitialized!) dbt. the comparison function
102     // should never be called and thus the dbt never actually read.
103     DBT arbitrary_dbt;
104 
105     c = cmp(&arbitrary_dbt, toku_dbt_positive_infinity());
106     invariant(c < 0);
107     c = cmp(toku_dbt_negative_infinity(), &arbitrary_dbt);
108     invariant(c < 0);
109 
110     c = cmp(toku_dbt_positive_infinity(), &arbitrary_dbt);
111     invariant(c > 0);
112     c = cmp(&arbitrary_dbt, toku_dbt_negative_infinity());
113     invariant(c > 0);
114 
115     c = cmp(toku_dbt_negative_infinity(), toku_dbt_negative_infinity());
116     invariant(c == 0);
117     c = cmp(toku_dbt_positive_infinity(), toku_dbt_positive_infinity());
118     invariant(c == 0);
119 
120     cmp.destroy();
121 }
122 
main(void)123 int main(void) {
124     test_desc();
125     test_infinity();
126     return 0;
127 }
128