Background: The code-base that I'm working on is quite large and heavily utilizes asserts. A large number of the issues found in testing end up being associated with an individual assert(a file and line number). If someone modifies the source code, however, the line number associated with the assert can change and it will become difficult to track when it reoccurs.
Example: During testing, say the testers encounter several asserts at main.cpp:1808. A bug is logged into our defect tracking system against this assert. The next day someone modifies main.cpp. When the testers re-execute their tests, the same assert still occurs but is now reported to occur at main.cpp:1790. Therefore it is difficult to determine whether this is a new assert or a re-occurance of the previously seen assert without looking at the source code.
Question: Is is possible to associate each individual assert() with an unique ID, that will persist across code changes, instead of relying on the line number? I wasn't able to think of a solution myself. I'm hoping someone smarter than me will have some ideas. Here's how I'm thinking a solution would behave:
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
//Can ASSERT to redefined to generate a UID?
#define ASSERT assert
void main(void)
{
std::string name;
int age;
std::cin >> name;
ASSERT(name.length() < 10); //Generate a UID if assert fails(ie 0001)
std::cin >> age;
ASSERT(age < 100); //Generate a UID if assert fails(ie 0002)
}
After some code changes
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
#define ASSERT assert
void main(void)
{
std::string name;
int age;
int height;
std::cin >> height;
ASSERT(height < 10); //Generate a UID if assert fails(ie 0003)
std::cin >> name;
ASSERT(name.length() < 10); //Generate a UID if assert fails(ie 0001)
std::cin >> age;
ASSERT(age < 100); //Generate a UID if assert fails(ie 0002)
}
You could use a slightly broader criteria than line number, such as function and assertion text. For example,
#define ASSERT(cond) \
if(!(cond)) \
{ \
std::cerr << "Assertion failure: "
<< __FILE__ << ":" << __FUNCTION__ << "-" << #cond << std::endl; \
abort(); \
} \
One could argue that if the function or condition change, even if the UID you would generate doesn't, its still a different error that should be looked into separately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With