tempbuf_ut.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "tempbuf.h"
  2. #include <utility>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. class TTempBufTest: public TTestBase {
  5. UNIT_TEST_SUITE(TTempBufTest);
  6. UNIT_TEST(TestCreate);
  7. UNIT_TEST(TestOps);
  8. UNIT_TEST(TestMoveCtor);
  9. UNIT_TEST(TestAppend);
  10. UNIT_TEST(TestProceed);
  11. UNIT_TEST_SUITE_END();
  12. public:
  13. void TestCreate();
  14. void TestOps();
  15. void TestMoveCtor();
  16. void TestProceed();
  17. void TestAppend() {
  18. TTempBuf tmp;
  19. tmp.Append("a", 1);
  20. tmp.Append("bc", 2);
  21. tmp.Append("def", 3);
  22. UNIT_ASSERT_EQUAL(tmp.Filled(), 6);
  23. UNIT_ASSERT_EQUAL(TString(tmp.Data(), tmp.Filled()), "abcdef");
  24. }
  25. };
  26. UNIT_TEST_SUITE_REGISTRATION(TTempBufTest);
  27. void TTempBufTest::TestCreate() {
  28. const size_t num = 1000000;
  29. size_t tmp = 0;
  30. const size_t len = 4096;
  31. for (size_t i = 0; i < num; ++i) {
  32. TTempBuf buf(len);
  33. tmp += (size_t)buf.Data();
  34. }
  35. UNIT_ASSERT(tmp != 0);
  36. }
  37. void TTempBufTest::TestOps() {
  38. TTempBuf tmp(201);
  39. tmp.Proceed(100);
  40. UNIT_ASSERT_EQUAL(tmp.Current() - tmp.Data(), 100);
  41. UNIT_ASSERT(tmp.Left() >= 101);
  42. UNIT_ASSERT(tmp.Size() >= 201);
  43. UNIT_ASSERT_EQUAL(tmp.Filled(), 100);
  44. tmp.Reset();
  45. UNIT_ASSERT_EQUAL(tmp.Current(), tmp.Data());
  46. UNIT_ASSERT(tmp.Left() >= 201);
  47. UNIT_ASSERT(tmp.Size() >= 201);
  48. UNIT_ASSERT_EQUAL(tmp.Filled(), 0);
  49. }
  50. void TTempBufTest::TestMoveCtor() {
  51. TTempBuf src;
  52. UNIT_ASSERT(!src.IsNull());
  53. src.Proceed(10);
  54. TTempBuf dst(std::move(src));
  55. UNIT_ASSERT(src.IsNull());
  56. UNIT_ASSERT(!dst.IsNull());
  57. UNIT_ASSERT_EQUAL(dst.Filled(), 10);
  58. }
  59. void TTempBufTest::TestProceed() {
  60. TTempBuf src;
  61. char* data = src.Proceed(100);
  62. UNIT_ASSERT_EQUAL(data, src.Data());
  63. UNIT_ASSERT_EQUAL(data + 100, src.Current());
  64. UNIT_ASSERT_EQUAL(100, src.Filled());
  65. char* second = src.Proceed(100);
  66. UNIT_ASSERT_EQUAL(data + 100, second);
  67. UNIT_ASSERT_EQUAL(data + 200, src.Current());
  68. UNIT_ASSERT_EQUAL(200, src.Filled());
  69. }