From b2a10d478d88e48ce898298eb9f7577bafc025f3 Mon Sep 17 00:00:00 2001 From: Rintaro Kuroiwa Date: Wed, 25 Jun 2014 18:40:40 -0700 Subject: [PATCH] Custom map compare function for better debug logging When running mpd unit tests, add --logging -v=2 to see verbose logs and -v=3 for more verbose logs Change-Id: I5484f3fd9be4c9e7527d86fa71205e8f025d102a --- mpd/test/xml_compare.cc | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/mpd/test/xml_compare.cc b/mpd/test/xml_compare.cc index fea32cb1d5..fbfd67596d 100644 --- a/mpd/test/xml_compare.cc +++ b/mpd/test/xml_compare.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include "base/logging.h" #include "mpd/base/xml/scoped_xml_ptr.h" @@ -20,6 +21,8 @@ xml::ScopedXmlPtr::type GetDocFromString(const std::string& xml_str) { // Make a map from attributes of the node. std::map GetMapOfAttributes(xmlNodePtr node) { + DVLOG(2) << "Getting attributes for node " + << reinterpret_cast(node->name); std::map attribute_map; for (xmlAttr* attribute = node->properties; attribute && attribute->name && attribute->children; @@ -29,18 +32,33 @@ std::map GetMapOfAttributes(xmlNodePtr node) { xmlNodeListGetString(node->doc, attribute->children, 1)); attribute_map[name] = reinterpret_cast(value.get()); - DLOG(INFO) << "In node " << reinterpret_cast(node->name) - << " found attribute " << name << " with value " - << reinterpret_cast(value.get()); + DVLOG(3) << "In node " << reinterpret_cast(node->name) + << " found attribute " << name << " with value " + << reinterpret_cast(value.get()); } return attribute_map; } +bool MapCompareFunc(std::pair a, + std::pair b) { + if (a.first != b.first) { + DLOG(INFO) << "Attribute mismatch " << a.first << " and " << b.first; + return false; + } + + if (a.second != b.second) { + DLOG(INFO) << "Value mismatch for " << a.first << std::endl << "Values are " + << a.second << " and " << b.second; + return false; + } + return true; +} + bool MapsEqual(const std::map& map1, const std::map& map2) { return map1.size() == map2.size() && - std::equal(map1.begin(), map1.end(), map2.begin()); + std::equal(map1.begin(), map1.end(), map2.begin(), MapCompareFunc); } // Return true if the nodes have the same attributes. @@ -50,8 +68,8 @@ bool CompareAttributes(xmlNodePtr node1, xmlNodePtr node2) { // Return true if the name of the nodes match. bool CompareNames(xmlNodePtr node1, xmlNodePtr node2) { - DLOG(INFO) << "Comparing " << reinterpret_cast(node1->name) - << " and " << reinterpret_cast(node2->name); + DVLOG(2) << "Comparing " << reinterpret_cast(node1->name) + << " and " << reinterpret_cast(node2->name); return xmlStrcmp(node1->name, node2->name) == 0; }