00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <basis/function.h>
00020 #include <basis/guards.h>
00021 #include <basis/istring.h>
00022 #include <basis/packable.h>
00023 #include <nodes/node.h>
00024 #include <nodes/tree.h>
00025 #include <opsystem/application_shell.h>
00026 #include <loggers/console_logger.h>
00027 #include <data_struct/static_memory_gremlin.h>
00028
00029 using namespace nodes;
00030
00031
00032
00033
00034 const int test_iterations = 20;
00035
00036 class bogotre : public packable, public tree
00037 {
00038 public:
00039 bogotre(const char *start = NIL) : who_cares(42), i_sure_dont('l'),
00040 another_useless_int(23) {
00041 istring to_init(start);
00042 if (to_init.length() < 1) to_init += "ack";
00043 to_init.stuff(the_actual_string, minimum(to_init.length()+1, 500));
00044 }
00045 IMPLEMENT_CLASS_NAME("bogotre");
00046 virtual ~bogotre() {}
00047 virtual void pack(byte_array &packed_form) const;
00048 virtual bool unpack(byte_array &to_unpack);
00049 virtual byte *held() const { return (byte *)the_actual_string; }
00050 virtual void print() const {
00051 #ifdef DEBUG_TEST_TREE
00052 printf(the_actual_string);
00053 #endif
00054 }
00055
00056 private:
00057 char the_actual_string[500];
00058 int who_cares;
00059 char i_sure_dont;
00060 int another_useless_int;
00061 };
00062
00064
00065
00066 typedef bogotre larch;
00067 typedef void (applier)(larch *apply_to);
00068 typedef tree::iterator traveller;
00069
00070 class test_tree : public application_shell
00071 {
00072 public:
00073 test_tree() : application_shell(class_name()) {}
00074 IMPLEMENT_CLASS_NAME("test_tree");
00075 virtual int execute();
00076 static void print_node(larch *curr_node);
00077 static larch *next(larch *&move, larch *hook, traveller &skip);
00078 static void apply(larch *apply_to, applier *to_apply,
00079 tree::traversal_directions order);
00080 };
00081
00083
00084 void bogotre::pack(byte_array &packed_form) const
00085 {
00086 istring(the_actual_string).pack(packed_form);
00087 basis::attach(packed_form, who_cares);
00088 basis::attach(packed_form, i_sure_dont);
00089 basis::attach(packed_form, another_useless_int);
00090 }
00091
00092 bool bogotre::unpack(byte_array &packed_form)
00093 {
00094
00095
00096 if (packed_form.length() < int(1 + sizeof(who_cares) + sizeof(i_sure_dont)
00097 + sizeof(another_useless_int)))
00098 deadly_error(class_name(), "bogotre_unpack", "size of package is too small");
00099 istring unpacked;
00100 if (!unpacked.unpack(packed_form))
00101 deadly_error(class_name(), "bogotre_unpack", "couldn't retrieve string");
00102 if (!basis::detach(packed_form, who_cares))
00103 deadly_error(class_name(), "bogotre_unpack", "couldn't retrieve who_cares");
00104 if (!basis::detach(packed_form, i_sure_dont))
00105 deadly_error(class_name(), "bogotre_unpack", "couldn't retrieve i_sure_dont");
00106 if (!basis::detach(packed_form, another_useless_int))
00107 deadly_error(class_name(), "bogotre_unpack", "couldn't retrieve another_...");
00108
00109 if (who_cares != 42)
00110 deadly_error(class_name(), "bogotre_unpack", "wrong value held in first int");
00111 if (i_sure_dont != 'l')
00112 deadly_error(class_name(), "bogotre_unpack", "wrong character held");
00113 if (another_useless_int != 23)
00114 deadly_error(class_name(), "bogotre_unpack", "wrong value held in second int");
00115 return true;
00116 }
00117
00119
00120
00121
00122
00123
00124
00125 void test_tree::print_node(larch *curr_node)
00126 {
00127 if (!curr_node)
00128 deadly_error(test_tree::static_class_name(), "print_node", "nil tree");
00129 bogotre *real_curr = dynamic_cast<bogotre *>(curr_node);
00130 if (!real_curr)
00131 deadly_error(test_tree::static_class_name(), "print_node", "nil contents");
00132 istring to_examine((char *)real_curr->held());
00133 #ifdef DEBUG_TEST_TREE
00134 to_examine += " ";
00135 printf(to_examine.s());
00136
00137 #endif
00138
00139 }
00140
00141 larch *test_tree::next(larch *&move, larch *formal(hook), traveller &skip)
00142 { move = dynamic_cast<larch *>(skip.next()); return move; }
00143
00144 void test_tree::apply(larch *apply_to, applier *to_apply,
00145 tree::traversal_directions order)
00146 {
00147 larch *curr = NIL;
00148 for (traveller skippy = apply_to->start(order);
00149 next(curr, apply_to, skippy); ) to_apply(curr);
00150 }
00151
00152 int test_tree::execute()
00153 {
00154 for (int qq = 0; qq < test_iterations; qq++) {
00155 larch *e1 = new larch("a");
00156 larch *e2 = new larch("b");
00157 larch *e3 = new larch("+");
00158 e3->attach(e1);
00159 e3->attach(e2);
00160
00161 larch *e4 = new larch("c");
00162 larch *e5 = new larch("-");
00163 e5->attach(e3);
00164 e5->attach(e4);
00165
00166 larch *e6 = new larch(">");
00167 larch *e7 = new larch("23");
00168 e6->attach(e5);
00169 e6->attach(e7);
00170
00171 larch *e8 = new larch("d");
00172 larch *e9 = new larch("=");
00173 e9->attach(e8);
00174 e9->attach(e6);
00175
00176 #ifdef DEBUG_TEST_TREE
00177 printf("infix is ");
00178 #endif
00179 apply(e9, print_node, tree::infix);
00180 #ifdef DEBUG_TEST_TREE
00181 printf("\nprefix is ");
00182 #endif
00183 apply(e9, print_node, tree::prefix);
00184 #ifdef DEBUG_TEST_TREE
00185 printf("\npostfix is ");
00186 #endif
00187 apply(e9, print_node, tree::postfix);
00188 #ifdef DEBUG_TEST_TREE
00189 printf("\n");
00190 printf("branches is ");
00191 #endif
00192 apply(e9, print_node, tree::to_branches);
00193 #ifdef DEBUG_TEST_TREE
00194 printf("\n");
00195 printf("branches reversed is ");
00196 #endif
00197 apply(e9, print_node, tree::reverse_branches);
00198 #ifdef DEBUG_TEST_TREE
00199 printf("\n");
00200 printf("before first pack");
00201 #endif
00202 byte_array packed_e9(0);
00203 e9->pack(packed_e9);
00204 #ifdef DEBUG_TEST_TREE
00205 printf("after first pack, size is %d\n", packed_e9.length());
00206 #endif
00207 larch *new_e9 = new larch();
00208 new_e9->unpack(packed_e9);
00209 #ifdef DEBUG_TEST_TREE
00210 printf("New tree after unpacking is (infix order):\n");
00211 #endif
00212 apply(new_e9, print_node, tree::infix);
00213 #ifdef DEBUG_TEST_TREE
00214 printf("\n");
00215 #endif
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 #ifdef DEBUG_TEST_TREE
00266 printf("deleting\n");
00267 #endif
00268 delete e9;
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 delete new_e9;
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 }
00292 guards::alert_message("tree:: works for those functions tested.");
00293 return 0;
00294 }
00295
00296 HOOPLE_MAIN(test_tree, )
00297