Browse Source

[3336] It is now possible to mark triplet unspecified.

Marcin Siodelski 11 years ago
parent
commit
0a48d176a2
2 changed files with 50 additions and 2 deletions
  1. 26 0
      src/lib/dhcpsrv/tests/triplet_unittest.cc
  2. 24 2
      src/lib/dhcpsrv/triplet.h

+ 26 - 0
src/lib/dhcpsrv/tests/triplet_unittest.cc

@@ -38,6 +38,7 @@ TEST(TripletTest, constructor) {
     EXPECT_EQ(min, x.getMin());
     EXPECT_EQ(value, x.get());
     EXPECT_EQ(max, x.getMax());
+    EXPECT_FALSE(x.unspecified());
 
     // requested values below min should return allowed min value
     EXPECT_EQ(min, x.get(min - 5));
@@ -58,6 +59,7 @@ TEST(TripletTest, constructor) {
     EXPECT_EQ(42, y.getMin()); // min, default and max are equal to 42
     EXPECT_EQ(42, y.get());    // it returns ...
     EXPECT_EQ(42, y.getMax()); // the exact value...
+    EXPECT_FALSE(x.unspecified());
 
     // requested values below or above are ignore
     EXPECT_EQ(42, y.get(5));   // all...
@@ -65,6 +67,29 @@ TEST(TripletTest, constructor) {
     EXPECT_EQ(42, y.get(80));  // time!
 }
 
+TEST(TripletTest, unspecified) {
+    Triplet<uint32_t> x;
+    // When using the constructor without parameters, the triplet
+    // value is unspecified.
+    EXPECT_EQ(0, x.getMin());
+    EXPECT_EQ(0, x.get());
+    EXPECT_EQ(0, x.getMax());
+    EXPECT_TRUE(x.unspecified());
+
+    // For the triplet which has unspecified value we can call accessors
+    // without an exception.
+    uint32_t exp_unspec = 0;
+    EXPECT_EQ(exp_unspec, x);
+
+    x = 72;
+    // Check if the new value has been assigned.
+    EXPECT_EQ(72, x.getMin());
+    EXPECT_EQ(72, x.get());
+    EXPECT_EQ(72, x.getMax());
+    // Triplet is now specified.
+    EXPECT_FALSE(x.unspecified());
+}
+
 // Triplets must be easy to use.
 // Simple to/from int conversions must be done on the fly.
 TEST(TripletTest, operator) {
@@ -79,6 +104,7 @@ TEST(TripletTest, operator) {
     EXPECT_EQ(4, foo.getMin());
     EXPECT_EQ(5, foo.get());
     EXPECT_EQ(6, foo.getMax());
+    EXPECT_FALSE(foo.unspecified());
 
     // assignment operator: uint32_t => triplet
     Triplet<uint32_t> y(0);

+ 24 - 2
src/lib/dhcpsrv/triplet.h

@@ -54,6 +54,8 @@ public:
         min_ = other;
         default_ = other;
         max_ = other;
+        // The value is now specified because we just assigned one.
+        unspecified_ = false;
         return (*this);
     }
 
@@ -65,6 +67,14 @@ public:
         return (default_);
     }
 
+    /// @brief Constructor without parameters.
+    ///
+    /// Marks value in @c Triplet unspecified.
+    Triplet()
+        : min_(0), default_(0), max_(0),
+          unspecified_(true) {
+    }
+
     /// @brief Sets a fixed value.
     ///
     /// This constructor assigns a fixed (i.e. no range, just a single value)
@@ -72,14 +82,16 @@ public:
     ///
     /// @param value A number to be assigned as min, max and default value.
     Triplet(T value)
-        :min_(value), default_(value), max_(value) {
+        : min_(value), default_(value), max_(value),
+          unspecified_(false) {
     }
 
     /// @brief Sets the default value and thresholds
     ///
     /// @throw BadValue if min <= def <= max rule is violated
     Triplet(T min, T def, T max)
-        :min_(min), default_(def), max_(max) {
+        : min_(min), default_(def), max_(max),
+          unspecified_(false) {
         if ( (min_ > def) || (def > max_) ) {
             isc_throw(BadValue, "Invalid triplet values.");
         }
@@ -118,6 +130,13 @@ public:
     /// @brief Returns a maximum allowed value
     T getMax() const { return (max_); }
 
+    /// @brief Check if the value has been specified.
+    ///
+    /// @return true if the value hasn't been specified, or false otherwise.
+    bool unspecified() const {
+        return (unspecified_);
+    }
+
 private:
 
     /// @brief the minimum value
@@ -128,6 +147,9 @@ private:
 
     /// @brief the maximum value
     T max_;
+
+    /// @brief Indicates whether the value is unspecified.
+    bool unspecified_;
 };