| 1 | #include <string> |
|---|---|
| 2 | |
| 3 | #include <iostream> |
| 4 | #include <IO/VarInt.h> |
| 5 | #include <IO/WriteBufferFromString.h> |
| 6 | #include <IO/ReadBufferFromString.h> |
| 7 | #include <IO/ReadHelpers.h> |
| 8 | #include <Poco/HexBinaryEncoder.h> |
| 9 | |
| 10 | |
| 11 | static void parse_trash_string_as_uint_must_fail(const std::string & str) |
| 12 | { |
| 13 | using namespace DB; |
| 14 | |
| 15 | unsigned x = 0xFF; |
| 16 | |
| 17 | try |
| 18 | { |
| 19 | x = parse<unsigned>(str); |
| 20 | } |
| 21 | catch (...) |
| 22 | { |
| 23 | /// Ok |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | std::cerr << "Parsing must fail, but finished sucessfully x="<< x; |
| 28 | exit(-1); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | int main(int argc, char ** argv) |
| 33 | { |
| 34 | parse_trash_string_as_uint_must_fail("trash"); |
| 35 | parse_trash_string_as_uint_must_fail("-1"); |
| 36 | |
| 37 | if (argc != 2) |
| 38 | { |
| 39 | std::cerr << "Usage: "<< std::endl |
| 40 | << argv[0] << " unsigned_number"<< std::endl; |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | DB::UInt64 x = DB::parse<UInt64>(argv[1]); |
| 45 | Poco::HexBinaryEncoder hex(std::cout); |
| 46 | DB::writeVarUInt(x, hex); |
| 47 | std::cout << std::endl; |
| 48 | |
| 49 | std::string s; |
| 50 | |
| 51 | { |
| 52 | DB::WriteBufferFromString wb(s); |
| 53 | DB::writeVarUInt(x, wb); |
| 54 | wb.next(); |
| 55 | } |
| 56 | |
| 57 | hex << s; |
| 58 | std::cout << std::endl; |
| 59 | |
| 60 | s.clear(); |
| 61 | s.resize(9); |
| 62 | |
| 63 | s.resize(DB::writeVarUInt(x, s.data()) - s.data()); |
| 64 | |
| 65 | hex << s; |
| 66 | std::cout << std::endl; |
| 67 | |
| 68 | DB::UInt64 y = 0; |
| 69 | |
| 70 | DB::ReadBufferFromString rb(s); |
| 71 | DB::readVarUInt(y, rb); |
| 72 | |
| 73 | std::cerr << "x: "<< x << ", y: "<< y << std::endl; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 |