t_directory_tree.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_directory_tree                                               *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Tests the directory_tree object on some well-known directories.          *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 2001-$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/string_array.h>
00022 #include <opsystem/application_shell.h>
00023 #include <loggers/console_logger.h>
00024 #include <opsystem/directory_tree.h>
00025 #include <opsystem/filename.h>
00026 #include <opsystem/filename_list.h>
00027 #include <data_struct/static_memory_gremlin.h>
00028 #include <textual/string_manipulation.h>
00029 
00030 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00031 
00032 class test_directory_tree : public application_shell
00033 {
00034 public:
00035   test_directory_tree() : application_shell(static_class_name()) {}
00036   IMPLEMENT_CLASS_NAME("test_directory_tree");
00037   int execute();
00038 };
00039 
00040 int test_directory_tree::execute()
00041 {
00042   FUNCDEF("execute");
00043 
00044 //temp junk!
00045 uint16 a = 23; // 10111
00046 uint16 b = 7;  // 00111
00047 if ((a ^ b) != 16) deadly_error("check", "check", "not working as expect");
00048 a = 0;
00049 b = 19;  // 10011
00050 if ((a ^ b) != 19) deadly_error("check", "check2", "not working as expect");
00051 
00052 
00053   // process the command line parameters, which are a directory name and
00054   // a pattern to use when scanning.
00055   istring path = "/usr/local";  // default path.
00056 #ifdef __WIN32__
00057   // default path for windoze.
00058   path = portable::env_string("COMMONPROGRAMFILES");
00059 #endif
00060   if (__argc >= 2)
00061     path = __argv[1];
00062 
00063   istring pattern = "*";
00064   if (__argc >= 3)
00065     pattern = __argv[2];
00066 
00067   {
00068     log(istring("Scanning directory tree at \"") + path + "\"");
00069     log(istring("Using pattern-match \"") + pattern + "\"");
00070 
00071     directory_tree dir(path, pattern.s());
00072     if (!dir.good())
00073       deadly_error(class_name(), "directory_tree construction",
00074           "the directory could not be read");
00075 
00076     dir_tree_iterator *ted = dir.start(directory_tree::prefix);
00077       // create our iterator to do a prefix traversal.
00078 
00079     int depth;  // current depth in tree.
00080     filename curr;  // the current path the iterator is at.
00081     string_array files;  // the filenames held at the iterator.
00082 
00083     while (directory_tree::current(*ted, curr, files)) {
00084       // we have a good directory to show.
00085       directory_tree::depth(*ted, depth);
00086       log(string_manipulation::indentation(depth * 2) + istring("[")
00087           + curr.raw() + "]");
00088       istring names;
00089       for (int i = 0; i < files.length(); i++) names += files[i] + " ";
00090       if (names.length()) {
00091         istring split;
00092         string_manipulation::split_lines(names, split, depth * 2 + 2);
00093         log(split);
00094       }
00095 
00096       // go to the next place.
00097       directory_tree::next(*ted);
00098     }
00099 
00100     directory_tree::throw_out(ted);
00101 
00102   }
00103 
00104   {
00105     // second test group.  seek operation.
00106 //scan the directory, create some temporary directories and junk filenames
00107 //therein, then seek to that location.
00108 
00109   }
00110 
00111   {
00112     // third test group.  tree comparison operation.
00113     log(istring("Self-comparing directory tree at \"") + path + "\"");
00114     log(istring("Using pattern-match \"") + pattern + "\"");
00115 
00116     LOG("reading tree 1.");
00117     directory_tree dir(path, pattern.s());
00118     if (!dir.good())
00119       deadly_error(class_name(), "directory_tree construction",
00120           "the directory could not be read");
00121 
00122     // now read a copy of the tree also.
00123     LOG("reading tree 2.");
00124     directory_tree dir2(path, pattern.s());
00125     if (!dir2.good())
00126       deadly_error(class_name(), "directory_tree construction",
00127           "the directory could not be read the second time?!");
00128 
00129     LOG("comparing the two trees.");
00130     filename_list diffs;
00131     directory_tree::compare_trees(dir, dir2, diffs);
00132 LOG(diffs.text_form());
00133 
00134     if (diffs.elements())
00135       deadly_error(class_name(), "directory_tree comparison",
00136           "there were differences when comparing identical directories!");
00137   }
00138 
00139   {
00140     // fourth test: see if the calculate function works.
00141     log(istring("Calculating sums for tree at \"") + path + "\"");
00142     log(istring("Using pattern-match \"") + pattern + "\"");
00143 
00144     LOG("reading tree 1.");
00145     directory_tree dir(path, pattern.s());
00146     if (!dir.good())
00147       deadly_error(class_name(), "directory_tree construction",
00148           "the directory could not be read");
00149 
00150     // now read a copy of the tree also.
00151     LOG("reading tree 2.");
00152     directory_tree dir2(path, pattern.s());
00153     if (!dir2.good())
00154       deadly_error(class_name(), "directory_tree construction",
00155           "the directory could not be read the second time?!");
00156 
00157     LOG("calculating checksums for tree 1.");
00158     if (!dir.calculate())
00159       deadly_error(class_name(), "directory_tree calculation",
00160           "the first tree could not be calculated");
00161 
00162     LOG("calculating checksums for tree 2.");
00163     if (!dir2.calculate())
00164       deadly_error(class_name(), "directory_tree calculation",
00165           "the second tree could not be calculated");
00166 
00167     LOG("comparing the two trees.");
00168     filename_list diffs;
00169     directory_tree::compare_trees(dir, dir2, diffs);
00170 LOG(diffs.text_form());
00171 
00172     if (diffs.elements())
00173       deadly_error(class_name(), "directory_tree comparison",
00174           "there were differences when comparing identical directories!");
00175   }
00176 
00177   {
00178     // fifth test: see if the packing works.
00179     log(istring("Reading tree for packing at \"") + path + "\"");
00180     log(istring("Using pattern-match \"") + pattern + "\"");
00181 
00182     LOG("reading tree.");
00183     directory_tree dir(path, pattern.s());
00184     if (!dir.good())
00185       deadly_error(class_name(), "directory_tree construction",
00186           "the directory could not be read");
00187 
00188     LOG("calculating checksums for tree.");
00189     if (!dir.calculate())
00190       deadly_error(class_name(), "directory_tree calculation",
00191           "the first tree could not be calculated");
00192 
00193     byte_array packed_form;
00194     dir.pack(packed_form);
00195 LOG(isprintf("tree became %d byte array", packed_form.length()));
00196 
00197     directory_tree dir2;
00198     if (!dir2.unpack(packed_form)) 
00199       deadly_error(class_name(), "directory_tree calculation",
00200           "the second tree could not be unpacked from the first");
00201 
00202     LOG("comparing the two trees.");
00203     filename_list diffs;
00204     directory_tree::compare_trees(dir, dir2, diffs);
00205 LOG(diffs.text_form());
00206 
00207     if (diffs.elements())
00208       deadly_error(class_name(), "directory_tree comparison",
00209           "there were differences when comparing identical directories!");
00210 
00211     directory_tree::compare_trees(dir2, dir, diffs);
00212     if (diffs.elements())
00213       deadly_error(class_name(), "directory_tree comparison",
00214           "there were differences when reverse comparing identical dirs!");
00215 
00216   }
00217 
00218 // nth test:
00219 // combine the results of the second test with a comparison like in the
00220 // third test.  delete all of those temporary files that were added.
00221 // rescan tree. make sure that a tree containing the temporaries
00222 // when compared with the current post-deletion tree produces a list
00223 // that contains all the temporary files and directories.
00224 
00225 
00226 //more tests!
00227 
00228   guards::alert_message("directory_tree:: works for those functions tested.");
00229   return 0;
00230 }
00231 
00232 HOOPLE_MAIN(test_directory_tree, )
00233 

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