feisty meow concerns codebase 2.140
test_symbol_tree.cpp
Go to the documentation of this file.
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
21#include <basis/astring.h>
22#include <basis/functions.h>
23#include <basis/guards.h>
25#include <mathematics/chaos.h>
26#include <nodes/symbol_tree.h>
29#include <unit_test/unit_base.h>
30
31using namespace application;
32using namespace basis;
33using namespace filesystem;
34using namespace loggers;
35using namespace mathematics;
36using namespace nodes;
37using namespace structures;
38using namespace textual;
39using namespace timely;
40using 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//const int MAX_NODES_TESTED = 4200;
49const int MAX_NODES_TESTED = 420;
50
51class test_symbol_tree : public unit_base, public application_shell
52{
53public:
54 test_symbol_tree() : unit_base() {}
55 DEFINE_CLASS_NAME("test_symbol_tree");
56 int execute();
57};
58
59int test_symbol_tree::execute()
60{
61 FUNCDEF("execute");
62
63 try {
64 // creates a crazy tree with only one branch per node, but hugely deep.
65 symbol_tree *t = new symbol_tree("blork");
66 symbol_tree *curr = t;
67 for (int i = 0; i < MAX_NODES_TESTED; i++) {
68 // if the current node has any branches, we'll jump on one as the next
69 // place.
70 if (curr->branches()) {
71 // move to a random branch.
72 int which = randomizer().inclusive(0, curr->branches() - 1);
73 curr = (symbol_tree *)curr->branch(which);
74 }
76 curr->add(new symbol_tree(rando));
77 }
78#ifdef DEBUG_TEST_SYMBOL_TREE
79 LOG("about to whack dynamic tree...");
80#endif
81 WHACK(t);
82 ASSERT_EQUAL(t, NULL_POINTER, "ensure pointer cleaned up");
83#ifdef DEBUG_TEST_SYMBOL_TREE
84 LOG("dynamic tree whacked.");
85#endif
86 } catch (...) {
87#ifdef DEBUG_TEST_SYMBOL_TREE
88 LOG("crashed during tree stuffing.");
89#endif
90 return 1;
91 }
92
93 ASSERT_TRUE(true, "testing succeeded without cleanup crashes");
94
95
96
97//hmmm: need more tests, like where we create a more balanced tree structure...
98// perform known operations and validate shape of tree.
99
100 return final_report();
101}
102
104
105HOOPLE_MAIN(test_symbol_tree, )
106
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
A symbol table that supports scope nesting and/or trees of symbol tables.
Definition symbol_tree.h:37
bool add(symbol_tree *to_add)
adds a child to this symbol_tree.
symbol_tree * branch(int index) const
returns the "index"th branch.
virtual int branches() const
Returns the number of branches currently connected to this tree.
Definition tree.cpp:431
static basis::astring make_random_name(int min=1, int max=64)
creates a random name, where the letters are between 'a' and 'z'.
#define NULL_POINTER
The value representing a pointer to nothing.
Definition definitions.h:32
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition functions.h:121
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
#define randomizer()
chaos rando
const int MAX_NODES_TESTED
#define LOG(to_print)
#define ASSERT_EQUAL(a, b, test_name)
Definition unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition unit_base.h:46