MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sp_test.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ndb_global.h>
20 
21 #include "SimpleProperties.hpp"
22 #include <NdbOut.hpp>
23 
24 Uint32 page[8192];
25 
26 int writer();
27 int reader(Uint32 *, Uint32 len);
28 int unpack(Uint32 *, Uint32 len);
29 
30 int main(){
31  int len = writer();
32  reader(page, len);
33  unpack(page, len);
34 
35  return 0;
36 }
37 
38 int
39 writer(){
40  LinearWriter w(&page[0], 8192);
41 
42  w.first();
43  w.add(1, 2);
44  w.add(7, 3);
45  w.add(3, "jonas");
46  w.add(5, "0123456789");
47  w.add(7, 4);
48  w.add(3, "e cool");
49  w.add(5, "9876543210");
50 
51  ndbout_c("WordsUsed = %d", w.getWordsUsed());
52 
53  return w.getWordsUsed();
54 }
55 
56 int
57 reader(Uint32 * pages, Uint32 len){
58  SimplePropertiesLinearReader it(pages, len);
59 
60  it.printAll(ndbout);
61  return 0;
62 }
63 
64 struct Test {
65  Uint32 val1;
66  Uint32 val7;
67  char val3[100];
68  Test() : val1(0xFFFFFFFF), val7(0xFFFFFFFF) { sprintf(val3, "bad");}
69 };
70 
71 static const
73 test_map [] = {
74  { 1, offsetof(Test, val1), SimpleProperties::Uint32Value, 0, ~0 },
75  { 7, offsetof(Test, val7), SimpleProperties::Uint32Value, 0, ~0 },
76  { 3, offsetof(Test, val3), SimpleProperties::StringValue, 0, sizeof(100) },
77  { 5, 0, SimpleProperties::InvalidValue, 0, 0 }
78 };
79 
80 static unsigned
81 test_map_sz = sizeof(test_map)/sizeof(test_map[0]);
82 
83 int
84 unpack(Uint32 * pages, Uint32 len){
85  Test test;
86  SimplePropertiesLinearReader it(pages, len);
88  while((status = SimpleProperties::unpack(it, &test, test_map, test_map_sz,
89  true, false)) == SimpleProperties::Break){
90  ndbout << "test.val1 = " << test.val1 << endl;
91  ndbout << "test.val7 = " << test.val7 << endl;
92  ndbout << "test.val3 = " << test.val3 << endl;
93  it.next();
94  }
95  assert(status == SimpleProperties::Eof);
96  return 0;
97 }