Refactorized testio.cpp

This commit is contained in:
PKEuS 2017-03-08 14:23:53 +01:00
parent 8c707876ed
commit a447d2c6a8
1 changed files with 19 additions and 40 deletions

View File

@ -67,7 +67,7 @@ private:
TEST_CASE(testPrintf0WithSuffix); // ticket #7069 TEST_CASE(testPrintf0WithSuffix); // ticket #7069
} }
void check(const std::string &code, bool inconclusive = false, bool portability = false, Settings::PlatformType platform = Settings::Unspecified) { void check(const char* code, bool inconclusive = false, bool portability = false, Settings::PlatformType platform = Settings::Unspecified) {
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
@ -745,22 +745,17 @@ private:
} }
std::string testScanfCode(const std::string & format, const std::string & type) { #define TEST_SCANF_CODE(format, type)\
return "void f() { " + type + " x; scanf(\"" + format + "\", &x); }"; "void f() { " type " x; scanf(\"" format "\", &x); }"
}
std::string testScanfErr(const std::string & format, const std::string & formatStr, const std::string & type) { #define TEST_SCANF_ERR(format, formatStr, type)\
return "[test.cpp:1]: (warning) " + format + " in format string (no. 1) requires '" + formatStr + " *' but the argument type is '" + type + " *'.\n"; "[test.cpp:1]: (warning) " format " in format string (no. 1) requires '" formatStr " *' but the argument type is '" type " *'.\n"
}
std::string testScanfErrAka(const std::string & format, const std::string & formatStr, const std::string & type, const std::string & akaType) { #define TEST_SCANF_ERR_AKA(format, formatStr, type, akaType)\
return "[test.cpp:1]: (portability) " + format + " in format string (no. 1) requires '" + formatStr + " *' but the argument type is '" + type + " * {aka " + akaType + " *}'.\n"; "[test.cpp:1]: (portability) " format " in format string (no. 1) requires '" formatStr " *' but the argument type is '" type " * {aka " akaType " *}'.\n"
}
void testScanfNoWarn(const char *filename, unsigned int linenr,
const std::string & format, const std::string & /* formatStr */ , const std::string & type) {
const std::string code = testScanfCode(format, type);
void testScanfNoWarn(const char *filename, unsigned int linenr, const char* code) {
check(code, true, false, Settings::Unix32); check(code, true, false, Settings::Unix32);
assertEquals(filename, linenr, "", errout.str()); assertEquals(filename, linenr, "", errout.str());
check(code, true, false, Settings::Unix64); check(code, true, false, Settings::Unix64);
@ -772,10 +767,7 @@ private:
} }
void testScanfWarn(const char *filename, unsigned int linenr, void testScanfWarn(const char *filename, unsigned int linenr,
const std::string & format, const std::string & formatStr, const std::string & type) { const char* code, const char* testScanfErrString) {
const std::string code = testScanfCode(format, type);
const std::string testScanfErrString = testScanfErr(format,formatStr,type);
check(code, true, false, Settings::Unix32); check(code, true, false, Settings::Unix32);
assertEquals(filename, linenr, testScanfErrString, errout.str()); assertEquals(filename, linenr, testScanfErrString, errout.str());
check(code, true, false, Settings::Unix64); check(code, true, false, Settings::Unix64);
@ -787,12 +779,7 @@ private:
} }
void testScanfWarnAka(const char *filename, unsigned int linenr, void testScanfWarnAka(const char *filename, unsigned int linenr,
const std::string & format, const std::string & formatStr, const std::string & type, const char* code, const char* testScanfErrAkaString, const char* testScanfErrAkaWin64String) {
const std::string & akaType, const std::string & akaTypeWin64) {
const std::string code = testScanfCode(format, type);
const std::string testScanfErrAkaString = testScanfErrAka(format,formatStr,type,akaType);
const std::string testScanfErrAkaWin64String = testScanfErrAka(format,formatStr,type,akaTypeWin64);
check(code, true, true, Settings::Unix32); check(code, true, true, Settings::Unix32);
assertEquals(filename, linenr, testScanfErrAkaString, errout.str()); assertEquals(filename, linenr, testScanfErrAkaString, errout.str());
check(code, true, true, Settings::Unix64); check(code, true, true, Settings::Unix64);
@ -804,11 +791,7 @@ private:
} }
void testScanfWarnAkaWin64(const char *filename, unsigned int linenr, void testScanfWarnAkaWin64(const char *filename, unsigned int linenr,
const std::string & format, const std::string & formatStr, const std::string & type, const char* code, const char* testScanfErrAkaWin64String) {
const std::string & akaTypeWin64) {
const std::string code = testScanfCode(format, type);
const std::string testScanfErrAkaWin64String = testScanfErrAka(format,formatStr,type,akaTypeWin64);
check(code, true, true, Settings::Unix32); check(code, true, true, Settings::Unix32);
assertEquals(filename, linenr, "", errout.str()); assertEquals(filename, linenr, "", errout.str());
check(code, true, true, Settings::Unix64); check(code, true, true, Settings::Unix64);
@ -820,11 +803,7 @@ private:
} }
void testScanfWarnAkaWin32(const char *filename, unsigned int linenr, void testScanfWarnAkaWin32(const char *filename, unsigned int linenr,
const std::string & format, const std::string & formatStr, const std::string & type, const char* code, const char* testScanfErrAkaString) {
const std::string & akaType) {
const std::string code = testScanfCode(format, type);
const std::string testScanfErrAkaString = testScanfErrAka(format,formatStr,type,akaType);
check(code, true, true, Settings::Unix32); check(code, true, true, Settings::Unix32);
assertEquals(filename, linenr, testScanfErrAkaString, errout.str()); assertEquals(filename, linenr, testScanfErrAkaString, errout.str());
check(code, true, true, Settings::Unix64); check(code, true, true, Settings::Unix64);
@ -836,15 +815,15 @@ private:
} }
#define TEST_SCANF_NOWARN(FORMAT, FORMATSTR, TYPE) \ #define TEST_SCANF_NOWARN(FORMAT, FORMATSTR, TYPE) \
testScanfNoWarn(__FILE__, __LINE__, FORMAT, FORMATSTR, TYPE) testScanfNoWarn(__FILE__, __LINE__, TEST_SCANF_CODE(FORMAT, TYPE))
#define TEST_SCANF_WARN(FORMAT, FORMATSTR, TYPE) \ #define TEST_SCANF_WARN(FORMAT, FORMATSTR, TYPE) \
testScanfWarn(__FILE__, __LINE__, FORMAT, FORMATSTR, TYPE) testScanfWarn(__FILE__, __LINE__, TEST_SCANF_CODE(FORMAT, TYPE), TEST_SCANF_ERR(FORMAT, FORMATSTR, TYPE))
#define TEST_SCANF_WARN_AKA(FORMAT, FORMATSTR, TYPE, AKATYPE, AKATYPE_WIN64) \ #define TEST_SCANF_WARN_AKA(FORMAT, FORMATSTR, TYPE, AKATYPE, AKATYPE_WIN64) \
testScanfWarnAka(__FILE__, __LINE__, FORMAT, FORMATSTR, TYPE, AKATYPE, AKATYPE_WIN64) testScanfWarnAka(__FILE__, __LINE__, TEST_SCANF_CODE(FORMAT, TYPE), TEST_SCANF_ERR_AKA(FORMAT, FORMATSTR, TYPE, AKATYPE), TEST_SCANF_ERR_AKA(FORMAT, FORMATSTR, TYPE, AKATYPE_WIN64))
#define TEST_SCANF_WARN_AKA_WIN64(FORMAT, FORMATSTR, TYPE, AKATYPE_WIN64) \ #define TEST_SCANF_WARN_AKA_WIN64(FORMAT, FORMATSTR, TYPE, AKATYPE_WIN64) \
testScanfWarnAkaWin64(__FILE__, __LINE__, FORMAT, FORMATSTR, TYPE, AKATYPE_WIN64) testScanfWarnAkaWin64(__FILE__, __LINE__, TEST_SCANF_CODE(FORMAT, TYPE), TEST_SCANF_ERR_AKA(FORMAT, FORMATSTR, TYPE, AKATYPE_WIN64))
#define TEST_SCANF_WARN_AKA_WIN32(FORMAT, FORMATSTR, TYPE, AKATYPE_WIN32) \ #define TEST_SCANF_WARN_AKA_WIN32(FORMAT, FORMATSTR, TYPE, AKATYPE_WIN32) \
testScanfWarnAkaWin32(__FILE__, __LINE__, FORMAT, FORMATSTR, TYPE, AKATYPE_WIN32) testScanfWarnAkaWin32(__FILE__, __LINE__, TEST_SCANF_CODE(FORMAT, TYPE), TEST_SCANF_ERR_AKA(FORMAT, FORMATSTR, TYPE, AKATYPE_WIN32))
void testScanfArgument() { void testScanfArgument() {
check("void foo() {\n" check("void foo() {\n"
@ -1364,8 +1343,8 @@ private:
" scanf(\"%zd\", &s2);\n" " scanf(\"%zd\", &s2);\n"
" scanf(\"%zd\", &s3);\n" " scanf(\"%zd\", &s3);\n"
"}\n"; "}\n";
std::string result("[test.cpp:5]: (portability) %zd in format string (no. 1) requires 'ptrdiff_t *' but the argument type is 'size_t * {aka unsigned long *}'.\n"); const char* result("[test.cpp:5]: (portability) %zd in format string (no. 1) requires 'ptrdiff_t *' but the argument type is 'size_t * {aka unsigned long *}'.\n");
std::string result_win64("[test.cpp:5]: (portability) %zd in format string (no. 1) requires 'ptrdiff_t *' but the argument type is 'size_t * {aka unsigned long long *}'.\n"); const char* result_win64("[test.cpp:5]: (portability) %zd in format string (no. 1) requires 'ptrdiff_t *' but the argument type is 'size_t * {aka unsigned long long *}'.\n");
check(code, false, true, Settings::Unix32); check(code, false, true, Settings::Unix32);
ASSERT_EQUALS(result, errout.str()); ASSERT_EQUALS(result, errout.str());