blob: 0953c19134ae52a2db19094464df5e5d9de24ff8 [file] [log] [blame]
David Reiss4e7530d2007-09-04 21:49:53 +00001/*
2../compiler/cpp/thrift -cpp -dense DebugProtoTest.thrift
3g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
4 DenseProtoTest.cpp gen-cpp/DebugProtoTest_types.cpp \
5 ../lib/cpp/.libs/libthrift.a -o DenseProtoTest
6./DenseProtoTest
7*/
8
David Reisse67c0e62007-09-07 01:34:12 +00009// I do this to reach into the guts of TDenseProtocol. Sorry.
10#define private public
11#define inline
12
13#undef NDEBUG
14#include <cassert>
David Reiss4e7530d2007-09-04 21:49:53 +000015#include <iostream>
16#include <cmath>
17#include "gen-cpp/DebugProtoTest_types.h"
18#include <protocol/TDenseProtocol.h>
19#include <transport/TTransportUtils.h>
20
David Reisse67c0e62007-09-07 01:34:12 +000021
22// Can't use memcmp here. GCC is too smart.
23bool my_memeq(const char* str1, const char* str2, int len) {
24 for (int i = 0; i < len; i++) {
25 if (str1[i] != str2[i]) {
26 return false;
27 }
28 }
29 return true;
30}
31
32
David Reiss4e7530d2007-09-04 21:49:53 +000033int main() {
34 using std::cout;
35 using std::endl;
36 using boost::shared_ptr;
37 using namespace thrift::test;
38 using namespace facebook::thrift::transport;
39 using namespace facebook::thrift::protocol;
40
41
42 OneOfEach ooe;
43 ooe.im_true = true;
44 ooe.im_false = false;
45 ooe.a_bite = 0xd6;
46 ooe.integer16 = 27000;
47 ooe.integer32 = 1<<24;
48 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
49 ooe.double_precision = M_PI;
50 ooe.some_characters = "Debug THIS!";
51 ooe.zomg_unicode = "\xd7\n\a\t";
52
53 //cout << facebook::thrift::ThriftDebugString(ooe) << endl << endl;
54
55
56 Nesting n;
57 n.my_ooe = ooe;
58 n.my_ooe.integer16 = 16;
59 n.my_ooe.integer32 = 32;
60 n.my_ooe.integer64 = 64;
61 n.my_ooe.double_precision = (std::sqrt(5)+1)/2;
62 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
63 n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
64 "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
65 "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
66 n.my_bonk.type = 31337;
67 n.my_bonk.message = "I am a bonk... xor!";
68
69 //cout << facebook::thrift::ThriftDebugString(n) << endl << endl;
70
71
72 HolyMoley hm;
73
74 hm.big.push_back(ooe);
75 hm.big.push_back(n.my_ooe);
76 hm.big[0].a_bite = 0x22;
77 hm.big[1].a_bite = 0x33;
78
79 std::vector<std::string> stage1;
80 stage1.push_back("and a one");
81 stage1.push_back("and a two");
82 hm.contain.insert(stage1);
83 stage1.clear();
84 stage1.push_back("then a one, two");
85 stage1.push_back("three!");
86 stage1.push_back("FOUR!!");
87 hm.contain.insert(stage1);
88 stage1.clear();
89 hm.contain.insert(stage1);
90
91 std::vector<Bonk> stage2;
92 hm.bonks["nothing"] = stage2;
93 stage2.resize(stage2.size()+1);
94 stage2.back().type = 1;
95 stage2.back().message = "Wait.";
96 stage2.resize(stage2.size()+1);
97 stage2.back().type = 2;
98 stage2.back().message = "What?";
99 hm.bonks["something"] = stage2;
100 stage2.clear();
101 stage2.resize(stage2.size()+1);
102 stage2.back().type = 3;
103 stage2.back().message = "quoth";
104 stage2.resize(stage2.size()+1);
105 stage2.back().type = 4;
106 stage2.back().message = "the raven";
107 stage2.resize(stage2.size()+1);
108 stage2.back().type = 5;
109 stage2.back().message = "nevermore";
110 hm.bonks["poe"] = stage2;
111
112 //cout << facebook::thrift::ThriftDebugString(hm) << endl << endl;
113
114 shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
115 shared_ptr<TDenseProtocol> proto(new TDenseProtocol(buffer));
116 proto->setTypeSpec(HolyMoley::local_reflection);
117
118 hm.write(proto.get());
119 HolyMoley hm2;
120 hm2.read(proto.get());
121
122 assert(hm == hm2);
123
124
David Reisse67c0e62007-09-07 01:34:12 +0000125 // Let's test out the variable-length ints, shall we?
126 uint64_t vli;
127 #define checkout(i, c) { \
128 buffer->resetBuffer(); \
129 proto->vliWrite(i); \
130 assert(my_memeq(buffer->getBufferAsString().data(), c, sizeof(c)-1)); \
131 proto->vliRead(vli); \
132 assert(vli == i); \
133 }
134
135 checkout(0x00000000, "\x00");
136 checkout(0x00000040, "\x40");
137 checkout(0x0000007F, "\x7F");
138 checkout(0x00000080, "\x81\x00");
139 checkout(0x00002000, "\xC0\x00");
140 checkout(0x00003FFF, "\xFF\x7F");
141 checkout(0x00004000, "\x81\x80\x00");
142 checkout(0x00100000, "\xC0\x80\x00");
143 checkout(0x001FFFFF, "\xFF\xFF\x7F");
144 checkout(0x00200000, "\x81\x80\x80\x00");
145 checkout(0x08000000, "\xC0\x80\x80\x00");
146 checkout(0x0FFFFFFF, "\xFF\xFF\xFF\x7F");
147 checkout(0x10000000, "\x81\x80\x80\x80\x00");
148 checkout(0x20000000, "\x82\x80\x80\x80\x00");
149 checkout(0x1FFFFFFF, "\x81\xFF\xFF\xFF\x7F");
150 checkout(0xFFFFFFFF, "\x8F\xFF\xFF\xFF\x7F");
151
152 checkout(0x0000000100000000ull, "\x90\x80\x80\x80\x00");
153 checkout(0x0000000200000000ull, "\xA0\x80\x80\x80\x00");
154 checkout(0x0000000300000000ull, "\xB0\x80\x80\x80\x00");
155 checkout(0x0000000700000000ull, "\xF0\x80\x80\x80\x00");
156 checkout(0x00000007F0000000ull, "\xFF\x80\x80\x80\x00");
157 checkout(0x00000007FFFFFFFFull, "\xFF\xFF\xFF\xFF\x7F");
158 checkout(0x0000000800000000ull, "\x81\x80\x80\x80\x80\x00");
159 checkout(0x1FFFFFFFFFFFFFFFull, "\x9F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
160 checkout(0x7FFFFFFFFFFFFFFFull, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
161 checkout(0xFFFFFFFFFFFFFFFFull, "\x81\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
162
163
David Reiss4e7530d2007-09-04 21:49:53 +0000164 return 0;
165}