]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3587] Extended OptionalValue with equality and assignment operators.
authorMarcin Siodelski <marcin@isc.org>
Thu, 16 Oct 2014 14:01:55 +0000 (16:01 +0200)
committerMarcin Siodelski <marcin@isc.org>
Tue, 21 Oct 2014 09:08:43 +0000 (11:08 +0200)
src/lib/util/optional_value.h
src/lib/util/tests/optional_value_unittest.cc

index 3b964f4b94bffac46f12191a38bd2f032811287c..0e47f891bba81a5818ca81cefef31ae3dc4a2fec 100644 (file)
@@ -91,6 +91,28 @@ public:
         return (specified_);
     }
 
+    /// @brief Specifies a new value value and marks it "specified".
+    ///
+    /// @param value New actual value.
+    void operator=(const T& value) {
+        specify(value);
+    }
+
+    /// @brief Equality operator.
+    ///
+    /// @param value Actual value to compare to.
+    bool operator==(const T& value) {
+        return (value_ == value);
+    }
+
+    /// @brief Inequality operator.
+    ///
+    /// @param value Actual value to compare to.
+    bool operator!=(const T& value) {
+        return (value_ != value);
+    }
+
+
 private:
     T value_;         ///< Encapsulated value.
     bool specified_;  ///< Flag which indicates if the value is specified.
index a5f7438085e101939116c7e15f9bde4b97261217..82558eeb3347e17597a3730cb67d2a826889ba3e 100644 (file)
@@ -80,4 +80,39 @@ TEST(OptionalValueTest, specifyValue) {
     ASSERT_TRUE(value.isSpecified());
 }
 
+// This test checks if the assignment operator assignining an actual
+// value to the optional value works as expected.
+TEST(OptionalValueTest, assignValue) {
+    OptionalValue<int> value(10);
+    ASSERT_EQ(10, value.get());
+    ASSERT_FALSE(value.isSpecified());
+
+    // Set the new value and mark it "specified".
+    value = 111;
+    ASSERT_EQ(111, value.get());
+    ASSERT_TRUE(value.isSpecified());
+
+    // Specify another value. The value should be still "specified".
+    value = 1000;
+    ASSERT_EQ(1000, value.get());
+    ASSERT_TRUE(value.isSpecified());
+}
+
+// This test checks if the equality and inequality operators work
+// correctly for the optional value.
+TEST(OptionalValueTest, equalityOperators) {
+    OptionalValue<int> value(10);
+    ASSERT_EQ(10, value.get());
+    ASSERT_FALSE(value.isSpecified());
+
+    EXPECT_TRUE(value == 10);
+    EXPECT_FALSE(value != 10);
+
+    value = 123;
+    EXPECT_TRUE(value == 123);
+    EXPECT_FALSE(value != 123);
+}
+
+
+
 } // end of anonymous namespace