test_reference.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "test_precomp.hpp"
  2. #include "../src/util/util.hpp"
  3. namespace opencv_test { namespace {
  4. /**
  5. * Makes sure that
  6. * - creating, reassigning and comparing `cvv::util::Reference<int>`s (from /src/util/util.hpp) works,
  7. * as well as the
  8. * - `makeRef()` and `getPtr()` functions; that
  9. * - `Reference<const int>`s can be created with `makeRef(const int)`, `{const int}` initializer list
  10. * and via `Reference<const int>{int}` and, finally, that
  11. * - `cvv::util::Reference`s of super classes can be constructed with derived classes and
  12. * - `Reference`s to base classes can be `castTo()` `References` to the derived classes, but not the other way.
  13. */
  14. using cvv::util::Reference;
  15. using cvv::util::makeRef;
  16. TEST(ReferenceTest, Construction)
  17. {
  18. int i = 3;
  19. Reference<int> ref1{ i };
  20. EXPECT_EQ(*ref1, 3);
  21. }
  22. TEST(ReferenceTest, Reassignment)
  23. {
  24. int i1 = 3;
  25. int i2 = 4;
  26. Reference<int> ref{ i1 };
  27. EXPECT_EQ(ref.getPtr(), &i1);
  28. EXPECT_EQ(*ref, 3);
  29. ref = Reference<int>{ i2 };
  30. EXPECT_EQ(*ref, 4);
  31. EXPECT_EQ(ref.getPtr(), &i2);
  32. }
  33. TEST(ReferenceTest, Comparing)
  34. {
  35. int i1 = 1, i2 = 1;
  36. auto ref1 = makeRef(i1);
  37. auto ref2 = makeRef(i1);
  38. auto ref3 = makeRef(i2);
  39. EXPECT_EQ(ref1 == ref2, true);
  40. EXPECT_EQ(ref1 != ref2, false);
  41. EXPECT_EQ(ref1 == ref3, false);
  42. EXPECT_EQ(ref1 != ref3, true);
  43. }
  44. TEST(ReferenceTest, MakeRef)
  45. {
  46. int i1 = 3, i2 = 4;
  47. auto ref = makeRef(i1);
  48. EXPECT_EQ(*ref, 3);
  49. EXPECT_EQ(ref.getPtr(), &i1);
  50. ref = makeRef(i2);
  51. EXPECT_EQ(*ref, 4);
  52. EXPECT_EQ(ref.getPtr(), &i2);
  53. }
  54. TEST(ReferenceTest, ConstRefs)
  55. {
  56. const int i = 3;
  57. auto ref1 = makeRef(i);
  58. Reference<const int> ref2{ i };
  59. EXPECT_EQ(ref1, ref2);
  60. }
  61. TEST(ReferenceTest, ConstRefsFromMutable)
  62. {
  63. int i;
  64. Reference<const int> ref{ i };
  65. EXPECT_EQ(ref.getPtr(), &i);
  66. }
  67. struct Base
  68. {
  69. virtual ~Base() = default;
  70. };
  71. struct Derived : Base
  72. {
  73. };
  74. struct Derived2 : Base
  75. {
  76. };
  77. TEST(ReferenceTest, LiberalConstruction)
  78. {
  79. Derived var;
  80. auto derivedRef = makeRef(var);
  81. Reference<Base> baseRef{ derivedRef };
  82. EXPECT_EQ(&var, baseRef.getPtr());
  83. }
  84. TEST(ReferenceTest, castTo)
  85. {
  86. Derived var;
  87. Reference<Base> baseRef{ var };
  88. auto derivedRef = baseRef.castTo<Derived>();
  89. EXPECT_EQ(&var, derivedRef.getPtr());
  90. EXPECT_THROW(baseRef.castTo<Derived2>(), std::bad_cast);
  91. // should result in a compiler-error:
  92. // EXPECT_THROW(baseRef.castTo<std::vector<int>>(), std::bad_cast);
  93. }
  94. }} // namespace