statistics_util.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "statistics_util.h"
  15. #include <gtest/gtest.h>
  16. #include <string>
  17. #include <map>
  18. namespace {
  19. void
  20. flatten(std::map<std::string, int>& flat_map, const std::string& prefix,
  21. const isc::data::ConstElementPtr map_element)
  22. {
  23. std::map<std::string, isc::data::ConstElementPtr> map =
  24. map_element->mapValue();
  25. for (std::map<std::string, isc::data::ConstElementPtr>::const_iterator
  26. i = map.begin(), e = map.end();
  27. i != e;
  28. ++i)
  29. {
  30. switch (i->second->getType()) {
  31. case isc::data::Element::map:
  32. flatten(flat_map, i->first + ".", i->second);
  33. break;
  34. case isc::data::Element::integer:
  35. flat_map[prefix + i->first] = i->second->intValue();
  36. break;
  37. default:
  38. FAIL() << "Element Parse Error";
  39. }
  40. }
  41. }
  42. }
  43. namespace isc {
  44. namespace auth {
  45. namespace unittest {
  46. void
  47. checkStatisticsCounters(const isc::data::ConstElementPtr counters,
  48. const std::map<std::string, int>& expect)
  49. {
  50. std::map<std::string, int> stats_map;
  51. flatten(stats_map, "", counters);
  52. for (std::map<std::string, int>::const_iterator
  53. i = stats_map.begin(), e = stats_map.end();
  54. i != e;
  55. ++i)
  56. {
  57. const int value =
  58. expect.find(i->first) == expect.end() ?
  59. 0 : expect.find(i->first)->second;
  60. EXPECT_EQ(value, i->second) << "Expected counter "
  61. << i->first << " = " << value << ", actual: "
  62. << i->second;
  63. }
  64. }
  65. } // end of unittest
  66. } // end of auth
  67. } // end of isc