tests: fix memory leaks

This commit is contained in:
Sébastien Helleu 2014-08-14 19:13:32 +02:00
parent 00e79af39c
commit 99b7aa9d27
5 changed files with 131 additions and 90 deletions

View File

@ -44,7 +44,7 @@ set(LIB_WEECHAT_UNIT_TESTS_SRC
add_library(weechat_unit_tests STATIC ${LIB_WEECHAT_UNIT_TESTS_SRC})
# binary to run tests
set(WEECHAT_TESTS_SRC tests.cpp)
set(WEECHAT_TESTS_SRC tests.cpp tests.h)
add_executable(tests ${WEECHAT_TESTS_SRC})
set(LIBS
${LIBS}

View File

@ -51,6 +51,7 @@ tests_LDADD = ./../src/core/lib_weechat_core.a \
$(CPPUTEST_LFLAGS) \
-lm
tests_SOURCES = tests.cpp
tests_SOURCES = tests.cpp \
tests.h
EXTRA_DIST = CMakeLists.txt

35
tests/tests.h Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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 3 of the License, or
* (at your option) any later version.
*
* WeeChat 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 WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WEECHAT_TESTS_H
#define WEECHAT_TESTS_H 1
#define WEE_TEST_STR(__result, __test) \
str = __test; \
if (__result == NULL) \
{ \
POINTERS_EQUAL(NULL, str); \
} \
else \
{ \
STRCMP_EQUAL(__result, str); \
} \
free (str);
#endif /* WEECHAT_TESTS_H */

View File

@ -26,6 +26,7 @@ extern "C"
#include <stdio.h>
#include <string.h>
#include <regex.h>
#include "../tests/tests.h"
#include "../src/core/wee-string.h"
}
@ -62,6 +63,7 @@ extern "C"
else \
{ \
STRCMP_EQUAL(__result_replace, result); \
free (result); \
} \
if (__result_regex == 0) \
regfree(&regex);
@ -80,6 +82,7 @@ extern "C"
else \
{ \
STRCMP_EQUAL(__result_replace, result); \
free (result); \
} \
if (__result_errors >= 0) \
{ \
@ -336,19 +339,21 @@ TEST(String, ExpandHome)
TEST(String, RemoveQuotes)
{
POINTERS_EQUAL(NULL, string_remove_quotes (NULL, NULL));
POINTERS_EQUAL(NULL, string_remove_quotes (NULL, "abc"));
POINTERS_EQUAL(NULL, string_remove_quotes ("abc", NULL));
STRCMP_EQUAL("", string_remove_quotes("", ""));
STRCMP_EQUAL("", string_remove_quotes("", "\"'"));
STRCMP_EQUAL("abc", string_remove_quotes("abc", "\"'"));
STRCMP_EQUAL(" abc ", string_remove_quotes(" abc ", "\"'"));
STRCMP_EQUAL("abc", string_remove_quotes("'abc'", "\"'"));
STRCMP_EQUAL("abc", string_remove_quotes(" 'abc' ", "\"'"));
STRCMP_EQUAL("'abc'", string_remove_quotes("\"'abc'\"", "\"'"));
STRCMP_EQUAL("'abc'", string_remove_quotes(" \"'abc'\" ", "\"'"));
STRCMP_EQUAL("'a'b'c'", string_remove_quotes("\"'a'b'c'\"", "\"'"));
STRCMP_EQUAL("'a'b'c'", string_remove_quotes(" \"'a'b'c'\" ", "\"'"));
char *str;
WEE_TEST_STR(NULL, string_remove_quotes (NULL, NULL));
WEE_TEST_STR(NULL, string_remove_quotes (NULL, "abc"));
WEE_TEST_STR(NULL, string_remove_quotes ("abc", NULL));
WEE_TEST_STR("", string_remove_quotes("", ""));
WEE_TEST_STR("", string_remove_quotes("", "\"'"));
WEE_TEST_STR("abc", string_remove_quotes("abc", "\"'"));
WEE_TEST_STR(" abc ", string_remove_quotes(" abc ", "\"'"));
WEE_TEST_STR("abc", string_remove_quotes("'abc'", "\"'"));
WEE_TEST_STR("abc", string_remove_quotes(" 'abc' ", "\"'"));
WEE_TEST_STR("'abc'", string_remove_quotes("\"'abc'\"", "\"'"));
WEE_TEST_STR("'abc'", string_remove_quotes(" \"'abc'\" ", "\"'"));
WEE_TEST_STR("'a'b'c'", string_remove_quotes("\"'a'b'c'\"", "\"'"));
WEE_TEST_STR("'a'b'c'", string_remove_quotes(" \"'a'b'c'\" ", "\"'"));
}
/*
@ -358,14 +363,16 @@ TEST(String, RemoveQuotes)
TEST(String, Strip)
{
POINTERS_EQUAL(NULL, string_strip (NULL, 1, 1, NULL));
POINTERS_EQUAL(NULL, string_strip (NULL, 1, 1, ".;"));
STRCMP_EQUAL("test", string_strip ("test", 1, 1, NULL));
STRCMP_EQUAL("test", string_strip ("test", 1, 1, ".;"));
STRCMP_EQUAL(".-test.-", string_strip (".-test.-", 0, 0, ".-"));
STRCMP_EQUAL("test", string_strip (".-test.-", 1, 1, ".-"));
STRCMP_EQUAL("test.-", string_strip (".-test.-", 1, 0, ".-"));
STRCMP_EQUAL(".-test", string_strip (".-test.-", 0, 1, ".-"));
char *str;
WEE_TEST_STR(NULL, string_strip (NULL, 1, 1, NULL));
WEE_TEST_STR(NULL, string_strip (NULL, 1, 1, ".;"));
WEE_TEST_STR("test", string_strip ("test", 1, 1, NULL));
WEE_TEST_STR("test", string_strip ("test", 1, 1, ".;"));
WEE_TEST_STR(".-test.-", string_strip (".-test.-", 0, 0, ".-"));
WEE_TEST_STR("test", string_strip (".-test.-", 1, 1, ".-"));
WEE_TEST_STR("test.-", string_strip (".-test.-", 1, 0, ".-"));
WEE_TEST_STR(".-test", string_strip (".-test.-", 0, 1, ".-"));
}
/*
@ -375,29 +382,31 @@ TEST(String, Strip)
TEST(String, ConvertEscapedChars)
{
POINTERS_EQUAL(NULL, string_convert_escaped_chars (NULL));
STRCMP_EQUAL("", string_convert_escaped_chars (""));
STRCMP_EQUAL("\"", string_convert_escaped_chars ("\\\""));
STRCMP_EQUAL("\\", string_convert_escaped_chars ("\\\\"));
STRCMP_EQUAL("\a", string_convert_escaped_chars ("\\a"));
STRCMP_EQUAL("\a", string_convert_escaped_chars ("\\a"));
STRCMP_EQUAL("\b", string_convert_escaped_chars ("\\b"));
STRCMP_EQUAL("\e", string_convert_escaped_chars ("\\e"));
STRCMP_EQUAL("\f", string_convert_escaped_chars ("\\f"));
STRCMP_EQUAL("\n", string_convert_escaped_chars ("\\n"));
STRCMP_EQUAL("\r", string_convert_escaped_chars ("\\r"));
STRCMP_EQUAL("\t", string_convert_escaped_chars ("\\t"));
STRCMP_EQUAL("\v", string_convert_escaped_chars ("\\v"));
STRCMP_EQUAL("\123", string_convert_escaped_chars ("\\0123"));
STRCMP_EQUAL("\123",
char *str;
WEE_TEST_STR(NULL, string_convert_escaped_chars (NULL));
WEE_TEST_STR("", string_convert_escaped_chars (""));
WEE_TEST_STR("\"", string_convert_escaped_chars ("\\\""));
WEE_TEST_STR("\\", string_convert_escaped_chars ("\\\\"));
WEE_TEST_STR("\a", string_convert_escaped_chars ("\\a"));
WEE_TEST_STR("\a", string_convert_escaped_chars ("\\a"));
WEE_TEST_STR("\b", string_convert_escaped_chars ("\\b"));
WEE_TEST_STR("\e", string_convert_escaped_chars ("\\e"));
WEE_TEST_STR("\f", string_convert_escaped_chars ("\\f"));
WEE_TEST_STR("\n", string_convert_escaped_chars ("\\n"));
WEE_TEST_STR("\r", string_convert_escaped_chars ("\\r"));
WEE_TEST_STR("\t", string_convert_escaped_chars ("\\t"));
WEE_TEST_STR("\v", string_convert_escaped_chars ("\\v"));
WEE_TEST_STR("\123", string_convert_escaped_chars ("\\0123"));
WEE_TEST_STR("\123",
string_convert_escaped_chars ("\\0123")); /* invalid */
STRCMP_EQUAL("\x41", string_convert_escaped_chars ("\\x41"));
STRCMP_EQUAL("\x04z", string_convert_escaped_chars ("\\x4z"));
STRCMP_EQUAL("\u0012zz", string_convert_escaped_chars ("\\u12zz"));
STRCMP_EQUAL("\U00123456", string_convert_escaped_chars ("\\U00123456"));
STRCMP_EQUAL("\U00000123zzz",
WEE_TEST_STR("\x41", string_convert_escaped_chars ("\\x41"));
WEE_TEST_STR("\x04z", string_convert_escaped_chars ("\\x4z"));
WEE_TEST_STR("\u0012zz", string_convert_escaped_chars ("\\u12zz"));
WEE_TEST_STR("\U00123456", string_convert_escaped_chars ("\\U00123456"));
WEE_TEST_STR("\U00000123zzz",
string_convert_escaped_chars ("\\U00123zzz"));
STRCMP_EQUAL("",
WEE_TEST_STR("",
string_convert_escaped_chars ("\\U12345678")); /* invalid */
}
@ -429,13 +438,15 @@ TEST(String, IsWordChar)
TEST(String, MaskToRegex)
{
POINTERS_EQUAL(NULL, string_mask_to_regex (NULL));
STRCMP_EQUAL("", string_mask_to_regex (""));
STRCMP_EQUAL("test", string_mask_to_regex ("test"));
STRCMP_EQUAL("test.*", string_mask_to_regex ("test*"));
STRCMP_EQUAL(".*test.*", string_mask_to_regex ("*test*"));
STRCMP_EQUAL(".*te.*st.*", string_mask_to_regex ("*te*st*"));
STRCMP_EQUAL("test\\.\\[\\]\\{\\}\\(\\)\\?\\+\\|\\^\\$\\\\",
char *str;
WEE_TEST_STR(NULL, string_mask_to_regex (NULL));
WEE_TEST_STR("", string_mask_to_regex (""));
WEE_TEST_STR("test", string_mask_to_regex ("test"));
WEE_TEST_STR("test.*", string_mask_to_regex ("test*"));
WEE_TEST_STR(".*test.*", string_mask_to_regex ("*test*"));
WEE_TEST_STR(".*te.*st.*", string_mask_to_regex ("*te*st*"));
WEE_TEST_STR("test\\.\\[\\]\\{\\}\\(\\)\\?\\+\\|\\^\\$\\\\",
string_mask_to_regex ("test.[]{}()?+|^$\\"));
}
@ -569,17 +580,19 @@ test_replace_cb (void *data, const char *text)
TEST(String, Replace)
{
POINTERS_EQUAL(NULL, string_replace (NULL, NULL, NULL));
POINTERS_EQUAL(NULL, string_replace ("string", NULL, NULL));
POINTERS_EQUAL(NULL, string_replace (NULL, "search", NULL));
POINTERS_EQUAL(NULL, string_replace (NULL, NULL, "replace"));
POINTERS_EQUAL(NULL, string_replace ("string", "search", NULL));
POINTERS_EQUAL(NULL, string_replace ("string", NULL, "replace"));
POINTERS_EQUAL(NULL, string_replace (NULL, "search", "replace"));
char *str;
STRCMP_EQUAL("test abc def", string_replace("test abc def", "xyz", "xxx"));
STRCMP_EQUAL("test xxx def", string_replace("test abc def", "abc", "xxx"));
STRCMP_EQUAL("xxx test xxx def xxx",
WEE_TEST_STR(NULL, string_replace (NULL, NULL, NULL));
WEE_TEST_STR(NULL, string_replace ("string", NULL, NULL));
WEE_TEST_STR(NULL, string_replace (NULL, "search", NULL));
WEE_TEST_STR(NULL, string_replace (NULL, NULL, "replace"));
WEE_TEST_STR(NULL, string_replace ("string", "search", NULL));
WEE_TEST_STR(NULL, string_replace ("string", NULL, "replace"));
WEE_TEST_STR(NULL, string_replace (NULL, "search", "replace"));
WEE_TEST_STR("test abc def", string_replace("test abc def", "xyz", "xxx"));
WEE_TEST_STR("test xxx def", string_replace("test abc def", "abc", "xxx"));
WEE_TEST_STR("xxx test xxx def xxx",
string_replace("abc test abc def abc", "abc", "xxx"));
}
@ -859,28 +872,24 @@ TEST(String, Iconv)
FILE *f;
/* string_iconv */
POINTERS_EQUAL(NULL, string_iconv (0, NULL, NULL, NULL));
STRCMP_EQUAL("", string_iconv (0, NULL, NULL, ""));
STRCMP_EQUAL("abc", string_iconv (0, NULL, NULL, "abc"));
STRCMP_EQUAL("abc", string_iconv (1, "UTF-8", "ISO-8859-15", "abc"));
STRCMP_EQUAL(noel_iso,
string_iconv (1, "UTF-8", "ISO-8859-15", noel_utf8));
STRCMP_EQUAL(noel_utf8,
string_iconv (0, "ISO-8859-15", "UTF-8", noel_iso));
WEE_TEST_STR(NULL, string_iconv (0, NULL, NULL, NULL));
WEE_TEST_STR("", string_iconv (0, NULL, NULL, ""));
WEE_TEST_STR("abc", string_iconv (0, NULL, NULL, "abc"));
WEE_TEST_STR("abc", string_iconv (1, "UTF-8", "ISO-8859-15", "abc"));
WEE_TEST_STR(noel_iso, string_iconv (1, "UTF-8", "ISO-8859-15", noel_utf8));
WEE_TEST_STR(noel_utf8, string_iconv (0, "ISO-8859-15", "UTF-8", noel_iso));
/* string_iconv_to_internal */
POINTERS_EQUAL(NULL, string_iconv_to_internal (NULL, NULL));
STRCMP_EQUAL("", string_iconv_to_internal (NULL, ""));
STRCMP_EQUAL("abc", string_iconv_to_internal (NULL, "abc"));
STRCMP_EQUAL(noel_utf8,
string_iconv_to_internal ("ISO-8859-15", noel_iso));
WEE_TEST_STR(NULL, string_iconv_to_internal (NULL, NULL));
WEE_TEST_STR("", string_iconv_to_internal (NULL, ""));
WEE_TEST_STR("abc", string_iconv_to_internal (NULL, "abc"));
WEE_TEST_STR(noel_utf8, string_iconv_to_internal ("ISO-8859-15", noel_iso));
/* string_iconv_from_internal */
POINTERS_EQUAL(NULL, string_iconv_from_internal (NULL, NULL));
STRCMP_EQUAL("", string_iconv_from_internal (NULL, ""));
STRCMP_EQUAL("abc", string_iconv_from_internal (NULL, "abc"));
STRCMP_EQUAL(noel_iso,
string_iconv_from_internal ("ISO-8859-15", noel_utf8));
WEE_TEST_STR(NULL, string_iconv_from_internal (NULL, NULL));
WEE_TEST_STR("", string_iconv_from_internal (NULL, ""));
WEE_TEST_STR("abc", string_iconv_from_internal (NULL, "abc"));
WEE_TEST_STR(noel_iso, string_iconv_from_internal ("ISO-8859-15", noel_utf8));
/* string_iconv_fprintf */
f = fopen ("/dev/null", "w");

View File

@ -26,14 +26,10 @@ extern "C"
#include <stdio.h>
#include <string.h>
#include <wctype.h>
#include "../tests/tests.h"
#include "../src/core/wee-utf8.h"
}
#define WEE_CHECK_STRNDUP(__result, __string, __length) \
str = utf8_strndup (__string, __length); \
STRCMP_EQUAL(__result, str); \
free (str);
const char *noel_valid = "no\xc3\xabl"; /* noël */
const char *noel_invalid = "no\xc3l";
const char *noel_invalid2 = "no\xff\xffl";
@ -337,10 +333,10 @@ TEST(Utf8, Duplicate)
{
char *str;
WEE_CHECK_STRNDUP("", noel_valid, 0);
WEE_CHECK_STRNDUP("n", noel_valid, 1);
WEE_CHECK_STRNDUP("no", noel_valid, 2);
WEE_CHECK_STRNDUP("noë", noel_valid, 3);
WEE_CHECK_STRNDUP("noël", noel_valid, 4);
WEE_CHECK_STRNDUP("noël", noel_valid, 5);
WEE_TEST_STR("", utf8_strndup (noel_valid, 0));
WEE_TEST_STR("n", utf8_strndup (noel_valid, 1));
WEE_TEST_STR("no", utf8_strndup (noel_valid, 2));
WEE_TEST_STR("noë", utf8_strndup (noel_valid, 3));
WEE_TEST_STR("noël", utf8_strndup (noel_valid, 4));
WEE_TEST_STR("noël", utf8_strndup (noel_valid, 5));
}