From 9e27ff8c9bb3fe77b755f211a5af229afe9281c3 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Sun, 8 Dec 2024 14:15:37 +0300 Subject: [PATCH] Test operator delete of 'class gc' in treetest * 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`. --- tests/tree.cc | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/tree.cc b/tests/tree.cc index 8730debf6..723de0ce0 100644 --- a/tests/tree.cc +++ b/tests/tree.cc @@ -25,6 +25,7 @@ class Tree : public GC_NS_QUALIFY(gc) public: GC_ATTR_EXPLICIT Tree(int a, int d); + ~Tree(); void verify(); private: @@ -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() { @@ -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) { @@ -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; }