resolved memory leaks triggered in select tests

This commit is contained in:
Pedro 2017-02-08 03:54:38 +01:00
parent 89a81848b4
commit a362e86da1
6 changed files with 72 additions and 27 deletions

View File

@ -16,8 +16,9 @@ namespace hsql {
Expr::~Expr() { Expr::~Expr() {
delete expr; delete expr;
delete expr2; delete expr2;
delete name; free(name);
delete table; free(table);
free(alias);
} }
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) { Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {

View File

@ -142,8 +142,14 @@ namespace hsql {
having(NULL) {} having(NULL) {}
GroupByDescription::~GroupByDescription() { GroupByDescription::~GroupByDescription() {
delete columns;
delete having; delete having;
if (columns != NULL) {
for (Expr* expr : *columns) {
delete expr;
}
delete columns;
}
} }
// SelectStatement // SelectStatement
@ -160,11 +166,18 @@ namespace hsql {
SelectStatement::~SelectStatement() { SelectStatement::~SelectStatement() {
delete fromTable; delete fromTable;
delete selectList;
delete whereClause; delete whereClause;
delete groupBy; delete groupBy;
delete order; delete order;
delete limit; delete limit;
// Delete each element in the select list.
if (selectList != NULL) {
for (Expr* expr : *selectList) {
delete expr;
}
delete selectList;
}
} }
// UpdateStatement // UpdateStatement
@ -191,8 +204,8 @@ namespace hsql {
join(NULL) {} join(NULL) {}
TableRef::~TableRef() { TableRef::~TableRef() {
delete name; free(name);
delete alias; free(alias);
delete select; delete select;
delete list; delete list;
} }

View File

@ -2,21 +2,21 @@
#define __HELPER_H__ #define __HELPER_H__
#define TEST_PARSE_SQL_QUERY(query, outputVar, numStatements) \ #define TEST_PARSE_SQL_QUERY(query, result, numStatements) \
const SQLParserResult* outputVar = SQLParser::parseSQLString(query); \ const SQLParserResult* result = SQLParser::parseSQLString(query); \
ASSERT(outputVar->isValid()); \ ASSERT(result->isValid()); \
ASSERT_EQ(outputVar->size(), numStatements); ASSERT_EQ(result->size(), numStatements);
#define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, outputVar) \ #define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, result, outputVar) \
TEST_PARSE_SQL_QUERY(query, stmt_list, 1); \ TEST_PARSE_SQL_QUERY(query, result, 1); \
ASSERT_EQ(stmt_list->getStatement(0)->type(), stmtType); \ ASSERT_EQ(result->getStatement(0)->type(), stmtType); \
const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(0); const stmtClass* outputVar = (const stmtClass*) result->getStatement(0);
#define TEST_CAST_STMT(stmt_list, stmt_index, stmtType, stmtClass, outputVar) \ #define TEST_CAST_STMT(result, stmt_index, stmtType, stmtClass, outputVar) \
ASSERT_EQ(stmt_list->getStatement(stmt_index)->type(), stmtType); \ ASSERT_EQ(result->getStatement(stmt_index)->type(), stmtType); \
const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(stmt_index); const stmtClass* outputVar = (const stmtClass*) result->getStatement(stmt_index);
#endif #endif

View File

@ -7,15 +7,28 @@
using namespace hsql; using namespace hsql;
TEST(SelectTest) { TEST(SelectTest) {
TEST_PARSE_SINGLE_SQL("SELECT * FROM students;", kStmtSelect, SelectStatement, stmt); TEST_PARSE_SINGLE_SQL(
"SELECT * FROM students;",
kStmtSelect,
SelectStatement,
result,
stmt);
ASSERT_NULL(stmt->whereClause); ASSERT_NULL(stmt->whereClause);
ASSERT_NULL(stmt->groupBy); ASSERT_NULL(stmt->groupBy);
delete result;
} }
TEST(SelectHavingTest) { TEST(SelectHavingTest) {
TEST_PARSE_SINGLE_SQL("SELECT city, AVG(grade) AS avg_grade FROM students GROUP BY city HAVING AVG(grade) < 2.0", kStmtSelect, SelectStatement, stmt); TEST_PARSE_SINGLE_SQL(
"SELECT city, AVG(grade) AS avg_grade FROM students GROUP BY city HAVING AVG(grade) < 2.0",
kStmtSelect,
SelectStatement,
result,
stmt);
ASSERT_FALSE(stmt->selectDistinct); ASSERT_FALSE(stmt->selectDistinct);
GroupByDescription* group = stmt->groupBy; GroupByDescription* group = stmt->groupBy;
@ -24,23 +37,39 @@ TEST(SelectHavingTest) {
ASSERT(group->having->isSimpleOp('<')); ASSERT(group->having->isSimpleOp('<'));
ASSERT(group->having->expr->isType(kExprFunctionRef)); ASSERT(group->having->expr->isType(kExprFunctionRef));
ASSERT(group->having->expr2->isType(kExprLiteralFloat)); ASSERT(group->having->expr2->isType(kExprLiteralFloat));
delete result;
} }
TEST(SelectDistinctTest) { TEST(SelectDistinctTest) {
TEST_PARSE_SINGLE_SQL("SELECT DISTINCT grade, city FROM students;", kStmtSelect, SelectStatement, stmt); TEST_PARSE_SINGLE_SQL(
"SELECT DISTINCT grade, city FROM students;",
kStmtSelect,
SelectStatement,
result,
stmt);
ASSERT(stmt->selectDistinct); ASSERT(stmt->selectDistinct);
ASSERT_NULL(stmt->whereClause); ASSERT_NULL(stmt->whereClause);
delete result;
} }
TEST(SelectGroupDistinctTest) { TEST(SelectGroupDistinctTest) {
TEST_PARSE_SINGLE_SQL("SELECT city, COUNT(name), COUNT(DISTINCT grade) FROM students GROUP BY city;", kStmtSelect, SelectStatement, stmt); TEST_PARSE_SINGLE_SQL(
"SELECT city, COUNT(name), COUNT(DISTINCT grade) FROM students GROUP BY city;",
kStmtSelect,
SelectStatement,
result,
stmt);
ASSERT_FALSE(stmt->selectDistinct); ASSERT_FALSE(stmt->selectDistinct);
ASSERT_EQ(stmt->selectList->size(), 3); ASSERT_EQ(stmt->selectList->size(), 3);
ASSERT(!stmt->selectList->at(1)->distinct); ASSERT(!stmt->selectList->at(1)->distinct);
ASSERT(stmt->selectList->at(2)->distinct); ASSERT(stmt->selectList->at(2)->distinct);
delete result;
} }

View File

@ -8,13 +8,13 @@ class TestsManager {
// http://www.parashift.com/c++-faq-lite/static-init-order-on-first-use.html // http://www.parashift.com/c++-faq-lite/static-init-order-on-first-use.html
public: public:
static std::vector<std::string>& testNames() { static std::vector<std::string>& testNames() {
static std::vector<std::string>* _testNames = new std::vector<std::string>; static std::vector<std::string> testNames;
return *_testNames; return testNames;
} }
static std::vector<void (*)(void)>& tests() { static std::vector<void (*)(void)>& tests() {
static std::vector<void (*)(void)>* tests = new std::vector<void (*)(void)>; static std::vector<void (*)(void)> tests;
return *tests; return tests;
} }
}; };

View File

@ -8,7 +8,9 @@
#define TEST(name) \ #define TEST(name) \
void name();\ void name();\
namespace g_dummy { int _##name = AddTest(name, #name); } \ namespace g_dummy {\
int _##name = AddTest(name, #name);\
}\
void name() void name()