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 "test.h"
40 
41 
42 
43 static void
cleanup_and_free(struct simple_dbt * v)44 cleanup_and_free(struct simple_dbt *v) {
45     if (v->data) toku_free(v->data);
46     v->data = NULL;
47     v->len = 0;
48 }
49 
50 static void
cleanup(struct simple_dbt * v)51 cleanup(struct simple_dbt *v) {
52     v->data = NULL;
53     v->len = 0;
54 }
55 
ybt_test0(void)56 static void ybt_test0 (void) {
57     struct simple_dbt v0 = {0,0}, v1 = {0,0};
58     DBT  t0,t1;
59     toku_init_dbt(&t0);
60     toku_init_dbt(&t1);
61     {
62 	const void *temp1 = "hello";
63         toku_dbt_set(6, temp1, &t0, &v0);
64     }
65     {
66         const void *temp2 = "foo";
67 	toku_dbt_set(  4, temp2, &t1, &v1);
68     }
69     assert(t0.size==6);
70     assert(strcmp((char*)t0.data, "hello")==0);
71     assert(t1.size==4);
72     assert(strcmp((char*)t1.data, "foo")==0);
73 
74     {
75         const void *temp3 = "byebye";
76 	toku_dbt_set(7, temp3, &t1, &v0);      /* Use v0, not v1 */
77     }
78     // This assertion would be wrong, since v0 may have been realloc'd, and t0.data may now point
79     // at the wrong place
80     //assert(strcmp(t0.data, "byebye")==0);     /* t0's data should be changed too, since it used v0 */
81     assert(strcmp((char*)t1.data, "byebye")==0);
82 
83     cleanup_and_free(&v0);
84     cleanup_and_free(&v1);
85 
86 
87     /* See if we can probe to find out how big something is by setting ulen=0 with YBT_USERMEM */
88     toku_init_dbt(&t0);
89     t0.flags = DB_DBT_USERMEM;
90     t0.ulen  = 0;
91     {
92         const void *temp4 = "hello";
93 	toku_dbt_set(6, temp4, &t0, 0);
94     }
95     assert(t0.data==0);
96     assert(t0.size==6);
97 
98     /* Check realloc. */
99     toku_init_dbt(&t0);
100     t0.flags = DB_DBT_REALLOC;
101     cleanup(&v0);
102     {
103         const void *temp5 = "internationalization";
104 	toku_dbt_set(21, temp5, &t0, &v0);
105     }
106     assert(v0.data==0); /* Didn't change v0 */
107     assert(t0.size==21);
108     assert(strcmp((char*)t0.data, "internationalization")==0);
109 
110     {
111         const void *temp6 = "provincial";
112 	toku_dbt_set(11, temp6, &t0, &v0);
113     }
114     assert(t0.size==11);
115     assert(strcmp((char*)t0.data, "provincial")==0);
116 
117     toku_free(t0.data);
118 
119 }
120 
121 int
test_main(int argc,const char * argv[])122 test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute__((__unused__))) {
123     ybt_test0();
124     return 0;
125 }
126