optional_tests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2018 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #include <opencv2/gapi/util/optional.hpp>
  8. #include <opencv2/gapi/util/compiler_hints.hpp> //suppress_unused_warning
  9. namespace opencv_test
  10. {
  11. TEST(Optional, EmptyCtor)
  12. {
  13. util::optional<int> o;
  14. EXPECT_FALSE(o.has_value());
  15. EXPECT_FALSE(static_cast<bool>(o));
  16. }
  17. TEST(Optional, ValueCTor)
  18. {
  19. util::optional<int> o(42);
  20. EXPECT_TRUE(o.has_value());
  21. EXPECT_TRUE(static_cast<bool>(o));
  22. }
  23. TEST(Optional, MoveCtr)
  24. {
  25. util::optional<std::string> os1(std::string("text"));
  26. EXPECT_TRUE(os1.has_value());
  27. util::optional<std::string> os2(std::move(os1));
  28. EXPECT_FALSE(os1.has_value());
  29. EXPECT_TRUE(os2.has_value());
  30. EXPECT_EQ("text", os2.value());
  31. }
  32. TEST(Optional, EmptyThrows)
  33. {
  34. struct foo { int bar; };
  35. util::optional<foo> om;
  36. const util::optional<foo> oc;
  37. int dummy;
  38. EXPECT_THROW(dummy = om->bar, util::bad_optional_access);
  39. EXPECT_THROW(dummy = oc->bar, util::bad_optional_access);
  40. cv::util::suppress_unused_warning(dummy);
  41. EXPECT_THROW(*om, util::bad_optional_access);
  42. EXPECT_THROW(*oc, util::bad_optional_access);
  43. EXPECT_THROW(om.value(), util::bad_optional_access);
  44. EXPECT_THROW(oc.value(), util::bad_optional_access);
  45. }
  46. TEST(Optional, ValueNoThrow)
  47. {
  48. struct foo { int bar; };
  49. util::optional<foo> om(foo{42});
  50. const util::optional<foo> oc(foo{42});
  51. int dummy;
  52. EXPECT_NO_THROW(dummy = om->bar);
  53. EXPECT_NO_THROW(dummy = oc->bar);
  54. cv::util::suppress_unused_warning(dummy);
  55. EXPECT_NO_THROW(*om);
  56. EXPECT_NO_THROW(*oc);
  57. EXPECT_NO_THROW(om.value());
  58. EXPECT_NO_THROW(oc.value());
  59. }
  60. TEST(Optional, Value)
  61. {
  62. util::optional<int> oi(42);
  63. struct foo { int bar; };
  64. util::optional<foo> of(foo{42});
  65. EXPECT_EQ(42, oi.value());
  66. EXPECT_EQ(42, *oi);
  67. EXPECT_EQ(42, of.value().bar);
  68. EXPECT_EQ(42, of->bar);
  69. }
  70. TEST(Optional, Mutable)
  71. {
  72. util::optional<int> oi(42);
  73. *oi = 43;
  74. EXPECT_EQ(43, *oi);
  75. struct foo { int bar; int baz; };
  76. util::optional<foo> of(foo{11,22});
  77. (*of).bar = 42;
  78. EXPECT_EQ(42, of->bar);
  79. EXPECT_EQ(22, of->baz);
  80. of->baz = 33;
  81. EXPECT_EQ(42, of->bar);
  82. EXPECT_EQ(33, of->baz);
  83. }
  84. TEST(Optional, MoveAssign)
  85. {
  86. util::optional<int> e, i(42);
  87. EXPECT_FALSE(e.has_value());
  88. EXPECT_TRUE(i.has_value());
  89. EXPECT_EQ(42, *i);
  90. e = std::move(i);
  91. EXPECT_TRUE(e.has_value());
  92. EXPECT_FALSE(i.has_value());
  93. EXPECT_EQ(42, *e);
  94. }
  95. TEST(Optional, CopyAssign)
  96. {
  97. util::optional<int> e;
  98. const util::optional<int> i(42);
  99. EXPECT_FALSE(e.has_value());
  100. EXPECT_TRUE(i.has_value());
  101. EXPECT_EQ(42, *i);
  102. e = i;
  103. EXPECT_TRUE(e.has_value());
  104. EXPECT_TRUE(i.has_value());
  105. EXPECT_EQ(42, *e);
  106. EXPECT_EQ(42, *i);
  107. }
  108. TEST(Optional, ValueOr)
  109. {
  110. util::optional<int> e;
  111. EXPECT_FALSE(e.has_value());
  112. EXPECT_EQ(42, e.value_or(42));
  113. EXPECT_EQ(42, e.value_or(42.1));
  114. }
  115. TEST(Optional, Swap)
  116. {
  117. util::optional<int> e, i(42);
  118. EXPECT_FALSE(e.has_value());
  119. EXPECT_TRUE(i.has_value());
  120. EXPECT_EQ(42, *i);
  121. e.swap(i);
  122. EXPECT_TRUE(e.has_value());
  123. EXPECT_FALSE(i.has_value());
  124. EXPECT_EQ(42, *e);
  125. }
  126. TEST(Optional, Reset)
  127. {
  128. util::optional<int> i(42);
  129. EXPECT_TRUE(i.has_value());
  130. i.reset();
  131. EXPECT_FALSE(i.has_value());
  132. }
  133. TEST(Optional, MakeOptional)
  134. {
  135. std::string s("text");
  136. auto os = util::make_optional(s);
  137. EXPECT_TRUE(os.has_value());
  138. EXPECT_EQ(s, os.value());
  139. }
  140. } // namespace opencv_test