// $Id$ // // \file TestUtility.cpp // // Copyright (C) 2012, ARM Research Labs, LLC. // See www.armresearch.com for the copyright terms. // // This is the unit test for the class Utility. // #include #include #include #include #include #include #include "Utility.hpp" /// Output error. #define Error(msg) \ { \ std::cerr << "In file " << __FILE__ << ":" << __LINE__ << ": "; \ std::cerr << msg; \ } /// Exit with error. #define ErrorExit(msg) \ { \ Error(msg) \ std::exit(-1); \ } bool TestReplaceXmlAttribute() { std::string Content; std::string ExpectedContent; std::string ElementName = "TestElement"; std::string AttributeName = "TestAttribute"; std::string AttributeNewValue = "NewValue"; // Test with valid input. std::string OriginalContent; Content = "\n" "\n" "\n" "\n" "\n"; OriginalContent = Content; ExpectedContent = "\n" "\n" "\n" "\n" "\n"; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); } catch (std::exception &e) { std::string Temp; Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: "; Temp += e.what(); Temp += "\n\nOriginal Content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } if (Content != ExpectedContent) { std::string Temp; Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n"; Temp += Content; Temp += "\n\nExpected:\n\n"; Temp += ExpectedContent + "\n"; Error(Temp); return false; } Content = "\n" "\n" "\n" "" "\n"; OriginalContent = Content; ExpectedContent = "\n" "\n" "\n" "" "\n"; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); } catch (std::exception &e) { std::string Temp; Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: "; Temp += e.what(); Temp += "\n\nOriginal Content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } if (Content != ExpectedContent) { std::string Temp; Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n"; Temp += Content; Temp += "\n\nExpected:\n\n"; Temp += ExpectedContent + "\n"; Error(Temp); return false; } Content = "\n" "\n" "\n" "\n"; OriginalContent = Content; ExpectedContent = "\n" "\n" "\n" "\n"; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); } catch (std::exception &e) { std::string Temp; Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: "; Temp += e.what(); Temp += "\n\nOriginal Content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } if (Content != ExpectedContent) { std::string Temp; Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n"; Temp += Content; Temp += "\n\nExpected:\n\n"; Temp += ExpectedContent + "\n"; Error(Temp); return false; } Content = "\n" "\n" "\n" "\n"; OriginalContent = Content; ExpectedContent = "\n" "\n" "\n" "\n"; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); } catch (std::exception &e) { std::string Temp; Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: "; Temp += e.what(); Temp += "\n\nOriginal Content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } if (Content != ExpectedContent) { std::string Temp; Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n"; Temp += Content; Temp += "\n\nExpected:\n\n"; Temp += ExpectedContent + "\n"; Error(Temp); return false; } // Test with invalid input. // Duplicate element. Content = "\n" "\n" "\n" "\n" "\n"; OriginalContent = Content; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); std::string Temp; Temp = "TestReplaceXmlAttribute: Exception not thrown with two elements. Original content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } catch (std::exception &e) { std::cout << "Duplicate TestElement: " << e.what() << "\n\n"; } // Missing element. Content = "\n" "\n" "\n"; OriginalContent = Content; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); std::string Temp; Temp = "TestReplaceXmlAttribute: Exception not thrown with missing element. Original content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } catch (std::exception &e) { std::cout << "Missing TestElement: " << e.what() << "\n\n"; } // Malformed element. Content = "\n" "\n" "\n"; OriginalContent = Content; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); std::string Temp; Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed element. Original content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } catch (std::exception &e) { std::cout << "No closing to TestElement: " << e.what() << "\n\n"; } // Duplicate attribute. Content = "\n" "\n" "\n" "\n"; OriginalContent = Content; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); std::string Temp; Temp = "TestReplaceXmlAttribute: Exception not thrown with two TestAttribute attributes. Original content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } catch (std::exception &e) { std::cout << "Duplicate attribute: " << e.what() << "\n\n"; } // Attribute in another element Content = "\n" "\n" "\n" "\n" "\n"; OriginalContent = Content; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); std::string Temp; Temp = "TestReplaceXmlAttribute: Exception not thrown with TestAttribute attribute in another element. " "Original content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } catch (std::exception &e) { std::cout << "TestAttribute in another element: " << e.what() << "\n\n"; } // Missing attribute. Content = "\n" "\n" "\n" "\n"; OriginalContent = Content; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); std::string Temp; Temp = "TestReplaceXmlAttribute: Exception not thrown with missing TestAttribute attribute. Original content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } catch (std::exception &e) { std::cout << "Missing attribute: " << e.what() << "\n\n"; } // Malformed attribute value. Content = "\n" "\n" "\n" "\n"; OriginalContent = Content; try { Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue); std::string Temp; Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed TestAttribute value. " "Original content:\n\n"; Temp += OriginalContent + "\n"; Error(Temp); return false; } catch (std::exception &e) { std::cout << "No closing to attribute: " << e.what() << "\n\n"; } return true; } /// Unit tests for Utility. // int main(int argc, char* argv[]) { try { // Catch anything that breaks loose. // Test ReplaceXmlAttribute(). if (!TestReplaceXmlAttribute()) { ErrorExit("TestReplaceXmlAttribute() failure.\n"); } } // That's all folks. catch(std::exception& e) { // Report any normal exceptions. std::cerr << "Utility exception: " << e.what() << std::endl; return -1; } catch(...) { // Report any unexpected exceptions. std::cerr << "Panic! Unknown Exception!" << std::endl; return -1; } return 0; // Normally we return zero. }