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() {
delete expr;
delete expr2;
delete name;
delete table;
free(name);
free(table);
free(alias);
}
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {

View File

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

View File

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

View File

@ -7,15 +7,28 @@
using namespace hsql;
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->groupBy);
delete result;
}
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);
GroupByDescription* group = stmt->groupBy;
@ -24,23 +37,39 @@ TEST(SelectHavingTest) {
ASSERT(group->having->isSimpleOp('<'));
ASSERT(group->having->expr->isType(kExprFunctionRef));
ASSERT(group->having->expr2->isType(kExprLiteralFloat));
delete result;
}
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_NULL(stmt->whereClause);
delete result;
}
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_EQ(stmt->selectList->size(), 3);
ASSERT(!stmt->selectList->at(1)->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
public:
static std::vector<std::string>& testNames() {
static std::vector<std::string>* _testNames = new std::vector<std::string>;
return *_testNames;
static std::vector<std::string> testNames;
return testNames;
}
static std::vector<void (*)(void)>& tests() {
static std::vector<void (*)(void)>* tests = new std::vector<void (*)(void)>;
return *tests;
static std::vector<void (*)(void)> tests;
return tests;
}
};

View File

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