Base method called in Google Test

纵然是瞬间 提交于 2019-12-11 09:06:18

问题


I am trying to write a binary tree implementation in C++ and I'm testing it using Google Test. In order to test the in-order traversal I am sub-classing the BTree class so that I can override the visit() method so that I can output to a string instead of to the console. The problem is that I want to use the existing logic to insert new nodes to the tree and it's inserting the base class instead of the derived class even though I am passing a pointer to a derived object to the insert() method. Is there a way to get it to insert the derived class for all the nodes?

Here's the test case:

TEST_F(BTreeTestSuite, inOrder)
{
    class InOrderTest : public BTree
    {
    public:
        InOrderTest(int data) throw(int) : BTree(data), itsVisitString() {};
        virtual ~InOrderTest() {};
        std::string visitString(void) const { return itsVisitString; }
        virtual void visit()
        {
            std::ostringstream oss;
            oss << itsData;
            itsVisitString += oss.str();
            std::cerr << "vs1: " << itsVisitString << '|' << std::endl;
            itsVisitString += " ";
            std::cerr << "vs2: " << itsVisitString << '|' << std::endl;
        }
    private:
        std::string itsVisitString;
    };
    InOrderTest iot(20);
    iot.insert(new InOrderTest(30));
    iot.insert(new InOrderTest(15));
    iot.insert(new InOrderTest(10));
    iot.inOrder();
    EXPECT_STREQ("10 15 20 30 ", iot.visitString().c_str());
}

Here's the relevant portions of the base class:

class BTree
{
public:
    BTree(int data) throw();        // constructor(s)
    ~BTree() throw();               // destructor

    virtual void insert(BTree *node);
    unsigned count() const;
    void inOrder();
    virtual void visit();

    int   data() const throw() { return itsData; };
    BTree *left() const throw() { return itsLeft; };
    BTree *right() const throw() { return itsRight; };

protected:
    int     itsData;
private:
    // Don't allow creation of BTree without data
    BTree() throw();        // constructor(s)
    BTree   *itsLeft;
    BTree   *itsRight;
};
...
BTree::BTree(int data) throw() : itsData(data)
{
    itsLeft = itsRight = 0;
}

void BTree::insert(BTree *node)
{
    if (node->itsData < itsData)
    {
        std::cerr << "Inserting data on the left\n";
        if (itsLeft)
        {
            itsLeft->insert(node);
        }
        else
        {
            itsLeft = new BTree(node->itsData);
        }

    }

    if (node->itsData > itsData)
    {
        std::cerr << "Inserting data on the right\n";
        if (itsRight)
        {
            itsRight->insert(node);
        }
        else
        {
            itsRight = new BTree(node->itsData);
        }
    }

    /* Drop value if it already exists in tree. */
}

void BTree::inOrder()
{
    if (itsLeft) itsLeft->inOrder();
    visit();
    if (itsRight) itsRight->inOrder();
}

void BTree::visit()
{
    cout << "base-visit: " << itsData << endl;
}

回答1:


The problem is in your BTree::insert method. Even though it's getting a InOrderTest * you're creating an instance of BTree from that on these two lines:

void BTree::insert(BTree *node)
{
  // logic checks...
  itsLeft = new BTree(node->itsData);
  // more logic checks...
  itsRight = new BTree(node->itsData);
  //...
}

There are two approaches you can take to solve this. You can just use the node passed in instead of constructing a new object. In this case, your root node assumes ownership of its children and should take care of deallocating them as appropriate. Since your unittest is doing new InOrderTest with no matching delete anyway, this approach can work.

The second way is to create a virtual clone method and have your derive classes handle its own creation of itself.

class BTree
{
  // BTree stuff
public:
  virtual BTree* BTree::clone() const;
  // more stuff
};

class InOrderTest : public BTree
{
public:
  InOrderTest* clone() const { return new InOrderTest(*this); }
};

Also, if you expect client code using BTree to derive from it then make BTree::~BTree destructor virtual. (See Effective C++ item#7)



来源:https://stackoverflow.com/questions/16727037/base-method-called-in-google-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!