/* This class tests (most of?) the functionality of tnode. */ #include #include "tnode.h" using namespace std; int main() { // test basic node creation tnode* tree = new tnode("root"); if (tree->parent() != tree) { cout << "error: root not own parent" << endl; } if (tree->getName() != "root") { cout << "error: node has wrong name" << endl; } // test addChild() tree->addChild("one"); if (!tree->addChild("two")) { cout << "error: successful add should return true" << endl; } if (tree->addChild("two")) { cout << "error: failed add should return false" << endl; } tree->printChildren(cout); // should print "one two" or "two one" // test getChild() tnode* current = tree->getChild("one"); if (!current || current->getName() != "one") { cout << "error: getChild failed" << endl; } if (tree->getChild("not there")) { cout << "error: getChild should have returned NULL (false)" << endl; } // confirm children can be added below the root current->addChild("a"); current->addChild("b"); current->printChildren(cout); // should print a b or b a // test moving up the tree current = current->parent(); if (current != tree) { cout << "error: wrong parent" << endl; } if (!current->remove("one")) { cout << "error: failed to remove child 'one'" << endl; } if (current->remove("not there")) { cout << "error: should return false for node not found" << endl; } current->printChildren(cout); // should print two }