Skip to content

Commit 1360c55

Browse files
committed
Extended isOp() tests to do proper negative testing against other operators
1 parent 7ef1107 commit 1360c55

1 file changed

Lines changed: 177 additions & 165 deletions

File tree

test/testtoken.cpp

Lines changed: 177 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "settings.h"
2323

2424
#include <cstring>
25+
#include <vector>
26+
#include <string>
2527

2628
extern std::ostringstream errout;
2729
class TestToken : public TestFixture {
@@ -30,8 +32,14 @@ class TestToken : public TestFixture {
3032
{ }
3133

3234
private:
35+
std::vector<std::string> arithmeticalOps;
36+
std::vector<std::string> normalOps;
37+
std::vector<std::string> extendedOps;
38+
std::vector<std::string> assignmentOps;
3339

3440
void run() {
41+
initOps();
42+
3543
TEST_CASE(nextprevious);
3644
TEST_CASE(multiCompare);
3745
TEST_CASE(multiCompare2); // #3294 - false negative multi compare between "=" and "=="
@@ -53,6 +61,7 @@ class TestToken : public TestFixture {
5361
TEST_CASE(matchOp);
5462

5563
TEST_CASE(isArithmeticalOp);
64+
TEST_CASE(isOp);
5665
TEST_CASE(isExtendedOp);
5766
TEST_CASE(isAssignmentOp);
5867
TEST_CASE(isStandardType);
@@ -225,6 +234,10 @@ class TestToken : public TestFixture {
225234

226235
givenACodeSampleToTokenize str("\"abc\"");
227236
ASSERT_EQUALS(true, Token::Match(str.tokens(), "%str%"));
237+
238+
// Empty string
239+
givenACodeSampleToTokenize emptyStr("\"\"");
240+
ASSERT_EQUALS(true, Token::Match(emptyStr.tokens(), "%str%"));
228241
}
229242

230243
void matchVarid() {
@@ -307,187 +320,186 @@ class TestToken : public TestFixture {
307320
ASSERT_EQUALS(true, Token::Match(logicalAnd.tokens(), "%oror%|&&"));
308321
}
309322

323+
void append_vector(std::vector<std::string> &dest, const std::vector<std::string> &src) {
324+
dest.insert(dest.end(), src.begin(), src.end());
325+
}
326+
327+
void initOps() {
328+
arithmeticalOps.push_back("+");
329+
arithmeticalOps.push_back("-");
330+
arithmeticalOps.push_back("*");
331+
arithmeticalOps.push_back("/");
332+
arithmeticalOps.push_back("%");
333+
arithmeticalOps.push_back("<<");
334+
arithmeticalOps.push_back(">>");
335+
336+
normalOps.push_back("&&");
337+
normalOps.push_back("||");
338+
normalOps.push_back("==");
339+
normalOps.push_back("!=");
340+
normalOps.push_back("<");
341+
normalOps.push_back("<=");
342+
normalOps.push_back(">");
343+
normalOps.push_back(">=");
344+
normalOps.push_back("&");
345+
normalOps.push_back("|");
346+
normalOps.push_back("^");
347+
normalOps.push_back("~");
348+
normalOps.push_back("!");
349+
350+
extendedOps.push_back(",");
351+
extendedOps.push_back("[");
352+
extendedOps.push_back("]");
353+
extendedOps.push_back("(");
354+
extendedOps.push_back(")");
355+
extendedOps.push_back("?");
356+
extendedOps.push_back(":");
357+
358+
assignmentOps.push_back("=");
359+
assignmentOps.push_back("+=");
360+
assignmentOps.push_back("-=");
361+
assignmentOps.push_back("*=");
362+
assignmentOps.push_back("/=");
363+
assignmentOps.push_back("%=");
364+
assignmentOps.push_back("&=");
365+
assignmentOps.push_back("^=");
366+
assignmentOps.push_back("|=");
367+
assignmentOps.push_back("<<=");
368+
assignmentOps.push_back(">>=");
369+
}
370+
310371
void matchOp() {
311-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("<<").tokens(), "%op%"));
312-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(">>").tokens(), "%op%"));
313-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("+").tokens(), "%op%"));
314-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("-").tokens(), "%op%"));
315-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("*").tokens(), "%op%"));
316-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("/").tokens(), "%op%"));
317-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("%").tokens(), "%op%"));
318-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("&&").tokens(), "%op%"));
319-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("||").tokens(), "%op%"));
320-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("==").tokens(), "%op%"));
321-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("!=").tokens(), "%op%"));
322-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("<").tokens(), "%op%"));
323-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("<=").tokens(), "%op%"));
324-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(">").tokens(), "%op%"));
325-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(">=").tokens(), "%op%"));
326-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("&").tokens(), "%op%"));
327-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("|").tokens(), "%op%"));
328-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("^").tokens(), "%op%"));
329-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("~").tokens(), "%op%"));
330-
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize("!").tokens(), "%op%"));
372+
std::vector<std::string> test_ops;
373+
append_vector(test_ops, arithmeticalOps);
374+
append_vector(test_ops, normalOps);
375+
376+
std::vector<std::string>::const_iterator test_op, test_ops_end = test_ops.end();
377+
for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) {
378+
ASSERT_EQUALS(true, Token::Match(givenACodeSampleToTokenize(*test_op).tokens(), "%op%"));
379+
}
380+
381+
// Negative test against other operators
382+
std::vector<std::string> other_ops;
383+
append_vector(other_ops, extendedOps);
384+
append_vector(other_ops, assignmentOps);
385+
386+
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
387+
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
388+
ASSERT_EQUALS_MSG(false, Token::Match(givenACodeSampleToTokenize(*other_op).tokens(), "%op%"), "Failing other operator: " + *other_op);
389+
}
331390
}
332391

333392
void isArithmeticalOp() {
334-
Token tok(NULL);
335-
336-
// Normal isOp()
337-
tok.str("<<");
338-
ASSERT_EQUALS(true, tok.isArithmeticalOp());
339-
tok.str(">>");
340-
ASSERT_EQUALS(true, tok.isArithmeticalOp());
341-
tok.str("+");
342-
ASSERT_EQUALS(true, tok.isArithmeticalOp());
343-
tok.str("-");
344-
ASSERT_EQUALS(true, tok.isArithmeticalOp());
345-
tok.str("*");
346-
ASSERT_EQUALS(true, tok.isArithmeticalOp());
347-
tok.str("/");
348-
ASSERT_EQUALS(true, tok.isArithmeticalOp());
349-
tok.str("%");
393+
std::vector<std::string>::const_iterator test_op, test_ops_end = arithmeticalOps.end();
394+
for (test_op = arithmeticalOps.begin(); test_op != test_ops_end; ++test_op) {
395+
Token tok(NULL);
396+
tok.str(*test_op);
397+
ASSERT_EQUALS(true, tok.isArithmeticalOp());
398+
}
399+
400+
// Negative test against other operators
401+
std::vector<std::string> other_ops;
402+
append_vector(other_ops, normalOps);
403+
append_vector(other_ops, extendedOps);
404+
append_vector(other_ops, assignmentOps);
405+
406+
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
407+
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
408+
Token tok(NULL);
409+
tok.str(*other_op);
410+
ASSERT_EQUALS_MSG(false, tok.isArithmeticalOp(), "Failing arithmetical operator: " + *other_op);
411+
}
412+
}
350413

351-
// Negative test
352-
tok.str("&");
353-
ASSERT_EQUALS(false, tok.isArithmeticalOp());
354-
tok.str("|");
355-
ASSERT_EQUALS(false, tok.isArithmeticalOp());
414+
void isOp() {
415+
std::vector<std::string> test_ops;
416+
append_vector(test_ops, arithmeticalOps);
417+
append_vector(test_ops, normalOps);
418+
419+
std::vector<std::string>::const_iterator test_op, test_ops_end = test_ops.end();
420+
for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) {
421+
Token tok(NULL);
422+
tok.str(*test_op);
423+
ASSERT_EQUALS(true, tok.isOp());
424+
}
425+
426+
// Negative test against other operators
427+
std::vector<std::string> other_ops;
428+
append_vector(other_ops, extendedOps);
429+
append_vector(other_ops, assignmentOps);
430+
431+
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
432+
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
433+
Token tok(NULL);
434+
tok.str(*other_op);
435+
ASSERT_EQUALS_MSG(false, tok.isOp(), "Failing normal operator: " + *other_op);
436+
}
356437
}
357438

358439
void isExtendedOp() {
359-
Token tok(NULL);
360-
361-
// Normal isOp()
362-
tok.str("<<");
363-
ASSERT_EQUALS(true, tok.isExtendedOp());
364-
tok.str(">>");
365-
ASSERT_EQUALS(true, tok.isExtendedOp());
366-
tok.str("+");
367-
ASSERT_EQUALS(true, tok.isExtendedOp());
368-
tok.str("-");
369-
ASSERT_EQUALS(true, tok.isExtendedOp());
370-
tok.str("*");
371-
ASSERT_EQUALS(true, tok.isExtendedOp());
372-
tok.str("/");
373-
ASSERT_EQUALS(true, tok.isExtendedOp());
374-
tok.str("%");
375-
ASSERT_EQUALS(true, tok.isExtendedOp());
376-
tok.str("&&");
377-
ASSERT_EQUALS(true, tok.isExtendedOp());
378-
tok.str("||");
379-
ASSERT_EQUALS(true, tok.isExtendedOp());
380-
tok.str("==");
381-
ASSERT_EQUALS(true, tok.isExtendedOp());
382-
tok.str("!=");
383-
ASSERT_EQUALS(true, tok.isExtendedOp());
384-
tok.str("<");
385-
ASSERT_EQUALS(true, tok.isExtendedOp());
386-
tok.str("<=");
387-
ASSERT_EQUALS(true, tok.isExtendedOp());
388-
tok.str(">");
389-
ASSERT_EQUALS(true, tok.isExtendedOp());
390-
tok.str(">=");
391-
ASSERT_EQUALS(true, tok.isExtendedOp());
392-
tok.str("&");
393-
ASSERT_EQUALS(true, tok.isExtendedOp());
394-
tok.str("|");
395-
ASSERT_EQUALS(true, tok.isExtendedOp());
396-
tok.str("^");
397-
ASSERT_EQUALS(true, tok.isExtendedOp());
398-
tok.str("~");
399-
ASSERT_EQUALS(true, tok.isExtendedOp());
400-
tok.str("!");
401-
ASSERT_EQUALS(true, tok.isExtendedOp());
402-
403-
// Extended operators
404-
tok.str(",");
405-
ASSERT_EQUALS(true, tok.isExtendedOp());
406-
tok.str("[");
407-
ASSERT_EQUALS(true, tok.isExtendedOp());
408-
tok.str("]");
409-
ASSERT_EQUALS(true, tok.isExtendedOp());
410-
tok.str("(");
411-
ASSERT_EQUALS(true, tok.isExtendedOp());
412-
tok.str(")");
413-
ASSERT_EQUALS(true, tok.isExtendedOp());
414-
tok.str("?");
415-
ASSERT_EQUALS(true, tok.isExtendedOp());
416-
tok.str(":");
417-
ASSERT_EQUALS(true, tok.isExtendedOp());
418-
419-
// Negative test for assignment operators
420-
tok.str("=");
421-
ASSERT_EQUALS(false, tok.isExtendedOp());
422-
tok.str("+=");
423-
ASSERT_EQUALS(false, tok.isExtendedOp());
424-
tok.str("-=");
425-
ASSERT_EQUALS(false, tok.isExtendedOp());
426-
tok.str("*=");
427-
ASSERT_EQUALS(false, tok.isExtendedOp());
428-
tok.str("/=");
429-
ASSERT_EQUALS(false, tok.isExtendedOp());
430-
tok.str("%=");
431-
ASSERT_EQUALS(false, tok.isExtendedOp());
432-
tok.str("&=");
433-
ASSERT_EQUALS(false, tok.isExtendedOp());
434-
tok.str("^=");
435-
ASSERT_EQUALS(false, tok.isExtendedOp());
436-
tok.str("|=");
437-
ASSERT_EQUALS(false, tok.isExtendedOp());
438-
tok.str("<<=");
439-
ASSERT_EQUALS(false, tok.isExtendedOp());
440-
tok.str(">>=");
441-
ASSERT_EQUALS(false, tok.isExtendedOp());
440+
std::vector<std::string> test_ops;
441+
append_vector(test_ops, arithmeticalOps);
442+
append_vector(test_ops, normalOps);
443+
append_vector(test_ops, extendedOps);
444+
445+
std::vector<std::string>::const_iterator test_op, test_ops_end = test_ops.end();
446+
for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) {
447+
Token tok(NULL);
448+
tok.str(*test_op);
449+
ASSERT_EQUALS(true, tok.isExtendedOp());
450+
}
451+
452+
// Negative test against assignment operators
453+
std::vector<std::string>::const_iterator other_op, other_ops_end = assignmentOps.end();
454+
for (other_op = assignmentOps.begin(); other_op != other_ops_end; ++other_op) {
455+
Token tok(NULL);
456+
tok.str(*other_op);
457+
ASSERT_EQUALS_MSG(false, tok.isExtendedOp(), "Failing assignment operator: " + *other_op);
458+
}
442459
}
443460

444461
void isAssignmentOp() {
445-
Token tok(NULL);
446-
447-
tok.str("=");
448-
ASSERT_EQUALS(true, tok.isAssignmentOp());
449-
tok.str("+=");
450-
ASSERT_EQUALS(true, tok.isAssignmentOp());
451-
tok.str("-=");
452-
ASSERT_EQUALS(true, tok.isAssignmentOp());
453-
tok.str("*=");
454-
ASSERT_EQUALS(true, tok.isAssignmentOp());
455-
tok.str("/=");
456-
ASSERT_EQUALS(true, tok.isAssignmentOp());
457-
tok.str("%=");
458-
ASSERT_EQUALS(true, tok.isAssignmentOp());
459-
tok.str("&=");
460-
ASSERT_EQUALS(true, tok.isAssignmentOp());
461-
tok.str("^=");
462-
ASSERT_EQUALS(true, tok.isAssignmentOp());
463-
tok.str("|=");
464-
ASSERT_EQUALS(true, tok.isAssignmentOp());
465-
tok.str("<<=");
466-
ASSERT_EQUALS(true, tok.isAssignmentOp());
467-
tok.str(">>=");
468-
ASSERT_EQUALS(true, tok.isAssignmentOp());
462+
std::vector<std::string>::const_iterator test_op, test_ops_end = assignmentOps.end();
463+
for (test_op = assignmentOps.begin(); test_op != test_ops_end; ++test_op) {
464+
Token tok(NULL);
465+
tok.str(*test_op);
466+
ASSERT_EQUALS(true, tok.isAssignmentOp());
467+
}
468+
469+
// Negative test against other operators
470+
std::vector<std::string> other_ops;
471+
append_vector(other_ops, arithmeticalOps);
472+
append_vector(other_ops, normalOps);
473+
append_vector(other_ops, extendedOps);
474+
475+
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
476+
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
477+
Token tok(NULL);
478+
tok.str(*other_op);
479+
ASSERT_EQUALS_MSG(false, tok.isAssignmentOp(), "Failing assignment operator: " + *other_op);
480+
}
469481
}
470482

471483
void isStandardType() {
472-
Token tok(NULL);
473-
tok.str("bool");
474-
ASSERT_EQUALS(true, tok.isStandardType());
475-
tok.str("char");
476-
ASSERT_EQUALS(true, tok.isStandardType());
477-
tok.str("short");
478-
ASSERT_EQUALS(true, tok.isStandardType());
479-
tok.str("int");
480-
ASSERT_EQUALS(true, tok.isStandardType());
481-
tok.str("long");
482-
ASSERT_EQUALS(true, tok.isStandardType());
483-
tok.str("float");
484-
ASSERT_EQUALS(true, tok.isStandardType());
485-
tok.str("double");
486-
ASSERT_EQUALS(true, tok.isStandardType());
487-
tok.str("size_t");
488-
ASSERT_EQUALS(true, tok.isStandardType());
484+
std::vector<std::string> standard_types;
485+
standard_types.push_back("bool");
486+
standard_types.push_back("char");
487+
standard_types.push_back("short");
488+
standard_types.push_back("int");
489+
standard_types.push_back("long");
490+
standard_types.push_back("float");
491+
standard_types.push_back("double");
492+
standard_types.push_back("size_t");
493+
494+
std::vector<std::string>::const_iterator test_op, test_ops_end = standard_types.end();
495+
for (test_op = standard_types.begin(); test_op != test_ops_end; ++test_op) {
496+
Token tok(NULL);
497+
tok.str(*test_op);
498+
ASSERT_EQUALS_MSG(true, tok.isStandardType(), "Failing standard type: " + *test_op);
499+
}
489500

490501
// Negative test
502+
Token tok(0);
491503
tok.str("string");
492504
ASSERT_EQUALS(false, tok.isStandardType());
493505
}

0 commit comments

Comments
 (0)