Printing actual and expected in hexadecimal format?

1,705 views
Skip to first unread message

Jeanne

unread,
Aug 5, 2011, 2:57:27 PM8/5/11
to Google C++ Testing Framework
I'm using Google Test 1.6.0 to test an embedded system that involves
direct register reads and writes. In these cases I'm working with
hexadecimal numbers. Seeing a failure message with "170" instead of
"0xaa" is less than helpful.

Has anyone made this kind of modification? The macro I'll likely use
most often for these tests is EXPECT_EQ. One option is to change the
code to output, say, "170 :: 0xaa" instead of just one or the other.
Looking at the code, it seems the place for this is probably inside
the macro GTEST_FORMAT_IMPL_. Or, perhaps I want to add a new macro?
If someone else has already done something like this, why reinvent the
wheel?

Any advice you can give me would be much appreciated regarding what to
change, where to change it, and how to change it. Thank you.

Zhanyong Wan (λx.x x)

unread,
Aug 5, 2011, 3:47:49 PM8/5/11
to Jeanne, Google C++ Testing Framework
You can define your own verification function to use inside
EXPECT_TRUE(). That way you can format the values whatever way you
want.

http://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Using_a_Function_That_Returns_an_AssertionResult

The result syntax would be something like

EXPECT_TRUE(Equals(a, b));

where Equals() is a function you define that returns an AssertionResult.

--
Zhanyong

Jeanne

unread,
Aug 10, 2011, 4:06:11 PM8/10/11
to Google C++ Testing Framework
I ended up modifying the file gtest-1.6.0/include/gtest/gtest-
printer.h
to extend the PrintTo template as such:

+inline void PrintTo(uint16_t x, ::std::ostream* os) {
+ *os << x << " (0x" << std::hex << x << ')';
+}
+
+inline void PrintTo(uint32_t x, ::std::ostream* os) {
+ *os << x << " (0x" << std::hex << x << ')';
+}

With that, unsigned integers are displayed in both decimal and hex
format.
It does shorten, say, 0x000a to 0xa but that's good enough for my
needs.


On Aug 5, 3:47 pm, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
> You can define your own verification function to use inside
> EXPECT_TRUE().  That way you can format the values whatever way you
> want.
>
> http://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Using_a_F...
>
> The result syntax would be something like
>
>   EXPECT_TRUE(Equals(a, b));
>
> where Equals() is a function you define that returns an AssertionResult.
>
>
>
>
>
> On Fri, Aug 5, 2011 at 11:57 AM, Jeanne <jea...@petrangelo.org> wrote:
> > I'm using Google Test 1.6.0 to test an embedded system that involves
> > direct register reads and writes. In these cases I'm working with
> > hexadecimal numbers. Seeing a failure message with "170" instead of
> > "0xaa" is less than helpful.
>
> > Has anyone made this kind of modification? The macro I'll likely use
> > most often for these tests is EXPECT_EQ. One option is to change the
> > code to output, say, "170 :: 0xaa" instead of just one or the other.
> > Looking at the code, it seems the place for this is probably inside
> > the macro GTEST_FORMAT_IMPL_. Or, perhaps I want to add a new macro?
> > If someone else has already done something like this, why reinvent the
> > wheel?
>
> > Any advice you can give me would be much appreciated regarding what to
> > change, where to change it, and how to change it.  Thank you.
>
> --
> Zhanyong- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages