aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2023-01-10 14:30:37 +0100
committerPierre Ossman <ossman@cendio.se>2023-02-04 14:03:13 +0100
commit337dbc392253af92b0577da062a5abc1d032b1ef (patch)
treee540dc7dc861f575d841970561651a9fac506120 /tests
parentdde95fccca9fffff0da2dc486d639b162115bb9e (diff)
downloadtigervnc-337dbc392253af92b0577da062a5abc1d032b1ef.tar.gz
tigervnc-337dbc392253af92b0577da062a5abc1d032b1ef.zip
Return std::string instead of dynamic allocations
We mostly use classical C strings, but the memory management around them can get confusing and error prone. Let's use std::string for the cases where we need to return a newly allocated string.
Diffstat (limited to 'tests')
-rw-r--r--tests/perf/fbperf.cxx9
-rw-r--r--tests/unit/convertlf.cxx16
-rw-r--r--tests/unit/hostport.cxx8
-rw-r--r--tests/unit/unicode.cxx16
4 files changed, 18 insertions, 31 deletions
diff --git a/tests/perf/fbperf.cxx b/tests/perf/fbperf.cxx
index 5d626602..dddf19fb 100644
--- a/tests/perf/fbperf.cxx
+++ b/tests/perf/fbperf.cxx
@@ -322,7 +322,6 @@ static void dotest(TestWindow* win)
double time[3];
double delay, rate;
- char s[1024];
// Run the test several times at different resolutions...
dosubtest(win, 800, 600, &pixels[0], &frames[0], &time[0]);
@@ -369,11 +368,9 @@ static void dotest(TestWindow* win)
}
fprintf(stderr, "Rendering delay: %g ms/frame\n", delay * 1000.0);
- if (rate == 0.0)
- strcpy(s, "N/A pixels/s");
- else
- rfb::siPrefix(1.0 / rate, "pixels/s", s, sizeof(s));
- fprintf(stderr, "Rendering rate: %s\n", s);
+ fprintf(stderr, "Rendering rate: %s\n",
+ (rate == 0.0) ? "N/A pixels/s" :
+ rfb::siPrefix(1.0 / rate, "pixels/s").c_str());
fprintf(stderr, "Maximum FPS: %g fps @ 1920x1080\n",
1.0 / (delay + rate * 1920 * 1080));
}
diff --git a/tests/unit/convertlf.cxx b/tests/unit/convertlf.cxx
index 4dff9098..1645532a 100644
--- a/tests/unit/convertlf.cxx
+++ b/tests/unit/convertlf.cxx
@@ -50,38 +50,34 @@ static const char* escape(const char* input)
static void testLF(const char* input, const char* expected)
{
- char* output;
+ std::string output;
printf("convertLF(\"%s\"): ", escape(input));
output = rfb::convertLF(input);
- if (strcmp(output, expected) != 0)
- printf("FAILED: got \"%s\"", escape(output));
+ if (output != expected)
+ printf("FAILED: got \"%s\"", escape(output.c_str()));
else
printf("OK");
printf("\n");
fflush(stdout);
-
- rfb::strFree(output);
}
static void testCRLF(const char* input, const char* expected)
{
- char* output;
+ std::string output;
printf("convertCRLF(\"%s\"): ", escape(input));
output = rfb::convertCRLF(input);
- if (strcmp(output, expected) != 0)
- printf("FAILED: got \"%s\"", escape(output));
+ if (output != expected)
+ printf("FAILED: got \"%s\"", escape(output.c_str()));
else
printf("OK");
printf("\n");
fflush(stdout);
-
- rfb::strFree(output);
}
int main(int /*argc*/, char** /*argv*/)
diff --git a/tests/unit/hostport.cxx b/tests/unit/hostport.cxx
index 93ebc4c7..d82c3d51 100644
--- a/tests/unit/hostport.cxx
+++ b/tests/unit/hostport.cxx
@@ -27,23 +27,21 @@
static void doTest(const char* hostAndPort,
const char* expectedHost, int expectedPort)
{
- char* host;
+ std::string host;
int port;
printf("\"%s\": ", hostAndPort);
rfb::getHostAndPort(hostAndPort, &host, &port);
- if (strcmp(host, expectedHost) != 0)
- printf("FAILED (\"%s\" != \"%s\")", host, expectedHost);
+ if (host != expectedHost)
+ printf("FAILED (\"%s\" != \"%s\")", host.c_str(), expectedHost);
else if (port != expectedPort)
printf("FAILED (%d != %d)", port, expectedPort);
else
printf("OK");
printf("\n");
fflush(stdout);
-
- rfb::strFree(host);
}
int main(int /*argc*/, char** /*argv*/)
diff --git a/tests/unit/unicode.cxx b/tests/unit/unicode.cxx
index 0cd0f077..32452ee3 100644
--- a/tests/unit/unicode.cxx
+++ b/tests/unit/unicode.cxx
@@ -93,8 +93,8 @@ int main(int /*argc*/, char** /*argv*/)
unsigned ucs4;
char utf8[5];
wchar_t utf16[3];
- char *out;
- wchar_t *wout;
+ std::string out;
+ std::wstring wout;
size_t len;
failures = 0;
@@ -157,20 +157,18 @@ int main(int /*argc*/, char** /*argv*/)
continue;
out = rfb::latin1ToUTF8(latin1utf8[i].latin1);
- if (strcmp(out, latin1utf8[i].utf8) != 0) {
+ if (out != latin1utf8[i].utf8) {
printf("FAILED: latin1ToUTF8() #%d\n", (int)i+1);
failures++;
}
- rfb::strFree(out);
}
for (i = 0;i < ARRAY_SIZE(latin1utf8);i++) {
out = rfb::utf8ToLatin1(latin1utf8[i].utf8);
- if (strcmp(out, latin1utf8[i].latin1) != 0) {
+ if (out != latin1utf8[i].latin1) {
printf("FAILED: utf8ToLatin1() #%d\n", (int)i+1);
failures++;
}
- rfb::strFree(out);
}
for (i = 0;i < ARRAY_SIZE(utf8utf16);i++) {
@@ -179,11 +177,10 @@ int main(int /*argc*/, char** /*argv*/)
continue;
out = rfb::utf16ToUTF8(utf8utf16[i].utf16);
- if (strcmp(out, utf8utf16[i].utf8) != 0) {
+ if (out != utf8utf16[i].utf8) {
printf("FAILED: utf16ToUTF8() #%d\n", (int)i+1);
failures++;
}
- rfb::strFree(out);
}
for (i = 0;i < ARRAY_SIZE(utf8utf16);i++) {
@@ -192,11 +189,10 @@ int main(int /*argc*/, char** /*argv*/)
continue;
wout = rfb::utf8ToUTF16(utf8utf16[i].utf8);
- if (wcscmp(wout, utf8utf16[i].utf16) != 0) {
+ if (wout != utf8utf16[i].utf16) {
printf("FAILED: utf8ToUTF16() #%d\n", (int)i+1);
failures++;
}
- rfb::strFree(wout);
}
if (failures == 0) {