blob: cc72b64892ce0a37a3085ed10cfd52710a382636 [file] [log] [blame]
David Reissdb0ea152008-02-18 01:49:37 +00001#include <iostream>
2#include <cmath>
3#include <transport/TTransportUtils.h>
4#include <protocol/TJSONProtocol.h>
5#include "gen-cpp/DebugProtoTest_types.h"
6
7int main() {
8 using std::cout;
9 using std::endl;
David Reiss2a4bfd62008-04-07 23:45:00 +000010 using namespace thrift::test::debug;
David Reissdb0ea152008-02-18 01:49:37 +000011 using facebook::thrift::transport::TMemoryBuffer;
12 using facebook::thrift::protocol::TJSONProtocol;
13
14 OneOfEach ooe;
15 ooe.im_true = true;
16 ooe.im_false = false;
17 ooe.a_bite = 0xd6;
18 ooe.integer16 = 27000;
19 ooe.integer32 = 1<<24;
20 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
21 ooe.double_precision = M_PI;
22 ooe.some_characters = "JSON THIS! \"\1";
23 ooe.zomg_unicode = "\xd7\n\a\t";
24 ooe.base64 = "\1\2\3\255";
25 cout << facebook::thrift::ThriftJSONString(ooe) << endl << endl;
26
27
28 Nesting n;
29 n.my_ooe = ooe;
30 n.my_ooe.integer16 = 16;
31 n.my_ooe.integer32 = 32;
32 n.my_ooe.integer64 = 64;
33 n.my_ooe.double_precision = (std::sqrt(5)+1)/2;
34 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
35 n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
36 "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
37 "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
38 n.my_bonk.type = 31337;
39 n.my_bonk.message = "I am a bonk... xor!";
40
41 cout << facebook::thrift::ThriftJSONString(n) << endl << endl;
42
43
44 HolyMoley hm;
45
46 hm.big.push_back(ooe);
47 hm.big.push_back(n.my_ooe);
48 hm.big[0].a_bite = 0x22;
49 hm.big[1].a_bite = 0x33;
50
51 std::vector<std::string> stage1;
52 stage1.push_back("and a one");
53 stage1.push_back("and a two");
54 hm.contain.insert(stage1);
55 stage1.clear();
56 stage1.push_back("then a one, two");
57 stage1.push_back("three!");
58 stage1.push_back("FOUR!!");
59 hm.contain.insert(stage1);
60 stage1.clear();
61 hm.contain.insert(stage1);
62
63 std::vector<Bonk> stage2;
64 hm.bonks["nothing"] = stage2;
65 stage2.resize(stage2.size()+1);
66 stage2.back().type = 1;
67 stage2.back().message = "Wait.";
68 stage2.resize(stage2.size()+1);
69 stage2.back().type = 2;
70 stage2.back().message = "What?";
71 hm.bonks["something"] = stage2;
72 stage2.clear();
73 stage2.resize(stage2.size()+1);
74 stage2.back().type = 3;
75 stage2.back().message = "quoth";
76 stage2.resize(stage2.size()+1);
77 stage2.back().type = 4;
78 stage2.back().message = "the raven";
79 stage2.resize(stage2.size()+1);
80 stage2.back().type = 5;
81 stage2.back().message = "nevermore";
82 hm.bonks["poe"] = stage2;
83
84 cout << facebook::thrift::ThriftJSONString(hm) << endl << endl;
85
86 boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
87 boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
88
89
90 cout << "Testing ooe" << endl;
91
92 ooe.write(proto.get());
93 OneOfEach ooe2;
94 ooe2.read(proto.get());
95
96 assert(ooe == ooe2);
97
98
99 cout << "Testing hm" << endl;
100
101 hm.write(proto.get());
102 HolyMoley hm2;
103 hm2.read(proto.get());
104
105 assert(hm == hm2);
106
David Reiss4a8f46c2008-03-07 20:12:07 +0000107 hm2.big[0].a_bite = 0xFF;
108
109 assert(hm != hm2);
110
David Reissdb0ea152008-02-18 01:49:37 +0000111 Doubles dub;
112 dub.nan = HUGE_VAL/HUGE_VAL;
113 dub.inf = HUGE_VAL;
114 dub.neginf = -HUGE_VAL;
115 dub.repeating = 10.0/3.0;
116 dub.big = 1E+305;
117 dub.small = 1E-305;
118 dub.zero = 0.0;
119 dub.negzero = -0.0;
120 cout << facebook::thrift::ThriftJSONString(dub) << endl << endl;
121
David Reiss4a8f46c2008-03-07 20:12:07 +0000122 cout << "Testing base" << endl;
123
124 Base64 base;
125 base.a = 123;
126 base.b1 = "1";
127 base.b2 = "12";
128 base.b3 = "123";
129 base.b4 = "1234";
130 base.b5 = "12345";
131 base.b6 = "123456";
132
133 base.write(proto.get());
134 Base64 base2;
135 base2.read(proto.get());
136
137 assert(base == base2);
138
David Reissdb0ea152008-02-18 01:49:37 +0000139 return 0;
140}