Skip to content

Commit

Permalink
Test operator delete of 'class gc' in treetest
Browse files Browse the repository at this point in the history
* tests/tree.cc (Tree): Declare destructor (`~Tree()`).
* tests/tree.cc (Tree::~Tree): Implement.
* tests/tree.cc [!DEL_EVERY_N_TREE] (DEL_EVERY_N_TREE): Define macro.
* tests/tree.cc (main): Define `is_find_leak`, `trees_cnt` local
variables; do not print a message about leak detection mode; invoke
`delete tree` and `delete keep_tree`.
  • Loading branch information
ivmai committed Dec 8, 2024
1 parent 32b23b4 commit 9e27ff8
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions tests/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Tree : public GC_NS_QUALIFY(gc)
public:
GC_ATTR_EXPLICIT
Tree(int a, int d);
~Tree();
void verify();

private:
Expand Down Expand Up @@ -54,6 +55,20 @@ Tree::Tree(int a, int d) : arity(a), depth(d)
GC_reachable_here(nodes);
}

Tree::~Tree()
{
if (depth > 0) {
for (int i = 0; i < arity; i++) {
delete m_nodes[i];
}
#ifdef GC_OPERATOR_NEW_ARRAY
gc::operator delete[](m_nodes);
#else
GC_FREE(m_nodes);
#endif
}
}

void
Tree::verify()
{
Expand Down Expand Up @@ -98,6 +113,10 @@ Tree::verify(int a, int d)
# define MAX_DEPTH 9
#endif

#ifndef DEL_EVERY_N_TREE
# define DEL_EVERY_N_TREE 7
#endif

int
main(void)
{
Expand All @@ -107,21 +126,25 @@ main(void)
#ifndef CPPCHECK
GC_INIT();
#endif
if (GC_get_find_leak())
printf("This test program is not designed for leak detection mode\n");
int is_find_leak = GC_get_find_leak();
#ifndef NO_INCREMENTAL
GC_enable_incremental();
#endif

Tree *keep_tree = new (USE_GC) Tree(MAX_ARITY, MAX_DEPTH);
keep_tree->verify();
int trees_cnt = 0;
for (int arity = 2; arity <= MAX_ARITY; arity++) {
for (int depth = 1; depth <= MAX_DEPTH; depth++) {
Tree *tree = new (USE_GC) Tree(arity, depth);
tree->verify();
if (is_find_leak || ++trees_cnt % DEL_EVERY_N_TREE == 0)
delete tree;
}
}
keep_tree->verify(); // recheck
if (is_find_leak)
delete keep_tree;
printf("SUCCEEDED\n");
return 0;
}

0 comments on commit 9e27ff8

Please sign in to comment.