From 1360c554ed57e261ae60d68e77d698cb8c19b8d6 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Sun, 6 Nov 2011 21:20:13 +0100 Subject: [PATCH] Extended isOp() tests to do proper negative testing against other operators --- test/testtoken.cpp | 334 +++++++++++++++++++++++---------------------- 1 file changed, 173 insertions(+), 161 deletions(-) diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 401e43605..b72f9e918 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -22,6 +22,8 @@ #include "settings.h" #include +#include +#include extern std::ostringstream errout; class TestToken : public TestFixture { @@ -30,8 +32,14 @@ public: { } private: + std::vector arithmeticalOps; + std::vector normalOps; + std::vector extendedOps; + std::vector assignmentOps; void run() { + initOps(); + TEST_CASE(nextprevious); TEST_CASE(multiCompare); TEST_CASE(multiCompare2); // #3294 - false negative multi compare between "=" and "==" @@ -53,6 +61,7 @@ private: TEST_CASE(matchOp); TEST_CASE(isArithmeticalOp); + TEST_CASE(isOp); TEST_CASE(isExtendedOp); TEST_CASE(isAssignmentOp); TEST_CASE(isStandardType); @@ -225,6 +234,10 @@ private: givenACodeSampleToTokenize str("\"abc\""); ASSERT_EQUALS(true, Token::Match(str.tokens(), "%str%")); + + // Empty string + givenACodeSampleToTokenize emptyStr("\"\""); + ASSERT_EQUALS(true, Token::Match(emptyStr.tokens(), "%str%")); } void matchVarid() { @@ -307,187 +320,186 @@ private: ASSERT_EQUALS(true, Token::Match(logicalAnd.tokens(), "%oror%|&&")); } + void append_vector(std::vector &dest, const std::vector &src) { + dest.insert(dest.end(), src.begin(), src.end()); + } + + void initOps() { + arithmeticalOps.push_back("+"); + arithmeticalOps.push_back("-"); + arithmeticalOps.push_back("*"); + arithmeticalOps.push_back("/"); + arithmeticalOps.push_back("%"); + arithmeticalOps.push_back("<<"); + arithmeticalOps.push_back(">>"); + + normalOps.push_back("&&"); + normalOps.push_back("||"); + normalOps.push_back("=="); + normalOps.push_back("!="); + normalOps.push_back("<"); + normalOps.push_back("<="); + normalOps.push_back(">"); + normalOps.push_back(">="); + normalOps.push_back("&"); + normalOps.push_back("|"); + normalOps.push_back("^"); + normalOps.push_back("~"); + normalOps.push_back("!"); + + extendedOps.push_back(","); + extendedOps.push_back("["); + extendedOps.push_back("]"); + extendedOps.push_back("("); + extendedOps.push_back(")"); + extendedOps.push_back("?"); + extendedOps.push_back(":"); + + assignmentOps.push_back("="); + assignmentOps.push_back("+="); + assignmentOps.push_back("-="); + assignmentOps.push_back("*="); + assignmentOps.push_back("/="); + assignmentOps.push_back("%="); + assignmentOps.push_back("&="); + assignmentOps.push_back("^="); + assignmentOps.push_back("|="); + assignmentOps.push_back("<<="); + assignmentOps.push_back(">>="); + } + void matchOp() { - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("<<").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(">>").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("+").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("-").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("*").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("/").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("%").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("&&").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("||").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("==").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("!=").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("<").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("<=").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(">").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(">=").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("&").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("|").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("^").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("~").tokens(), "%op%")); - ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("!").tokens(), "%op%")); + std::vector test_ops; + append_vector(test_ops, arithmeticalOps); + append_vector(test_ops, normalOps); + + std::vector::const_iterator test_op, test_ops_end = test_ops.end(); + for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) { + ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(*test_op).tokens(), "%op%")); + } + + // Negative test against other operators + std::vector other_ops; + append_vector(other_ops, extendedOps); + append_vector(other_ops, assignmentOps); + + std::vector::const_iterator other_op, other_ops_end = other_ops.end(); + for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) { + ASSERT_EQUALS_MSG(false, Token::Match(givenACodeSampleToTokenize(*other_op).tokens(), "%op%"), "Failing other operator: " + *other_op); + } } void isArithmeticalOp() { - Token tok(NULL); + std::vector::const_iterator test_op, test_ops_end = arithmeticalOps.end(); + for (test_op = arithmeticalOps.begin(); test_op != test_ops_end; ++test_op) { + Token tok(NULL); + tok.str(*test_op); + ASSERT_EQUALS(true, tok.isArithmeticalOp()); + } - // Normal isOp() - tok.str("<<"); - ASSERT_EQUALS(true, tok.isArithmeticalOp()); - tok.str(">>"); - ASSERT_EQUALS(true, tok.isArithmeticalOp()); - tok.str("+"); - ASSERT_EQUALS(true, tok.isArithmeticalOp()); - tok.str("-"); - ASSERT_EQUALS(true, tok.isArithmeticalOp()); - tok.str("*"); - ASSERT_EQUALS(true, tok.isArithmeticalOp()); - tok.str("/"); - ASSERT_EQUALS(true, tok.isArithmeticalOp()); - tok.str("%"); + // Negative test against other operators + std::vector other_ops; + append_vector(other_ops, normalOps); + append_vector(other_ops, extendedOps); + append_vector(other_ops, assignmentOps); - // Negative test - tok.str("&"); - ASSERT_EQUALS(false, tok.isArithmeticalOp()); - tok.str("|"); - ASSERT_EQUALS(false, tok.isArithmeticalOp()); + std::vector::const_iterator other_op, other_ops_end = other_ops.end(); + for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) { + Token tok(NULL); + tok.str(*other_op); + ASSERT_EQUALS_MSG(false, tok.isArithmeticalOp(), "Failing arithmetical operator: " + *other_op); + } + } + + void isOp() { + std::vector test_ops; + append_vector(test_ops, arithmeticalOps); + append_vector(test_ops, normalOps); + + std::vector::const_iterator test_op, test_ops_end = test_ops.end(); + for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) { + Token tok(NULL); + tok.str(*test_op); + ASSERT_EQUALS(true, tok.isOp()); + } + + // Negative test against other operators + std::vector other_ops; + append_vector(other_ops, extendedOps); + append_vector(other_ops, assignmentOps); + + std::vector::const_iterator other_op, other_ops_end = other_ops.end(); + for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) { + Token tok(NULL); + tok.str(*other_op); + ASSERT_EQUALS_MSG(false, tok.isOp(), "Failing normal operator: " + *other_op); + } } void isExtendedOp() { - Token tok(NULL); + std::vector test_ops; + append_vector(test_ops, arithmeticalOps); + append_vector(test_ops, normalOps); + append_vector(test_ops, extendedOps); - // Normal isOp() - tok.str("<<"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str(">>"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("+"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("-"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("*"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("/"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("%"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("&&"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("||"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("=="); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("!="); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("<"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("<="); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str(">"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str(">="); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("&"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("|"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("^"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("~"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("!"); - ASSERT_EQUALS(true, tok.isExtendedOp()); + std::vector::const_iterator test_op, test_ops_end = test_ops.end(); + for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) { + Token tok(NULL); + tok.str(*test_op); + ASSERT_EQUALS(true, tok.isExtendedOp()); + } - // Extended operators - tok.str(","); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("["); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("]"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("("); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str(")"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str("?"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - tok.str(":"); - ASSERT_EQUALS(true, tok.isExtendedOp()); - - // Negative test for assignment operators - tok.str("="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("+="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("-="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("*="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("/="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("%="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("&="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("^="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("|="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str("<<="); - ASSERT_EQUALS(false, tok.isExtendedOp()); - tok.str(">>="); - ASSERT_EQUALS(false, tok.isExtendedOp()); + // Negative test against assignment operators + std::vector::const_iterator other_op, other_ops_end = assignmentOps.end(); + for (other_op = assignmentOps.begin(); other_op != other_ops_end; ++other_op) { + Token tok(NULL); + tok.str(*other_op); + ASSERT_EQUALS_MSG(false, tok.isExtendedOp(), "Failing assignment operator: " + *other_op); + } } void isAssignmentOp() { - Token tok(NULL); + std::vector::const_iterator test_op, test_ops_end = assignmentOps.end(); + for (test_op = assignmentOps.begin(); test_op != test_ops_end; ++test_op) { + Token tok(NULL); + tok.str(*test_op); + ASSERT_EQUALS(true, tok.isAssignmentOp()); + } - tok.str("="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("+="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("-="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("*="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("/="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("%="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("&="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("^="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("|="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str("<<="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); - tok.str(">>="); - ASSERT_EQUALS(true, tok.isAssignmentOp()); + // Negative test against other operators + std::vector other_ops; + append_vector(other_ops, arithmeticalOps); + append_vector(other_ops, normalOps); + append_vector(other_ops, extendedOps); + + std::vector::const_iterator other_op, other_ops_end = other_ops.end(); + for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) { + Token tok(NULL); + tok.str(*other_op); + ASSERT_EQUALS_MSG(false, tok.isAssignmentOp(), "Failing assignment operator: " + *other_op); + } } void isStandardType() { - Token tok(NULL); - tok.str("bool"); - ASSERT_EQUALS(true, tok.isStandardType()); - tok.str("char"); - ASSERT_EQUALS(true, tok.isStandardType()); - tok.str("short"); - ASSERT_EQUALS(true, tok.isStandardType()); - tok.str("int"); - ASSERT_EQUALS(true, tok.isStandardType()); - tok.str("long"); - ASSERT_EQUALS(true, tok.isStandardType()); - tok.str("float"); - ASSERT_EQUALS(true, tok.isStandardType()); - tok.str("double"); - ASSERT_EQUALS(true, tok.isStandardType()); - tok.str("size_t"); - ASSERT_EQUALS(true, tok.isStandardType()); + std::vector standard_types; + standard_types.push_back("bool"); + standard_types.push_back("char"); + standard_types.push_back("short"); + standard_types.push_back("int"); + standard_types.push_back("long"); + standard_types.push_back("float"); + standard_types.push_back("double"); + standard_types.push_back("size_t"); + + std::vector::const_iterator test_op, test_ops_end = standard_types.end(); + for (test_op = standard_types.begin(); test_op != test_ops_end; ++test_op) { + Token tok(NULL); + tok.str(*test_op); + ASSERT_EQUALS_MSG(true, tok.isStandardType(), "Failing standard type: " + *test_op); + } // Negative test + Token tok(0); tok.str("string"); ASSERT_EQUALS(false, tok.isStandardType()); }