David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <cmath> |
| 3 | #include <transport/TTransportUtils.h> |
| 4 | #include <protocol/TBinaryProtocol.h> |
| 5 | #include <gen-cpp/DebugProtoTest_types.h> |
| 6 | |
| 7 | using std::cout; |
| 8 | using std::endl; |
| 9 | using namespace thrift::test::debug; |
| 10 | using namespace apache::thrift::transport; |
| 11 | using namespace apache::thrift::protocol; |
| 12 | |
| 13 | typedef TBinaryProtocolT<TMemoryBuffer> MyProtocol; |
| 14 | //typedef TBinaryProtocolT<TTransport> MyProtocol; |
| 15 | |
| 16 | int main() { |
| 17 | |
| 18 | OneOfEach ooe; |
| 19 | ooe.im_true = true; |
| 20 | ooe.im_false = false; |
| 21 | ooe.a_bite = 0xd6; |
| 22 | ooe.integer16 = 27000; |
| 23 | ooe.integer32 = 1<<24; |
| 24 | ooe.integer64 = (uint64_t)6000 * 1000 * 1000; |
| 25 | ooe.double_precision = M_PI; |
| 26 | ooe.some_characters = "JSON THIS! \"\1"; |
| 27 | ooe.zomg_unicode = "\xd7\n\a\t"; |
| 28 | ooe.base64 = "\1\2\3\255"; |
| 29 | |
| 30 | Nesting n; |
| 31 | n.my_ooe = ooe; |
| 32 | n.my_ooe.integer16 = 16; |
| 33 | n.my_ooe.integer32 = 32; |
| 34 | n.my_ooe.integer64 = 64; |
| 35 | n.my_ooe.double_precision = (std::sqrt(5)+1)/2; |
| 36 | n.my_ooe.some_characters = ":R (me going \"rrrr\")"; |
| 37 | n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20" |
| 38 | "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e" |
| 39 | "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc"; |
| 40 | n.my_bonk.type = 31337; |
| 41 | n.my_bonk.message = "I am a bonk... xor!"; |
| 42 | |
| 43 | HolyMoley hm; |
| 44 | |
| 45 | hm.big.push_back(ooe); |
| 46 | hm.big.push_back(n.my_ooe); |
| 47 | hm.big[0].a_bite = 0x22; |
| 48 | hm.big[1].a_bite = 0x33; |
| 49 | |
| 50 | std::vector<std::string> stage1; |
| 51 | stage1.push_back("and a one"); |
| 52 | stage1.push_back("and a two"); |
| 53 | hm.contain.insert(stage1); |
| 54 | stage1.clear(); |
| 55 | stage1.push_back("then a one, two"); |
| 56 | stage1.push_back("three!"); |
| 57 | stage1.push_back("FOUR!!"); |
| 58 | hm.contain.insert(stage1); |
| 59 | stage1.clear(); |
| 60 | hm.contain.insert(stage1); |
| 61 | |
| 62 | std::vector<Bonk> stage2; |
| 63 | hm.bonks["nothing"] = stage2; |
| 64 | stage2.resize(stage2.size()+1); |
| 65 | stage2.back().type = 1; |
| 66 | stage2.back().message = "Wait."; |
| 67 | stage2.resize(stage2.size()+1); |
| 68 | stage2.back().type = 2; |
| 69 | stage2.back().message = "What?"; |
| 70 | hm.bonks["something"] = stage2; |
| 71 | stage2.clear(); |
| 72 | stage2.resize(stage2.size()+1); |
| 73 | stage2.back().type = 3; |
| 74 | stage2.back().message = "quoth"; |
| 75 | stage2.resize(stage2.size()+1); |
| 76 | stage2.back().type = 4; |
| 77 | stage2.back().message = "the raven"; |
| 78 | stage2.resize(stage2.size()+1); |
| 79 | stage2.back().type = 5; |
| 80 | stage2.back().message = "nevermore"; |
| 81 | hm.bonks["poe"] = stage2; |
| 82 | |
| 83 | boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); |
| 84 | boost::shared_ptr<TProtocol> proto(new MyProtocol(buffer)); |
| 85 | |
| 86 | cout << "Testing ooe" << endl; |
| 87 | |
| 88 | ooe.write(proto.get()); |
| 89 | OneOfEach ooe2; |
| 90 | ooe2.read(proto.get()); |
| 91 | |
| 92 | assert(ooe == ooe2); |
| 93 | |
| 94 | |
| 95 | cout << "Testing hm" << endl; |
| 96 | |
| 97 | hm.write(proto.get()); |
| 98 | HolyMoley hm2; |
| 99 | hm2.read(proto.get()); |
| 100 | |
| 101 | assert(hm == hm2); |
| 102 | |
| 103 | hm2.big[0].a_bite = 0xFF; |
| 104 | |
| 105 | assert(hm != hm2); |
| 106 | |
| 107 | return 0; |
| 108 | } |