Update to version 2.6.2.

Change-Id: Ia9255d7c343e0a5c9bd22b951b189e347a119e0e
diff --git a/xmltest.cpp b/xmltest.cpp
index 8aa350e..663c157 100644
--- a/xmltest.cpp
+++ b/xmltest.cpp
@@ -3,8 +3,6 @@
 */
 
 
-#include "tinyxml.h"
-
 #ifdef TIXML_USE_STL
 	#include <iostream>
 	#include <sstream>
@@ -14,19 +12,22 @@
 #endif
 
 #if defined( WIN32 ) && defined( TUNE )
-	#include <windows.h>
-	// Apologies to non-windows users! But I need some good timers for
-	// profiling, and these are very platform specific.
-	__int64 start;
-	__int64 end;
-	__int64 freq;
+	#include <crtdbg.h>
+	_CrtMemState startMemState;
+	_CrtMemState endMemState;
 #endif
 
+#include "tinyxml.h"
+
+bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho = false);
+bool XmlTest( const char* testString, int expected, int found, bool noEcho = false );
+
 static int gPass = 0;
 static int gFail = 0;
 
 
-bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho = false)
+
+bool XmlTest (const char* testString, const char* expected, const char* found, bool noEcho )
 {
 	bool pass = !strcmp( expected, found );
 	if ( pass )
@@ -47,7 +48,7 @@
 }
 
 
-bool XmlTest( const char* testString, int expected, int found, bool noEcho = false )
+bool XmlTest( const char* testString, int expected, int found, bool noEcho )
 {
 	bool pass = ( expected == found );
 	if ( pass )
@@ -68,6 +69,17 @@
 }
 
 
+void NullLineEndings( char* p )
+{
+	while( p && *p ) {
+		if ( *p == '\n' || *p == '\r' ) {
+			*p = 0;
+			return;
+		}
+		++p;
+	}
+}
+
 //
 // This file demonstrates some basic functionality of TinyXml.
 // Note that the example is very contrived. It presumes you know
@@ -77,6 +89,7 @@
 
 int main()
 {
+
 	//
 	// We start with the 'demoStart' todo list. Process it. And
 	// should hopefully end up with the todo list as illustrated.
@@ -90,274 +103,393 @@
 		"<Item priority=\"2\" distance='none'> Do bills   </Item>"
 		"<Item priority=\"2\" distance='far &amp; back'> Look for Evil Dinosaurs! </Item>"
 		"</ToDo>";
-
-#ifdef TIXML_USE_STL
-	/*	What the todo list should look like after processing.
-		In stream (no formatting) representation. */
-	const char* demoEnd =
-		"<?xml version=\"1.0\" standalone=\"no\" ?>"
-		"<!-- Our to do list data -->"
-		"<ToDo>"
-		"<!-- Do I need a secure PDA? -->"
-		"<Item priority=\"2\" distance=\"close\">Go to the"
-		"<bold>Toy store!"
-		"</bold>"
-		"</Item>"
-		"<Item priority=\"1\" distance=\"far\">Talk to:"
-		"<Meeting where=\"School\">"
-		"<Attendee name=\"Marple\" position=\"teacher\" />"
-		"<Attendee name=\"Voel\" position=\"counselor\" />"
-		"</Meeting>"
-		"<Meeting where=\"Lunch\" />"
-		"</Item>"
-		"<Item priority=\"2\" distance=\"here\">Do bills"
-		"</Item>"
-		"</ToDo>";
-#endif
-
-	// The example parses from the character string (above):
-	#if defined( WIN32 ) && defined( TUNE )
-	QueryPerformanceCounter( (LARGE_INTEGER*) (&start) );
-	#endif	
-
+		
 	{
-		// Write to a file and read it back, to check file I/O.
+
+	#ifdef TIXML_USE_STL
+		//	What the todo list should look like after processing.
+		// In stream (no formatting) representation.
+		const char* demoEnd =
+			"<?xml version=\"1.0\" standalone=\"no\" ?>"
+			"<!-- Our to do list data -->"
+			"<ToDo>"
+			"<!-- Do I need a secure PDA? -->"
+			"<Item priority=\"2\" distance=\"close\">Go to the"
+			"<bold>Toy store!"
+			"</bold>"
+			"</Item>"
+			"<Item priority=\"1\" distance=\"far\">Talk to:"
+			"<Meeting where=\"School\">"
+			"<Attendee name=\"Marple\" position=\"teacher\" />"
+			"<Attendee name=\"Voel\" position=\"counselor\" />"
+			"</Meeting>"
+			"<Meeting where=\"Lunch\" />"
+			"</Item>"
+			"<Item priority=\"2\" distance=\"here\">Do bills"
+			"</Item>"
+			"</ToDo>";
+	#endif
+
+		// The example parses from the character string (above):
+		#if defined( WIN32 ) && defined( TUNE )
+		_CrtMemCheckpoint( &startMemState );
+		#endif	
+
+		{
+			// Write to a file and read it back, to check file I/O.
+
+			TiXmlDocument doc( "demotest.xml" );
+			doc.Parse( demoStart );
+
+			if ( doc.Error() )
+			{
+				printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
+				exit( 1 );
+			}
+			doc.SaveFile();
+		}
 
 		TiXmlDocument doc( "demotest.xml" );
-		doc.Parse( demoStart );
+		bool loadOkay = doc.LoadFile();
 
-		if ( doc.Error() )
+		if ( !loadOkay )
 		{
-			printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
+			printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
 			exit( 1 );
 		}
-		doc.SaveFile();
+
+		printf( "** Demo doc read from disk: ** \n\n" );
+		printf( "** Printing via doc.Print **\n" );
+		doc.Print( stdout );
+
+		{
+			printf( "** Printing via TiXmlPrinter **\n" );
+			TiXmlPrinter printer;
+			doc.Accept( &printer );
+			fprintf( stdout, "%s", printer.CStr() );
+		}
+		#ifdef TIXML_USE_STL	
+		{
+			printf( "** Printing via operator<< **\n" );
+			std::cout << doc;
+		}
+		#endif
+		TiXmlNode* node = 0;
+		TiXmlElement* todoElement = 0;
+		TiXmlElement* itemElement = 0;
+
+
+		// --------------------------------------------------------
+		// An example of changing existing attributes, and removing
+		// an element from the document.
+		// --------------------------------------------------------
+
+		// Get the "ToDo" element.
+		// It is a child of the document, and can be selected by name.
+		node = doc.FirstChild( "ToDo" );
+		assert( node );
+		todoElement = node->ToElement();
+		assert( todoElement  );
+
+		// Going to the toy store is now our second priority...
+		// So set the "priority" attribute of the first item in the list.
+		node = todoElement->FirstChildElement();	// This skips the "PDA" comment.
+		assert( node );
+		itemElement = node->ToElement();
+		assert( itemElement  );
+		itemElement->SetAttribute( "priority", 2 );
+
+		// Change the distance to "doing bills" from
+		// "none" to "here". It's the next sibling element.
+		itemElement = itemElement->NextSiblingElement();
+		assert( itemElement );
+		itemElement->SetAttribute( "distance", "here" );
+
+		// Remove the "Look for Evil Dinosaurs!" item.
+		// It is 1 more sibling away. We ask the parent to remove
+		// a particular child.
+		itemElement = itemElement->NextSiblingElement();
+		todoElement->RemoveChild( itemElement );
+
+		itemElement = 0;
+
+		// --------------------------------------------------------
+		// What follows is an example of created elements and text
+		// nodes and adding them to the document.
+		// --------------------------------------------------------
+
+		// Add some meetings.
+		TiXmlElement item( "Item" );
+		item.SetAttribute( "priority", "1" );
+		item.SetAttribute( "distance", "far" );
+
+		TiXmlText text( "Talk to:" );
+
+		TiXmlElement meeting1( "Meeting" );
+		meeting1.SetAttribute( "where", "School" );
+
+		TiXmlElement meeting2( "Meeting" );
+		meeting2.SetAttribute( "where", "Lunch" );
+
+		TiXmlElement attendee1( "Attendee" );
+		attendee1.SetAttribute( "name", "Marple" );
+		attendee1.SetAttribute( "position", "teacher" );
+
+		TiXmlElement attendee2( "Attendee" );
+		attendee2.SetAttribute( "name", "Voel" );
+		attendee2.SetAttribute( "position", "counselor" );
+
+		// Assemble the nodes we've created:
+		meeting1.InsertEndChild( attendee1 );
+		meeting1.InsertEndChild( attendee2 );
+
+		item.InsertEndChild( text );
+		item.InsertEndChild( meeting1 );
+		item.InsertEndChild( meeting2 );
+
+		// And add the node to the existing list after the first child.
+		node = todoElement->FirstChild( "Item" );
+		assert( node );
+		itemElement = node->ToElement();
+		assert( itemElement );
+
+		todoElement->InsertAfterChild( itemElement, item );
+
+		printf( "\n** Demo doc processed: ** \n\n" );
+		doc.Print( stdout );
+
+
+	#ifdef TIXML_USE_STL
+		printf( "** Demo doc processed to stream: ** \n\n" );
+		cout << doc << endl << endl;
+	#endif
+
+		// --------------------------------------------------------
+		// Different tests...do we have what we expect?
+		// --------------------------------------------------------
+
+		int count = 0;
+		TiXmlElement*	element;
+
+		//////////////////////////////////////////////////////
+
+	#ifdef TIXML_USE_STL
+		cout << "** Basic structure. **\n";
+		ostringstream outputStream( ostringstream::out );
+		outputStream << doc;
+		XmlTest( "Output stream correct.",	string( demoEnd ).c_str(),
+											outputStream.str().c_str(), true );
+	#endif
+
+		node = doc.RootElement();
+		assert( node );
+		XmlTest( "Root element exists.", true, ( node != 0 && node->ToElement() ) );
+		XmlTest ( "Root element value is 'ToDo'.", "ToDo",  node->Value());
+
+		node = node->FirstChild();
+		XmlTest( "First child exists & is a comment.", true, ( node != 0 && node->ToComment() ) );
+		node = node->NextSibling();
+		XmlTest( "Sibling element exists & is an element.", true, ( node != 0 && node->ToElement() ) );
+		XmlTest ( "Value is 'Item'.", "Item", node->Value() );
+
+		node = node->FirstChild();
+		XmlTest ( "First child exists.", true, ( node != 0 && node->ToText() ) );
+		XmlTest ( "Value is 'Go to the'.", "Go to the", node->Value() );
+
+
+		//////////////////////////////////////////////////////
+		printf ("\n** Iterators. **\n");
+
+		// Walk all the top level nodes of the document.
+		count = 0;
+		for( node = doc.FirstChild();
+			 node;
+			 node = node->NextSibling() )
+		{
+			count++;
+		}
+		XmlTest( "Top level nodes, using First / Next.", 3, count );
+
+		count = 0;
+		for( node = doc.LastChild();
+			 node;
+			 node = node->PreviousSibling() )
+		{
+			count++;
+		}
+		XmlTest( "Top level nodes, using Last / Previous.", 3, count );
+
+		// Walk all the top level nodes of the document,
+		// using a different syntax.
+		count = 0;
+		for( node = doc.IterateChildren( 0 );
+			 node;
+			 node = doc.IterateChildren( node ) )
+		{
+			count++;
+		}
+		XmlTest( "Top level nodes, using IterateChildren.", 3, count );
+
+		// Walk all the elements in a node.
+		count = 0;
+		for( element = todoElement->FirstChildElement();
+			 element;
+			 element = element->NextSiblingElement() )
+		{
+			count++;
+		}
+		XmlTest( "Children of the 'ToDo' element, using First / Next.",
+			3, count );
+
+		// Walk all the elements in a node by value.
+		count = 0;
+		for( node = todoElement->FirstChild( "Item" );
+			 node;
+			 node = node->NextSibling( "Item" ) )
+		{
+			count++;
+		}
+		XmlTest( "'Item' children of the 'ToDo' element, using First/Next.", 3, count );
+
+		count = 0;
+		for( node = todoElement->LastChild( "Item" );
+			 node;
+			 node = node->PreviousSibling( "Item" ) )
+		{
+			count++;
+		}
+		XmlTest( "'Item' children of the 'ToDo' element, using Last/Previous.", 3, count );
+
+	#ifdef TIXML_USE_STL
+		{
+			cout << "\n** Parsing. **\n";
+			istringstream parse0( "<Element0 attribute0='foo0' attribute1= noquotes attribute2 = '&gt;' />" );
+			TiXmlElement element0( "default" );
+			parse0 >> element0;
+
+			XmlTest ( "Element parsed, value is 'Element0'.", "Element0", element0.Value() );
+			XmlTest ( "Reads attribute 'attribute0=\"foo0\"'.", "foo0", element0.Attribute( "attribute0" ));
+			XmlTest ( "Reads incorrectly formatted 'attribute1=noquotes'.", "noquotes", element0.Attribute( "attribute1" ) );
+			XmlTest ( "Read attribute with entity value '>'.", ">", element0.Attribute( "attribute2" ) );
+		}
+	#endif
+
+		{
+			const char* error =	"<?xml version=\"1.0\" standalone=\"no\" ?>\n"
+								"<passages count=\"006\" formatversion=\"20020620\">\n"
+								"    <wrong error>\n"
+								"</passages>";
+
+			TiXmlDocument docTest;
+			docTest.Parse( error );
+			XmlTest( "Error row", docTest.ErrorRow(), 3 );
+			XmlTest( "Error column", docTest.ErrorCol(), 17 );
+			//printf( "error=%d id='%s' row %d col%d\n", (int) doc.Error(), doc.ErrorDesc(), doc.ErrorRow()+1, doc.ErrorCol() + 1 );
+
+		}
+
+	#ifdef TIXML_USE_STL
+		{
+			//////////////////////////////////////////////////////
+			cout << "\n** Streaming. **\n";
+
+			// Round trip check: stream in, then stream back out to verify. The stream
+			// out has already been checked, above. We use the output
+
+			istringstream inputStringStream( outputStream.str() );
+			TiXmlDocument document0;
+
+			inputStringStream >> document0;
+
+			ostringstream outputStream0( ostringstream::out );
+			outputStream0 << document0;
+
+			XmlTest( "Stream round trip correct.",	string( demoEnd ).c_str(), 
+													outputStream0.str().c_str(), true );
+
+			std::string str;
+			str << document0;
+
+			XmlTest( "String printing correct.", string( demoEnd ).c_str(), 
+												 str.c_str(), true );
+		}
+	#endif
 	}
 
-	TiXmlDocument doc( "demotest.xml" );
-	bool loadOkay = doc.LoadFile();
-
-	if ( !loadOkay )
-	{
-		printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
-		exit( 1 );
-	}
-
-	printf( "** Demo doc read from disk: ** \n\n" );
-	doc.Print( stdout );
-
-	TiXmlNode* node = 0;
-	TiXmlElement* todoElement = 0;
-	TiXmlElement* itemElement = 0;
-
-
-	// --------------------------------------------------------
-	// An example of changing existing attributes, and removing
-	// an element from the document.
-	// --------------------------------------------------------
-
-	// Get the "ToDo" element.
-	// It is a child of the document, and can be selected by name.
-	node = doc.FirstChild( "ToDo" );
-	assert( node );
-	todoElement = node->ToElement();
-	assert( todoElement  );
-
-	// Going to the toy store is now our second priority...
-	// So set the "priority" attribute of the first item in the list.
-	node = todoElement->FirstChildElement();	// This skips the "PDA" comment.
-	assert( node );
-	itemElement = node->ToElement();
-	assert( itemElement  );
-	itemElement->SetAttribute( "priority", 2 );
-
-	// Change the distance to "doing bills" from
-	// "none" to "here". It's the next sibling element.
-	itemElement = itemElement->NextSiblingElement();
-	assert( itemElement );
-	itemElement->SetAttribute( "distance", "here" );
-
-	// Remove the "Look for Evil Dinosaurs!" item.
-	// It is 1 more sibling away. We ask the parent to remove
-	// a particular child.
-	itemElement = itemElement->NextSiblingElement();
-	todoElement->RemoveChild( itemElement );
-
-	itemElement = 0;
-
-	// --------------------------------------------------------
-	// What follows is an example of created elements and text
-	// nodes and adding them to the document.
-	// --------------------------------------------------------
-
-	// Add some meetings.
-	TiXmlElement item( "Item" );
-	item.SetAttribute( "priority", "1" );
-	item.SetAttribute( "distance", "far" );
-
-	TiXmlText text( "Talk to:" );
-
-	TiXmlElement meeting1( "Meeting" );
-	meeting1.SetAttribute( "where", "School" );
-
-	TiXmlElement meeting2( "Meeting" );
-	meeting2.SetAttribute( "where", "Lunch" );
-
-	TiXmlElement attendee1( "Attendee" );
-	attendee1.SetAttribute( "name", "Marple" );
-	attendee1.SetAttribute( "position", "teacher" );
-
-	TiXmlElement attendee2( "Attendee" );
-	attendee2.SetAttribute( "name", "Voel" );
-	attendee2.SetAttribute( "position", "counselor" );
-
-	// Assemble the nodes we've created:
-	meeting1.InsertEndChild( attendee1 );
-	meeting1.InsertEndChild( attendee2 );
-
-	item.InsertEndChild( text );
-	item.InsertEndChild( meeting1 );
-	item.InsertEndChild( meeting2 );
-
-	// And add the node to the existing list after the first child.
-	node = todoElement->FirstChild( "Item" );
-	assert( node );
-	itemElement = node->ToElement();
-	assert( itemElement );
-
-	todoElement->InsertAfterChild( itemElement, item );
-
-	printf( "\n** Demo doc processed: ** \n\n" );
-	doc.Print( stdout );
-
-
-#ifdef TIXML_USE_STL
-	printf( "** Demo doc processed to stream: ** \n\n" );
-	cout << doc << endl << endl;
-#endif
-
-	// --------------------------------------------------------
-	// Different tests...do we have what we expect?
-	// --------------------------------------------------------
-
-	int count = 0;
-	TiXmlElement*	element;
-
-	//////////////////////////////////////////////////////
-
-#ifdef TIXML_USE_STL
-	cout << "** Basic structure. **\n";
-	ostringstream outputStream( ostringstream::out );
-	outputStream << doc;
-	XmlTest( "Output stream correct.",	string( demoEnd ).c_str(),
-										outputStream.str().c_str(), true );
-#endif
-
-	node = doc.RootElement();
-	XmlTest( "Root element exists.", true, ( node != 0 && node->ToElement() ) );
-	XmlTest ( "Root element value is 'ToDo'.", "ToDo",  node->Value());
-
-	node = node->FirstChild();
-	XmlTest( "First child exists & is a comment.", true, ( node != 0 && node->ToComment() ) );
-	node = node->NextSibling();
-	XmlTest( "Sibling element exists & is an element.", true, ( node != 0 && node->ToElement() ) );
-	XmlTest ( "Value is 'Item'.", "Item", node->Value() );
-
-	node = node->FirstChild();
-	XmlTest ( "First child exists.", true, ( node != 0 && node->ToText() ) );
-	XmlTest ( "Value is 'Go to the'.", "Go to the", node->Value() );
-
-
-	//////////////////////////////////////////////////////
-	printf ("\n** Iterators. **\n");
-
-	// Walk all the top level nodes of the document.
-	count = 0;
-	for( node = doc.FirstChild();
-		 node;
-		 node = node->NextSibling() )
-	{
-		count++;
-	}
-	XmlTest( "Top level nodes, using First / Next.", 3, count );
-
-	count = 0;
-	for( node = doc.LastChild();
-		 node;
-		 node = node->PreviousSibling() )
-	{
-		count++;
-	}
-	XmlTest( "Top level nodes, using Last / Previous.", 3, count );
-
-	// Walk all the top level nodes of the document,
-	// using a different syntax.
-	count = 0;
-	for( node = doc.IterateChildren( 0 );
-		 node;
-		 node = doc.IterateChildren( node ) )
-	{
-		count++;
-	}
-	XmlTest( "Top level nodes, using IterateChildren.", 3, count );
-
-	// Walk all the elements in a node.
-	count = 0;
-	for( element = todoElement->FirstChildElement();
-		 element;
-		 element = element->NextSiblingElement() )
-	{
-		count++;
-	}
-	XmlTest( "Children of the 'ToDo' element, using First / Next.",
-		3, count );
-
-	// Walk all the elements in a node by value.
-	count = 0;
-	for( node = todoElement->FirstChild( "Item" );
-		 node;
-		 node = node->NextSibling( "Item" ) )
-	{
-		count++;
-	}
-	XmlTest( "'Item' children of the 'ToDo' element, using First/Next.", 3, count );
-
-	count = 0;
-	for( node = todoElement->LastChild( "Item" );
-		 node;
-		 node = node->PreviousSibling( "Item" ) )
-	{
-		count++;
-	}
-	XmlTest( "'Item' children of the 'ToDo' element, using Last/Previous.", 3, count );
-
-#ifdef TIXML_USE_STL
-	{
-		cout << "\n** Parsing. **\n";
-		istringstream parse0( "<Element0 attribute0='foo0' attribute1= noquotes attribute2 = '&gt;' />" );
-		TiXmlElement element0( "default" );
-		parse0 >> element0;
-
-		XmlTest ( "Element parsed, value is 'Element0'.", "Element0", element0.Value() );
-		XmlTest ( "Reads attribute 'attribute0=\"foo0\"'.", "foo0", element0.Attribute( "attribute0" ));
-		XmlTest ( "Reads incorrectly formatted 'attribute1=noquotes'.", "noquotes", element0.Attribute( "attribute1" ) );
-		XmlTest ( "Read attribute with entity value '>'.", ">", element0.Attribute( "attribute2" ) );
-	}
-#endif
-
 	{
-		const char* error =	"<?xml version=\"1.0\" standalone=\"no\" ?>\n"
-							"<passages count=\"006\" formatversion=\"20020620\">\n"
-							"    <wrong error>\n"
-							"</passages>";
+		const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
 
-        TiXmlDocument doc;
-		doc.Parse( error );
-		XmlTest( "Error row", doc.ErrorRow(), 3 );
-		XmlTest( "Error column", doc.ErrorCol(), 17 );
-		//printf( "error=%d id='%s' row %d col%d\n", (int) doc.Error(), doc.ErrorDesc(), doc.ErrorRow()+1, doc.ErrorCol() + 1 );
+		TiXmlDocument doc;
+		doc.Parse( str );
 
+		TiXmlElement* ele = doc.FirstChildElement();
+
+		int iVal, result;
+		double dVal;
+
+		result = ele->QueryDoubleAttribute( "attr0", &dVal );
+		XmlTest( "Query attribute: int as double", result, TIXML_SUCCESS );
+		XmlTest( "Query attribute: int as double", (int)dVal, 1 );
+		result = ele->QueryDoubleAttribute( "attr1", &dVal );
+		XmlTest( "Query attribute: double as double", (int)dVal, 2 );
+		result = ele->QueryIntAttribute( "attr1", &iVal );
+		XmlTest( "Query attribute: double as int", result, TIXML_SUCCESS );
+		XmlTest( "Query attribute: double as int", iVal, 2 );
+		result = ele->QueryIntAttribute( "attr2", &iVal );
+		XmlTest( "Query attribute: not a number", result, TIXML_WRONG_TYPE );
+		result = ele->QueryIntAttribute( "bar", &iVal );
+		XmlTest( "Query attribute: does not exist", result, TIXML_NO_ATTRIBUTE );
 	}
+
+	{
+		const char* str = "<doc/>";
+
+		TiXmlDocument doc;
+		doc.Parse( str );
+
+		TiXmlElement* ele = doc.FirstChildElement();
+
+		int iVal;
+		double dVal;
+
+		ele->SetAttribute( "str", "strValue" );
+		ele->SetAttribute( "int", 1 );
+		ele->SetDoubleAttribute( "double", -1.0 );
+
+		const char* cStr = ele->Attribute( "str" );
+		ele->QueryIntAttribute( "int", &iVal );
+		ele->QueryDoubleAttribute( "double", &dVal );
+
+		XmlTest( "Attribute round trip. c-string.", "strValue", cStr );
+		XmlTest( "Attribute round trip. int.", 1, iVal );
+		XmlTest( "Attribute round trip. double.", -1, (int)dVal );
+	}
+	
+	{
+		const char* str =	"\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n"
+							"</room>";
+
+		TiXmlDocument doc;
+		doc.SetTabSize( 8 );
+		doc.Parse( str );
+
+		TiXmlHandle docHandle( &doc );
+		TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" );
+
+		assert( docHandle.Node() );
+		assert( roomHandle.Element() );
+
+		TiXmlElement* room = roomHandle.Element();
+		assert( room );
+		TiXmlAttribute* doors = room->FirstAttribute();
+		assert( doors );
+
+		XmlTest( "Location tracking: Tab 8: room row", room->Row(), 1 );
+		XmlTest( "Location tracking: Tab 8: room col", room->Column(), 49 );
+		XmlTest( "Location tracking: Tab 8: doors row", doors->Row(), 1 );
+		XmlTest( "Location tracking: Tab 8: doors col", doors->Column(), 55 );
+	}
+	
 	{
 		const char* str =	"\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n"
 							"  <!-- Silly example -->\n"
@@ -365,7 +497,7 @@
 							"\t<door wall='east'/>"
 							"</room>";
 
-        TiXmlDocument doc;
+		TiXmlDocument doc;
 		doc.Parse( str );
 
 		TiXmlHandle docHandle( &doc );
@@ -409,82 +541,7 @@
 		XmlTest( "Location tracking: door1 row", door1->Row(), 4 );
 		XmlTest( "Location tracking: door1 col", door1->Column(), 5 );
 	}
-	{
-		const char* str =	"\t<?xml version=\"1.0\" standalone=\"no\" ?>\t<room doors='2'>\n"
-							"</room>";
 
-        TiXmlDocument doc;
-		doc.SetTabSize( 8 );
-		doc.Parse( str );
-
-		TiXmlHandle docHandle( &doc );
-		TiXmlHandle roomHandle = docHandle.FirstChildElement( "room" );
-
-		assert( docHandle.Node() );
-		assert( roomHandle.Element() );
-
-		TiXmlElement* room = roomHandle.Element();
-		assert( room );
-		TiXmlAttribute* doors = room->FirstAttribute();
-		assert( doors );
-
-		XmlTest( "Location tracking: Tab 8: room row", room->Row(), 1 );
-		XmlTest( "Location tracking: Tab 8: room col", room->Column(), 49 );
-		XmlTest( "Location tracking: Tab 8: doors row", doors->Row(), 1 );
-		XmlTest( "Location tracking: Tab 8: doors col", doors->Column(), 55 );
-	}
-
-	{
-		const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
-
-		TiXmlDocument doc;
-		doc.Parse( str );
-
-		TiXmlElement* ele = doc.FirstChildElement();
-
-		int iVal, result;
-		double dVal;
-
-		result = ele->QueryDoubleAttribute( "attr0", &dVal );
-		XmlTest( "Query attribute: int as double", result, TIXML_SUCCESS );
-		XmlTest( "Query attribute: int as double", (int)dVal, 1 );
-		result = ele->QueryDoubleAttribute( "attr1", &dVal );
-		XmlTest( "Query attribute: double as double", (int)dVal, 2 );
-		result = ele->QueryIntAttribute( "attr1", &iVal );
-		XmlTest( "Query attribute: double as int", result, TIXML_SUCCESS );
-		XmlTest( "Query attribute: double as int", iVal, 2 );
-		result = ele->QueryIntAttribute( "attr2", &iVal );
-		XmlTest( "Query attribute: not a number", result, TIXML_WRONG_TYPE );
-		result = ele->QueryIntAttribute( "bar", &iVal );
-		XmlTest( "Query attribute: does not exist", result, TIXML_NO_ATTRIBUTE );
-	}
-
-#ifdef TIXML_USE_STL
-	{
-		//////////////////////////////////////////////////////
-		cout << "\n** Streaming. **\n";
-
-		// Round trip check: stream in, then stream back out to verify. The stream
-		// out has already been checked, above. We use the output
-
-		istringstream inputStringStream( outputStream.str() );
-		TiXmlDocument document0;
-
-		inputStringStream >> document0;
-
-		ostringstream outputStream0( ostringstream::out );
-		outputStream0 << document0;
-
-		XmlTest( "Stream round trip correct.",	string( demoEnd ).c_str(), 
-												outputStream0.str().c_str(), true );
-
-		std::string str;
-		str << document0;
-
-		XmlTest( "String printing correct.", string( demoEnd ).c_str(), 
-											 str.c_str(), true );
-	}
-#endif
 
 	// --------------------------------------------------------
 	// UTF-8 testing. It is important to test:
@@ -541,20 +598,30 @@
 
 			FILE* saved  = fopen( "utf8testout.xml", "r" );
 			FILE* verify = fopen( "utf8testverify.xml", "r" );
+
+			//bool firstLineBOM=true;
 			if ( saved && verify )
 			{
 				while ( fgets( verifyBuf, 256, verify ) )
 				{
 					fgets( savedBuf, 256, saved );
-					if ( strcmp( verifyBuf, savedBuf ) )
+					NullLineEndings( verifyBuf );
+					NullLineEndings( savedBuf );
+
+					if ( /*!firstLineBOM && */ strcmp( verifyBuf, savedBuf ) )
 					{
+						printf( "verify:%s<\n", verifyBuf );
+						printf( "saved :%s<\n", savedBuf );
 						okay = 0;
 						break;
 					}
+					//firstLineBOM = false;
 				}
-				fclose( saved );
-				fclose( verify );
 			}
+			if ( saved )
+				fclose( saved );
+			if ( verify )
+				fclose( verify );
 			XmlTest( "UTF-8: Verified multi-language round trip.", 1, okay );
 
 			// On most Western machines, this is an element that contains
@@ -588,7 +655,7 @@
 		XmlTest( "Copy/Assign: element copy #2.", "value", elementCopy.Attribute( "name" ) );
 		XmlTest( "Copy/Assign: element assign #1.", "element", elementAssign.Value() );
 		XmlTest( "Copy/Assign: element assign #2.", "value", elementAssign.Attribute( "name" ) );
-		XmlTest( "Copy/Assign: element assign #3.", 0, (int) elementAssign.Attribute( "foo" ) );
+		XmlTest( "Copy/Assign: element assign #3.", true, ( 0 == elementAssign.Attribute( "foo" )) );
 
 		TiXmlComment comment;
 		comment.Parse( "<!--comment-->", 0, TIXML_ENCODING_UNKNOWN );
@@ -648,15 +715,15 @@
 #ifdef TIXML_USE_STL
 	printf ("\n** Parsing, no Condense Whitespace **\n");
 	TiXmlBase::SetCondenseWhiteSpace( false );
+	{
+		istringstream parse1( "<start>This  is    \ntext</start>" );
+		TiXmlElement text1( "text" );
+		parse1 >> text1;
 
-	istringstream parse1( "<start>This  is    \ntext</start>" );
-	TiXmlElement text1( "text" );
-	parse1 >> text1;
-
-	XmlTest ( "Condense white space OFF.", "This  is    \ntext",
-				text1.FirstChild()->Value(),
-				true );
-
+		XmlTest ( "Condense white space OFF.", "This  is    \ntext",
+					text1.FirstChild()->Value(),
+					true );
+	}
 	TiXmlBase::SetCondenseWhiteSpace( true );
 #endif
 
@@ -699,7 +766,7 @@
 							"</xmlElement>";
 		TiXmlDocument doc;
 		doc.Parse( str );
-		//doc.Print();
+		doc.Print();
 
 		XmlTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(), 
 								 "I am > the rules!\n...since I make symbolic puns",
@@ -726,8 +793,99 @@
 								 "I am > the rules!\n...since I make symbolic puns",
 								 true );
 	}
+	{
+		// [ 1482728 ] Wrong wide char parsing
+		char buf[256];
+		buf[255] = 0;
+		for( int i=0; i<255; ++i ) {
+			buf[i] = (char)((i>=32) ? i : 32);
+		}
+		TIXML_STRING str( "<xmlElement><![CDATA[" );
+		str += buf;
+		str += "]]></xmlElement>";
+
+		TiXmlDocument doc;
+		doc.Parse( str.c_str() );
+
+		TiXmlPrinter printer;
+		printer.SetStreamPrinting();
+		doc.Accept( &printer );
+
+		XmlTest( "CDATA with all bytes #1.", str.c_str(), printer.CStr(), true );
+
+		#ifdef TIXML_USE_STL
+		doc.Clear();
+		istringstream iss( printer.Str() );
+		iss >> doc;
+		std::string out;
+		out << doc;
+		XmlTest( "CDATA with all bytes #2.", out.c_str(), printer.CStr(), true );
+		#endif
+	}
+	{
+		// [ 1480107 ] Bug-fix for STL-streaming of CDATA that contains tags
+		// CDATA streaming had a couple of bugs, that this tests for.
+		const char* str =	"<xmlElement>"
+								"<![CDATA["
+									"<b>I am > the rules!</b>\n"
+									"...since I make symbolic puns"
+								"]]>"
+							"</xmlElement>";
+		TiXmlDocument doc;
+		doc.Parse( str );
+		doc.Print();
+
+		XmlTest( "CDATA parse. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(), 
+								 "<b>I am > the rules!</b>\n...since I make symbolic puns",
+								 true );
+
+		#ifdef TIXML_USE_STL
+
+		doc.Clear();
+
+		istringstream parse0( str );
+		parse0 >> doc;
+
+		XmlTest( "CDATA stream. [ 1480107 ]", doc.FirstChildElement()->FirstChild()->Value(), 
+								 "<b>I am > the rules!</b>\n...since I make symbolic puns",
+								 true );
+		#endif
+
+		TiXmlDocument doc1 = doc;
+		//doc.Print();
+
+		XmlTest( "CDATA copy. [ 1480107 ]", doc1.FirstChildElement()->FirstChild()->Value(), 
+								 "<b>I am > the rules!</b>\n...since I make symbolic puns",
+								 true );
+	}
+	//////////////////////////////////////////////////////
+	// Visit()
 
 
+
+	//////////////////////////////////////////////////////
+	printf( "\n** Fuzzing... **\n" );
+
+	const int FUZZ_ITERATION = 300;
+
+	// The only goal is not to crash on bad input.
+	int len = (int) strlen( demoStart );
+	for( int i=0; i<FUZZ_ITERATION; ++i ) 
+	{
+		char* demoCopy = new char[ len+1 ];
+		strcpy( demoCopy, demoStart );
+
+		demoCopy[ i%len ] = (char)((i+1)*3);
+		demoCopy[ (i*7)%len ] = '>';
+		demoCopy[ (i*11)%len ] = '<';
+
+		TiXmlDocument xml;
+		xml.Parse( demoCopy );
+
+		delete [] demoCopy;
+	}
+	printf( "** Fuzzing Complete. **\n" );
+	
 	//////////////////////////////////////////////////////
 	printf ("\n** Bug regression tests **\n");
 
@@ -955,8 +1113,8 @@
 		TiXmlDocument doc;
 		doc.Parse( doctype );
 		
-		XmlTest( "Parsing repeated attributes.", 0, (int)doc.Error() );	// not an  error to tinyxml
-		XmlTest( "Parsing repeated attributes.", "blue", doc.FirstChildElement( "element" )->Attribute( "attr" ) );
+		XmlTest( "Parsing repeated attributes.", true, doc.Error() );	// is an  error to tinyxml (didn't use to be, but caused issues)
+		//XmlTest( "Parsing repeated attributes.", "blue", doc.FirstChildElement( "element" )->Attribute( "attr" ) );
 	}
 
 	{
@@ -987,8 +1145,6 @@
             TiXmlDocument doc;
             doc.Parse( str );
 
-			//doc.Print( stdout, 0 );
-
             TiXmlHandle docHandle( &doc );
             TiXmlHandle aHandle = docHandle.FirstChildElement( "ä" );
             TiXmlHandle tHandle = aHandle.Child( 0 );
@@ -1038,15 +1194,200 @@
 		XmlTest( "Low entities.", xml.FirstChildElement()->GetText(), result );
 		xml.Print();
 	}
+	{
+		// Bug [ 1451649 ] Attribute values with trailing quotes not handled correctly
+		TiXmlDocument xml;
+		xml.Parse( "<foo attribute=bar\" />" );
+		XmlTest( "Throw error with bad end quotes.", xml.Error(), true );
+	}
+	#ifdef TIXML_USE_STL
+	{
+		// Bug [ 1449463 ] Consider generic query
+		TiXmlDocument xml;
+		xml.Parse( "<foo bar='3' barStr='a string'/>" );
+
+		TiXmlElement* ele = xml.FirstChildElement();
+		double d;
+		int i;
+		float f;
+		bool b;
+		std::string str;
+
+		XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &d ), TIXML_SUCCESS );
+		XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &i ), TIXML_SUCCESS );
+		XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &f ), TIXML_SUCCESS );
+		XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "bar", &b ), TIXML_WRONG_TYPE );
+		XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "nobar", &b ), TIXML_NO_ATTRIBUTE );
+		XmlTest( "QueryValueAttribute", ele->QueryValueAttribute( "barStr", &str ), TIXML_SUCCESS );
+
+		XmlTest( "QueryValueAttribute", (d==3.0), true );
+		XmlTest( "QueryValueAttribute", (i==3), true );
+		XmlTest( "QueryValueAttribute", (f==3.0f), true );
+		XmlTest( "QueryValueAttribute", (str==std::string( "a string" )), true );
+	}
+	#endif
+
+	#ifdef TIXML_USE_STL
+	{
+		// [ 1505267 ] redundant malloc in TiXmlElement::Attribute
+		TiXmlDocument xml;
+		xml.Parse( "<foo bar='3' />" );
+		TiXmlElement* ele = xml.FirstChildElement();
+		double d;
+		int i;
+
+		std::string bar = "bar";
+
+		const std::string* atrrib = ele->Attribute( bar );
+		ele->Attribute( bar, &d );
+		ele->Attribute( bar, &i );
+
+		XmlTest( "Attribute", atrrib->empty(), false );
+		XmlTest( "Attribute", (d==3.0), true );
+		XmlTest( "Attribute", (i==3), true );
+	}
+	#endif
+
+	{
+		// [ 1356059 ] Allow TiXMLDocument to only be at the top level
+		TiXmlDocument xml, xml2;
+		xml.InsertEndChild( xml2 );
+		XmlTest( "Document only at top level.", xml.Error(), true );
+		XmlTest( "Document only at top level.", xml.ErrorId(), TiXmlBase::TIXML_ERROR_DOCUMENT_TOP_ONLY );
+	}
+
+	{
+		// [ 1663758 ] Failure to report error on bad XML
+		TiXmlDocument xml;
+		xml.Parse("<x>");
+		XmlTest("Missing end tag at end of input", xml.Error(), true);
+		xml.Parse("<x> ");
+		XmlTest("Missing end tag with trailing whitespace", xml.Error(), true);
+	} 
+
+	{
+		// [ 1635701 ] fail to parse files with a tag separated into two lines
+		// I'm not sure this is a bug. Marked 'pending' for feedback.
+		TiXmlDocument xml;
+		xml.Parse( "<title><p>text</p\n><title>" );
+		//xml.Print();
+		//XmlTest( "Tag split by newline", xml.Error(), false );
+	}
+
+	#ifdef TIXML_USE_STL
+	{
+		// [ 1475201 ] TinyXML parses entities in comments
+		TiXmlDocument xml;
+		istringstream parse1( "<!-- declarations for <head> & <body> -->"
+						      "<!-- far &amp; away -->" );
+		parse1 >> xml;
+
+		TiXmlNode* e0 = xml.FirstChild();
+		TiXmlNode* e1 = e0->NextSibling();
+		TiXmlComment* c0 = e0->ToComment();
+		TiXmlComment* c1 = e1->ToComment();
+
+		XmlTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
+		XmlTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
+	}
+	#endif
+
+	{
+		// [ 1475201 ] TinyXML parses entities in comments
+		TiXmlDocument xml;
+		xml.Parse("<!-- declarations for <head> & <body> -->"
+				  "<!-- far &amp; away -->" );
+
+		TiXmlNode* e0 = xml.FirstChild();
+		TiXmlNode* e1 = e0->NextSibling();
+		TiXmlComment* c0 = e0->ToComment();
+		TiXmlComment* c1 = e1->ToComment();
+
+		XmlTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
+		XmlTest( "Comments ignore entities.", " far &amp; away ", c1->Value(), true );
+	}
+
+	{
+		TiXmlDocument xml;
+		xml.Parse( "<Parent>"
+						"<child1 att=''/>"
+						"<!-- With this comment, child2 will not be parsed! -->"
+						"<child2 att=''/>"
+					"</Parent>" );
+		int count = 0;
+
+		TiXmlNode* ele = 0;
+		while ( (ele = xml.FirstChildElement( "Parent" )->IterateChildren( ele ) ) != 0 ) {
+			++count;
+		}
+		XmlTest( "Comments iterate correctly.", 3, count );
+	}
+
+	{
+		// trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
+		unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
+		buf[60] = 239;
+		buf[61] = 0;
+
+		TiXmlDocument doc;
+		doc.Parse( (const char*)buf);
+	} 
+
+
+	{
+		// bug 1827248 Error while parsing a little bit malformed file
+		// Actually not malformed - should work.
+		TiXmlDocument xml;
+		xml.Parse( "<attributelist> </attributelist >" );
+		XmlTest( "Handle end tag whitespace", false, xml.Error() );
+	}
+
+	{
+		// This one must not result in an infinite loop
+		TiXmlDocument xml;
+		xml.Parse( "<infinite>loop" );
+		XmlTest( "Infinite loop test.", true, true );
+	}
+
+	{
+		// 1709904 - can not repro the crash
+		{
+			TiXmlDocument xml;
+			xml.Parse( "<tag>/</tag>" );
+			XmlTest( "Odd XML parsing.", xml.FirstChild()->Value(), "tag" );
+		}
+		/* Could not repro. {
+			TiXmlDocument xml;
+			xml.LoadFile( "EQUI_Inventory.xml" );
+			//XmlTest( "Odd XML parsing.", xml.FirstChildElement()->Value(), "XML" );
+			TiXmlPrinter printer;
+			xml.Accept( &printer );
+			fprintf( stdout, "%s", printer.CStr() );
+		}*/
+	}
+
+	/*  1417717 experiment
+	{
+		TiXmlDocument xml;
+		xml.Parse("<text>Dan & Tracie</text>");
+		xml.Print(stdout);
+	}
+	{
+		TiXmlDocument xml;
+		xml.Parse("<text>Dan &foo; Tracie</text>");
+		xml.Print(stdout);
+	}
+	*/
 
 	#if defined( WIN32 ) && defined( TUNE )
-	QueryPerformanceCounter( (LARGE_INTEGER*) (&end) );
-	QueryPerformanceFrequency( (LARGE_INTEGER*) (&freq) );
-	printf( "Time for run: %f\n", ( double )( end-start ) / (double) freq );
+	_CrtMemCheckpoint( &endMemState );
+	//_CrtMemDumpStatistics( &endMemState );
+
+	_CrtMemState diffMemState;
+	_CrtMemDifference( &diffMemState, &startMemState, &endMemState );
+	_CrtMemDumpStatistics( &diffMemState );
 	#endif
 
 	printf ("\nPass %d, Fail %d\n", gPass, gFail);
 	return gFail;
 }
-
-