holder_vector_ut.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "holder_vector.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(THolderVectorTest) {
  4. Y_UNIT_TEST(TestCreateEmpty) {
  5. THolderVector<int> ints;
  6. UNIT_ASSERT_EQUAL(ints.Size(), 0);
  7. UNIT_ASSERT(!ints);
  8. }
  9. Y_UNIT_TEST(TestCreateNonEmpty) {
  10. THolderVector<int> ints(5);
  11. UNIT_ASSERT_EQUAL(ints.Size(), 5);
  12. UNIT_ASSERT(ints);
  13. for (size_t i = 0; i < ints.Size(); ++i) {
  14. UNIT_ASSERT_EQUAL(ints[i], (int*)nullptr);
  15. }
  16. }
  17. Y_UNIT_TEST(TestResetValue) {
  18. THolderVector<int> ints;
  19. ints.PushBack(new int(0));
  20. ints.PushBack(new int(1));
  21. UNIT_ASSERT_VALUES_EQUAL(*ints[0], 0);
  22. UNIT_ASSERT_VALUES_EQUAL(*ints[1], 1);
  23. ints.Reset(0, MakeHolder<int>(2));
  24. ints.Reset(1, MakeHolder<int>(3));
  25. UNIT_ASSERT_VALUES_EQUAL(*ints[0], 2);
  26. UNIT_ASSERT_VALUES_EQUAL(*ints[1], 3);
  27. }
  28. Y_UNIT_TEST(TestResetNoValue) {
  29. THolderVector<int> ints;
  30. ints.Resize(1);
  31. UNIT_ASSERT_EQUAL(ints[0], (int*)nullptr);
  32. ints.Reset(0, MakeHolder<int>(1));
  33. UNIT_ASSERT_UNEQUAL(ints[0], (int*)nullptr);
  34. UNIT_ASSERT_VALUES_EQUAL(*ints[0], 1);
  35. }
  36. Y_UNIT_TEST(TestResetSmartPtr) {
  37. THolderVector<int> ints;
  38. ints.Resize(2);
  39. THolder<int> holder(new int(1));
  40. ints.Reset(0, std::move(holder));
  41. UNIT_ASSERT_VALUES_EQUAL(*ints[0], 1);
  42. UNIT_ASSERT(!holder);
  43. }
  44. Y_UNIT_TEST(TestSwap) {
  45. THolderVector<int> v1;
  46. v1.PushBack(new int(1));
  47. THolderVector<int> v2;
  48. v1.Swap(v2);
  49. UNIT_ASSERT(v1.empty() && v2.size() == 1 && *v2.front() == 1);
  50. }
  51. Y_UNIT_TEST(TestUniquePtr) {
  52. THolderVector<TString> v;
  53. std::unique_ptr<TString> str(new TString("hello"));
  54. v.PushBack(std::move(str));
  55. UNIT_ASSERT(v.Size() == 1);
  56. UNIT_ASSERT(str.get() == nullptr);
  57. }
  58. } // Y_UNIT_TEST_SUITE(THolderVectorTest)