t_sequence.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_sequence                                                     *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1992-$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 #include <basis/byte_array.h>
00016 #include <basis/function.h>
00017 #include <basis/guards.h>
00018 #include <basis/istring.h>
00019 #include <basis/sequence.cpp>
00020 #include <opsystem/application_shell.h>
00021 #include <loggers/console_logger.h>
00022 #include <data_struct/static_memory_gremlin.h>
00023 
00024 #include <stdio.h>
00025 
00026 //#define DEBUG_SEQUENCE
00027 
00028 #define WHERE __WHERE__.s()
00029 
00030 const int test_iterations = 100000;
00031 
00032 int compnum = 0;
00033 
00034 #define LOG(s) EMERGENCY_LOG(program_wide_logger(), s)
00035 
00036 // test: reports an error if the condition evaluates to true.
00037 #define test(expr) \
00038   compnum++; \
00039   if (expr) { \
00040     char temp[500]; \
00041     sprintf(temp, "operator test %d", compnum); \
00042     deadly_error(WHERE, temp, "failure"); \
00043     return 1; \
00044   }
00045 
00047 
00048 #ifndef EMBEDDED_BUILD
00049   typedef sequence<char> fake_string;
00050 #else
00051   #define fake_string sequence<char>
00052 #endif
00053 
00054 class test_sequence : public application_shell
00055 {
00056 public:
00057   test_sequence() : application_shell(class_name()) {}
00058   IMPLEMENT_CLASS_NAME("test_sequence");
00059   virtual int execute();
00060 };
00061 
00062 const int flags = byte_array::SIMPLE_COPY | byte_array::EXPONE;
00063 
00064 int test_sequence::execute()
00065 {
00066   for (int i = 0; i < test_iterations; i++) {
00067 #ifdef DEBUG_SEQUENCE
00068     LOG(isprintf("index %d", i));
00069 #endif
00070     fake_string nil_seq(1, "\0", flags);
00071     {
00072       fake_string fred1 = fake_string(int(strlen("hyeargh!")+1), "hyeargh!", flags);
00073       fake_string fred2 = fake_string(int(strlen("matey.")+1), "matey.", flags);
00074       fake_string fred3 = fake_string(0, NIL, flags);
00075       fake_string fred4 = fake_string(fred1);
00076       fake_string fred5 = fake_string(fred2);
00077 
00078       fred3 = fred1.subarray(0, fred1.length() - 2);
00079       fred3.concatenate(fred2);
00080       fred4.put(2, 'u');
00081       fred4.put(3, 'r');
00082       fred5.reset(int(strlen("a leather hell.")+1), "a leather hell.");
00083 
00084 #ifdef DEBUG_SEQUENCE
00085       LOG(istring("[ ") + fred1 + " & " + fred2 + "] -> " + fred3);
00086       LOG("to first debug block.");
00087 #endif
00088 
00089       // do not blame me for this stupid test: one compiler (saber) complains
00090       // that "temporary in || that needs destructor is not implemented"....
00091       int die = false;
00092       if (fred1 != fake_string(int(strlen("hyeargh!")+1), "hyeargh!", flags))
00093         die++;
00094       if (fred2 != fake_string(int(strlen("matey.")+1), "matey.", flags))
00095         die++;
00096       if (fred3 != fake_string(int(strlen("hyeargh!matey.")+1),
00097           "hyeargh!matey.", flags))
00098         die++;
00099       if (fred4 != fake_string(int(strlen("hyurrgh!")+1), "hyurrgh!", flags))
00100         die++;
00101       if (fred5 != fake_string(int(strlen("a leather hell.")+1),
00102           "a leather hell.", flags))
00103         die++;
00104       if ( (fred5[2] != 'l') || (fred5[5] != 't') ) die++;
00105       if (die) deadly_error(WHERE, "first test group", "failure in comparison");
00106     }
00107     {
00108       fake_string *fred1 = new fake_string
00109         (int(strlen("flipper ate") + 1), "flipper ate", flags);
00110       fake_string *fred2 = new fake_string
00111         (int(strlen(" my sandwich.")+1), " my sandwich.", flags);
00112       fake_string *fred3 = new fake_string(0, NIL, flags);
00113       fred3->operator = (fred1->subarray(0, fred1->length() - 2));
00114       fred3->concatenate(*fred2);
00115 
00116 #ifdef DEBUG_SEQUENCE
00117       LOG(istring("[ ") + *fred1 + " & " + *fred2 + "] -> " + *fred3);
00118 #endif
00119 
00120       int die = false;
00121       if (*fred1 != fake_string(int(strlen("flipper ate")+1),
00122           "flipper ate", flags))
00123         die++;
00124       if (*fred2 != fake_string(int(strlen(" my sandwich.")+1),
00125           " my sandwich.", flags))
00126         die++;
00127       if (*fred3 != fake_string(int(strlen("flipper ate my sandwich.")+1),
00128           "flipper ate my sandwich.", flags))
00129         die++;
00130       if (die)
00131         deadly_error(WHERE, "second test group", "failure in comparison");
00132       delete fred1;
00133       delete fred2;
00134       delete fred3;
00135     }
00136 
00137     {
00138       fake_string fleermipe = fake_string(int(strlen("hello my frobious.")+1),
00139           "hello my frobious.", flags);
00140       fleermipe.zap(0, fleermipe.length() - 1);
00141       if (fleermipe.length() != 0)
00142         deadly_error(WHERE, "third test group",
00143             "length not 0 after deleting entire sequence");
00144     }
00145 
00146     {
00147       fake_string test_sequence = fake_string(int(strlen("this test "
00148           "sequence will be chopped up.")+1),
00149           "this test sequence will be chopped up.", flags);
00150 
00151 #ifdef DEBUG_SEQUENCE
00152       LOG(istring("original is: ") + test_sequence);
00153 #endif
00154 
00155       fake_string fred = fake_string(test_sequence);
00156       int found = 0;
00157       outcome outcome;
00158       fred.zap(0, fred.find('w', found, outcome));
00159 #ifdef DEBUG_SEQUENCE
00160       LOG(istring("now, the one chopped through 'w' is: ") + fred);
00161 #endif
00162 
00163       if (fred != fake_string(int(strlen("ill be chopped up.")+1),
00164           "ill be chopped up.", flags))
00165         deadly_error(WHERE, "fourth test group", "first zap failed");
00166 
00167       fake_string blorg = fake_string(test_sequence);
00168       int pos = 0;
00169       blorg.zap(blorg.find('p', pos, outcome), blorg.length() - 2);
00170 #ifdef DEBUG_SEQUENCE
00171       LOG(istring("now the one chopped from p to the end: ") + blorg);
00172 #endif
00173 
00174       if (blorg != fake_string(int(strlen("this test sequence will be cho")+1),
00175           "this test sequence will be cho", flags))
00176         deadly_error(WHERE, "fourth test group", "second zap failed");
00177 
00178       fake_string fleen = fake_string(0, NIL, flags);
00179       fleen.concatenate(test_sequence);
00180       fleen.zap(7, 14);
00181 #ifdef DEBUG_SEQUENCE
00182       LOG(istring("now the one with 7 through 14 missing: ") + fleen);
00183 #endif
00184 
00185       if (fleen != fake_string(int(strlen("this tence will be chopped up.")+1),
00186           "this tence will be chopped up.", flags))
00187         deadly_error(WHERE, "fourth test group", "third zap failed");
00188 
00189 #ifdef DEBUG_SEQUENCE
00190       LOG(istring("original sequence is now: ") + test_sequence);
00191 #endif
00192       if (test_sequence != fake_string(int(
00193           strlen("this test sequence will be chopped up.")+1),
00194          "this test sequence will be chopped up.", flags))
00195         deadly_error(WHERE, "fourth test group", "original sequence was changed");
00196     }
00197 
00198     {
00199 #ifdef DEBUG_SEQUENCE
00200       LOG("about to test weird things:");
00201 #endif
00202       fake_string frieda = fake_string(int(strlen("glorp")+1), "glorp", flags);
00203       fake_string jorb = fake_string(frieda);
00204       fake_string *kleeg = new fake_string(jorb);
00205       fake_string plok = frieda;
00206       test(frieda != jorb);
00207       test(jorb != *kleeg);
00208       test(*kleeg != plok);
00209       test(plok != frieda);
00210       fake_string glorp = fake_string(int(strlen("glorp")+1), "glorp", flags);
00211       test(frieda != glorp);
00212       delete kleeg;
00213 
00214 #ifdef DEBUG_SEQUENCE
00215       LOG("sequences matched up okay.");
00216 #endif
00217     }
00218 
00219     {
00220       fake_string bungee = fake_string(0, NIL, flags);
00221       bungee.concatenate(fake_string(int(strlen("this sequence")),
00222           "this sequence", flags));
00223       bungee.concatenate(fake_string(
00224         int(strlen(" has been constructed gradually")),
00225         " has been constructed gradually"));
00226       fake_string blorpun = fake_string(int(strlen(" out of sever")), " out of sever", flags);
00227       bungee.concatenate(blorpun);
00228       fake_string signow = fake_string(1, "a", flags);
00229       bungee.concatenate(signow);
00230       bungee.concatenate('l');
00231       bungee.concatenate(fake_string(int(strlen(" different bits,\nincluding")),
00232         " different bits,\nincluding", flags));
00233       bungee = bungee.concatenation(fake_string
00234         (int(strlen(" arrrrooooo")), " arrrrooooo", flags));
00235       bungee.concatenate(fake_string
00236         (int(strlen(" radians away.")), " radians away.", flags));
00237       bungee.concatenate(fake_string(int(strlen("\nhow does it look?\n")+1),
00238         "\nhow does it look?\n", flags));
00239 #ifdef DEBUG_SEQUENCE
00240       LOG(bungee);
00241 #endif
00242 
00243       if (bungee != fake_string(int(strlen("this \
00244 sequence has been constructed gradually out of several \
00245 different bits,\nincluding arrrrooooo radians away.\nhow \
00246 does it look?\n")+1), "this sequence has been constructed \
00247 gradually out of several different bits,\
00248 \nincluding arrrrooooo radians away.\nhow does it look?\n", flags))
00249         deadly_error(WHERE, "sixth test group", "constructed sequence is wrong");
00250     }
00251 
00252     {
00253       fake_string a = fake_string(int(strlen("axes")+1), "axes", flags);
00254       fake_string b = fake_string(int(strlen("bakesales")+1), "bakesales", flags);
00255       fake_string x = fake_string(int(strlen("xylophone")+1), "xylophone", flags);
00256 
00257       test(a >= x);
00258       test(a == b);
00259       test(a > b);
00260       test(b >= x);
00261       test(x <= a);
00262       test(x != x);
00263       test(a != a);
00264 #ifdef DEBUG_SEQUENCE
00265       LOG("comparisons worked");
00266 #endif
00267     }
00268 
00269     {
00270 #ifdef DEBUG_SEQUENCE
00271       LOG("now testing concatenation operator");
00272 #endif
00273       fake_string a = fake_string(int(strlen("fred")), "fred", flags);
00274       fake_string b = fake_string(int(strlen(" is")), " is", flags);
00275       fake_string c = fake_string(int(strlen(" his")), " his", flags);
00276       fake_string d = fake_string(int(strlen(" name.")), " name.", flags);
00277       fake_string e = fake_string(0, NIL, flags);
00278       e = a.concatenation(b.concatenation(c.concatenation(d)));
00279       e.concatenate(nil_seq);
00280 #ifdef DEBUG_SEQUENCE
00281       LOG(istring("result is: ") + e);
00282 #endif
00283       fake_string f = fake_string(0, NIL, flags);
00284       f.concatenate(d.concatenation(c.concatenation(b.concatenation(a))));
00285       f.concatenate(nil_seq);
00286 #ifdef DEBUG_SEQUENCE
00287       LOG(istring("reverse is: ") + f);
00288 #endif
00289       fake_string g = fake_string(0, NIL, flags);
00290       g.concatenate(a.concatenation(c.concatenation(d.concatenation(b))));
00291       g.concatenate(nil_seq);
00292 #ifdef DEBUG_SEQUENCE
00293       LOG(istring("tibetan style is: ") + g);
00294 #endif
00295       int die = false;
00296       if (e != fake_string(int(strlen("fred is his name.")+1),
00297           "fred is his name.", flags))
00298         die++;
00299       if (f != fake_string(int(strlen(" name. his isfred")+1),
00300           " name. his isfred", flags))
00301         die++;
00302       if (g != fake_string(int(strlen("fred his name. is")+1),
00303           "fred his name. is", flags))
00304         die++;
00305       if (die) deadly_error(WHERE, "eighth test group", "sequence looks wrong");
00306     }
00307 
00308     {
00309       fake_string bleer = fake_string(int(strlen("urghla burgla #23\n")+1),
00310           "urghla burgla #23\n", flags);
00311       for (int i = 0; i < bleer.length()-2; i++) {
00312         fake_string temp = fake_string(bleer.subarray(0, i));
00313         fake_string holder = fake_string(i + 1, NIL, flags);
00314         holder.overwrite(0, temp);
00315         holder.concatenate(nil_seq);
00316 #ifdef DEBUG_SEQUENCE
00317         LOG(isprintf("[%d] has ", i) + holder);
00318 #endif
00319         fake_string my_copy = fake_string(bleer);
00320         my_copy.zap(i+1, my_copy.length() - 2);
00321         if (my_copy != holder)
00322           deadly_error(WHERE, "ninth test group", "inaccurate stuff");
00323       }
00324     }
00325     {
00326 #ifdef DEBUG_SEQUENCE
00327       LOG("The tenth ones:");
00328 #endif
00329       fake_string george = fake_string(int(strlen("this one will be mangled.")+1),
00330           "this one will be mangled.", flags);
00331       fake_string tmp1 = fake_string(george.subarray(1, 7));  // constructor.
00332       fake_string tmp2 = george.subarray(10, george.length() - 1);  // constructor.
00333       if (tmp1 == tmp2)
00334         deadly_error(WHERE, "tenth compare", "bizarre equality occurred!!");
00335       if ( (tmp1 > tmp2) || (tmp2 < tmp1) )
00336         deadly_error(WHERE, "tenth compare", "bizarre comparison error.");
00337 #ifdef DEBUG_SEQUENCE
00338       LOG(istring(fake_string(george.subarray(1, 7))) + "... "
00339            + fake_string(george.subarray(10, george.length() - 1)));
00340 #endif
00341       int place_to_stuff = george.length() - 1;
00342       george.insert(place_to_stuff, int(strlen("terribly ")));
00343       george.overwrite(place_to_stuff,
00344           fake_string(int(strlen("terribly ")), "terribly ", flags));
00345 #ifdef DEBUG_SEQUENCE
00346       LOG(george);
00347 #endif
00348 
00349       fake_string fred = fake_string(george);
00350       george.zap(place_to_stuff - 7, place_to_stuff);
00351 #ifdef DEBUG_SEQUENCE
00352       LOG(george);
00353 #endif
00354 
00355       fake_string fred_part = fake_string(0, NIL, flags);
00356       fred_part = fred.subarray(0, 13);
00357 #ifdef DEBUG_SEQUENCE
00358       LOG(fred_part);
00359 #endif
00360       fred_part = fred.subarray(13, fred.length() - 1);
00361 #ifdef DEBUG_SEQUENCE
00362       LOG(fred_part);
00363       LOG("compares okay");
00364 #endif
00365     }
00366     {
00367       // 11th test:
00368       char junk[20];
00369       fake_string glob = fake_string(int(strlen("too long to all fit in junk")),
00370         "too long to all fit in junk", flags);
00371       glob.stuff(19, junk);
00372       junk[19] = '\0';
00373       if (strcmp(junk, "too long to all fit"))
00374         deadly_error(WHERE, "stuff test group", "failure in comparison");
00375     }
00376     {
00377 // turned off until array with limits is working.
00378 #if 0
00379       // 12th test:
00380       const char *test1 = "this is a test string that will be too long";
00381       fake_string limitted = fake_string(int(strlen(test1)+1), test1, flags);
00382       fake_string lim_copy = fake_string(limitted);
00383       lim_copy.limit(1000);
00384       if (lim_copy != limitted)
00385         deadly_error(WHERE, "limit test group", "failure in simple comparison");
00386       lim_copy.limit(12);
00388       lim_copy += nil_seq;
00389 guards::alert_message(istring("limitted guy is now: %s", lim_copy.observe()).s());
00390       fake_string new_val = fake_string(int(strlen("this is a te") + 1), "this is a te", flags);
00391       if (lim_copy != new_val)
00392         deadly_error(WHERE, "limit test group", "failure in limitted comparison");
00393       lim_copy.limit(35);
00394       for (int j = 0; j < 20; j++) {
00395         lim_copy += fake_string(int(strlen("extra stuff") + 1), "extra stuff", flags);
00396         if (lim_copy.length() > 35)
00397           deadly_error(WHERE, "limit test group", "failure to keep under limit");
00398       }
00399       if (lim_copy.length() != 35)
00400         deadly_error(WHERE, "limit test group", "final limit check");
00401 #endif
00402     }
00403     {
00404 // test 13 is now open for business.  the prior test has been tossed.
00405       // 13th test: unknown?
00406     }
00407   }  // end of test iterations..
00408   fake_string to_print = fake_string(int(strlen("sequence:: works for those functions tested.")+1),
00409      "sequence:: works for those functions tested.", flags);
00410   guards::alert_message(to_print.observe());
00411   return 0;
00412 }
00413 
00414 HOOPLE_MAIN(test_sequence, )
00415 
00416 

Generated on Fri Nov 21 04:30:05 2008 for HOOPLE Libraries by  doxygen 1.5.1