Skip to content

ASSERT() on calls to Tokenizer::tokenize() in test code#3501

Merged
danmar merged 26 commits intocppcheck-opensource:mainfrom
chrchr-github:chr_AssertTokenize
Nov 29, 2021
Merged

ASSERT() on calls to Tokenizer::tokenize() in test code#3501
danmar merged 26 commits intocppcheck-opensource:mainfrom
chrchr-github:chr_AssertTokenize

Conversation

@chrchr-github
Copy link
Copy Markdown
Collaborator

No description provided.

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

And how about this one? ;)

Comment thread test/test64bit.cpp Outdated
LOAD_LIB_2(settings.library, "std.cfg");
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is pretty good. We want to fail if this fails. Unfortunately the output will not make much sense. Can we do something alternative that will make the output somewhat more helpful if this fails.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, like a macro ASSERT_TOKENIZE() that adds something to errout on failure? Or what do you have in mind?

Copy link
Copy Markdown
Collaborator

@danmar danmar Oct 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think ASSERT_TOKENIZE will work neither it would point out the line where the tokenize fails right? So you have no idea where check is called from.
How about:

if (!tokenizer.tokenize(istr, "test.cpp))
    errout << "tokenize failed!";

So execution will continue and when errout is checked there will be an assertion error that is more clear.
An alternative could be to change check() so it takes a line number and then we can print the line number in the error message.

#define check(code)   check_(code, __LINE__)
void check_(const char code[], int line)
.....

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I want is that if the test case at line 654 calls check() directly or indirectly. Then I want that the assertion message says that the test case at line 654 fails. I fear that it will complain about line 53 with your suggestion where the tokenizer.tokenize() is called. And then you have to manually debug to figure out where the problem is.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require numerous changes, since every test defines its own check()-like functions... Could be done though.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require numerous changes, since every test defines its own check()-like functions... Could be done though.

could you try it out on 1 check function to start with so we see that it looks reasonable.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added something to test64bit.cpp

Comment thread test/testastutils.cpp Outdated
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT(tokenizer.tokenize(istr, "test.cpp")) false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks some search and replace bug - there's more of that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASSERT needs to return something here, Maybe that's not the way it's supposed to be used though...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this looks weird.

Comment thread test/testexprengine.cpp Outdated
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT(tokenizer.tokenize(istr, "test.cpp")) {};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks some search and replace bug - there's more of that.

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It looks promising

Comment thread test/testsuite.h Outdated

#define TEST_CASE( NAME ) do { if (prepareTest(#NAME)) { setVerbose(false); NAME(); } } while (false)
#define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, (CONDITION))) return
#define ASSERT_LOC( CONDITION ) if (!assert_(file, line, (CONDITION))) return
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that the macro takes the file,line as arguments so the interface is clear.

I don't think a return here will help much, the test will continue to run. Probably a very misleading assertion failure would follow. I suggest that the return is removed and the test will continue as before.

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

There is quite a number of check()-like functions in some of the files, so this could get tedious. Anyway, I have found another broken test case, see the latest commit.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Oct 29, 2021

There is quite a number of check()-like functions in some of the files, so this could get tedious

maybe we can merge it step by step. If you clean this up I would not mind to merge it.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Oct 29, 2021

Anyway, I have found another broken test case, see the latest commit.

good 👍

Comment thread test/testastutils.cpp Outdated
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT(tokenizer.tokenize(istr, "test.cpp")) false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before I merge I want that this is fixed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, there is still a lot to do yet. You could also push this separately: fa69d3c

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed that with f363d89

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

I think I'm more or less done now. Feel free to merge anything you like...

@danmar danmar merged commit ca311eb into cppcheck-opensource:main Nov 29, 2021
@chrchr-github chrchr-github deleted the chr_AssertTokenize branch April 10, 2022 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants