empty_value_test.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2018 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/core/empty_value.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. struct empty {
  10. int value() const {
  11. return 1;
  12. }
  13. int value() {
  14. return 2;
  15. }
  16. };
  17. class type {
  18. public:
  19. explicit type(int count)
  20. : value_(count) { }
  21. int value() const {
  22. return value_ + 1;
  23. }
  24. int value() {
  25. return value_ + 2;
  26. }
  27. private:
  28. int value_;
  29. };
  30. void test_int()
  31. {
  32. const boost::empty_value<int> v1(boost::empty_init_t(), 7);
  33. BOOST_TEST(v1.get() == 7);
  34. boost::empty_value<int> v2 = boost::empty_init_t();
  35. BOOST_TEST(v2.get() == 0);
  36. v2 = v1;
  37. BOOST_TEST(v2.get() == 7);
  38. v2.get() = 8;
  39. BOOST_TEST(v2.get() == 8);
  40. }
  41. void test_empty()
  42. {
  43. const boost::empty_value<empty> v1 = boost::empty_init_t();
  44. BOOST_TEST(v1.get().value() == 1);
  45. boost::empty_value<empty> v2;
  46. BOOST_TEST(v2.get().value() == 2);
  47. }
  48. void test_type()
  49. {
  50. const boost::empty_value<type> v1(boost::empty_init_t(), 2);
  51. BOOST_TEST(v1.get().value() == 3);
  52. boost::empty_value<type> v2(boost::empty_init_t(), 3);
  53. BOOST_TEST(v2.get().value() == 5);
  54. v2 = v1;
  55. BOOST_TEST(v2.get().value() == 4);
  56. v2.get() = type(4);
  57. BOOST_TEST(v2.get().value() == 6);
  58. }
  59. int main()
  60. {
  61. test_int();
  62. test_empty();
  63. test_type();
  64. return boost::report_errors();
  65. }