Fix ticket #84 (unit testing: use "protected" instead of preprocessor)

This commit is contained in:
Reijo Tomperi 2009-02-07 20:06:00 +00:00
parent 798d86216a
commit b211b8cbe8
19 changed files with 146 additions and 98 deletions

View File

@ -115,9 +115,7 @@ private:
std::list<AllocFunc> _listAllocFunc;
// Experimental functionality..
#ifdef UNIT_TESTING
public:
#endif
protected:
Token *functionParameterCode(const Token *ftok, int parameter);
};

View File

@ -68,9 +68,7 @@ public:
/** Returning pointer to local data */
void returnPointerToStackData();
#ifndef UNIT_TESTING
private:
#endif
protected:
void CheckVariableScope_LookupVar(const Token *tok1, const char varname[]);
// Redundant condition

View File

@ -73,9 +73,19 @@ public:
*/
static std::string getcode(const std::string &filedata, std::string cfg);
#ifndef UNIT_TESTING
protected:
/**
* Replace "#if defined" with "#ifdef" where possible
*
* @param str The string to be converted
* @return The replaced string
*/
static std::string replaceIfDefined(const std::string &str);
static std::string expandMacros(std::string code);
private:
#endif
/**
* Remove space that has new line character on left or right side of it.
@ -85,14 +95,6 @@ private:
*/
static std::string removeSpaceNearNL(const std::string &str);
/**
* Replace "#if defined" with "#ifdef" where possible
*
* @param str The string to be converted
* @return The replaced string
*/
static std::string replaceIfDefined(const std::string &str);
/**
* Get all possible configurations. By looking at the ifdefs and ifndefs in filedata
*/
@ -102,8 +104,6 @@ private:
static bool match_cfg_def(std::string cfg, const std::string &def);
static std::string expandMacros(std::string code);
/**
* Search includes from code and append code from the included
* file

View File

@ -75,11 +75,32 @@ public:
const Token *GetFunctionTokenByName(const char funcname[]) const;
const Token *tokens() const;
protected:
/** Add braces to an if-block
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyIfAddBraces();
/** Simplify casts
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyCasts();
/**
* A simplify function that replaces a variable with its value in cases
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"
*
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifyKnownVariables();
std::vector<const Token *> _functionList;
#ifndef UNIT_TESTING
private:
#endif
/**
* Finds matching "end" for "start".
@ -92,12 +113,6 @@ private:
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);
/** Add braces to an if-block
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyIfAddBraces();
/** Simplify conditions
* @return true if something is modified
* false if nothing is done.
@ -111,27 +126,12 @@ private:
*/
bool removeReduntantConditions();
/** Simplify casts
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyCasts();
/** Simplify function calls - constant return value
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyFunctionReturn();
/**
* A simplify function that replaces a variable with its value in cases
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"
*
* @return true if modifications to token-list are done.
* false if no modifications are done.
*/
bool simplifyKnownVariables();
/**
* Remove redundant paranthesis: "((x))" => "(x)"
* @return true if modifications to token-list are done.
@ -149,7 +149,6 @@ private:
Token *_tokensBack;
std::map<std::string, unsigned int> _typeSize;
std::vector<const Token *> _functionList;
std::vector<std::string> _files;
Token *_tokens;
};

View File

@ -18,7 +18,6 @@
*/
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkbufferoverrun.h"
#include "testsuite.h"

View File

@ -18,7 +18,6 @@
*/
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkother.h"
#include "testsuite.h"

View File

@ -19,7 +19,6 @@
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkclass.h"
#include "testsuite.h"

View File

@ -19,7 +19,6 @@
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkclass.h"
#include "testsuite.h"

View File

@ -18,7 +18,6 @@
*/
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkdangerousfunctions.h"
#include "testsuite.h"

View File

@ -22,7 +22,6 @@
// such as "svar / uvar". Treating "svar" as unsigned data is not good
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkother.h"
#include "testsuite.h"

View File

@ -18,7 +18,6 @@
*/
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "testsuite.h"
#include "../src/checkfunctionusage.h"

View File

@ -21,7 +21,7 @@
// Check for unused variables..
#define UNIT_TESTING
#include "testsuite.h"
#include "../src/tokenize.h"
#include "../src/checkother.h"

View File

@ -20,7 +20,7 @@
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkmemoryleak.h"
#include "testsuite.h"

View File

@ -18,8 +18,6 @@
*/
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkmemoryleak.h"
#include "testsuite.h"
@ -33,6 +31,20 @@ public:
TestMemleakMultiPass() : TestFixture("TestMemleakMultiPass")
{ }
class OurCheckMemoryLeakClass : public CheckMemoryLeakClass
{
public:
OurCheckMemoryLeakClass(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
: CheckMemoryLeakClass(tokenizer, settings, errorLogger)
{
}
Token *functionParameterCode(const Token *ftok, int parameter)
{
return CheckMemoryLeakClass::functionParameterCode(ftok, parameter);
}
};
private:
void run()
@ -58,7 +70,7 @@ private:
// Check..
Settings settings;
CheckMemoryLeakClass checkMemoryLeak(&tokenizer, settings, this);
OurCheckMemoryLeakClass checkMemoryLeak(&tokenizer, settings, this);
Token *tok = checkMemoryLeak.functionParameterCode(tokenizer.tokens(), 1);
// Compare tokens..

View File

@ -23,7 +23,6 @@
#include "testsuite.h"
#define UNIT_TESTING
#include "../src/preprocessor.h"
#include <map>
@ -35,6 +34,20 @@ public:
TestPreprocessor() : TestFixture("TestPreprocessor")
{ }
class OurPreprocessor : public Preprocessor
{
public:
static std::string replaceIfDefined(const std::string &str)
{
return Preprocessor::replaceIfDefined(str);
}
static std::string expandMacros(std::string code)
{
return Preprocessor::expandMacros(code);
}
};
private:
void run()
@ -473,7 +486,7 @@ private:
std::string expected("#ifdef AAA\n#endif\n");
// Compare result..
ASSERT_EQUALS(expected, Preprocessor::replaceIfDefined(filedata));
ASSERT_EQUALS(expected, OurPreprocessor::replaceIfDefined(filedata));
}
@ -481,28 +494,28 @@ private:
{
const char filedata[] = "#define AAA(aa) f(aa)\n"
"AAA(5);\n";
ASSERT_EQUALS("\nf(5);\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\nf(5);\n", OurPreprocessor::expandMacros(filedata));
}
void macro_simple2()
{
const char filedata[] = "#define min(x,y) x<y?x:y\n"
"min(a(),b());\n";
ASSERT_EQUALS("\na()<b()?a():b();\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\na()<b()?a():b();\n", OurPreprocessor::expandMacros(filedata));
}
void macro_simple3()
{
const char filedata[] = "#define A 4\n"
"A AA\n";
ASSERT_EQUALS("\n4 AA\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n4 AA\n", OurPreprocessor::expandMacros(filedata));
}
void macro_simple4()
{
const char filedata[] = "#define TEMP_1 if( temp > 0 ) return 1;\n"
"TEMP_1\n";
ASSERT_EQUALS("\nif( temp > 0 ) return 1;\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\nif( temp > 0 ) return 1;\n", OurPreprocessor::expandMacros(filedata));
}
void macro_simple5()
@ -514,21 +527,21 @@ private:
" int temp = 0;\n"
" ABC\n"
"}\n";
ASSERT_EQUALS("\n\nvoid foo()\n{\n int temp = 0;\n if( temp > 0 ) return 1;\n}\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n\nvoid foo()\n{\n int temp = 0;\n if( temp > 0 ) return 1;\n}\n", OurPreprocessor::expandMacros(filedata));
}
void macro_simple6()
{
const char filedata[] = "#define ABC (a+b+c)\n"
"ABC";
ASSERT_EQUALS("\n(a+b+c)", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n(a+b+c)", OurPreprocessor::expandMacros(filedata));
}
void macro_simple7()
{
const char filedata[] = "#define ABC(str) str\n"
"ABC(\"(\")";
ASSERT_EQUALS("\n\"(\"", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n\"(\"", OurPreprocessor::expandMacros(filedata));
}
void macro_simple8()
@ -536,14 +549,14 @@ private:
const char filedata[] = "#define ABC 123\n"
"#define ABCD 1234\n"
"ABC ABCD";
ASSERT_EQUALS("\n\n123 1234", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n\n123 1234", OurPreprocessor::expandMacros(filedata));
}
void macro_mismatch()
{
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"
"AAA(5);\n";
ASSERT_EQUALS("\nAAA(5);\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\nAAA(5);\n", OurPreprocessor::expandMacros(filedata));
}
void string1()
@ -570,7 +583,7 @@ private:
"str = \"AAA\"";
// Compare results..
ASSERT_EQUALS("\nstr = \"AAA\"", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\nstr = \"AAA\"", OurPreprocessor::expandMacros(filedata));
}
@ -582,7 +595,7 @@ private:
"AAA\n";
// Compare results..
ASSERT_EQUALS("\n\n\nchar b=0;\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n\n\nchar b=0;\n", OurPreprocessor::expandMacros(filedata));
}
void defdef()
@ -593,7 +606,7 @@ private:
"AAA\n";
// Compare results..
ASSERT_EQUALS("\n\n\n789\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\n\n\n789\n", OurPreprocessor::expandMacros(filedata));
}
void preprocessor_doublesharp()
@ -602,13 +615,13 @@ private:
"TEST(foo,20);\n";
// Compare results..
ASSERT_EQUALS("\nfoo=20;\n", Preprocessor::expandMacros(filedata));
ASSERT_EQUALS("\nfoo=20;\n", OurPreprocessor::expandMacros(filedata));
const char filedata2[] = "#define TEST(var,val) var##_##val = val\n"
"TEST(foo,20);\n";
// Compare results..
ASSERT_EQUALS("\nfoo_20=20;\n", Preprocessor::expandMacros(filedata2));
ASSERT_EQUALS("\nfoo_20=20;\n", OurPreprocessor::expandMacros(filedata2));
}
@ -641,7 +654,7 @@ private:
"DBG(\"[0x%lx-0x%lx)\", pstart, pend);";
// Preprocess..
std::string actual = Preprocessor::expandMacros(filedata);
std::string actual = OurPreprocessor::expandMacros(filedata);
ASSERT_EQUALS("\nprintf(\"[0x%lx-0x%lx)\", pstart, pend);", actual);
}
@ -652,7 +665,7 @@ private:
"DBG(\"hello\");";
// Preprocess..
std::string actual = Preprocessor::expandMacros(filedata);
std::string actual = OurPreprocessor::expandMacros(filedata);
ASSERT_EQUALS("\nprintf(\"hello\");", actual);
}
@ -688,7 +701,7 @@ private:
"STRINGIFY(abc)";
// expand macros..
std::string actual = Preprocessor::expandMacros(filedata);
std::string actual = OurPreprocessor::expandMacros(filedata);
ASSERT_EQUALS("\n\"abc\"", actual);
}

View File

@ -22,7 +22,6 @@
// such as "svar / uvar". Treating "svar" as unsigned data is not good
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkother.h"
#include "testsuite.h"
@ -37,6 +36,21 @@ public:
TestRedundantIf() : TestFixture("TestRedundantIf")
{ }
class OurCheckOther : public CheckOther
{
public:
OurCheckOther(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger)
: CheckOther(tokenizer, settings, errorLogger)
{
}
void redundantCondition2()
{
CheckOther::redundantCondition2();
}
};
private:
void check(const char code[])
{
@ -49,7 +63,7 @@ private:
errout.str("");
// Check for redundant condition..
CheckOther checkOther(&tokenizer, Settings(), this);
OurCheckOther checkOther(&tokenizer, Settings(), this);
checkOther.redundantCondition2();
}

View File

@ -24,7 +24,6 @@
#include <cstring>
#include "testsuite.h"
#define UNIT_TESTING // Get access to "private" data in Tokenizer
#include "../src/tokenize.h"
extern std::ostringstream errout;
@ -34,6 +33,30 @@ public:
TestTokenizer() : TestFixture("TestTokenizer")
{ }
class OurTokenizer : public Tokenizer
{
public:
void simplifyCasts()
{
Tokenizer::simplifyCasts();
}
bool simplifyIfAddBraces()
{
return Tokenizer::simplifyIfAddBraces();
}
bool simplifyKnownVariables()
{
return Tokenizer::simplifyKnownVariables();
}
std::vector<const Token *> &getFunctionList()
{
return _functionList;
}
};
private:
void run()
@ -153,7 +176,7 @@ private:
const char code[] = "int *f(int *);";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -208,14 +231,14 @@ private:
"void b()\n"
"{ }\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.fillFunctionList();
ASSERT_EQUALS(1, static_cast<unsigned int>(tokenizer._functionList.size()));
ASSERT_EQUALS(std::string("b"), tokenizer._functionList[0]->aaaa());
ASSERT_EQUALS(1, static_cast<unsigned int>(tokenizer.getFunctionList().size()));
ASSERT_EQUALS(std::string("b"), tokenizer.getFunctionList()[0]->aaaa());
}
void const_and_volatile_functions()
@ -239,18 +262,18 @@ private:
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.fillFunctionList();
ASSERT_EQUALS(3, static_cast<unsigned int>(tokenizer._functionList.size()));
if (tokenizer._functionList.size() == 3)
ASSERT_EQUALS(3, static_cast<unsigned int>(tokenizer.getFunctionList().size()));
if (tokenizer.getFunctionList().size() == 3)
{
ASSERT_EQUALS(std::string("a"), tokenizer._functionList[0]->str());
ASSERT_EQUALS(std::string("b"), tokenizer._functionList[1]->str());
ASSERT_EQUALS(std::string("c"), tokenizer._functionList[2]->str());
ASSERT_EQUALS(std::string("a"), tokenizer.getFunctionList()[0]->str());
ASSERT_EQUALS(std::string("b"), tokenizer.getFunctionList()[1]->str());
ASSERT_EQUALS(std::string("c"), tokenizer.getFunctionList()[2]->str());
}
}
@ -284,7 +307,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -304,7 +327,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -324,7 +347,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -349,7 +372,7 @@ private:
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -370,7 +393,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -393,7 +416,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -419,7 +442,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -441,7 +464,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -463,7 +486,7 @@ private:
"}\n";
// tokenize..
Tokenizer tokenizer;
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");

View File

@ -18,7 +18,6 @@
*/
#define UNIT_TESTING
#include "../src/tokenize.h"
#include "../src/checkclass.h"
#include "testsuite.h"

View File

@ -21,7 +21,6 @@
// Check for unused variables..
#define UNIT_TESTING
#include "testsuite.h"
#include "../src/tokenize.h"
#include "../src/checkother.h"