function_ref_test.cc 649 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <string>
  5. #include "opentelemetry/nostd/function_ref.h"
  6. using namespace opentelemetry::nostd;
  7. int Call(function_ref<int()> f)
  8. {
  9. return f();
  10. }
  11. int Return3()
  12. {
  13. return 3;
  14. }
  15. TEST(FunctionRefTest, Call)
  16. {
  17. int x = 9;
  18. auto f = [&] { return x; };
  19. EXPECT_EQ(Call(f), 9);
  20. EXPECT_EQ(Call(Return3), 3);
  21. }
  22. TEST(FunctionRefTest, BoolConversion)
  23. {
  24. auto f = [] { return 0; };
  25. function_ref<int()> fref1{nullptr};
  26. function_ref<int()> fref2{f};
  27. EXPECT_TRUE(!static_cast<bool>(fref1));
  28. EXPECT_TRUE(static_cast<bool>(fref2));
  29. }