triplet_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2012 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 <stdint.h>
  16. #include <gtest/gtest.h>
  17. #include <dhcp/triplet.h>
  18. #include <exceptions/exceptions.h>
  19. using namespace isc::dhcp;
  20. using namespace isc;
  21. namespace {
  22. // constructor validation
  23. TEST(TripletTest, constructor) {
  24. const uint32_t min = 10;
  25. const uint32_t value = 20;
  26. const uint32_t max = 30;
  27. Triplet<uint32_t> x(min, value, max);
  28. EXPECT_EQ(min, x.getMin());
  29. EXPECT_EQ(value, x.get());
  30. EXPECT_EQ(max, x.getMax());
  31. // requested values below min should return allowed min value
  32. EXPECT_EQ(min, x.get(min - 5));
  33. EXPECT_EQ(min, x.get(min));
  34. // requesting a value from within the range (min < x < max) should
  35. // return the requested value
  36. EXPECT_EQ(17, x.get(17));
  37. EXPECT_EQ(max, x.get(max));
  38. EXPECT_EQ(max, x.get(max + 5));
  39. // this will be boring. It is expected to return 42 no matter what
  40. Triplet<uint32_t> y(42);
  41. EXPECT_EQ(42, y.getMin()); // min, default and max are equal to 42
  42. EXPECT_EQ(42, y.get()); // it returns ...
  43. EXPECT_EQ(42, y.getMax()); // the exact value...
  44. // requested values below or above are ignore
  45. EXPECT_EQ(42, y.get(5)); // all...
  46. EXPECT_EQ(42, y.get(42)); // the...
  47. EXPECT_EQ(42, y.get(80)); // time!
  48. }
  49. // Triplets must be easy to use.
  50. // Simple to/from int conversions must be done on the fly.
  51. TEST(TripletTest, operator) {
  52. uint32_t x = 47;
  53. Triplet<uint32_t> foo(1,2,3);
  54. Triplet<uint32_t> bar(4,5,6);
  55. foo = bar;
  56. EXPECT_EQ(4, foo.getMin());
  57. EXPECT_EQ(5, foo.get());
  58. EXPECT_EQ(6, foo.getMax());
  59. // assignment operator: uint32_t => triplet
  60. Triplet<uint32_t> y(0);
  61. y = x;
  62. EXPECT_EQ(x, y.get());
  63. // let's try the other way around: triplet => uint32_t
  64. uint32_t z = 0;
  65. z = y;
  66. EXPECT_EQ(x, z);
  67. }
  68. // check if specified values are sane
  69. TEST(TripletTest, sanity_check) {
  70. // min is larger than default
  71. EXPECT_THROW(Triplet<uint32_t>(6,5,5), BadValue);
  72. // max is smaller than default
  73. EXPECT_THROW(Triplet<uint32_t>(5,5,4), BadValue);
  74. }
  75. }; // end of anonymous namespace