bc938d1d80eefbea49a430a403bd700fb878ca57
[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 //#include <stdio.h>
32 //#include <stdlib.h>
33
34 using namespace application;
35 using namespace basis;
36 using namespace filesystem;
37 using namespace loggers;
38 using namespace mathematics;
39 using namespace nodes;
40 using namespace structures;
41 using namespace textual;
42 using namespace timely;
43 using namespace unit_test;
44
45 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
46
47 #define DEBUG_SYMBOL_TREE
48
49 // how many nodes we add to the tree.
50 const int MAX_NODES_TESTED = 40000;
51
52 class test_symbol_tree : public unit_base, public application_shell
53 {
54 public:
55   test_symbol_tree() : unit_base() {}
56   DEFINE_CLASS_NAME("test_symbol_tree");
57   int execute();
58 };
59
60 int test_symbol_tree::execute()
61 {
62   FUNCDEF("execute");
63   LOG("please check memory usage and record it, then hit a key to start testing.");
64
65   try {
66     // creates a crazy tree with only one branch per node, but 40,000 deep.
67     symbol_tree t("blork");
68     symbol_tree *curr = &t;
69     for (int i = 0; i < MAX_NODES_TESTED; i++) {
70       // if the current node has any branches, we'll jump on one as the next
71       // place.
72       if (curr->branches()) {
73         // move to a random branch.
74         int which = randomizer().inclusive(0, curr->branches() - 1);
75         curr = (symbol_tree *)curr->branch(which);
76       }
77       astring rando = string_manipulation::make_random_name(1, 10);
78       curr->add(new symbol_tree(rando));
79     }
80     LOG("check memory usage now with full size.  then hit a key.");
81   } catch (...) {
82     LOG("crashed during tree stuffing.");
83     return 1;
84   }
85
86   LOG("check memory usage after the run.  then hit a key to end "
87       "the program.");
88
89 //create a more balanced tree structure...
90 //perform known operations and validate shape of tree.
91
92   return final_report();
93 }
94
95 //////////////
96
97 HOOPLE_MAIN(test_symbol_tree, )
98