t_filename.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_filename                                                     *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1993-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #define DEBUG_FILENAME_TEST
00016 
00017 #include <basis/function.h>
00018 #include <basis/guards.h>
00019 #include <basis/istring.h>
00020 #include <basis/string_array.h>
00021 #include <opsystem/application_shell.h>
00022 #include <loggers/console_logger.h>
00023 #include <opsystem/filename.h>
00024 #include <data_struct/static_memory_gremlin.h>
00025 
00026 class test_filename : public application_shell
00027 {
00028 public:
00029   test_filename() : application_shell(class_name()) {}
00030   IMPLEMENT_CLASS_NAME("test_filename");
00031   virtual int execute();
00032   void clean_sequel(istring &sequel);
00033 };
00034 
00035 void test_filename::clean_sequel(istring &sequel)
00036 { sequel.replace_all('\\', '/'); }
00037 
00038 int test_filename::execute()
00039 {
00040   {
00041     // first test group.
00042     const char *GROUP = "first: test exists";
00043     filename gorgeola("");
00044     if (gorgeola.exists())
00045       deadly_error(class_name(), GROUP, "an empty filename exists??");
00046   }
00047 
00048   {
00049     // second test group.
00050     const char *GROUP = "second: test separate";
00051     filename turkey("/omega/ralph/turkey/buzzard.txt");
00052     string_array pieces;
00053     turkey.separate(pieces);
00054     if (pieces[1] != "omega")
00055       deadly_error(class_name(), GROUP, "the first piece didn't match.");
00056     if (pieces[2] != "ralph")
00057       deadly_error(class_name(), GROUP, "the second piece didn't match.");
00058     if (pieces[3] != "turkey")
00059       deadly_error(class_name(), GROUP, "the third piece didn't match.");
00060     if (pieces[4] != "buzzard.txt")
00061       deadly_error(class_name(), GROUP, "the fourth piece didn't match.");
00062     if (pieces.length() != 5)
00063       deadly_error(class_name(), GROUP, "the list was the wrong length");
00064   }
00065 
00066   {
00067     // third test group.
00068     const char *GROUP = "third: test compare_prefix";
00069     filename turkey("/omega/ralph/turkey/buzzard.txt");
00070     filename murpin1("/omega");
00071     filename murpin2("/omega/ralph");
00072     filename murpin3("/omega/ralph/turkey");
00073     filename murpin4("/omega/ralph/turkey/buzzard.txt");
00074     filename murpin_x1("ralph/turkey/buzzard.txt");
00075     filename murpin_x2("/omega/ralph/turkey/buzzard.txt2");
00076     filename murpin_x3("/omega/turkey/buzzard.txt");
00077     filename murpin_x4("/omega/ralph/turkey/b0/buzzard.txt");
00078     filename murpin_x5("moomega/ralph/turkey");
00079 
00080     istring sequel;
00081     if (!murpin1.compare_prefix(turkey, sequel))
00082       deadly_error(class_name(), GROUP, "first should match but didn't");
00084     clean_sequel(sequel);
00086     if (sequel != "ralph/turkey/buzzard.txt")
00087       deadly_error(class_name(), GROUP, "first sequel was wrong");
00088     if (!murpin2.compare_prefix(turkey, sequel))
00089       deadly_error(class_name(), GROUP, "second should match but didn't");
00090     clean_sequel(sequel);
00091     if (sequel != "turkey/buzzard.txt")
00092       deadly_error(class_name(), GROUP, "second sequel was wrong");
00093     if (!murpin3.compare_prefix(turkey, sequel))
00094       deadly_error(class_name(), GROUP, "third should match but didn't");
00095     clean_sequel(sequel);
00096     if (sequel != "buzzard.txt")
00097       deadly_error(class_name(), GROUP, "third sequel was wrong");
00098     if (!murpin4.compare_prefix(turkey, sequel))
00099       deadly_error(class_name(), GROUP, "fourth should match but didn't");
00100     if (sequel.t())
00101       deadly_error(class_name(), GROUP, "fourth had a sequel but shouldn't");
00102 
00103     if (murpin_x1.compare_prefix(turkey, sequel))
00104       deadly_error(class_name(), GROUP, "x-first should not match but did");
00105     if (sequel.t())
00106       deadly_error(class_name(), GROUP, "x-first had a sequel but shouldn't");
00107     if (murpin_x2.compare_prefix(turkey, sequel))
00108       deadly_error(class_name(), GROUP, "x-second should not match but did");
00109     if (sequel.t())
00110       deadly_error(class_name(), GROUP, "x-second had a sequel but shouldn't");
00111     if (murpin_x3.compare_prefix(turkey, sequel))
00112       deadly_error(class_name(), GROUP, "x-third should not match but did");
00113     if (sequel.t())
00114       deadly_error(class_name(), GROUP, "x-third had a sequel but shouldn't");
00115     if (murpin_x4.compare_prefix(turkey, sequel))
00116       deadly_error(class_name(), GROUP, "x-fourth should not match but did");
00117     if (sequel.t())
00118       deadly_error(class_name(), GROUP, "x-fourth had a sequel but shouldn't");
00119     if (murpin_x5.compare_prefix(turkey, sequel))
00120       deadly_error(class_name(), GROUP, "x-fifth should not match but did");
00121     if (sequel.t())
00122       deadly_error(class_name(), GROUP, "x-fifth had a sequel but shouldn't");
00123 
00124     // check that the functions returning no sequel are still correct.
00125     if (!murpin1.compare_prefix(turkey))
00126       deadly_error(class_name(), GROUP, "the two versions differed!");
00127     if (murpin_x1.compare_prefix(turkey))
00128       deadly_error(class_name(), GROUP, "x-the two versions differed!");
00129   }
00130 
00131   {
00132     // fourth test group.
00133     const char *GROUP = "fourth: test compare_suffix";
00134     filename turkey("/omega/ralph/turkey/buzzard.txt");
00135     filename murpin1("turkey\\buzzard.txt");
00136     filename murpin2("turkey/buzzard.txt");
00137     filename murpin3("ralph/turkey/buzzard.txt");
00138     filename murpin4("omega/ralph/turkey/buzzard.txt");
00139     filename murpin5("/omega/ralph/turkey/buzzard.txt");
00140 
00141     if (!murpin1.compare_suffix(turkey))
00142       deadly_error(class_name(), GROUP, "compare 1 failed");
00143     if (!murpin2.compare_suffix(turkey))
00144       deadly_error(class_name(), GROUP, "compare 2 failed");
00145     if (!murpin3.compare_suffix(turkey))
00146       deadly_error(class_name(), GROUP, "compare 3 failed");
00147     if (!murpin4.compare_suffix(turkey))
00148       deadly_error(class_name(), GROUP, "compare 4 failed");
00149     if (!murpin5.compare_suffix(turkey))
00150       deadly_error(class_name(), GROUP, "compare 5 failed");
00151 
00152     if (turkey.compare_suffix(murpin1))
00153       deadly_error(class_name(), GROUP, "compare x.1 failed");
00154   }
00155 
00156   {
00157     // fifth test group.
00158     // tests out the canonicalization method on any parameters given on
00159     // the command line, including the program name.
00160     const char *GROUP = "fifth: canonicalize command-line paths";
00161     log(GROUP);
00162     for (int i = 0; i < __argc; i++) {
00163       filename canony(__argv[i]);
00164       log(isprintf("parm %d:\n\tfrom \"%s\"\n\t  to \"%s\"", i, __argv[i],
00165           canony.raw().s()));
00166     }
00167   }
00168 
00169   {
00170     // sixth test group.
00171     const char *GROUP = "sixth: testing pop and push";
00172     // test dossy paths.
00173     filename test1("c:/flug/blumen/klemper/smooden");
00174     if (test1.basename() != istring("smooden"))
00175       deadly_error(class_name(), GROUP, "basename 1 failed");
00176     if (test1.dirname() != filename("c:/flug/blumen/klemper"))
00177       deadly_error(class_name(), GROUP, "d-dirname 1 failed");
00178     filename test2 = test1;
00179     istring popped = test2.pop();
00180     if (popped != istring("smooden"))
00181       deadly_error(class_name(), GROUP, "dpop 1 return failed");
00182     if (test2 != filename("c:/flug/blumen/klemper"))
00183       deadly_error(class_name(), GROUP, "dpop 1 failed");
00184     test2.pop();
00185     test2.pop();
00186     if (test2 != filename("c:/flug"))
00187       deadly_error(class_name(), GROUP, "dpop 2 failed");
00188     popped = test2.pop();
00189     if (popped != istring("flug"))
00190       deadly_error(class_name(), GROUP, "dpop 1 return failed");
00191     if (test2 != filename("c:/"))
00192       deadly_error(class_name(), GROUP, "dpop 3 failed");
00193     test2.pop();
00194     if (test2 != filename("c:/"))
00195       deadly_error(class_name(), GROUP, "dpop 3 failed");
00196     test2.push("flug");
00197     test2.push("blumen");
00198     test2.push("klemper");
00199     if (test2 != filename("c:/flug/blumen/klemper"))
00200       deadly_error(class_name(), GROUP, "dpush 1 failed");
00201     // test unix paths.
00202     filename test3("/flug/blumen/klemper/smooden");
00203     if (test3.basename() != istring("smooden"))
00204       deadly_error(class_name(), GROUP, "basename 1 failed");
00205     if (test3.dirname() != filename("/flug/blumen/klemper"))
00206       deadly_error(class_name(), GROUP, "u-dirname 1 failed");
00207     filename test4 = test3;
00208     popped = test4.pop();
00209     if (popped != istring("smooden"))
00210       deadly_error(class_name(), GROUP, "upop 1 return failed");
00211     if (test4 != filename("/flug/blumen/klemper"))
00212       deadly_error(class_name(), GROUP, "upop 1 failed");
00213     test4.pop();
00214     test4.pop();
00215     if (test4 != filename("/flug"))
00216       deadly_error(class_name(), GROUP, "upop 2 failed");
00217     popped = test4.pop();
00218     if (popped != istring("flug"))
00219       deadly_error(class_name(), GROUP, "upop 1 return failed");
00220     if (test4 != filename("/"))
00221       deadly_error(class_name(), GROUP, "upop 3 failed");
00222     test4.pop();
00223     if (test4 != filename("/"))
00224       deadly_error(class_name(), GROUP, "upop 3 failed");
00225     test4.push("flug");
00226     test4.push("blumen");
00227     test4.push("klemper");
00228     if (test4 != filename("/flug/blumen/klemper"))
00229       deadly_error(class_name(), GROUP, "upush 1 failed");
00230   }
00231 
00232   istring to_print("filename:: works for those functions tested.");
00233   guards::alert_message(to_print.s());
00234   return 0;
00235 }
00236 
00237 HOOPLE_MAIN(test_filename, )
00238 

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