c++ - UnitTest failing only when all tests get executed at once -
i'm facing odd problem when running microsoft c++ unittest
within vs2013.
the test failing each time execute run all
tests in ide, when use run selected tests
on specific one, succeeds.
what i'm testing usage of own binarysearchtree
class. in case of failure, exception code c0000005
gets returned (this may due wrong internal linkage of pointers).
so can reason this?
additional
the test failing on last piece of code, generate random id strings, add them individually bst
, remove them randomly till whole bst
empty again.
binarysearchtree clientlist; client client; // test random adding & deletion std::vector<std::string> idaddlist; (unsigned int = 0; < 100; i++) idaddlist.push_back(randomid(20)); std::vector<std::string> idremovelist; unsigned int id; while (idaddlist.size() > 0) { // pick random id list , delete id = rand() % idaddlist.size(); client.uniqueid = idaddlist.at(id); clientlist.add(client); if (clientlist.lasticlresult() == ilist::iclresult::ok) idremovelist.push_back(idaddlist.at(id)); idaddlist.erase(idaddlist.begin() + id); } while (idremovelist.size() > 0) { // pick random client list , delete id = rand() % idremovelist.size(); clientlist.remove(idremovelist.at(id)); if (clientlist.lasticlresult() == ilist::iclresult::ok) idremovelist.erase(idremovelist.begin() + id); } assert::areequal<unsigned int>(0, clientlist.size());
here code randomid()
function:
std::string randomid(const unsigned int maxlength) { static const char alphanum[] = "0123456789" "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz" "öÖäÄüÜ" "!=[]$%&"; std::string result; (unsigned int = 0; < maxlength; i++) result += alphanum[rand() % (sizeof(alphanum - 1))]; return result; }
relevant class-specific code problem removematch()
function , according node
class. bst
takes client
class data object add()
function , adds given client if id string isn't in bst
. internal there functions fetchminnode()
, fetchmaxnode()
, either retrieve pointer left-most or right-most node in bst
. main/root node called m_rootnode
, get's assigned nullptr
if bst
gets empty.
in case of removematch()
function, go right child node/subtree of deleted node, , - if possible - reassign internal node tree on specific place.
void ilist::removematch(node* tobedeletednode) { if (tobedeletednode) { // in case given node root node if (!tobedeletednode->parent) { // case 1: root node node in bst if (!tobedeletednode->left && !tobedeletednode->right) handlerootleaf(tobedeletednode); // case 2: either both child nodes available or right // child node available else if ((tobedeletednode->left && tobedeletednode->right) || tobedeletednode->right) handlerootrsubtree(tobedeletednode); // case 3: left child node present else handlerootlsubtree(tobedeletednode); } // in case given node child node else { // case 1: if given node leaf node if (!tobedeletednode->left && !tobedeletednode->right) handlechildleaf(tobedeletednode); // case 2: if given node has either both child nodes or right child node else if ((tobedeletednode->left && tobedeletednode->right) || tobedeletednode->right) handlechildrsubtree(tobedeletednode); // case 3: if given node has left child node else handlechildlsubtree(tobedeletednode); } m_elementcount--; } }
and here according private functions:
[private] handlerootlsubtree()
void handlerootlsubtree(node* tobedeletednode) { // fetch max node in left subtree node* maxnode = fetchmaxnode(tobedeletednode->left); // in case left child node max node if (maxnode->parent != tobedeletednode) { if (maxnode->left) { maxnode->left->parent = maxnode->parent; maxnode->parent->right = maxnode->left; } else maxnode->parent->right = nullptr; maxnode->left = tobedeletednode->left; maxnode->parent->parent = maxnode; } maxnode->parent = nullptr; // cut linkage tobedeletednode->left = nullptr; delete tobedeletednode; m_rootnode = maxnode; }
[private] handlerootrsubtree()
void handlerootrsubtree(node* tobedeletednode) { // fetch min node in right subtree node* minnode = fetchminnode(tobedeletednode->right); // in case right child node isn't min node if (minnode->parent != tobedeletednode) { if (minnode->right) { minnode->right->parent = minnode->parent; minnode->parent->left = minnode->right; } else minnode->parent->left = nullptr; minnode->right = tobedeletednode->right; tobedeletednode->right->parent = minnode; } minnode->left = tobedeletednode->left; minnode->parent = nullptr; if (tobedeletednode->left) tobedeletednode->left->parent = minnode; // cut linkage tobedeletednode->left = nullptr; tobedeletednode->right = nullptr; delete tobedeletednode; m_rootnode = minnode; }
[private] handlerootleaf()
void handlerootleaf(node* tobedeletednode) { delete m_rootnode; m_rootnode = nullptr; }
[private] handlechildlsubtree()
void handlechildlsubtree(node* tobedeletednode) { // fetch max node in left subtree node* maxnode = fetchmaxnode(tobedeletednode->left); // in case left child node max node if (maxnode->parent == tobedeletednode) maxnode->parent = tobedeletednode->parent; else { if (maxnode->left) { maxnode->left->parent = maxnode->parent; maxnode->parent->right = maxnode->left; } else maxnode->parent->right = nullptr; maxnode->left = tobedeletednode->left; tobedeletednode->left->parent = maxnode; maxnode->parent = tobedeletednode->parent; } if (tobedeletednode->parent->left && tobedeletednode->parent->left == tobedeletednode) tobedeletednode->parent->left = maxnode; else tobedeletednode->parent->right = maxnode; // cut linkage tobedeletednode->left = nullptr; tobedeletednode->right = nullptr; delete tobedeletednode; }
[private] handlechildrsubtree()
void handlechildrsubtree(node* tobedeletednode) { // fetch min node in right subtree node* minnode = fetchminnode(tobedeletednode->right); // in case right child node min node if (minnode->parent == tobedeletednode) { if (tobedeletednode->left) { minnode->left = tobedeletednode->left; tobedeletednode->left->parent = minnode; } minnode->parent = tobedeletednode->parent; } else { if (minnode->right) { minnode->right->parent = minnode->parent; minnode->parent->left = minnode->right; } else minnode->parent->left = nullptr; minnode->left = tobedeletednode->left; if (tobedeletednode->left) tobedeletednode->left->parent = minnode; minnode->right = tobedeletednode->right; tobedeletednode->right->parent = minnode; minnode->parent = tobedeletednode->parent; } if (tobedeletednode->parent->left && tobedeletednode->parent->left == tobedeletednode) tobedeletednode->parent->left = minnode; else tobedeletednode->parent->right = minnode; // cut linkage tobedeletednode->left = nullptr; tobedeletednode->right = nullptr; delete tobedeletednode; }
[private] handlechildleaf()
void handlechildleaf(node* tobedeletednode) { if (tobedeletednode->parent->left && tobedeletednode->parent->left == tobedeletednode) tobedeletednode->parent->left = nullptr; else tobedeletednode->parent->right = nullptr; delete tobedeletednode; }
node
class:
struct node { node() : client(nullptr), parent(nullptr), left(nullptr), right(nullptr) {} ~node(); client* client; node* parent; node* left; node* right; };
the cause of error lies within handlerootlsubtree()
function:
the line
maxnode->parent->parent = maxnode;
has replaced with
tobedeletednode->left->parent = maxnode;
Comments
Post a Comment