lazy_constructor.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/util/lazy_constructor.hpp>
  6. TEST(LazyConstructor, Basic)
  7. {
  8. using ::uavcan::LazyConstructor;
  9. LazyConstructor<std::string> a;
  10. LazyConstructor<std::string> b;
  11. ASSERT_FALSE(a);
  12. ASSERT_FALSE(b.isConstructed());
  13. /*
  14. * Construction
  15. */
  16. a.destroy(); // no-op
  17. a.construct();
  18. b.construct<const char*>("Hello world");
  19. ASSERT_TRUE(a);
  20. ASSERT_TRUE(b.isConstructed());
  21. ASSERT_NE(*a, *b);
  22. ASSERT_STRNE(a->c_str(), b->c_str());
  23. ASSERT_EQ(*a, "");
  24. ASSERT_EQ(*b, "Hello world");
  25. /*
  26. * Copying
  27. */
  28. a = b; // Assignment operator performs destruction and immediate copy construction
  29. ASSERT_EQ(*a, *b);
  30. ASSERT_EQ(*a, "Hello world");
  31. LazyConstructor<std::string> c(a); // Copy constructor call is forwarded to std::string
  32. ASSERT_EQ(*c, *a);
  33. *a = "123";
  34. ASSERT_NE(*c, *a);
  35. ASSERT_EQ(*c, *b);
  36. *c = "456";
  37. ASSERT_NE(*a, *c);
  38. ASSERT_NE(*b, *a);
  39. ASSERT_NE(*c, *b);
  40. /*
  41. * Destruction
  42. */
  43. ASSERT_TRUE(c);
  44. c.destroy();
  45. ASSERT_FALSE(c);
  46. }