implemented new working destructor for tree
[feisty_meow.git] / nucleus / library / tests_nodes / test_symbol_tree.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : test_symbol_tree                                                  *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *  Purpose:                                                                   *
7 *                                                                             *
8 *    Creates a symbol_tree and performs some operations on it to assure       *
9 *  basic functionality.                                                       *
10 *                                                                             *
11 *******************************************************************************
12 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
13 * redistribute it and/or modify it under the terms of the GNU General Public  *
14 * License as published by the Free Software Foundation; either version 2 of   *
15 * the License or (at your option) any later version.  This is online at:      *
16 *     http://www.fsf.org/copyleft/gpl.html                                    *
17 * Please send any updates to: fred@gruntose.com                               *
18 \*****************************************************************************/
19
20 #include <application/hoople_main.h>
21 #include <basis/astring.h>
22 #include <basis/functions.h>
23 #include <basis/guards.h>
24 #include <loggers/program_wide_logger.h>
25 #include <mathematics/chaos.h>
26 #include <nodes/symbol_tree.h>
27 #include <structures/static_memory_gremlin.h>
28 #include <textual/string_manipulation.h>
29 #include <unit_test/unit_base.h>
30
31 using namespace application;
32 using namespace basis;
33 using namespace filesystem;
34 using namespace loggers;
35 using namespace mathematics;
36 using namespace nodes;
37 using namespace structures;
38 using namespace textual;
39 using namespace timely;
40 using namespace unit_test;
41
42 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
43
44 //#define DEBUG_TEST_SYMBOL_TREE
45
46 // how many nodes we add to the tree.
47 const int MAX_NODES_TESTED = 40000;
48
49 class test_symbol_tree : public unit_base, public application_shell
50 {
51 public:
52   test_symbol_tree() : unit_base() {}
53   DEFINE_CLASS_NAME("test_symbol_tree");
54   int execute();
55 };
56
57 int test_symbol_tree::execute()
58 {
59   FUNCDEF("execute");
60
61   try {
62     // creates a crazy tree with only one branch per node, but hugely deep.
63     symbol_tree *t = new symbol_tree("blork");
64     symbol_tree *curr = t;
65     for (int i = 0; i < MAX_NODES_TESTED; i++) {
66       // if the current node has any branches, we'll jump on one as the next
67       // place.
68       if (curr->branches()) {
69         // move to a random branch.
70         int which = randomizer().inclusive(0, curr->branches() - 1);
71         curr = (symbol_tree *)curr->branch(which);
72       }
73       astring rando = string_manipulation::make_random_name(1, 10);
74       curr->add(new symbol_tree(rando));
75     }
76 #ifdef DEBUG_TEST_SYMBOL_TREE
77     LOG("about to whack dynamic tree...");
78 #endif
79     WHACK(t);
80     ASSERT_EQUAL(t, NULL_POINTER, "ensure pointer cleaned up");
81 #ifdef DEBUG_TEST_SYMBOL_TREE
82     LOG("dynamic tree whacked.");
83 #endif
84   } catch (...) {
85 #ifdef DEBUG_TEST_SYMBOL_TREE
86     LOG("crashed during tree stuffing.");
87 #endif
88     return 1;
89   }
90
91   ASSERT_TRUE(true, "testing succeeded without cleanup crashes");
92
93
94
95 //hmmm: need more tests, like where we create a more balanced tree structure...
96 //      perform known operations and validate shape of tree.
97
98   return final_report();
99 }
100
101 //////////////
102
103 HOOPLE_MAIN(test_symbol_tree, )
104