labeled_value_unittests.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <d2/labeled_value.h>
  7. #include <gtest/gtest.h>
  8. using namespace std;
  9. using namespace isc;
  10. using namespace isc::d2;
  11. namespace {
  12. /// @brief Verifies basic construction and accessors for LabeledValue.
  13. TEST(LabeledValue, construction) {
  14. /// Verify that an empty label is not allowed.
  15. ASSERT_THROW(LabeledValue(1, ""), LabeledValueError);
  16. /// Verify that a valid constructor works.
  17. LabeledValuePtr lvp;
  18. ASSERT_NO_THROW(lvp.reset(new LabeledValue(1, "NotBlank")));
  19. ASSERT_TRUE(lvp);
  20. // Verify that the value can be accessed.
  21. EXPECT_EQ(1, lvp->getValue());
  22. // Verify that the label can be accessed.
  23. EXPECT_EQ("NotBlank", lvp->getLabel());
  24. }
  25. /// @brief Verifies the logical operators defined for LabeledValue.
  26. TEST(LabeledValue, operators) {
  27. LabeledValuePtr lvp1;
  28. LabeledValuePtr lvp1Also;
  29. LabeledValuePtr lvp2;
  30. // Create three instances, two of which have the same numeric value.
  31. ASSERT_NO_THROW(lvp1.reset(new LabeledValue(1, "One")));
  32. ASSERT_NO_THROW(lvp1Also.reset(new LabeledValue(1, "OneAlso")));
  33. ASSERT_NO_THROW(lvp2.reset(new LabeledValue(2, "Two")));
  34. // Verify each of the operators.
  35. EXPECT_TRUE(*lvp1 == *lvp1Also);
  36. EXPECT_TRUE(*lvp1 != *lvp2);
  37. EXPECT_TRUE(*lvp1 < *lvp2);
  38. EXPECT_FALSE(*lvp2 < *lvp1);
  39. }
  40. /// @brief Verifies the default constructor for LabeledValueSet.
  41. TEST(LabeledValueSet, construction) {
  42. ASSERT_NO_THROW (LabeledValueSet());
  43. }
  44. /// @brief Verifies the basic operations of a LabeledValueSet.
  45. /// Essentially we verify that we can define a set of valid entries and
  46. /// look them up without issue.
  47. TEST(LabeledValueSet, basicOperation) {
  48. const char* labels[] = {"Zero", "One", "Two", "Three" };
  49. LabeledValueSet lvset;
  50. LabeledValuePtr lvp;
  51. // Verify the we cannot add an empty pointer to the set.
  52. EXPECT_THROW(lvset.add(lvp), LabeledValueError);
  53. // Verify that we can add an entry to the set via pointer.
  54. ASSERT_NO_THROW(lvp.reset(new LabeledValue(0, labels[0])));
  55. EXPECT_NO_THROW(lvset.add(lvp));
  56. // Verify that we cannot add a duplicate entry.
  57. EXPECT_THROW(lvset.add(lvp), LabeledValueError);
  58. // Add the remaining entries using add(int,char*) variant.
  59. for (int i = 1; i < 3; i++) {
  60. EXPECT_NO_THROW(lvset.add(i, labels[i]));
  61. }
  62. // Verify that we can't add a duplicate entry this way either.
  63. EXPECT_THROW ((lvset.add(0, labels[0])), LabeledValueError);
  64. // Verify that we can look up all of the defined entries properly.
  65. for (int i = 1; i < 3; i++) {
  66. EXPECT_TRUE(lvset.isDefined(i));
  67. EXPECT_NO_THROW(lvp = lvset.get(i));
  68. EXPECT_EQ(lvp->getValue(), i);
  69. EXPECT_EQ(lvp->getLabel(), labels[i]);
  70. EXPECT_EQ(lvset.getLabel(i), labels[i]);
  71. }
  72. // Verify behavior for a value that is not defined.
  73. EXPECT_FALSE(lvset.isDefined(4));
  74. EXPECT_NO_THROW(lvp = lvset.get(4));
  75. EXPECT_FALSE(lvp);
  76. EXPECT_EQ(lvset.getLabel(4), LabeledValueSet::UNDEFINED_LABEL);
  77. }
  78. }