optional_value_unittest.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <util/optional_value.h>
  16. #include <gtest/gtest.h>
  17. namespace {
  18. using namespace isc::util;
  19. // This test checks that the constructor sets the values passed as arguments.
  20. TEST(OptionalValueTest, constructor) {
  21. // Do not specify the second parameter. The default should be that
  22. // the value is "unspecified".
  23. OptionalValue<int> value1(10);
  24. EXPECT_EQ(10, value1.get());
  25. EXPECT_FALSE(value1.isSpecified());
  26. // Use the non-default value for second parameter.
  27. OptionalValue<int> value2(2, true);
  28. EXPECT_EQ(2, value2.get());
  29. EXPECT_TRUE(value2.isSpecified());
  30. }
  31. // This test checks that the OptionalValue::set and OptionalValue::specify
  32. // set the values as expected.
  33. TEST(OptionalValueTest, set) {
  34. OptionalValue<int> value(10);
  35. ASSERT_EQ(10, value.get());
  36. ASSERT_FALSE(value.isSpecified());
  37. // Set new value. This should not change the "specified" flag.
  38. value.set(100);
  39. ASSERT_EQ(100, value.get());
  40. ASSERT_FALSE(value.isSpecified());
  41. // Mark value "specified". The value itself should not change.
  42. value.specify(true);
  43. ASSERT_EQ(100, value.get());
  44. ASSERT_TRUE(value.isSpecified());
  45. // Once it is "specified", set the new value. It should remain specified.
  46. value.set(5);
  47. ASSERT_EQ(5, value.get());
  48. ASSERT_TRUE(value.isSpecified());
  49. // Mark it "unspecified". The value should remain the same.
  50. value.specify(false);
  51. ASSERT_EQ(5, value.get());
  52. ASSERT_FALSE(value.isSpecified());
  53. }
  54. // This test checks that the OptionalValue::specify functions may be used
  55. // to set the new value and to mark value specified.
  56. TEST(OptionalValueTest, specifyValue) {
  57. OptionalValue<int> value(10);
  58. ASSERT_EQ(10, value.get());
  59. ASSERT_FALSE(value.isSpecified());
  60. // Set the new value and mark it "specified".
  61. value.specify(123);
  62. ASSERT_EQ(123, value.get());
  63. ASSERT_TRUE(value.isSpecified());
  64. // Specify another value. The value should be still "specified".
  65. value.specify(1000);
  66. ASSERT_EQ(1000, value.get());
  67. ASSERT_TRUE(value.isSpecified());
  68. }
  69. } // end of anonymous namespace