diff --git a/test/teststl.cpp b/test/teststl.cpp index c7acdb7f0..39fedcf15 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -49,195 +49,195 @@ private: TEST_CASE(invalidcode); } - void check(const char code[]) - { - // Tokenize.. - Tokenizer tokenizer; - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); + void check(const char code[]) + { + // Tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); - // Clear the error buffer.. - errout.str(""); + // Clear the error buffer.. + errout.str(""); - // Check.. - CheckStl checkStl; - checkStl.runChecks(&tokenizer, (const Settings *)0, this); - } + // Check.. + CheckStl checkStl; + checkStl.runChecks(&tokenizer, (const Settings *)0, this); + } - void iterator1() + void iterator1() + { + check("void foo()\n" + "{\n" + " for (it = foo.begin(); it != bar.end(); ++it)\n" + " { }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str()); + } + + void iterator2() + { + check("void foo()\n" + "{\n" + " it = foo.begin();\n" + " while (it != bar.end())\n" + " {\n" + " ++it;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str()); + } + + + void STLSize() + { + check("void foo()\n" + "{\n" + " std::vector foo;\n" + " for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n" + " {\n" + " foo[ii] = 0;\n" + " }\n" + "}\n"); + ASSERT_EQUALS(std::string("[test.cpp:6]: (error) When ii == size(), foo [ ii ] is out of bounds\n"), errout.str()); + } + + void STLSizeNoErr() + { { check("void foo()\n" "{\n" - " for (it = foo.begin(); it != bar.end(); ++it)\n" - " { }\n" - "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str()); - } - - void iterator2() - { - check("void foo()\n" - "{\n" - " it = foo.begin();\n" - " while (it != bar.end())\n" + " std::vector foo;\n" + " for (unsigned int ii = 0; ii < foo.size(); ++ii)\n" " {\n" - " ++it;\n" + " foo[ii] = 0;\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str()); + ASSERT_EQUALS(std::string(""), errout.str()); } - - void STLSize() { check("void foo()\n" "{\n" " std::vector foo;\n" " for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n" " {\n" - " foo[ii] = 0;\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:6]: (error) When ii == size(), foo [ ii ] is out of bounds\n"), errout.str()); + ASSERT_EQUALS(std::string(""), errout.str()); } - void STLSizeNoErr() { - { - check("void foo()\n" - "{\n" - " std::vector foo;\n" - " for (unsigned int ii = 0; ii < foo.size(); ++ii)\n" - " {\n" - " foo[ii] = 0;\n" - " }\n" - "}\n"); - ASSERT_EQUALS(std::string(""), errout.str()); - } - - { - check("void foo()\n" - "{\n" - " std::vector foo;\n" - " for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n" - " {\n" - " }\n" - "}\n"); - ASSERT_EQUALS(std::string(""), errout.str()); - } - - { - check("void foo()\n" - "{\n" - " std::vector foo;\n" - " for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n" - " {\n" - " if (ii == foo.size())\n" - " {\n" - " }\n" - " else\n" - " {\n" - " foo[ii] = 0;\n" - " }\n" - " }\n" - "}\n"); - // TODO ASSERT_EQUALS(std::string(""), errout.str()); - } - } - - - - - - void erase() - { - check("void f()\n" + check("void foo()\n" "{\n" - " for (it = foo.begin(); it != foo.end(); ++it)\n" + " std::vector foo;\n" + " for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n" " {\n" - " foo.erase(it);\n" + " if (ii == foo.size())\n" + " {\n" + " }\n" + " else\n" + " {\n" + " foo[ii] = 0;\n" + " }\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of erase\n", errout.str()); - } - - void eraseBreak() - { - check("void f()\n" - "{\n" - " for (it = foo.begin(); it != foo.end(); ++it)\n" - " {\n" - " foo.erase(it);\n" - " break;\n" - " }\n" - "}\n"); - ASSERT_EQUALS("", errout.str()); - } - - void eraseReturn() - { - check("void f()\n" - "{\n" - " for (it = foo.begin(); it != foo.end(); ++it)\n" - " {\n" - " foo.erase(it);\n" - " return;\n" - " }\n" - "}\n"); - ASSERT_EQUALS("", errout.str()); - } - - void eraseGoto() - { - check("void f()\n" - "{\n" - " for (it = foo.begin(); it != foo.end(); ++it)\n" - " {\n" - " foo.erase(it);\n" - " goto abc;\n" - " }\n" - "bar:\n" - "}\n"); - ASSERT_EQUALS("", errout.str()); - } - - void eraseAssign() - { - check("void f()\n" - "{\n" - " for (it = foo.begin(); it != foo.end(); ++it)\n" - " {\n" - " foo.erase(it);\n" - " it = foo.begin();\n" - " }\n" - "}\n"); - ASSERT_EQUALS("", errout.str()); + // TODO ASSERT_EQUALS(std::string(""), errout.str()); } + } - void pushback1() - { - check("void f()\n" - "{\n" - " std::vector::const_iterator it = foo.begin();\n" - " foo.push_back(123);\n" - " *it;\n" - "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str()); - } + void erase() + { + check("void f()\n" + "{\n" + " for (it = foo.begin(); it != foo.end(); ++it)\n" + " {\n" + " foo.erase(it);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of erase\n", errout.str()); + } - void invalidcode() - { - check("void f()\n" - "{\n" - " for ( \n" - "}\n"); - ASSERT_EQUALS("", errout.str()); - } + void eraseBreak() + { + check("void f()\n" + "{\n" + " for (it = foo.begin(); it != foo.end(); ++it)\n" + " {\n" + " foo.erase(it);\n" + " break;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + void eraseReturn() + { + check("void f()\n" + "{\n" + " for (it = foo.begin(); it != foo.end(); ++it)\n" + " {\n" + " foo.erase(it);\n" + " return;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + void eraseGoto() + { + check("void f()\n" + "{\n" + " for (it = foo.begin(); it != foo.end(); ++it)\n" + " {\n" + " foo.erase(it);\n" + " goto abc;\n" + " }\n" + "bar:\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + void eraseAssign() + { + check("void f()\n" + "{\n" + " for (it = foo.begin(); it != foo.end(); ++it)\n" + " {\n" + " foo.erase(it);\n" + " it = foo.begin();\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + + + + + + void pushback1() + { + check("void f()\n" + "{\n" + " std::vector::const_iterator it = foo.begin();\n" + " foo.push_back(123);\n" + " *it;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str()); + } + + void invalidcode() + { + check("void f()\n" + "{\n" + " for ( \n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestStl)