File: NStringTest.cpp

package info (click to toggle)
nzbget 21.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,128 kB
  • sloc: cpp: 62,884; sh: 5,311; python: 1,381; makefile: 491
file content (189 lines) | stat: -rw-r--r-- 5,113 bytes parent folder | download | duplicates (4)
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
/*
 *  This file is part of nzbget. See <http://nzbget.net>.
 *
 *  Copyright (C) 2015-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
									 

#include "nzbget.h"

#include "catch.h"
					   
#include "NString.h"


TEST_CASE("BString", "[NString][Quick]")
{
	BString<100> str;
	REQUIRE(sizeof(str) == sizeof(char[100]));
	REQUIRE(str.Empty());
	REQUIRE(str);
	str = "Hello, world";
	REQUIRE(!str.Empty());
	REQUIRE(str);
	REQUIRE(!strcmp(str, "Hello, world"));

	str.Format("Hi, %s%c: %i", "World", '!', 21);
	REQUIRE(!strcmp(str, "Hi, World!: 21"));

	BString<20> str2;
	str2 = "Hello, world 01234567890123456789";
	REQUIRE(!strcmp(str2, "Hello, world 012345"));

	str2.Format("0123456789 Hi, %s%c: %i", "World", '!', 21);
	REQUIRE(!strcmp(str2, "0123456789 Hi, Worl"));

	BString<20> str3;
	memcpy(str3, "Hello, 0123456789 world", str3.Capacity());
	str3[str3.Capacity()] = '\0';
	REQUIRE(!strcmp(str3, "Hello, 0123456789 w"));

	str3 = "String 3 Test World, Hello!";
	REQUIRE(!strcmp(str3, "String 3 Test World"));

	BString<100> str4;
	str4 = "String 4 initialized";
	REQUIRE(!strcmp(str4, "String 4 initialized"));

	BString<20> str5("Hi, %s%c: %i", "World", '!', 21);
	REQUIRE(!strcmp(str5, "Hi, World!: 21"));

	BString<20> str6;
	str6.Append("Hello, World");
	str6.Append("String5String5");
	str6.Append("67");
	str6.Append("0123456789", 5);
	REQUIRE(!strcmp(str6, "Hello, WorldString5"));

	BString<20> str7;
	str7.Append("0123456789", 5);
	REQUIRE(!strcmp(str7, "01234"));

	BString<20> str8;
	str8.Append("0123456789", 5);
	str8.AppendFmt("%i:%i", 87, 65);
	REQUIRE(!strcmp(str8, "0123487:65"));

	const char* txt = "String 9 initialized";
	BString<100> str9 = txt;
	REQUIRE(!strcmp(str9, "String 9 initialized"));
}

TEST_CASE("CString", "[NString][Quick]")
{
	CString str;
	REQUIRE(sizeof(str) == sizeof(char*));
	REQUIRE(str.Empty());
	REQUIRE(!str);
	str = "Hello, world";
	REQUIRE(!str.Empty());
	REQUIRE(str);
	REQUIRE(!strcmp(str, "Hello, world"));

	str.Format("Hi, %s%c: %i", "World", '!', 21);
	REQUIRE(!strcmp(str, "Hi, World!: 21"));

	char* tmp = strdup("Hello there");
	CString str2;
	str2.Bind(tmp);
	const char* tmp3 = *str2;
	REQUIRE(tmp == tmp3);
	REQUIRE(!strcmp(str2, "Hello there"));
	REQUIRE(tmp == *str2);
	free(tmp);

	char* tmp2 = str2.Unbind();
	REQUIRE(tmp2 == tmp);
	REQUIRE(str2.Empty());
	REQUIRE(*str2 == nullptr);

	CString str3("World 12345678901234567890");
	char buf[50];
	snprintf(buf, sizeof(buf), "Hi, %s%c: %i", *str3, '!', 21);
	REQUIRE(!strcmp(buf, "Hi, World 12345678901234567890!: 21"));

	CString str4;
	REQUIRE(*str4 == nullptr);
	REQUIRE((char*)str4 == nullptr);
	REQUIRE((const char*)str4 == nullptr);
	REQUIRE(str4.Str() != nullptr);
	REQUIRE(*str4.Str() == '\0');

	CString str6;
	str6.Append("");
	str6.Append("Hello, World");
	str6.Append("String5String5");
	str6.Append("67");
	REQUIRE(!strcmp(str6, "Hello, WorldString5String567"));

	str6.Clear();
	str6.Append("0123456789", 5);
	str6.AppendFmt("%i:%i", 87, 65);
	REQUIRE(!strcmp(str6, "0123487:65"));

	std::vector<CString> vec1;
	vec1.push_back("Hello, there");
	CString& str7 = vec1.back();
	REQUIRE(!strcmp(str7, "Hello, there"));

	REQUIRE(!strcmp(CString::FormatStr("Many %s ago", "days"), "Many days ago"));

	CString str8("Hello, World");
	str8.Replace(1, 4, "i");
	REQUIRE(!strcmp(str8, "Hi, World"));
	str8.Replace(4, 5, "everybody");
	REQUIRE(!strcmp(str8, "Hi, everybody"));
	str8.Replace(4, 5, "nome", 2);
	REQUIRE(!strcmp(str8, "Hi, nobody"));

	CString str9 = " Long string with spaces \t\r\n ";
	str9.TrimRight();
	REQUIRE(!strcmp(str9, " Long string with spaces"));
	int pos = str9.Find("with");
	REQUIRE(pos == 13);
	str9.Replace("string", "with");
	REQUIRE(!strcmp(str9, " Long with with spaces"));
	str9.Replace("with", "without");
	REQUIRE(!strcmp(str9, " Long without without spaces"));
	str9.Replace("without", "");
	REQUIRE(!strcmp(str9, " Long   spaces"));

	str8 = "test string";
	str9 = "test string";
	REQUIRE(str8 == str9);
	bool eq = str8 == "test string";
	REQUIRE(eq);
}

TEST_CASE("StringBuilder", "[NString][Quick]")
{
	StringBuilder str6;
	str6.Append("");
	str6.Append("Hello, World");
	str6.Append("String5String5");
	str6.Append("67");
	REQUIRE(!strcmp(str6, "Hello, WorldString5String567"));
}

TEST_CASE("CharBuffer", "[NString][Quick]")
{
	CharBuffer buf(1024);
	REQUIRE(buf.Size() == 1024);
	buf.Reserve(2048);
	REQUIRE(buf.Size() == 2048);
	buf.Clear();
	REQUIRE(buf.Size() == 0);
}