Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.0 KiB

  1. #include "tier0/dbg.h"
  2. #include "unitlib/unitlib.h"
  3. #include "tier1/utlstring.h"
  4. DEFINE_TESTSUITE( UtlStringTestSuite )
  5. static void ConstructorTests()
  6. {
  7. CUtlString string;
  8. Shipping_Assert( string.Length() == 0 );
  9. Shipping_Assert( string.IsEmpty() == true );
  10. Shipping_Assert( string.GetForModify() && string.GetForModify()[ 0 ] == 0 );
  11. CUtlString string2( "shiz" );
  12. Shipping_Assert( string2.Length() == 4 );
  13. Shipping_Assert( !V_stricmp( string2.Get(), "shiz" ) );
  14. CUtlString string3( "thisstringismuchlongerthantwentywholehugecharacters", 20 );
  15. Shipping_Assert( string3.Length() == 20 );
  16. }
  17. static void BasicFunctionalityTests()
  18. {
  19. CUtlString empty;
  20. empty.SetLength( 10 );
  21. Assert( empty.Length() == 10 );
  22. V_memcpy( empty.GetForModify(), "blah", 4 );
  23. empty.GetForModify()[ 4 ] = 0;
  24. Assert( empty.Length() == 4 );
  25. Shipping_Assert( !V_stricmp( empty.Get(), "blah" ) );
  26. empty.Clear();
  27. Shipping_Assert( empty.IsEmpty() );
  28. empty = "blah";
  29. Shipping_Assert( !empty.IsEmpty() );
  30. empty.Purge();
  31. Shipping_Assert( empty.IsEmpty() );
  32. empty = "CaMeLcAsE";
  33. Shipping_Assert( empty.IsEqual_CaseSensitive( "CaMeLcAsE" ) );
  34. Shipping_Assert( empty.IsEqual_CaseInsensitive( "camelCASE" ) );
  35. CUtlString copy( empty );
  36. Shipping_Assert( empty == copy );
  37. empty.ToLower();
  38. Shipping_Assert( empty != copy );
  39. empty.Append( "271" );
  40. Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271" ) );
  41. empty.Append( "35123", 3 );
  42. Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351" ) );
  43. empty.Append( 'A' );
  44. Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351A" ) );
  45. empty.Append( '/' );
  46. empty.Append( '\\' );
  47. Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351A/\\" ) );
  48. empty.StripTrailingSlash();
  49. empty.StripTrailingSlash();
  50. Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351A" ) );
  51. empty = "sometext";
  52. empty.SetLength( 4 );
  53. Shipping_Assert( empty.Get()[ 4 ] == '\0' ); // Check for null terminator
  54. Shipping_Assert( empty.Length() == 4 );
  55. Shipping_Assert( empty == "some" );
  56. }
  57. static void TrimAPITests()
  58. {
  59. CUtlString orig( " testy " );
  60. CUtlString orig2( "\n \n\ttesty\t\r\n \n\t\r" );
  61. CUtlString s;
  62. s = orig;
  63. s.TrimLeft( ' ' );
  64. Shipping_Assert( !V_stricmp( s.Get(), "testy " ) );
  65. s = orig;
  66. s.TrimRight( ' ' );
  67. Shipping_Assert( !V_stricmp( s.Get(), " testy" ) );
  68. s = orig2;
  69. s.TrimLeft();
  70. s.TrimRight();
  71. Shipping_Assert( !V_stricmp( s.Get(), "testy" ) );
  72. s = orig;
  73. s.Trim( ' ' );
  74. Shipping_Assert( !V_stricmp( s.Get(), "testy" ) );
  75. s = orig2;
  76. s.Trim();
  77. Shipping_Assert( !V_stricmp( s.Get(), "testy" ) );
  78. }
  79. static void OperatorAPITests()
  80. {
  81. CUtlString orig( "base" );
  82. CUtlString orig2( "different" );
  83. // operator = on CUtlString
  84. orig = orig2;
  85. Shipping_Assert( !V_stricmp( orig.Get(), "different" ) );
  86. // perator = on const char *
  87. orig = "different2";
  88. Shipping_Assert( !V_stricmp( orig.Get(), "different2" ) );
  89. orig = orig2;
  90. // op ==
  91. Shipping_Assert( orig == orig2 );
  92. orig = "base";
  93. // op !=
  94. Shipping_Assert( orig != orig2 );
  95. orig += "1";
  96. Shipping_Assert( !V_stricmp( orig.Get(), "base1" ) );
  97. orig2 = "2";
  98. orig += orig2;
  99. Shipping_Assert( !V_stricmp( orig.Get(), "base12" ) );
  100. orig += '3';
  101. Shipping_Assert( !V_stricmp( orig.Get(), "base123" ) );
  102. // integer
  103. orig += 123;
  104. Shipping_Assert( !V_stricmp( orig.Get(), "base123123" ) );
  105. orig += 1.5f;
  106. Shipping_Assert( !V_stricmp( orig.Get(), "base1231231.5" ) );
  107. orig = "1";
  108. orig2 = "2";
  109. CUtlString newString = orig + orig2;
  110. Shipping_Assert( !V_stricmp( newString.Get(), "12" ) );
  111. newString = orig + "3";
  112. Shipping_Assert( !V_stricmp( newString.Get(), "13" ) );
  113. newString = orig + 42;
  114. Shipping_Assert( !V_stricmp( newString.Get(), "142" ) );
  115. orig = "this is a long string";
  116. newString = orig.Slice( 4 );
  117. Shipping_Assert( !V_stricmp( newString.Get(), " is a long string" ) );
  118. newString = orig.Slice( 5, 10 );
  119. Shipping_Assert( !V_stricmp( newString.Get(), "is a " ) );
  120. newString = orig.Left( 4 );
  121. Shipping_Assert( !V_stricmp( newString.Get(), "this" ) );
  122. newString = orig.Right( 6 );
  123. Shipping_Assert( !V_stricmp( newString.Get(), "string" ) );
  124. newString = orig.Replace( 's', 'q' );
  125. Shipping_Assert( !V_stricmp( newString.Get(), "thiq iq a long qtring" ) );
  126. }
  127. static void PatternTests()
  128. {
  129. CUtlString str( "this is a very long pattern of very long things" );
  130. CUtlString pattern( "this is*" );
  131. Shipping_Assert( str.MatchesPattern( pattern ) );
  132. pattern = "notpresent";
  133. Shipping_Assert( !str.MatchesPattern( pattern ) );
  134. }
  135. static void FmtStr( CUtlString &str, const char *pFmt, ... )
  136. {
  137. va_list argptr;
  138. va_start( argptr, pFmt );
  139. str.FormatV( pFmt, argptr );
  140. va_end( argptr );
  141. }
  142. static void FormatTests()
  143. {
  144. CUtlString str;
  145. str.Format( "%s %s %i", "shiz", "baz", 1 );
  146. Shipping_Assert( !V_stricmp( str.Get(), "shiz baz 1" ) );
  147. FmtStr( str, "blah%i", 3 );
  148. Shipping_Assert( !V_stricmp( str.Get(), "blah3" ) );
  149. }
  150. static void FileNameAPITests()
  151. {
  152. CUtlString path( "c:\\source2\\game\\source2\\somefile.ext" );
  153. CUtlString absPath = path.AbsPath();
  154. Shipping_Assert( absPath == path );
  155. CUtlString file = path.UnqualifiedFilename();
  156. Shipping_Assert( !V_stricmp( file.Get(), "somefile.ext" ) );
  157. CUtlString dir = path.DirName();
  158. Shipping_Assert( !V_stricmp( dir.Get(), "c:\\source2\\game\\source2" ) );
  159. dir = dir.DirName();
  160. Shipping_Assert( !V_stricmp( dir.Get(), "c:\\source2\\game" ) );
  161. CUtlString baseName = path.StripExtension();
  162. Shipping_Assert( !V_stricmp( baseName.Get(), "c:\\source2\\game\\source2\\somefile" ) );
  163. dir = path.StripFilename();
  164. Shipping_Assert( !V_stricmp( dir.Get(), "c:\\source2\\game\\source2" ) );
  165. file = path.GetBaseFilename();
  166. Shipping_Assert( !V_stricmp( file.Get(), "somefile" ) );
  167. CUtlString ext = path.GetExtension();
  168. Shipping_Assert( !V_stricmp( ext.Get(), "ext" ) );
  169. absPath = path.PathJoin( dir.Get(), file.Get() );
  170. Shipping_Assert( !V_stricmp( absPath.Get(), "c:\\source2\\game\\source2\\somefile" ) );
  171. }
  172. DEFINE_TESTCASE( UtlStringTest, UtlStringTestSuite )
  173. {
  174. Msg( "Running CUtlString tests\n" );
  175. ConstructorTests();
  176. BasicFunctionalityTests();
  177. TrimAPITests();
  178. OperatorAPITests();
  179. PatternTests();
  180. FormatTests();
  181. FileNameAPITests();
  182. }