David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 20 | #define _USE_MATH_DEFINES |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <cmath> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 23 | #include <thrift/transport/TBufferTransports.h> |
| 24 | #include <thrift/protocol/TJSONProtocol.h> |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 25 | #include "gen-cpp/DebugProtoTest_types.h" |
| 26 | |
| 27 | int main() { |
| 28 | using std::cout; |
| 29 | using std::endl; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 30 | using namespace thrift::test::debug; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 31 | using apache::thrift::transport::TMemoryBuffer; |
| 32 | using apache::thrift::protocol::TJSONProtocol; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 33 | |
| 34 | OneOfEach ooe; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 35 | ooe.im_true = true; |
| 36 | ooe.im_false = false; |
| 37 | ooe.a_bite = 0x7f; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 38 | ooe.integer16 = 27000; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 39 | ooe.integer32 = 1 << 24; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 40 | ooe.integer64 = (uint64_t)6000 * 1000 * 1000; |
| 41 | ooe.double_precision = M_PI; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 42 | ooe.some_characters = "JSON THIS! \"\1"; |
| 43 | ooe.zomg_unicode = "\xd7\n\a\t"; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 44 | ooe.base64 = "\1\2\3\255"; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 45 | cout << apache::thrift::ThriftJSONString(ooe) << endl << endl; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 46 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 47 | Nesting n; |
| 48 | n.my_ooe = ooe; |
| 49 | n.my_ooe.integer16 = 16; |
| 50 | n.my_ooe.integer32 = 32; |
| 51 | n.my_ooe.integer64 = 64; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 52 | n.my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2; |
| 53 | n.my_ooe.some_characters = ":R (me going \"rrrr\")"; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 54 | n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20" |
| 55 | "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e" |
| 56 | "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 57 | n.my_bonk.type = 31337; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 58 | n.my_bonk.message = "I am a bonk... xor!"; |
| 59 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 60 | cout << apache::thrift::ThriftJSONString(n) << endl << endl; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 61 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 62 | HolyMoley hm; |
| 63 | |
| 64 | hm.big.push_back(ooe); |
| 65 | hm.big.push_back(n.my_ooe); |
| 66 | hm.big[0].a_bite = 0x22; |
| 67 | hm.big[1].a_bite = 0x33; |
| 68 | |
| 69 | std::vector<std::string> stage1; |
| 70 | stage1.push_back("and a one"); |
| 71 | stage1.push_back("and a two"); |
| 72 | hm.contain.insert(stage1); |
| 73 | stage1.clear(); |
| 74 | stage1.push_back("then a one, two"); |
| 75 | stage1.push_back("three!"); |
| 76 | stage1.push_back("FOUR!!"); |
| 77 | hm.contain.insert(stage1); |
| 78 | stage1.clear(); |
| 79 | hm.contain.insert(stage1); |
| 80 | |
| 81 | std::vector<Bonk> stage2; |
| 82 | hm.bonks["nothing"] = stage2; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 83 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 84 | stage2.back().type = 1; |
| 85 | stage2.back().message = "Wait."; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 86 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 87 | stage2.back().type = 2; |
| 88 | stage2.back().message = "What?"; |
| 89 | hm.bonks["something"] = stage2; |
| 90 | stage2.clear(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 91 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 92 | stage2.back().type = 3; |
| 93 | stage2.back().message = "quoth"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 94 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 95 | stage2.back().type = 4; |
| 96 | stage2.back().message = "the raven"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 97 | stage2.resize(stage2.size() + 1); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 98 | stage2.back().type = 5; |
| 99 | stage2.back().message = "nevermore"; |
| 100 | hm.bonks["poe"] = stage2; |
| 101 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 102 | cout << apache::thrift::ThriftJSONString(hm) << endl << endl; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 103 | |
| 104 | boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 105 | boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); |
| 106 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 107 | cout << "Testing ooe" << endl; |
| 108 | |
| 109 | ooe.write(proto.get()); |
| 110 | OneOfEach ooe2; |
| 111 | ooe2.read(proto.get()); |
| 112 | |
| 113 | assert(ooe == ooe2); |
| 114 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 115 | cout << "Testing hm" << endl; |
| 116 | |
| 117 | hm.write(proto.get()); |
| 118 | HolyMoley hm2; |
| 119 | hm2.read(proto.get()); |
| 120 | |
| 121 | assert(hm == hm2); |
| 122 | |
Christian Lavoie | cbf87cb | 2010-11-28 14:34:26 +0000 | [diff] [blame] | 123 | hm2.big[0].a_bite = 0x00; |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 124 | |
| 125 | assert(hm != hm2); |
| 126 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 127 | Doubles dub; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 128 | dub.nan = HUGE_VAL / HUGE_VAL; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 129 | dub.inf = HUGE_VAL; |
| 130 | dub.neginf = -HUGE_VAL; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 131 | dub.repeating = 10.0 / 3.0; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 132 | dub.big = 1E+305; |
Roger Meier | 22888ce | 2014-02-09 11:31:02 +0100 | [diff] [blame] | 133 | dub.tiny = 1E-305; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 134 | dub.zero = 0.0; |
| 135 | dub.negzero = -0.0; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 136 | cout << apache::thrift::ThriftJSONString(dub) << endl << endl; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 137 | |
David Reiss | 4a8f46c | 2008-03-07 20:12:07 +0000 | [diff] [blame] | 138 | cout << "Testing base" << endl; |
| 139 | |
| 140 | Base64 base; |
| 141 | base.a = 123; |
| 142 | base.b1 = "1"; |
| 143 | base.b2 = "12"; |
| 144 | base.b3 = "123"; |
| 145 | base.b4 = "1234"; |
| 146 | base.b5 = "12345"; |
| 147 | base.b6 = "123456"; |
| 148 | |
| 149 | base.write(proto.get()); |
| 150 | Base64 base2; |
| 151 | base2.read(proto.get()); |
| 152 | |
| 153 | assert(base == base2); |
| 154 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |