accessors_ut.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "accessors.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/buffer.h>
  4. #include <util/generic/vector.h>
  5. #include <array>
  6. class TAccessorsTest: public TTestBase {
  7. UNIT_TEST_SUITE(TAccessorsTest);
  8. UNIT_TEST(TestAccessors);
  9. UNIT_TEST_SUITE_END();
  10. private:
  11. template <typename T>
  12. void TestRead(const T& t, const char* comm) {
  13. const char* beg = (const char*)NAccessors::Begin(t);
  14. const char* end = (const char*)NAccessors::End(t);
  15. long sz = NAccessors::Size(t) * sizeof(typename TMemoryTraits<T>::TElementType);
  16. UNIT_ASSERT_VALUES_EQUAL_C(end - beg, sz, comm);
  17. }
  18. template <typename T>
  19. void TestWrite(const char* comm) {
  20. typename TMemoryTraits<T>::TElementType val[4] = {'t', 'e', 's', 't'};
  21. T t;
  22. NAccessors::Init(t);
  23. NAccessors::Reserve(t, 6);
  24. size_t sz = NAccessors::Size(t);
  25. UNIT_ASSERT_VALUES_EQUAL_C(0u, sz, comm);
  26. NAccessors::Append(t, 'a');
  27. sz = NAccessors::Size(t);
  28. UNIT_ASSERT_VALUES_EQUAL_C(1u, sz, comm);
  29. NAccessors::Append(t, val, val + 4);
  30. sz = NAccessors::Size(t);
  31. UNIT_ASSERT_VALUES_EQUAL_C(5u, sz, comm);
  32. NAccessors::Clear(t);
  33. sz = NAccessors::Size(t);
  34. UNIT_ASSERT_VALUES_EQUAL_C(0u, sz, comm);
  35. }
  36. void TestAccessors() {
  37. TestRead('a', "char");
  38. TestRead(1, "int");
  39. int t[4] = {0, 1, 2, 3};
  40. TestRead(t, "int[4]");
  41. TStringBuf sbuf = "test";
  42. TestRead(sbuf, "TStringBuf");
  43. TUtf16String wtr;
  44. wtr.resize(10, 1024);
  45. TestRead(wtr, "TUtf16String");
  46. TBuffer buf;
  47. buf.Resize(30);
  48. TestRead(buf, "TBuffer");
  49. TVector<ui64> vec(10, 100);
  50. TestRead(vec, "TVector<ui64>");
  51. TestWrite<TString>("TString");
  52. TestWrite<TVector<char>>("TVector<char>");
  53. TestWrite<TBuffer>("TBuffer");
  54. TestWrite<TVector<ui64>>("TVector<ui64>");
  55. TestWrite<TUtf16String>("TUtf16String");
  56. std::array<TString, 10> sarr;
  57. NAccessors::Init(sarr);
  58. NAccessors::Clear(sarr);
  59. std::array<char, 10> carr;
  60. NAccessors::Init(carr);
  61. NAccessors::Clear(carr);
  62. TestRead(carr, "std::array<char, 10>");
  63. }
  64. };
  65. UNIT_TEST_SUITE_REGISTRATION(TAccessorsTest)