xiaowanmay / googletest

Automatically exported from code.google.com/p/googletest
0 stars 0 forks source link

Hexadecimal Printing in Test Fail messages #222

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Compare two numerical values using ASSERT_EQ function; 
2. The output shows expected and actual values in base 10 format
3. There is no way to print these expected and default values in
hexadecimal/octal format

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
unix

Please provide any additional information below, such as a code snippet.

Original issue reported on code.google.com by giris...@gmail.com on 12 Nov 2009 at 5:07

GoogleCodeExporter commented 9 years ago
ASSERT/EXPECT_EQ prints its arguments by streaming to ostream (see 
http://code.google.com/p/googletest/wiki/GoogleTestPrimer). The ostream by 
default formats integers in decimal format. You can either define a class such 
as this:

class Hex {
 public:
  explicit Hex(int n) : number_(n) {}
  operator int() { return number; }

 private:
  int number_;
};

then define an operator to stream Hex as a hexadecimal and then 
EXPECT_EQ(Hex(expected), Hex(actual)) will print values in hexadecimal.

If you prefer a more robust approach (and not typing Hex everywhere), you can 
write 
a macro that prints comparison values in hex. See 
http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Using_a_Pred
icate-Formatter for more details.

Original comment by vladlosev on 12 Nov 2009 at 6:39