I'm working on adding a new flavor of assertion inspired by what I've
seen in some unit-testing frameworks.  The idea is that if you assert
"foo == 3" and it fails, it would be nice to know what the actual
value of foo was without having to rerun the simulation or load up the
core dump (if you even have one), so why not print out those values if
the assertion fails.

My initial attempt looked something like this:

#define assert_rel(lhs, op, rhs)
    if (!(lhs op rhs)) {
        panic("Assertion failed: " #lhs " " #op " " #rhs ", actual
values %d " #op " %d\n", lhs, rhs);
    }

...which works fine as long as the things you are comparing are
integers.  Of course the first thing I tried it on was an assertion
involving a MemCmd value, which is a class that wraps an enum, so I
got quite a spew of error messages when I tried to compile this since
there's no conversion from MemCmd to int.  Also, MemCmd has a nice
toString() method which is what we really want to use to print the
values.  We can't call lhs.toString() directly though either, even if
we wanted to add toString() to all our types, since you can't add
methods to built-in types like int.

What we really want is something like python's __str__, but C++
doesn't have that.  The closest thing is good ol' operator<<, which
isn't pretty but at least it works on built-in types.  What I've got
right now uses the following inside the if, which seems to work (given
a definition for operator<<(std::ostream &os, MemCmd &m)) but is
somewhat awkward:

        std::cerr << "Actual values: " << lhs << " " #op " " << rhs <<
std::endl;
        panic("Assertion failed: " #lhs " " #op " " #rhs "\n");

My modest proposal is to add a new cprintf format character (maybe
"%O") that prints the associated argument directly using operator<<
without assuming anything about the argument's type (other than that
there's an operator<< defined for it).  Then I can write assert_rel
like the original version only with %O in place of %d, and we can
define operator<< on classes as needed so that they work for
assert_rel.

I figured I'd give Nate a chance to comment on the cprintf thing
before I added it since that's his baby... comments from others or on
the whole assert_rel idea are welcome too.

Steve
_______________________________________________
m5-dev mailing list
m5-dev@m5sim.org
http://m5sim.org/mailman/listinfo/m5-dev

Reply via email to