File: comparator-test.cc

package info (click to toggle)
mariadb-10.0 10.0.32-0%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 476,064 kB
  • sloc: cpp: 1,400,131; ansic: 832,140; perl: 54,391; sh: 41,304; pascal: 32,365; yacc: 14,921; xml: 5,257; sql: 4,667; cs: 4,647; makefile: 4,555; ruby: 4,465; python: 2,292; lex: 1,427; java: 941; asm: 295; awk: 54; php: 22; sed: 16
file content (127 lines) | stat: -rw-r--r-- 3,898 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* -*- 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 <http://www.gnu.org/licenses/>.

----------------------------------------

    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 <http://www.gnu.org/licenses/>.
======= */

#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."

#include <stdlib.h>
#include <ft/comparator.h>

static int MAGIC = 49;
static DBT dbt_a;
static DBT dbt_b;
static DESCRIPTOR expected_desc;

static int magic_compare(DB *db, const DBT *a, const DBT *b) {
    invariant(db && a && b);
    invariant(db->cmp_descriptor == expected_desc);
    invariant(a == &dbt_a);
    invariant(b == &dbt_b);
    return MAGIC;
}

static void test_desc(void) {
    int c;
    toku::comparator cmp;
    DESCRIPTOR_S d1, d2;

    // create with d1, make sure it gets used
    cmp.create(magic_compare, &d1);
    expected_desc = &d1;
    c = cmp(&dbt_a, &dbt_b);
    invariant(c == MAGIC);

    // set desc to d2, make sure it gets used
    toku::comparator cmp2;
    cmp2.create(magic_compare, &d2);
    cmp.inherit(cmp2);
    expected_desc = &d2;
    c = cmp(&dbt_a, &dbt_b);
    invariant(c == MAGIC);
    cmp2.destroy();

    // go back to using d1, but using the create_from API
    toku::comparator cmp3, cmp4;
    cmp3.create(magic_compare, &d1); // cmp3 has d1
    cmp4.create_from(cmp3); // cmp4 should get d1 from cmp3
    expected_desc = &d1;
    c = cmp3(&dbt_a, &dbt_b);
    invariant(c == MAGIC);
    c = cmp4(&dbt_a, &dbt_b);
    invariant(c == MAGIC);
    cmp3.destroy();
    cmp4.destroy();

    cmp.destroy();
}

static int dont_compare_me_bro(DB *db, const DBT *a, const DBT *b) {
    abort();
    return db && a && b;
}

static void test_infinity(void) {
    int c;
    toku::comparator cmp;
    cmp.create(dont_compare_me_bro, nullptr);

    // make sure infinity-valued end points compare as expected
    // to an arbitrary (uninitialized!) dbt. the comparison function
    // should never be called and thus the dbt never actually read.
    DBT arbitrary_dbt;

    c = cmp(&arbitrary_dbt, toku_dbt_positive_infinity());
    invariant(c < 0);
    c = cmp(toku_dbt_negative_infinity(), &arbitrary_dbt);
    invariant(c < 0);

    c = cmp(toku_dbt_positive_infinity(), &arbitrary_dbt);
    invariant(c > 0);
    c = cmp(&arbitrary_dbt, toku_dbt_negative_infinity());
    invariant(c > 0);

    c = cmp(toku_dbt_negative_infinity(), toku_dbt_negative_infinity());
    invariant(c == 0);
    c = cmp(toku_dbt_positive_infinity(), toku_dbt_positive_infinity());
    invariant(c == 0);

    cmp.destroy();
}

int main(void) {
    test_desc();
    test_infinity();
    return 0;
}