[PATCH:libXt] Add test framework similar to xserver and use it to test XtAsprintf

Cyril Brulebois kibi at debian.org
Tue Mar 8 14:40:13 PST 2011

Alan Coopersmith <alan.coopersmith at oracle.com> (08/03/2011):
> Simple test case just compares the results of snprintf to a static
> buffer with the new buffer returned by XtAsprintf.

Good idea. I think you should also have another test, with a bigger
string to hit the second code path (the vsnprintf call returning more
than the buffer's size: 256).

> Test run currently fails due to correctly detecting the bug in null
> terminating the XtAsprintf returned string.

It actually doesn't fail here. (And yes, I checked without the fix.)

To build some stats:
  $ ok=0; nok=0; for i in $(seq 1 100); do make -C build/test check >/dev/null 2>&1; if [ $? -eq 2 ]; then nok=$((nok+1)); else ok=$((ok+1)); fi; done; echo "ok: $ok"; echo "nok: $nok"

I supposed it depends again on the size we're considering. Using "foo"
instead of program_name, I'm having this kind of stats:
  ok: 28
  nok: 72
  ok: 24
  nok: 76

(Might depend on the length of the string representation for the
random int.)

While with program_name, I'm hitting those everytime:
  ok: 100
  nok: 0

Note that program_name is much longer than "foo", which might explain
the differences at run time:

Therefore I'd suggest running 3 tests:
 - with a short string ("foo"-like) + integer
 - with a “medium yet random” string (program_name) + integer
 - with a longer string (to hit the 256 limit) + integer

And running those in loops of several-dozens/hundreds occurrences
might be a good idea.

> diff --git a/test/Alloc.c b/test/Alloc.c
> new file mode 100644
> index 0000000..5f9e27a
> --- /dev/null
> +++ b/test/Alloc.c
> +static void test_XtAsprintf(void)
> +{
> +    char snbuf[1024];
> +    char *asbuf;
> +    gint32 r = g_test_rand_int();
> +    int snlen, aslen;
> +    

Whitespace here.

> +    snlen = snprintf(snbuf, sizeof(snbuf), "%s: %d\n", program_name, r);
> +    aslen = XtAsprintf(&asbuf, "%s: %d\n", program_name, r);
> +
> +    g_assert(asbuf != NULL);
> +    g_assert(snlen == aslen);
> +    g_assert(strcmp(snbuf, asbuf) == 0);
> +}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.x.org/archives/xorg-devel/attachments/20110308/70fafaea/attachment.pgp>

More information about the xorg-devel mailing list