Browse Source

Test: PityTest - test_pitytree now design example

master
heck 4 years ago
parent
commit
2293191e12
  1. 150
      test/test_pitytree.cc

150
test/test_pitytree.cc

@ -1,62 +1,144 @@
#include "../src/PityTree.hh" #include "../src/PityTree.hh"
#include "../src/PityTest.hh" #include "../src/PityTest.hh"
//#include "../../../src/utils.hh" #include "../../../src/std_utils.hh"
#include "../../../src/utils.hh"
#include <iostream> #include <iostream>
#include <exception> #include <exception>
using namespace pEp;
using namespace pEp::PityTest11; using namespace pEp::PityTest11;
class Node : public PityTree<Node> { // -----------------------------------------------------------------------------------------------
class AbstractNode : public PityTree<AbstractNode> {
public: public:
Node() = delete; // Constructors
Node(const std::string &name); AbstractNode() = delete;
Node(const std::string &name, Node &parent); explicit AbstractNode(const std::string &name);
explicit AbstractNode(const std::string &name, AbstractNode &parent);
AbstractNode(const AbstractNode &rhs, AbstractNode &self);
// methods
virtual int implMe(int magic_nr) = 0;
AbstractNode *clone() override = 0;
// fields
std::string color; std::string color;
int age; int age{};
}; };
Node::Node(const std::string &name) : PityTree<Node>(*this, name) {} AbstractNode::AbstractNode(const std::string &name) : PityTree<AbstractNode>(*this, name) {}
Node::Node(const std::string &name, Node &parent) : PityTree<Node>(*this, name, parent) {} AbstractNode::AbstractNode(const std::string &name, AbstractNode &parent) :
PityTree<AbstractNode>(*this, name, parent)
{
}
std::string readKey() AbstractNode::AbstractNode(const AbstractNode &rhs, AbstractNode &self) :
PityTree<AbstractNode>(rhs, self)
{ {
std::string ret; color = rhs.color;
std::cin >> ret; age = rhs.age;
return ret;
} }
void not_throwing(){
throw std::runtime_error{"Fsd"}; // -----------------------------------------------------------------------------------------------
class ANode : public AbstractNode {
public:
explicit ANode(const std::string &name);
explicit ANode(const std::string &name, AbstractNode &parent);
ANode(const ANode &rhs);
ANode *clone() override;
int implMe(int magic_nr) override;
};
ANode::ANode(const std::string &name) : AbstractNode(name) {}
ANode::ANode(const std::string &name, AbstractNode &parent) : AbstractNode(name, parent) {}
ANode::ANode(const ANode &rhs) : AbstractNode(rhs, *this) {}
int ANode::implMe(int magic_nr)
{
return 23;
} }
void throwing(){ ANode *ANode::clone()
throw std::runtime_error{"Fsd"}; {
return new ANode(*this);
}
// -----------------------------------------------------------------------------------------------
class BNode : public AbstractNode {
public:
explicit BNode(const std::string &name);
explicit BNode(const std::string &name, AbstractNode &parent);
BNode(const BNode &rhs);
BNode *clone() override;
int implMe(int magic_nr) override;
};
BNode::BNode(const std::string &name) : AbstractNode(name) {}
BNode::BNode(const std::string &name, AbstractNode &parent) : AbstractNode(name, parent) {}
BNode::BNode(const BNode &rhs) : AbstractNode(rhs, *this) {}
int BNode::implMe(int magic_nr)
{
return 42;
}
BNode *BNode::clone()
{
return new BNode(*this);
}
// -----------------------------------------------------------------------------------------------
void not_throwing()
{
throw std::runtime_error{ "Fsd" };
}
void throwing()
{
throw std::runtime_error{ "Fsd" };
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Node a{ "a" }; // Create lone node
ANode a{ "a" };
std::cout << a.getPath() << std::endl; std::cout << a.getPath() << std::endl;
PITYASSERT(a.isRoot() == true, ""); PITYASSERT(a.isRoot() == true, "a");
PITYASSERT(a.getName() == "a",""); PITYASSERT(a.getName() == "a", "b");
PITYASSERT(&(a.getRoot()) == &a,""); PITYASSERT(&(a.getRoot()) == &a, "c");
PITYASSERT(a.getParent() == nullptr,""); PITYASSERT(a.getParent() == nullptr, "d");
PITYASSERT(a.getChildren().size() == 0,""); PITYASSERT(a.getChildRefs().size() == 0, "e");
Node b{ "b", a }; // Create node here, and make it a child of another node
BNode b{ "b", a };
std::cout << b.getPath() << std::endl; std::cout << b.getPath() << std::endl;
PITYASSERT(a.isRoot() == true,""); PITYASSERT(a.isRoot() == true, "f");
PITYASSERT(&(a.getRoot()) == &a,""); PITYASSERT(&(a.getRoot()) == &a, "g");
PITYASSERT(a.getParent() == nullptr,""); PITYASSERT(a.getParent() == nullptr, "1");
PITYASSERT(a.getChildren().size() == 1,""); PITYASSERT(a.getChildRefs().size() == 1, "2");
PITYASSERT(&b == &(a.getChild("b")),"");
PITYASSERT_THROWS(a.getChild("invalid"),""); PITYASSERT(&b == &(a.getChild("b")), "3");
PITYASSERT_THROWS(a.getChild("invalid"), "4");
PITYASSERT(b.isRoot() == false,""); PITYASSERT(b.isRoot() == false, "5");
PITYASSERT(&(b.getRoot()) == &a,""); PITYASSERT(&(b.getRoot()) == &a, "6");
PITYASSERT(b.getParent() == &a,""); PITYASSERT(b.getParent() == &a, "7");
PITYASSERT(b.getChildren().size() == 0,""); PITYASSERT(b.getChildRefs().size() == 0, "8");
// Create a copy of the node in the parent node
b.addCopy(ANode("c")).addCopy(ANode("d"));
std::cout << a.to_string() << std::endl;
b.addCopy(BNode("c1")).addCopy(BNode("e"));
std::cout << a.to_string() << std::endl;
b.getChild("c1").getChild("e").addCopy(ANode(a), "a1");
std::cout << a.to_string() << std::endl;
a.getChild("b").addCopy(ANode(a), a.getName() + "1");
std::cout << a.to_string() << std::endl; std::cout << a.to_string() << std::endl;
} }

Loading…
Cancel
Save