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 "ft/serialize/rbtree_mhs.h"
40 #include "test.h"
41 #include <algorithm>
42 #include <vector>
43 #include <ctime>
44 #include <cstdlib>
45 
46 #define N 1000000
47 std::vector<MhsRbTree::Node::BlockPair> input_vector;
48 MhsRbTree::Node::BlockPair old_vector[N];
49 
myrandom(int i)50 static int myrandom(int i) { return std::rand() % i; }
51 
generate_random_input()52 static void generate_random_input() {
53     std::srand(unsigned(std::time(0)));
54 
55     // set some values:
56     for (uint64_t i = 0; i < N; ++i) {
57         MhsRbTree::Node::BlockPair bp = {i+1, 0};
58         input_vector.push_back(bp);
59         old_vector[i] = bp;
60     }
61     // using built-in random generator:
62     std::random_shuffle(input_vector.begin(), input_vector.end(), myrandom);
63 }
64 
test_insert_remove(void)65 static void test_insert_remove(void) {
66     int i;
67     MhsRbTree::Tree *tree = new MhsRbTree::Tree();
68     verbose = 0;
69     generate_random_input();
70     if (verbose) {
71         printf("\n we are going to insert the following block offsets\n");
72         for (i = 0; i < N; i++)
73             printf("%" PRIu64 "\t", input_vector[i]._offset.ToInt());
74     }
75     for (i = 0; i < N; i++) {
76         tree->Insert(input_vector[i]);
77         // tree->ValidateBalance();
78     }
79     tree->ValidateBalance();
80     MhsRbTree::Node::BlockPair *p_bps = &old_vector[0];
81     tree->ValidateInOrder(p_bps);
82     printf("min node of the tree:%" PRIu64 "\n",
83            rbn_offset(tree->MinNode()).ToInt());
84     printf("max node of the tree:%" PRIu64 "\n",
85            rbn_offset(tree->MaxNode()).ToInt());
86 
87     for (i = 0; i < N; i++) {
88         // tree->ValidateBalance();
89         tree->RawRemove(input_vector[i]._offset.ToInt());
90     }
91 
92     tree->Destroy();
93     delete tree;
94 }
95 
test_main(int argc,const char * argv[])96 int test_main(int argc, const char *argv[]) {
97     default_parse_args(argc, argv);
98 
99     test_insert_remove();
100     if (verbose)
101         printf("test ok\n");
102     return 0;
103 }
104