function_view_ut.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/memory/function_view.h>
  3. #include <util/generic/string.h>
  4. #include <util/string/cast.h>
  5. namespace NYT {
  6. namespace {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. struct TNoCopy
  9. {
  10. int Value = 42;
  11. TNoCopy() = default;
  12. TNoCopy(const TNoCopy&) = delete;
  13. TNoCopy(TNoCopy&&)
  14. { }
  15. };
  16. int Foo()
  17. {
  18. return 42;
  19. }
  20. int& Bar()
  21. {
  22. static int bar = 0;
  23. return bar;
  24. }
  25. const TNoCopy& ImmutBar()
  26. {
  27. static TNoCopy bar = {};
  28. return bar;
  29. }
  30. TString Baz(const int& x)
  31. {
  32. return ToString(x);
  33. }
  34. void NoExFoo() noexcept
  35. { }
  36. struct TCallable
  37. {
  38. int InvocationCount = 0;
  39. mutable int ConstInvocationCount = 0;
  40. void operator()() &
  41. {
  42. ++InvocationCount;
  43. }
  44. void operator()() const &
  45. {
  46. ++ConstInvocationCount;
  47. }
  48. };
  49. ////////////////////////////////////////////////////////////////////////////////
  50. TEST(TFunctionViewTest, JustWorks)
  51. {
  52. auto stackLambda = [] (int val) {
  53. return val + 1;
  54. };
  55. {
  56. TFunctionView<int(int)> view(stackLambda);
  57. EXPECT_EQ(view(42), 43);
  58. }
  59. }
  60. TEST(TFunctionViewTest, FreeFunction)
  61. {
  62. TFunctionView<int()> view(Foo);
  63. EXPECT_EQ(view(), 42);
  64. }
  65. TEST(TFunctionViewTest, RefReturn)
  66. {
  67. TFunctionView<int&()> view(Bar);
  68. ++view();
  69. EXPECT_EQ(view(), 1);
  70. TFunctionView<const TNoCopy&()> immut_view(ImmutBar);
  71. EXPECT_EQ(immut_view().Value, 42);
  72. }
  73. TEST(TFunctionViewTest, RefArgument)
  74. {
  75. TFunctionView<TString(const int&)> view(Baz);
  76. EXPECT_EQ(view(77), TString("77"));
  77. }
  78. TEST(TFunctionViewTest, NoExcept)
  79. {
  80. TFunctionView<void() noexcept> view(NoExFoo);
  81. static_assert(std::is_nothrow_invocable_r_v<void, decltype(view)>);
  82. view();
  83. }
  84. TEST(TFunctionViewTest, CVOverloads)
  85. {
  86. TCallable callable;
  87. TFunctionView<void()> view(callable);
  88. // NB: & overload overshadows every other overload.
  89. // const auto& viewRef = view;
  90. // viewRef();
  91. view();
  92. EXPECT_EQ(callable.InvocationCount, 1);
  93. EXPECT_EQ(callable.ConstInvocationCount, 0);
  94. }
  95. TEST(TFunctionViewTest, CopyView)
  96. {
  97. int counter = 0;
  98. auto lambda = [&counter] {
  99. ++counter;
  100. };
  101. TFunctionView<void()> view1(lambda);
  102. TFunctionView<void()> view2 = view1;
  103. view1();
  104. EXPECT_EQ(counter, 1);
  105. view2();
  106. EXPECT_EQ(counter, 2);
  107. view1();
  108. EXPECT_EQ(counter, 3);
  109. }
  110. TEST(TFunctionViewTest, AssignView)
  111. {
  112. int counter = 0;
  113. auto lambda = [&counter] {
  114. ++counter;
  115. };
  116. TFunctionView<void()> view(lambda);
  117. view();
  118. EXPECT_EQ(counter, 1);
  119. {
  120. auto innerCounter = 0;
  121. auto lambda = [&innerCounter] {
  122. ++innerCounter;
  123. };
  124. view = lambda;
  125. view();
  126. EXPECT_EQ(counter, 1);
  127. EXPECT_EQ(innerCounter, 1);
  128. }
  129. // NB: Even though object is dead view will remain "valid".
  130. // Be careful with lifetimes!
  131. EXPECT_TRUE(view.IsValid());
  132. }
  133. TEST(TFunctionViewTest, ReleaseSemantics)
  134. {
  135. int counter = 0;
  136. auto lambda = [&counter] {
  137. ++counter;
  138. };
  139. TFunctionView<void()> view1(lambda);
  140. view1();
  141. EXPECT_EQ(counter, 1);
  142. TFunctionView view2 = view1.Release();
  143. EXPECT_FALSE(view1.IsValid());
  144. EXPECT_TRUE(view2.IsValid());
  145. view2();
  146. EXPECT_EQ(counter, 2);
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////
  149. } // namespace
  150. } // namespace NYT