views:

335

answers:

4

Hi

I would like to have a portable implemenation of my application. However, I have heard that there are some issues with printf from the stdlib on certain machines where it does not behave as intended. For instance, when using the conversion specifier %f then it can happen that on certain architectures the printf implementation includes a decimal point in the output!

Now I am wondering, if there are maybe some testing routines out there which I could use to test the semantic correctness of stdlib c implementation, in particular the printf routine. Maybe there are some good resources that point out some issues when porting programs?

Many thanks, Heinz

+1  A: 

You should write your own test suite that covers the issues that concern you. It's very simple to call printf 100 times with varying inputs, and the output is simple text, so it's simple to check against the output you expect.

Ned Batchelder
+3  A: 

I think Postel's law ("be conservative in what you do, be liberal in what you accept from others") applies here, too. Don't write your tests to require a character-by-character match in order to consider the printf() implementation to be working.

Instead, do it a a higher level; parse the text output by printf() into the expected datatype, and compare against a value of that type.

I.e., if printing "2.25", parse the text (using strtod() or equivalent) and compare against the actual number 2.25, not the literal text string "2.25".

unwind
A: 

I would recommend to test it in the following way: use sprintf() to produce some testing templates and compare them to your "correct" one.

I did something like this using fprintf (just to avoid the caching in our embedded system).

I think that results would not differ for the printf and sprintf: the formatting algorithm is the same.

avp
The algorithms are the same because in most instances all the printf family uses a common implementation. Comparing one family member to another will only confirm that they got the same answer, but not help decide if it is the right answer.
RBerteig
A: 

The wine test suite for the msvcrt dll looks interesting : http://github.com/mirrors/wine/blob/master/dlls/msvcrt/tests/printf.c

Drealmer