example_tc_pack.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_pack.h"
  17. #include <iostream>
  18. #include <cassert>
  19. using namespace tars;
  20. int main(int argc, char *argv[])
  21. {
  22. try
  23. {
  24. bool b = true;
  25. char c = 'a';
  26. short si = 3;
  27. int ii = 4;
  28. char cn[] = "abc";
  29. string sn = "def";
  30. TC_PackIn pi;
  31. pi << b << c << si << ii << cn << sn;
  32. string s = pi.topacket();
  33. TC_PackOut po(s.c_str(), s.length());
  34. po >> b;
  35. assert(b == true);
  36. cout << "bool OK" << endl;
  37. po >> c;
  38. assert(c == 'a');
  39. cout << "char OK" << endl;
  40. po >> si;
  41. assert(si == 3);
  42. cout << "short OK" << endl;
  43. po >> ii;
  44. assert(ii == 4);
  45. cout << "int OK" << endl;
  46. po >> cn;
  47. assert(cn == string("abc"));
  48. cout << "char[] OK" << endl;
  49. po >> sn;
  50. assert(sn == "def");
  51. cout << "string OK" << endl;
  52. {
  53. pi.clear();
  54. pi << b << c;
  55. pi.insert(1) << cn;
  56. s = pi.topacket();
  57. po.init(s.c_str(), s.length());
  58. po >> b;
  59. assert(b == true);
  60. cout << "bool OK" << endl;
  61. po >> cn;
  62. assert(cn == string("abc"));
  63. cout << "char[] OK" << endl;
  64. po >> c;
  65. assert(c == 'a');
  66. cout << "char OK" << endl;
  67. }
  68. {
  69. pi.clear();
  70. pi << b << c;
  71. pi.replace(1) << 'b';
  72. s = pi.topacket();
  73. po.init(s.c_str(), s.length());
  74. po >> b;
  75. assert(b == true);
  76. cout << "bool OK" << endl;
  77. po >> c;
  78. assert(c == 'b');
  79. cout << "char OK" << endl;
  80. }
  81. }
  82. catch(exception &ex)
  83. {
  84. cout << ex.what() << endl;
  85. }
  86. return 0;
  87. }