fstringtest.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2002-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Mon Jun 24 17:14:36 2002
00010 // commit: $Id: fstringtest.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/pkgs/whitebox/fstringtest.cc $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_PKGS_WHITEBOX_FSTRINGTEST_CC_UTC20050626084022_DEFINED
00035 #define GROOVX_PKGS_WHITEBOX_FSTRINGTEST_CC_UTC20050626084022_DEFINED
00036 
00037 #include "pkgs/whitebox/fstringtest.h"
00038 
00039 #include "tcl/pkg.h"
00040 
00041 #include "rutz/fstring.h"
00042 #include "rutz/sfmt.h"
00043 #include "rutz/unittest.h"
00044 
00045 #include <cstring>
00046 #include <sstream>
00047 
00048 #include "rutz/trace.h"
00049 
00050 using rutz::char_range;
00051 using rutz::fstring;
00052 
00053 namespace
00054 {
00055   void testDefaultConstruct()
00056   {
00057     fstring s;
00058     TEST_REQUIRE(s.is_empty());
00059     TEST_REQUIRE(s.length() == 0);
00060     TEST_REQUIRE(s.c_str() != 0);
00061     TEST_REQUIRE(s.c_str()[0] == '\0');
00062   }
00063 
00064   void testConstruct1()
00065   {
00066     fstring s("hello world!");
00067     TEST_REQUIRE(!s.is_empty());
00068     TEST_REQUIRE(s.length() == 12);
00069     TEST_REQUIRE(s.c_str() != 0);
00070     TEST_REQUIRE(s.c_str()[s.length()] == '\0');
00071     TEST_REQUIRE(std::strlen(s.c_str()) == s.length());
00072     TEST_REQUIRE(std::strcmp(s.c_str(), "hello world!") == 0);
00073     TEST_REQUIRE(s == "hello world!");
00074     TEST_REQUIRE(s != "hello world");
00075     TEST_REQUIRE(s != "hello world!!");
00076     TEST_REQUIRE(s != "hello world?");
00077     TEST_REQUIRE(s != "");
00078     TEST_REQUIRE(s.ends_with("ld!"));
00079   }
00080 
00081   void testConstruct2()
00082   {
00083     fstring s = rutz::sfmt("%s%s", "foo", " fighters!");
00084     TEST_REQUIRE(s.length() == 13);
00085     TEST_REQUIRE(s == "foo fighters!");
00086   }
00087 
00088   void testConstructNum()
00089   {
00090     fstring s = rutz::sfmt("double val: %g, int val: %d", 1.234, 1234);
00091     TEST_REQUIRE(s == "double val: 1.234, int val: 1234");
00092   }
00093 
00094   void testConstructCharRange()
00095   {
00096     // note that this array is NOT null-terminated
00097     const char init[19] =
00098       {
00099         't', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ',
00100         'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x'
00101       };
00102 
00103     {
00104       fstring s((char_range(&init[0], 0)));
00105       TEST_REQUIRE(s == "");
00106     }
00107 
00108     {
00109       fstring s((char_range(&init[0], 1)));
00110       TEST_REQUIRE(s == "t");
00111     }
00112 
00113     {
00114       fstring s((char_range(&init[0], 4)));
00115       TEST_REQUIRE(s == "the ");
00116     }
00117 
00118     {
00119       fstring s((char_range(&init[4], 5)));
00120       TEST_REQUIRE(s == "quick");
00121     }
00122 
00123     {
00124       fstring s((char_range(&init[0], 19)));
00125       TEST_REQUIRE(s == "the quick brown fox");
00126     }
00127   }
00128 
00129   void testSwap()
00130   {
00131     fstring s1("foo");
00132     fstring s2("bar");
00133 
00134     s1.swap(s2);
00135 
00136     TEST_REQUIRE(s1 == "bar");
00137     TEST_REQUIRE(s2 == "foo");
00138   }
00139 
00140   void testAssign1()
00141   {
00142     fstring s1("foo");
00143 
00144     s1 = "bar";
00145 
00146     TEST_REQUIRE(s1 == "bar");
00147   }
00148 
00149   void testAssign2()
00150   {
00151     fstring s1("foo");
00152 
00153     s1 = fstring("bar");
00154 
00155     TEST_REQUIRE(s1 == "bar");
00156   }
00157 
00158   void testAssignEmptyString()
00159   {
00160     fstring s1("foo");
00161 
00162     s1 = "";
00163 
00164     TEST_REQUIRE(s1 == "");
00165   }
00166 
00167   void testAssignNullString()
00168   {
00169     fstring s1("foo");
00170 
00171     s1 = static_cast<const char*>(0);
00172 
00173     TEST_REQUIRE(s1 == "");
00174   }
00175 
00176   void testLessThan()
00177   {
00178     fstring s("apple");
00179 
00180     TEST_REQUIRE(s < "banana");
00181     TEST_REQUIRE(s < "apples");
00182     TEST_REQUIRE(s < "azure");
00183   }
00184 
00185   void testGreaterThan()
00186   {
00187     fstring s("pineapple");
00188 
00189     TEST_REQUIRE(s > "orange");
00190     TEST_REQUIRE(s > "pine");
00191     TEST_REQUIRE(s > "panini");
00192   }
00193 
00194   void testRead()
00195   {
00196     const std::string orig("orange panini");
00197     std::istringstream in(orig);
00198 
00199     fstring dummy; (void)dummy; // this is here to make sure that the
00200                                 // "empty_string_rep" is shared,
00201                                 // forcing us to make sure that the
00202                                 // real "out" fstring uniquifies
00203                                 // itself before trying to read from
00204                                 // the istringstream
00205     fstring out;
00206     in >> out;
00207     TEST_REQUIRE_EQ(out, "orange");
00208   }
00209 
00210   void testReadsome()
00211   {
00212     const std::string orig("azure banana");
00213     std::istringstream in(orig);
00214 
00215     fstring dummy; (void)dummy; // see comment in testRead();
00216 
00217     fstring out;
00218     out.readsome(in, 10);
00219     TEST_REQUIRE_EQ(out, "azure bana");
00220   }
00221 
00222   void testReadline1()
00223   {
00224     const std::string orig("marooned on\nan island!\nforever\n");
00225     std::istringstream in(orig);
00226 
00227     fstring dummy; (void)dummy; // see comment in testRead();
00228 
00229     fstring out;
00230     out.readline(in);
00231     TEST_REQUIRE_EQ(out, "marooned on");
00232   }
00233 
00234   void testReadline2()
00235   {
00236     const std::string orig("marooned on\nan island!\nforever\n");
00237     std::istringstream in(orig);
00238 
00239     fstring dummy; (void)dummy; // see comment in testRead();
00240 
00241     fstring out;
00242     out.readline(in, '!');
00243     TEST_REQUIRE_EQ(out, "marooned on\nan island");
00244   }
00245 
00246   void testWrite()
00247   {
00248     fstring orig("blue\ncrayon\n\tskies");
00249 
00250     std::ostringstream out;
00251 
00252     out << orig;
00253 
00254     TEST_REQUIRE_EQ(out.str().c_str(), fstring("blue\ncrayon\n\tskies"));
00255   }
00256 }
00257 
00258 extern "C"
00259 int Fstringtest_Init(Tcl_Interp* interp)
00260 {
00261 GVX_TRACE("Fstringtest_Init");
00262 
00263   GVX_PKG_CREATE(pkg, interp, "Fstringtest", "4.$Revision: 10065 $");
00264 
00265   DEF_TEST(pkg, testDefaultConstruct);
00266   DEF_TEST(pkg, testConstruct1);
00267   DEF_TEST(pkg, testConstruct2);
00268   DEF_TEST(pkg, testConstructNum);
00269   DEF_TEST(pkg, testConstructCharRange);
00270   DEF_TEST(pkg, testSwap);
00271   DEF_TEST(pkg, testAssign1);
00272   DEF_TEST(pkg, testAssign2);
00273   DEF_TEST(pkg, testAssignEmptyString);
00274   DEF_TEST(pkg, testAssignNullString);
00275   DEF_TEST(pkg, testLessThan);
00276   DEF_TEST(pkg, testGreaterThan);
00277   DEF_TEST(pkg, testRead);
00278   DEF_TEST(pkg, testReadsome);
00279   DEF_TEST(pkg, testReadline1);
00280   DEF_TEST(pkg, testReadline2);
00281   DEF_TEST(pkg, testWrite);
00282 
00283   GVX_PKG_RETURN(pkg);
00284 }
00285 
00286 // Need these to avoid dyld errors on Mac OS X
00287 extern "C" int Fstringtest_SafeInit(Tcl_Interp*) { return 1; }
00288 
00289 extern "C" int Fstringtest_Unload(Tcl_Interp* interp, int /*flags*/)
00290 {
00291 GVX_TRACE("Fstringtest_Unload");
00292   return tcl::pkg::destroy_on_unload(interp, "Fstringtest");
00293 }
00294 
00295 extern "C" int Fstringtest_SafeUnload(Tcl_Interp*, int /*flags*/) { return 1; }
00296 
00297 static const char __attribute__((used)) vcid_groovx_pkgs_whitebox_fstringtest_cc_utc20050626084022[] = "$Id: fstringtest.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00298 #endif // !GROOVX_PKGS_WHITEBOX_FSTRINGTEST_CC_UTC20050626084022_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.