t_tree.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_tree                                                         *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Tests out the tree class.                                                *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 1993-$now By Author.  This program is free software; you can  *
00012 * redistribute it and/or modify it under the terms of the GNU General Public  *
00013 * License as published by the Free Software Foundation; either version 2 of   *
00014 * the License or (at your option) any later version.  This is online at:      *
00015 *     http://www.fsf.org/copyleft/gpl.html                                    *
00016 * Please send any updates to: fred@gruntose.com                               *
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 //#define DEBUG_TEST_TREE
00032   // uncomment if you want the noisy streams version.
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 // forward.
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   // 5 is the magic knowledge of minimum packed string.
00095 //hmmm: make the minimum packed size a property of packables?
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 bogotre *togen(char *to_store)
00122 { bogotre *to_return = new bogotre(istring(to_store).s()); return to_return; }
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 //remove it again if we reenable the cut.
00137 #endif
00138 //  if (to_examine == to_look_for) real_curr->cut();
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 #ifdef DEBUG_TEST_TREE
00218   printf("the following dumps are in the order: infix, prefix, postfix.\n\n");
00219   printf("now trying cut on the character '>':\n");
00220 #endif
00221   to_look_for = ">";
00222   new_e9->apply(&print_node, tree::infix);
00223 #ifdef DEBUG_TEST_TREE
00224   p("\n");
00225 #endif
00226   new_e9->apply(&print_node, tree::prefix);
00227 #ifdef DEBUG_TEST_TREE
00228   p("\n");
00229 #endif
00230   new_e9->apply(&print_node, tree::postfix);
00231 #ifdef DEBUG_TEST_TREE
00232   p("\nnow trying cut on the character +:\n");
00233 #endif
00234   to_look_for = "+";
00235   new_e9->apply(&print_node, tree::infix);
00236 #ifdef DEBUG_TEST_TREE
00237   p("\n");
00238 #endif
00239   new_e9->apply(&print_node, tree::prefix);
00240 #ifdef DEBUG_TEST_TREE
00241   p("\n");
00242 #endif
00243   new_e9->apply(&print_node, tree::postfix);
00244 #ifdef DEBUG_TEST_TREE
00245   p("\n");
00246 #endif
00247   to_look_for = "";
00248 
00249 #ifdef DEBUG_TEST_TREE
00250   p("okay, trying to resume at -\n");
00251 #endif
00252   e5->resume(&print_node, tree::infix);
00253 #ifdef DEBUG_TEST_TREE
00254   p("\n");
00255 #endif
00256   e5->resume(&print_node, tree::prefix);
00257 #ifdef DEBUG_TEST_TREE
00258   p("\n");
00259 #endif
00260   e5->resume(&print_node, tree::postfix);
00261 #ifdef DEBUG_TEST_TREE
00262   p("\n");
00263 #endif
00264 */
00265 #ifdef DEBUG_TEST_TREE
00266   printf("deleting\n");
00267 #endif
00268   delete e9;
00269 /*
00270 printf("second pack\n");
00271   byte_array second_pack;
00272 printf("packing\n");
00273   new_e9->pack(second_pack);
00274 #ifdef DEBUG_TEST_TREE
00275   printf("after second pack, size is %d\n", size);
00276 #endif
00277 */
00278   delete new_e9;
00279 /*
00280   larch *newest_e9 = new larch(SELF_CLEANING);
00281   newest_e9->unpack(second_pack);
00282 #ifdef DEBUG_TEST_TREE
00283   printf("after second unpack... tree is (infix):\n");
00284 #endif
00285   newest_e9->apply(print_node, tree::infix);
00286   delete newest_e9;
00287 #ifdef DEBUG_TEST_TREE
00288   p("\n");
00289 #endif
00290 */
00291   }
00292   guards::alert_message("tree:: works for those functions tested.");
00293   return 0;
00294 }
00295 
00296 HOOPLE_MAIN(test_tree, )
00297 

Generated on Fri Nov 28 04:29:39 2008 for HOOPLE Libraries by  doxygen 1.5.1