nodetest.cpp

来自「This software aims to create an applet a」· C++ 代码 · 共 1,140 行 · 第 1/3 页

CPP
1,140
字号
//
// NodeTest.cpp
//
// $Id: //poco/Main/XML/testsuite/src/NodeTest.cpp#5 $
//
// Copyright (c) 2004-2005, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
//    how to obtain complete source code for this software and any
//    accompanying software that uses this software.  The source code
//    must either be included in the distribution or be available for no
//    more than the cost of distribution plus a nominal fee, and must be
//    freely redistributable under reasonable conditions.  For an
//    executable file, complete source code means the source code for all
//    modules it contains.  It does not include source code for modules or
//    files that typically accompany the major components of the operating
//    system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//


#include "NodeTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "DOM/Document.h"
#include "DOM/Element.h"
#include "DOM/DocumentFragment.h"
#include "DOM/AutoPtr.h"


using XML::Element;
using XML::Document;
using XML::DocumentFragment;
using XML::Node;
using XML::AutoPtr;


NodeTest::NodeTest(const std::string& name): CppUnit::TestCase(name)
{
}


NodeTest::~NodeTest()
{
}


void NodeTest::testInsert()
{
	AutoPtr<Document> pDoc = new Document;
	AutoPtr<Element> pRoot = pDoc->createElement("root");

	assert (!pRoot->hasChildNodes());
	assert (pRoot->firstChild() == 0);
	assert (pRoot->lastChild() == 0);

	AutoPtr<Element> pChild1 = pDoc->createElement("child1");
	pRoot->insertBefore(pChild1, 0);
	assert (pRoot->hasChildNodes());
	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild1);
	
	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == 0);
	
	AutoPtr<Element> pChild3 = pDoc->createElement("child3");
	pRoot->insertBefore(pChild3, 0);
	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild3);

	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild1);
	assert (pChild3->nextSibling() == 0);

	AutoPtr<Element> pChild0 = pDoc->createElement("child0");
	pRoot->insertBefore(pChild0, pChild1);
	assert (pRoot->firstChild() == pChild0);
	assert (pRoot->lastChild() == pChild3);
	
	assert (pChild0->previousSibling() == 0);
	assert (pChild0->nextSibling() == pChild1);
	assert (pChild1->previousSibling() == pChild0);
	assert (pChild1->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild1);
	assert (pChild3->nextSibling() == 0);
	
	AutoPtr<Element> pChild2 = pDoc->createElement("child2");
	pRoot->insertBefore(pChild2, pChild3);
	assert (pRoot->firstChild() == pChild0);
	assert (pRoot->lastChild() == pChild3);

	assert (pChild0->previousSibling() == 0);
	assert (pChild0->nextSibling() == pChild1);
	assert (pChild1->previousSibling() == pChild0);
	assert (pChild1->nextSibling() == pChild2);
	assert (pChild2->previousSibling() == pChild1);
	assert (pChild2->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild2);
	assert (pChild3->nextSibling() == 0);
}


void NodeTest::testAppend()
{
	AutoPtr<Document> pDoc = new Document;
	AutoPtr<Element> pRoot = pDoc->createElement("root");

	AutoPtr<Element> pChild1 = pDoc->createElement("child1");
	pRoot->appendChild(pChild1);
	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild1);

	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == 0);

	AutoPtr<Element> pChild2 = pDoc->createElement("child2");
	pRoot->appendChild(pChild2);
	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild2);

	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == pChild2);
	assert (pChild2->previousSibling() == pChild1);
	assert (pChild2->nextSibling() == 0);

	AutoPtr<Element> pChild3 = pDoc->createElement("child3");
	pRoot->appendChild(pChild3);
	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild3);

	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == pChild2);
	assert (pChild2->previousSibling() == pChild1);
	assert (pChild2->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild2);
	assert (pChild3->nextSibling() == 0);
}


void NodeTest::testRemove()
{
	AutoPtr<Document> pDoc = new Document;
	AutoPtr<Element> pRoot = pDoc->createElement("root");

	AutoPtr<Element> pChild1 = pDoc->createElement("child1");
	pRoot->appendChild(pChild1);

	AutoPtr<Element> pChild2 = pDoc->createElement("child2");
	pRoot->appendChild(pChild2);

	AutoPtr<Element> pChild3 = pDoc->createElement("child3");
	pRoot->appendChild(pChild3);

	AutoPtr<Element> pChild4 = pDoc->createElement("child4");
	pRoot->appendChild(pChild4);
	
	pRoot->removeChild(pChild2);
	assert (pChild2->previousSibling() == 0);
	assert (pChild2->nextSibling() == 0);
	
	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild1);
	assert (pChild3->nextSibling() == pChild4);
	assert (pChild4->previousSibling() == pChild3);
	assert (pChild4->nextSibling() == 0);

	pRoot->removeChild(pChild4);
	assert (pChild4->previousSibling() == 0);
	assert (pChild4->nextSibling() == 0);
	
	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild1);
	assert (pChild3->nextSibling() == 0);

	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild3);

	pRoot->removeChild(pChild1);
	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == 0);
	
	assert (pRoot->firstChild() == pChild3);
	assert (pRoot->lastChild() == pChild3);
	
	pRoot->removeChild(pChild3);
	assert (pChild3->previousSibling() == 0);
	assert (pChild3->nextSibling() == 0);
	assert (pRoot->firstChild() == 0);
	assert (pRoot->lastChild() == 0);
}


void NodeTest::testReplace()
{
	AutoPtr<Document> pDoc = new Document;
	AutoPtr<Element> pRoot = pDoc->createElement("root");

	AutoPtr<Element> pChild1 = pDoc->createElement("child1");
	pRoot->appendChild(pChild1);

	AutoPtr<Element> pChild2 = pDoc->createElement("child2");
	pRoot->appendChild(pChild2);

	AutoPtr<Element> pChild3 = pDoc->createElement("child3");
	pRoot->appendChild(pChild3);

	AutoPtr<Element> pChild4 = pDoc->createElement("child4");
	pRoot->appendChild(pChild4);

	AutoPtr<Element> pChild11 = pDoc->createElement("child11");
	pRoot->replaceChild(pChild11, pChild1);
	
	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == 0);
	assert (pRoot->firstChild() == pChild11);
	assert (pRoot->lastChild() == pChild4);
	assert (pChild11->previousSibling() == 0);
	assert (pChild11->nextSibling() == pChild2);
	assert (pChild2->previousSibling() == pChild11);
	assert (pChild2->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild2);
	assert (pChild3->nextSibling() == pChild4);
	assert (pChild4->previousSibling() == pChild3);
	assert (pChild4->nextSibling() == 0);

	AutoPtr<Element> pChild22 = pDoc->createElement("child22");
	pRoot->replaceChild(pChild22, pChild2);

	assert (pChild2->previousSibling() == 0);
	assert (pChild2->nextSibling() == 0);
	assert (pRoot->firstChild() == pChild11);
	assert (pRoot->lastChild() == pChild4);
	assert (pChild11->previousSibling() == 0);
	assert (pChild11->nextSibling() == pChild22);
	assert (pChild22->previousSibling() == pChild11);
	assert (pChild22->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild22);
	assert (pChild3->nextSibling() == pChild4);
	assert (pChild4->previousSibling() == pChild3);
	assert (pChild4->nextSibling() == 0);

	AutoPtr<Element> pChild33 = pDoc->createElement("child33");
	pRoot->replaceChild(pChild33, pChild3);

	assert (pChild3->previousSibling() == 0);
	assert (pChild3->nextSibling() == 0);
	assert (pRoot->firstChild() == pChild11);
	assert (pRoot->lastChild() == pChild4);
	assert (pChild11->previousSibling() == 0);
	assert (pChild11->nextSibling() == pChild22);
	assert (pChild22->previousSibling() == pChild11);
	assert (pChild22->nextSibling() == pChild33);
	assert (pChild33->previousSibling() == pChild22);
	assert (pChild33->nextSibling() == pChild4);
	assert (pChild4->previousSibling() == pChild33);
	assert (pChild4->nextSibling() == 0);

	AutoPtr<Element> pChild44 = pDoc->createElement("child44");
	pRoot->replaceChild(pChild44, pChild4);

	assert (pChild4->previousSibling() == 0);
	assert (pChild4->nextSibling() == 0);
	assert (pRoot->firstChild() == pChild11);
	assert (pRoot->lastChild() == pChild44);
	assert (pChild11->previousSibling() == 0);
	assert (pChild11->nextSibling() == pChild22);
	assert (pChild22->previousSibling() == pChild11);
	assert (pChild22->nextSibling() == pChild33);
	assert (pChild33->previousSibling() == pChild22);
	assert (pChild33->nextSibling() == pChild44);
	assert (pChild44->previousSibling() == pChild33);
	assert (pChild44->nextSibling() == 0);
}


void NodeTest::testInsertFragment1()
{
	AutoPtr<Document> pDoc = new Document;
	AutoPtr<Element> pRoot = pDoc->createElement("root");
	AutoPtr<DocumentFragment> pFrag = pDoc->createDocumentFragment();

	assert (!pRoot->hasChildNodes());
	assert (pRoot->firstChild() == 0);
	assert (pRoot->lastChild() == 0);

	AutoPtr<Element> pChild1 = pDoc->createElement("child1");
	pFrag->appendChild(pChild1);
	pRoot->insertBefore(pFrag, 0);
	assert (pFrag->firstChild() == 0);
	assert (pFrag->lastChild() == 0);
	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild1);
	
	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == 0);
	
	AutoPtr<Element> pChild3 = pDoc->createElement("child3");
	pFrag->appendChild(pChild3);
	pRoot->insertBefore(pFrag, 0);
	assert (pRoot->firstChild() == pChild1);
	assert (pRoot->lastChild() == pChild3);

	assert (pChild1->previousSibling() == 0);
	assert (pChild1->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild1);
	assert (pChild3->nextSibling() == 0);

	AutoPtr<Element> pChild0 = pDoc->createElement("child0");
	pFrag->appendChild(pChild0);
	pRoot->insertBefore(pFrag, pChild1);
	assert (pRoot->firstChild() == pChild0);
	assert (pRoot->lastChild() == pChild3);
	
	assert (pChild0->previousSibling() == 0);
	assert (pChild0->nextSibling() == pChild1);
	assert (pChild1->previousSibling() == pChild0);
	assert (pChild1->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild1);
	assert (pChild3->nextSibling() == 0);
	
	AutoPtr<Element> pChild2 = pDoc->createElement("child2");
	pFrag->appendChild(pChild2);
	pRoot->insertBefore(pFrag, pChild3);
	assert (pRoot->firstChild() == pChild0);
	assert (pRoot->lastChild() == pChild3);

	assert (pChild0->previousSibling() == 0);
	assert (pChild0->nextSibling() == pChild1);
	assert (pChild1->previousSibling() == pChild0);
	assert (pChild1->nextSibling() == pChild2);
	assert (pChild2->previousSibling() == pChild1);
	assert (pChild2->nextSibling() == pChild3);
	assert (pChild3->previousSibling() == pChild2);
	assert (pChild3->nextSibling() == 0);
}


void NodeTest::testInsertFragment2()
{
	AutoPtr<Document> pDoc = new Document;
	AutoPtr<Element> pRoot = pDoc->createElement("root");
	AutoPtr<DocumentFragment> pFrag = pDoc->createDocumentFragment();

	assert (!pRoot->hasChildNodes());
	assert (pRoot->firstChild() == 0);
	assert (pRoot->lastChild() == 0);

	AutoPtr<Element> pChild2 = pDoc->createElement("child2");
	AutoPtr<Element> pChild3 = pDoc->createElement("child3");
	pFrag->appendChild(pChild2);
	pFrag->appendChild(pChild3);
	pRoot->insertBefore(pFrag, 0);
	assert (pFrag->firstChild() == 0);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?