Test printf implementation
Asked Answered
E

4

5

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

Endres answered 4/9, 2009 at 9:46 Comment(3)
What's wrong with decimal point in the output? IMO it's more related to l10n than portability.Mutate
When you use it for testing, you basically get different outputs on different platforms; this could mean that a test might fail because of the decimal point leads to a different represenation although the values itself are the same.Endres
@Heinz, you can set the locale to "C" in your testing code (i.e. setlocale(LC_NUMERICAL, "C")). This should output always a decimal point and not a comma or something else.Fixture
M
4

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".

Multicolor answered 4/9, 2009 at 12:50 Comment(0)
S
3

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

Suppuration answered 6/10, 2010 at 15:57 Comment(0)
O
1

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.

Overline answered 4/9, 2009 at 12:40 Comment(0)
J
0

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.

Jugendstil answered 4/9, 2009 at 12:58 Comment(1)
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.Colbert

© 2022 - 2024 — McMap. All rights reserved.