stringftest.cpp

Go to the documentation of this file.
00001 /*
00002 This program is distributed under the terms of the 'MIT license'. The text
00003 of this licence follows...
00004 
00005 Copyright (c) 2007-2009 J.D.Medhurst (a.k.a. Tixy)
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00020 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023 THE SOFTWARE.
00024 */
00025 
00032 #include "common.h"
00033 #include "test/test.h"
00034 #include "../stringf.h"
00035 
00036 
00037 
00038 //
00039 // Testing helpers
00040 //
00041 
00042 static bool Match(const char* start,int size,const char* expected)
00043     {
00044     while(size>0)
00045         {
00046         if(!*expected || *start!=*expected)
00047             {
00048             BREAKPOINT;
00049             return false;
00050             }
00051         ++start;
00052         ++expected;
00053         --size;
00054         }
00055     if(*expected!=0)
00056         {
00057         BREAKPOINT;
00058         return false;
00059         }
00060     if(size!=0)
00061         {
00062         BREAKPOINT;
00063         return false;
00064         }
00065     return true;
00066     }
00067 
00068 #define CHECK(expected) (void)(Match(buffer,size,expected)||TEST_FAILED)
00069 #define CHECK_EQ(a,b)   (void)((a)==(b)||TEST_FAILED)
00070 
00071 
00072 static int SPrintF(char* str, const char* format, ...)
00073     {
00074     va_list args;
00075     va_start(args,format);
00076     int size = vsprintf(str,format,args);
00077     va_end(args);
00078     return size;
00079     }
00080 
00081 
00082 static int HexDumpLine(char* dst, const void* data, size_t size, ptrdiff_t addressOffset)
00083     {
00084     StringBufferFormatter formatter(dst,StringFormatter::HexDumpLineSize);
00085     formatter.HexDumpLine(data,size,addressOffset);
00086     return formatter.End()-dst;
00087     }
00088 
00089 
00090 
00091 //
00092 // Top level test function
00093 //
00094 
00095 void TestStringF()
00096     {
00097     char buffer[256];
00098     int size;
00099 
00100     // test plain text...
00101 
00102     size = SPrintF(buffer,"");
00103     CHECK("");
00104     size = SPrintF(buffer,"a%%d");
00105     CHECK("a%d");
00106 
00107     // test decimal integers...
00108 
00109     size = SPrintF(buffer,"%u,%u,%u,%u",0,1,1234567890,4294967295u);
00110     CHECK("0,1,1234567890,4294967295");
00111     size = SPrintF(buffer,"%d,%d,%d,%d",0,1,1234567890,2147483647);
00112     CHECK("0,1,1234567890,2147483647");
00113     size = SPrintF(buffer,"%d,%d,%d,%d",0,-1,-1234567890,-2147483647-1);
00114     CHECK("0,-1,-1234567890,-2147483648");
00115     size = SPrintF(buffer,"%i,%i,%i,%i",0,1,1234567890,2147483647);
00116     CHECK("0,1,1234567890,2147483647");
00117     size = SPrintF(buffer,"%i,%i,%i,%i",0,-1,-1234567890,-2147483647-1);
00118     CHECK("0,-1,-1234567890,-2147483648");
00119 
00120     // test hex integers...
00121 
00122     size = SPrintF(buffer,"%x,%x,%x,%x",0,1,0x89abcdef,0xffffffff);
00123     CHECK("0,1,89abcdef,ffffffff");
00124     size = SPrintF(buffer,"%X,%X,%X,%X",0,1,0x89abcdef,0xffffffff);
00125     CHECK("0,1,89ABCDEF,FFFFFFFF");
00126     size = SPrintF(buffer,"%#x,%#x,%#x,%#x",0,1,0x89abcdef,0xffffffff);
00127     CHECK("0,0x1,0x89abcdef,0xffffffff");
00128 
00129     // test octal integers...
00130 
00131     size = SPrintF(buffer,"%o,%o,%o,%o",0,1,01234567,037777777777);
00132     CHECK("0,1,1234567,37777777777");
00133     size = SPrintF(buffer,"%#o,%#o,%#o,%#o",0,1,01234567,037777777777);
00134     CHECK("0,01,01234567,037777777777");
00135 
00136     // test sign character specifiers (what's the correct sign for zero?)...
00137 
00138     size = SPrintF(buffer,"%+d,%+d,% d,% d",0,2147483647,0,2147483647);
00139     CHECK("+0,+2147483647, 0, 2147483647");
00140     size = SPrintF(buffer,"% +d,% +d,%+ d,%+ d",0,2147483647,0,2147483647);
00141     CHECK("+0,+2147483647,+0,+2147483647");
00142 
00143     // test different sized integers...
00144 
00145     ASSERT_COMPILE(sizeof(short)==sizeof(int16_t));
00146     size = SPrintF(buffer,"%hd,%hd,%hd,%hd,%hd",0,1,-1,32767,-32768);
00147     CHECK("0,1,-1,32767,-32768");
00148     size = SPrintF(buffer,"%hd,%hd,%hd,%hd,%hd",0^0x10000,1^0x10000,-1^0x10000,32767^0x10000,-32768^0x10000);
00149     CHECK("0,1,-1,32767,-32768");
00150 
00151     ASSERT_COMPILE(sizeof(char)==sizeof(int8_t));
00152     size = SPrintF(buffer,"%hhd,%hhd,%hhd,%hhd,%hhd",0,1,-1,127,-128);
00153     CHECK("0,1,-1,127,-128");
00154     size = SPrintF(buffer,"%hhd,%hhd,%hhd,%hhd,%hhd",0^0x100,1^0x100,-1^0x100,127^0x100,-128^0x100);
00155     CHECK("0,1,-1,127,-128");
00156 
00157     size = SPrintF(buffer,"%ld,%ld,%ld,%ld,%ld",(long)0,(long)1,-(long)1,(long)2147483647,-(long)2147483647-1);
00158     CHECK("0,1,-1,2147483647,-2147483648");
00159     size = SPrintF(buffer,"%lld,%lld,%lld,%lld,%lld",(longlong)0,(longlong)1,-(longlong)1,(longlong)2147483647,-(longlong)2147483647-1);
00160     CHECK("0,1,-1,2147483647,-2147483648");
00161 
00162     ASSERT_COMPILE(sizeof(longlong)==sizeof(int64_t));
00163     size = SPrintF(buffer,"%lld,%lld,%llu",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
00164     CHECK("9223372036854775807,-9223372036854775808,18446744073709551615");
00165     size = SPrintF(buffer,"%llx,%llx,%llx",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
00166     CHECK("7fffffffffffffff,8000000000000000,ffffffffffffffff");
00167     size = SPrintF(buffer,"%llo,%llo,%llo",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
00168     CHECK("777777777777777777777,1000000000000000000000,1777777777777777777777");
00169 
00170     ASSERT_COMPILE(sizeof(intmax_t)==sizeof(int64_t));
00171     size = SPrintF(buffer,"%lld,%lld,%llu",(~(ulonglong)0)>>1,~((~(ulonglong)0)>>1),~(ulonglong)0);
00172     CHECK("9223372036854775807,-9223372036854775808,18446744073709551615");
00173 
00174     ASSERT_COMPILE(sizeof(size_t)==sizeof(int32_t));
00175     size = SPrintF(buffer,"%zd,%zd,%zd,%zd,%zd",(size_t)0,(size_t)1,-(size_t)1,(size_t)2147483647,-(size_t)2147483647-1);
00176     CHECK("0,1,-1,2147483647,-2147483648");
00177 
00178     ASSERT_COMPILE(sizeof(ptrdiff_t)==sizeof(int32_t));
00179     size = SPrintF(buffer,"%td,%td,%td,%td,%td",(ptrdiff_t)0,(ptrdiff_t)1,-(ptrdiff_t)1,(ptrdiff_t)2147483647,-(ptrdiff_t)2147483647-1);
00180     CHECK("0,1,-1,2147483647,-2147483648");
00181 
00182     // test characters and strings (assumes 'l' type modifier is ignored)...
00183 
00184     ASSERT_COMPILE(sizeof(char)==sizeof(int8_t));
00185     size = SPrintF(buffer,"%c%c%lc",'Q','*'+0x100,'A'+0x100);
00186     CHECK("Q*A");
00187     size = SPrintF(buffer,"%s%s%ls","","abc","xyz");
00188     CHECK("abcxyz");
00189 
00190     // test %p...
00191 
00192     ASSERT_COMPILE(sizeof(void*)==sizeof(uint32_t));
00193     size = SPrintF(buffer,"%p",(void*)0);
00194     CHECK("00000000");
00195     size = SPrintF(buffer,"%p",(void*)0x78abcdef);
00196     CHECK("78abcdef");
00197 
00198     // test %n...
00199 
00200     {
00201     int l[4] = { ~0,~0,~0,~0 };
00202     size = SPrintF(buffer,"%n%d%n",l,123,l+2);
00203     CHECK("123");
00204     CHECK_EQ(l[0],0);
00205     CHECK_EQ(l[1],~0);
00206     CHECK_EQ(l[2],3);
00207     CHECK_EQ(l[3],~0);
00208     }
00209 
00210     {
00211     short l[4] = { ~0,~0,~0,~0 };
00212     size = SPrintF(buffer,"%hn%d%hn",l,123,l+2);
00213     CHECK("123");
00214     CHECK_EQ(l[0],0);
00215     CHECK_EQ(l[1],~0);
00216     CHECK_EQ(l[2],3);
00217     CHECK_EQ(l[3],~0);
00218     }
00219 
00220     {
00221     char l[4] = { ~0,~0,~0,~0 };
00222     size = SPrintF(buffer,"%hhn%d%hhn",l,123,l+2);
00223     CHECK("123");
00224     CHECK_EQ(l[0],0);
00225     CHECK_EQ(l[1],~0);
00226     CHECK_EQ(l[2],3);
00227     CHECK_EQ(l[3],~0);
00228     }
00229 
00230     {
00231     long l[4] = { ~0l,~0l,~0l,~0l };
00232     size = SPrintF(buffer,"%ln%d%ln",l,123,l+2);
00233     CHECK("123");
00234     CHECK_EQ(l[0],0l);
00235     CHECK_EQ(l[1],~0l);
00236     CHECK_EQ(l[2],3l);
00237     CHECK_EQ(l[3],~0l);
00238     }
00239 
00240     {
00241     longlong l[4] = { ~(longlong)0,~(longlong)0,~(longlong)0,~(longlong)0 };
00242     size = SPrintF(buffer,"%lln%d%lln",l,123,l+2);
00243     CHECK("123");
00244     CHECK_EQ(l[0],(longlong)0);
00245     CHECK_EQ(l[1],~(longlong)0);
00246     CHECK_EQ(l[2],(longlong)3);
00247     CHECK_EQ(l[3],~(longlong)0);
00248     }
00249 
00250     // test padding...
00251 
00252     size = SPrintF(buffer,"%04u,%04u,%04u,%04u",0,123,1234,12345);
00253     CHECK("0000,0123,1234,12345");
00254     size = SPrintF(buffer,"%04d,%04d,%04d,%04d",0,123,1234,12345);
00255     CHECK("0000,0123,1234,12345");
00256     size = SPrintF(buffer,"%04d,%04d,%04d,%04d",-12,-123,-1234,-12345);
00257     CHECK("-012,-123,-1234,-12345");
00258     size = SPrintF(buffer,"%04x,%04x,%04x,%04x",0,0x123,0x1234,0x12345);
00259     CHECK("0000,0123,1234,12345");
00260     size = SPrintF(buffer,"%#06x,%#06x,%#06x,%#06x",0,0x123,0x1234,0x12345);
00261     CHECK("000000,0x0123,0x1234,0x12345");
00262     size = SPrintF(buffer,"%04o,%04o,%04o,%04o",0,0123,01234,012345);
00263     CHECK("0000,0123,1234,12345");
00264     size = SPrintF(buffer,"%#05o,%#05o,%#05o,%#05o",0,0123,01234,012345);
00265     CHECK("00000,00123,01234,012345");
00266 
00267     size = SPrintF(buffer,"%-4d,%-4d,%-4d,%-4d",0,123,1234,12345);
00268     CHECK("0   ,123 ,1234,12345");
00269     size = SPrintF(buffer,"%-4d,%-4d,%-4d,%-4d",-12,-123,-1234,-12345);
00270     CHECK("-12 ,-123,-1234,-12345");
00271     size = SPrintF(buffer,"%#-6x,%#-6x,%#-6x,%#-6x",0,0x123,0x1234,0x12345);
00272     CHECK("0     ,0x123 ,0x1234,0x12345");
00273     size = SPrintF(buffer,"%#-5o,%#-5o,%#-5o,%#-5o",0,0123,01234,012345);
00274     CHECK("0    ,0123 ,01234,012345");
00275 
00276     size = SPrintF(buffer,"%4d,%4d,%4d,%4d",0,123,1234,12345);
00277     CHECK("   0, 123,1234,12345");
00278     size = SPrintF(buffer,"%4d,%4d,%4d,%4d",-12,-123,-1234,-12345);
00279     CHECK(" -12,-123,-1234,-12345");
00280     size = SPrintF(buffer,"%#6x,%#6x,%#6x,%#6x",0,0x123,0x1234,0x12345);
00281     CHECK("     0, 0x123,0x1234,0x12345");
00282     size = SPrintF(buffer,"%#5o,%#5o,%#5o,%#5o",0,0123,01234,012345);
00283     CHECK("    0, 0123,01234,012345");
00284 
00285     size = SPrintF(buffer,"%2s,%2s,%2s,%2s","","a","bc","xyz");
00286     CHECK("  , a,bc,xyz");
00287     size = SPrintF(buffer,"%-2s,%-2s,%-2s,%-2s","","a","bc","xyz");
00288     CHECK("  ,a ,bc,xyz");
00289     size = SPrintF(buffer,"%1c,%2c,%-1c,%-2c",'a','b','c','d');
00290     CHECK("a, b,c,d ");
00291 
00292     // test precission...
00293 
00294     size = SPrintF(buffer,"%.4u,%.4u,%.4u,%.4u",0,123,1234,12345);
00295     CHECK("0000,0123,1234,12345");
00296     size = SPrintF(buffer,"%.4d,%.4d,%.4d,%.4d",0,123,1234,12345);
00297     CHECK("0000,0123,1234,12345");
00298     size = SPrintF(buffer,"%.4d,%.4d,%.4d,%.4d",-12,-123,-1234,-12345);
00299     CHECK("-0012,-0123,-1234,-12345");
00300     size = SPrintF(buffer,"%.4x,%.4x,%.4x,%.4x",0,0x123,0x1234,0x12345);
00301     CHECK("0000,0123,1234,12345");
00302     size = SPrintF(buffer,"%#.4x,%#.4x,%#.4x,%#.4x",0,0x123,0x1234,0x12345);
00303     CHECK("0000,0x0123,0x1234,0x12345");
00304     size = SPrintF(buffer,"%04o,%04o,%04o,%04o",0,0123,01234,012345);
00305     CHECK("0000,0123,1234,12345");
00306     size = SPrintF(buffer,"%#.5o,%#.5o,%#.5o,%#.5o",0,0123,01234,012345);
00307     CHECK("00000,00123,01234,012345");
00308 
00309     size = SPrintF(buffer,"%06.4d,%06.4d,%06.4d,%06.4d",0,123,1234,12345);
00310     CHECK("  0000,  0123,  1234, 12345");
00311     size = SPrintF(buffer,"%06.4d,%06.4d,%06.4d,%06.4d",-12,-123,-1234,-12345);
00312     CHECK(" -0012, -0123, -1234,-12345");
00313 
00314     size = SPrintF(buffer,"%.0u,%.0u,%.0u,%.0u",0,1,12,123);
00315     CHECK(",1,12,123");
00316     size = SPrintF(buffer,"%.u,%.u,%.u,%.u",0,1,12,123);
00317     CHECK(",1,12,123");
00318 
00319     size = SPrintF(buffer,"%#.0x,%#.0x,%#.4x,%#.4x",0,0x123,0x1234,0x12345);
00320     CHECK(",0x123,0x1234,0x12345");
00321     size = SPrintF(buffer,"%.2s%.0s%.2s%.4s","","q","abc","xyz");
00322     CHECK("abxyz");
00323 
00324     // test '*' ...
00325 
00326     size = SPrintF(buffer,"%.*u,%.*u,%.*u,%.*u",4,0,4,123,4,1234,4,12345);
00327     CHECK("0000,0123,1234,12345");
00328     size = SPrintF(buffer,"%.*u,%.*u,%.*u,%.*u",-4,0,-4,123,-4,1234,-4,12345);
00329     CHECK("0,123,1234,12345");
00330     size = SPrintF(buffer,"%*u,%*u,%*u,%*u",4,0,4,123,4,1234,4,12345);
00331     CHECK("   0, 123,1234,12345");
00332     size = SPrintF(buffer,"%*u,%*u,%*u,%*u",-4,0,-4,123,-4,1234,-4,12345);
00333     CHECK("0   ,123 ,1234,12345");
00334     size = SPrintF(buffer,"%*.*u,%*.*u",6,4,123,-6,4,123);
00335     CHECK("  0123,0123  ");
00336 
00337     // test sign character specifiers with width and precission...
00338 
00339     size = SPrintF(buffer,"%+3d,%+3d,%+3d",-1,0,1);
00340     CHECK(" -1, +0, +1");
00341     size = SPrintF(buffer,"%+-3d,%+-3d,%+-3d",-1,0,1);
00342     CHECK("-1 ,+0 ,+1 ");
00343     size = SPrintF(buffer,"%+03d,%+03d,%+03d",-1,0,1);
00344     CHECK("-01,+00,+01");
00345     size = SPrintF(buffer,"%+.0d,%+.0d,%+.0d",-1,0,1);
00346     CHECK("-1,+,+1");
00347     size = SPrintF(buffer,"% +.0d,% +.0d,% +.0d",-1,0,1);
00348     CHECK("-1,+,+1");
00349 
00350     // test bad format...
00351 
00352     size = SPrintF(buffer,"%");
00353     CHECK("");
00354     size = SPrintF(buffer,"%\0x");
00355     CHECK("");
00356     size = SPrintF(buffer,",%0.1");
00357     CHECK(",");
00358     size = SPrintF(buffer,",%0.1-");
00359     CHECK(",");
00360 
00361     // test hex dump...
00362 
00363     ASSERT_COMPILE(sizeof(void*)==sizeof(uint32_t));
00364     const char* dump = "\nbcdefg ijklmno\377";
00365     size = HexDumpLine(buffer, dump, 17, 0x12345678-(uintptr_t)dump);
00366     CHECK("12345678  0a 62 63 64  65 66 67 20  69 6a 6b 6c  6d 6e 6f ff  .bcdefg ijklmno.\n");
00367     size = HexDumpLine(buffer, dump, 15, 0x123456-(uintptr_t)dump);
00368     CHECK("00123456  0a 62 63 64  65 66 67 20  69 6a 6b 6c  6d 6e 6f     .bcdefg ijklmno \n");
00369     }

Generated by  doxygen 1.6.1