summaryrefslogtreecommitdiff
path: root/unittests/tier1test/utlstringtest.cpp
blob: c0fe87299f22fe334d2a8b61b1f08959c028033f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "tier0/dbg.h"
#include "unitlib/unitlib.h"
#include "tier1/utlstring.h"

DEFINE_TESTSUITE( UtlStringTestSuite )

static void ConstructorTests()
{
	CUtlString string;
	Shipping_Assert( string.Length() == 0 );
	Shipping_Assert( string.IsEmpty() == true );
	Shipping_Assert( string.GetForModify() && string.GetForModify()[ 0 ] == 0 );

	CUtlString string2( "shiz" );
	Shipping_Assert( string2.Length() == 4 );
	Shipping_Assert( !V_stricmp( string2.Get(), "shiz" ) );

	CUtlString string3( "thisstringismuchlongerthantwentywholehugecharacters", 20 );
	Shipping_Assert( string3.Length() == 20 );
}

static void BasicFunctionalityTests()
{
	CUtlString empty;
	empty.SetLength( 10 );
	Assert( empty.Length() == 10 );
	V_memcpy( empty.GetForModify(), "blah", 4 );
	empty.GetForModify()[ 4 ] = 0;
	Assert( empty.Length() == 4 );
	Shipping_Assert( !V_stricmp( empty.Get(), "blah" ) );

	empty.Clear();
	Shipping_Assert( empty.IsEmpty() );
	empty = "blah";
	Shipping_Assert( !empty.IsEmpty() );
	empty.Purge();
	Shipping_Assert( empty.IsEmpty() );

	empty = "CaMeLcAsE";

	Shipping_Assert( empty.IsEqual_CaseSensitive( "CaMeLcAsE" ) );
	Shipping_Assert( empty.IsEqual_CaseInsensitive( "camelCASE" ) );

	CUtlString copy( empty );
	Shipping_Assert( empty == copy );
	empty.ToLower();
	Shipping_Assert( empty != copy );

	empty.Append( "271" );
	Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271" ) );
	empty.Append( "35123", 3 );
	Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351" ) );
	empty.Append( 'A' );
	Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351A" ) );

	empty.Append( '/' );
	empty.Append( '\\' );
	Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351A/\\" ) );
	empty.StripTrailingSlash();
	empty.StripTrailingSlash();
	Shipping_Assert( !V_stricmp( empty.Get(), "camelcase271351A" ) );

	empty = "sometext";
	empty.SetLength( 4 );
	Shipping_Assert( empty.Get()[ 4 ] == '\0' ); // Check for null terminator
	Shipping_Assert( empty.Length() == 4 );
	Shipping_Assert( empty == "some" );
}

static void TrimAPITests()
{
	CUtlString orig( "  testy  " );
	CUtlString orig2( "\n \n\ttesty\t\r\n \n\t\r" );
	CUtlString s;
	
	s = orig;
	s.TrimLeft( ' ' );
	Shipping_Assert( !V_stricmp( s.Get(), "testy  " ) );

	s = orig;
	s.TrimRight( ' ' );
	Shipping_Assert( !V_stricmp( s.Get(), "  testy" ) );

	s = orig2;
	s.TrimLeft();
	s.TrimRight();
	Shipping_Assert( !V_stricmp( s.Get(), "testy" ) );

	s = orig;
	s.Trim( ' ' );
	Shipping_Assert( !V_stricmp( s.Get(), "testy" ) );
	s = orig2;
	s.Trim();
	Shipping_Assert( !V_stricmp( s.Get(), "testy" ) );
}

static void OperatorAPITests()
{
	CUtlString orig( "base" );
	CUtlString orig2( "different" );

	// operator = on CUtlString
	orig = orig2;
	Shipping_Assert( !V_stricmp( orig.Get(), "different" ) );
	// perator = on const char *
	orig = "different2";
	Shipping_Assert( !V_stricmp( orig.Get(), "different2" ) );
	
	orig = orig2;
	// op ==
	Shipping_Assert( orig == orig2 );
	orig = "base";
	// op !=
	Shipping_Assert( orig != orig2 );

	orig += "1";
	Shipping_Assert( !V_stricmp( orig.Get(), "base1" ) );
	orig2 = "2";
	orig += orig2;
	Shipping_Assert( !V_stricmp( orig.Get(), "base12" ) );
	orig += '3';
	Shipping_Assert( !V_stricmp( orig.Get(), "base123" ) );
	// integer
	orig += 123;
	Shipping_Assert( !V_stricmp( orig.Get(), "base123123" ) );
	orig += 1.5f;
	Shipping_Assert( !V_stricmp( orig.Get(), "base1231231.5" ) );

	orig = "1";
	orig2 = "2";
	CUtlString newString = orig + orig2;
	Shipping_Assert( !V_stricmp( newString.Get(), "12" ) );
	newString = orig + "3";
	Shipping_Assert( !V_stricmp( newString.Get(), "13" ) );
	newString = orig + 42;
	Shipping_Assert( !V_stricmp( newString.Get(), "142" ) );

	orig = "this is a long string";
	newString = orig.Slice( 4 );
	Shipping_Assert( !V_stricmp( newString.Get(), " is a long string" ) );
	newString = orig.Slice( 5, 10 );
	Shipping_Assert( !V_stricmp( newString.Get(), "is a " ) );

	newString = orig.Left( 4 );
	Shipping_Assert( !V_stricmp( newString.Get(), "this" ) );
	newString = orig.Right( 6 );
	Shipping_Assert( !V_stricmp( newString.Get(), "string" ) );
	newString = orig.Replace( 's', 'q' );
	Shipping_Assert( !V_stricmp( newString.Get(), "thiq iq a long qtring" ) );
}

static void PatternTests()
{
	CUtlString str( "this is a very long pattern of very long things" );
	CUtlString pattern( "this is*" );

	Shipping_Assert( str.MatchesPattern( pattern ) );

	pattern = "notpresent";
	Shipping_Assert( !str.MatchesPattern( pattern ) );
}

static void FmtStr( CUtlString &str, const char *pFmt, ... )
{
	va_list argptr;
	va_start( argptr, pFmt );
	str.FormatV( pFmt, argptr );
	va_end( argptr );
}

static void FormatTests()
{
	CUtlString str;
	str.Format( "%s %s %i", "shiz", "baz", 1 );
	Shipping_Assert( !V_stricmp( str.Get(), "shiz baz 1" ) );

	FmtStr( str, "blah%i", 3 );
	Shipping_Assert( !V_stricmp( str.Get(), "blah3" ) );
}

static void FileNameAPITests()
{
	CUtlString path( "c:\\source2\\game\\source2\\somefile.ext" );

	CUtlString absPath = path.AbsPath();
	Shipping_Assert( absPath == path );
	CUtlString file = path.UnqualifiedFilename();
	Shipping_Assert( !V_stricmp( file.Get(), "somefile.ext" ) );
	CUtlString dir = path.DirName();
	Shipping_Assert( !V_stricmp( dir.Get(), "c:\\source2\\game\\source2" ) );
	dir = dir.DirName();
	Shipping_Assert( !V_stricmp( dir.Get(), "c:\\source2\\game" ) );
	CUtlString baseName = path.StripExtension();
	Shipping_Assert( !V_stricmp( baseName.Get(), "c:\\source2\\game\\source2\\somefile" ) );
	dir = path.StripFilename();
	Shipping_Assert( !V_stricmp( dir.Get(), "c:\\source2\\game\\source2" ) );

	file = path.GetBaseFilename();
	Shipping_Assert( !V_stricmp( file.Get(), "somefile" ) );
	CUtlString ext = path.GetExtension();
	Shipping_Assert( !V_stricmp( ext.Get(), "ext" ) );

	absPath = path.PathJoin( dir.Get(), file.Get() );
	Shipping_Assert( !V_stricmp( absPath.Get(), "c:\\source2\\game\\source2\\somefile" ) );
}

DEFINE_TESTCASE( UtlStringTest, UtlStringTestSuite )
{
	Msg( "Running CUtlString tests\n" );

	ConstructorTests();

	BasicFunctionalityTests();

	TrimAPITests();

	OperatorAPITests();

	PatternTests();

	FormatTests();

	FileNameAPITests();
}