bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] inttostr-tests: depend on snprintf, not snprintf-posix


From: Jim Meyering
Subject: Re: [PATCH] inttostr-tests: depend on snprintf, not snprintf-posix
Date: Sat, 12 Jun 2010 15:01:27 +0200

Bruno Haible wrote:
>> coreutils bootstrap would fail like this:
>>
>>     ./bootstrap: m4/xsize.m4 overrides ._bootmp2/m4/xsize.m4
>>     ./bootstrap: aclocal --force -I m4 ...
>>     missing file gnulib-tests/vasnprintf.c
>>     configure.ac:45: error: expected source file, required through 
>> AC_LIBSOURCES, not found
>>     m4/gnulib-comp.m4:1729: gl_INIT is expanded from...
>>     configure.ac:45: the top level
>>     autom4te: /usr/bin/m4 failed with exit status: 1
>>     aclocal: error: autom4te failed with exit status: 1
>> ...
>> I've just noticed that snprintf-posix includes this notice:
>>
>>     Comment:
>>     This module should not be used as a dependency from a test module,
>>     otherwise when this module occurs as a tests-related module, it will
>>     have side effects on the compilation of the 'vasnprintf' module, if
>>     that module occurs among the main modules in lib/.
>
> Yes, this comment exactly describes the situation. The fact that the
> 'vasnprintf' and 'vasnprintf-posix' modules use the same source file with
> different config.h parametrizations is what triggers the error message and
> is also the reason for the comment.
>
>> Yet, the inttostr tests require the ability to use %jd and %ju formats,
>> so I've changed that module to depend on the snprintf module instead.
>
> But the 'snprintf' module does not guarantee that the %jd and %ju formats
> can be used. (See doc/posix-functions/snprintf.texi.) On Solaris 9 SPARC,
> both in 32-bit mode and in 64-bit mode, as well as on Solaris 9 x86 (32-bit
> mode), I get this test failure:

Hi Bruno,

>   test-inttostr.c:68: assertion failed
>   FAIL: test-inttostr

Thanks for reporting that.

> IMO imaxtostr etc. is a function that is meant to do something that the system
> functions cannot do. Therefore its unit test must necessarily do something
> different than comparing its result to the one that the system functions
> produce.

Not really.
Each implements one small part of snprintf,
using less than 1/50th the amount of code.

> While the CK macro can be used for the types 'int' and 'unsigned int',
> (because snprintf is portably usable with %d and %u), I think a test for
> the larger types (off_t, intmax_t, uintmax_t) requires either
>   - hardcoding the expected values (one for 32-bit, one for 64-bit, maybe
>     one for 128-bit integers), or
>   - computing the expected value by calling sprintf on
>     x/10^18, (x/10^9) mod 10^9, x mod 10^9 and combining the results.

Thanks for the suggestion, but that amount of obfuscation (in a test,
for the likes of old systems like Solaris 9) is not worthwhile.
I prefer to skip the test on systems with deficient snprintf: I've
confirmed that with the change below,

    ./gnulib-tool --create-testdir --with-tests --test inttostr

still runs the test, and it passes.

>From 387a24f6b4d1e1233ece4a665ea76dafd15415ca Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sat, 12 Jun 2010 14:47:43 +0200
Subject: [PATCH] test-inttostr: avoid spurious failure on Solaris 9

* tests/test-inttostr.c (main): Skip the test when snprintf fails
to accept "%ju".  Reported by Bruno Haible.
---
 ChangeLog             |    6 ++++++
 tests/test-inttostr.c |   24 ++++++++++++++++++------
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7b6394d..88e01ab 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2010-06-12  Jim Meyering  <address@hidden>
+
+       test-inttostr: avoid spurious failure on Solaris 9
+       * tests/test-inttostr.c (main): Skip the test when snprintf fails
+       to accept "%ju".  Reported by Bruno Haible.
+
 2010-06-11  Jim Meyering  <address@hidden>

        test-sys_socket: mark variables as used more readably
diff --git a/tests/test-inttostr.c b/tests/test-inttostr.c
index e53d22a..bf18621 100644
--- a/tests/test-inttostr.c
+++ b/tests/test-inttostr.c
@@ -65,10 +65,22 @@
 int
 main (void)
 {
-  CK (int,          inttostr);
-  CK (unsigned int, uinttostr);
-  CK (off_t,        offtostr);
-  CK (uintmax_t,    umaxtostr);
-  CK (intmax_t,     imaxtostr);
-  return 0;
+  char buf[2];
+
+  /* Ideally we would rely on the snprintf-posix module, in which case
+     this guard would not be required, but due to limitations in gnulib's
+     implementation (see modules/snprintf-posix), we cannot.  */
+  if (snprintf (buf, sizeof buf, "%ju", (uintmax_t) 3) == 1
+      && buf[0] == '3' && buf[1] == '\0')
+    {
+      CK (int,          inttostr);
+      CK (unsigned int, uinttostr);
+      CK (off_t,        offtostr);
+      CK (uintmax_t,    umaxtostr);
+      CK (intmax_t,     imaxtostr);
+      return 0;
+    }
+
+  /* snprintf doesn't accept %ju; skip this test.  */
+  return 77;
 }
--
1.7.1.501.g23b46



reply via email to

[Prev in Thread] Current Thread [Next in Thread]