From 60cfcc72e6d0a28aef1dbe39525291b0ee55dc95 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 16 Oct 2015 00:05:26 +0300 Subject: [PATCH] snprintf: moved self-test to a separated test --- .gitignore | 1 + Makefile.am | 5 ++- src/snprintf.c | 76 ------------------------------------ tests/test_snprintf.c | 89 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 77 deletions(-) create mode 100644 tests/test_snprintf.c diff --git a/.gitignore b/.gitignore index 438ab53..0b1ef2d 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ tests/test_rand tests/test_sasl tests/test_scram tests/test_sha1 +tests/test_snprintf tests/test_sock m4/ libstrophe.project diff --git a/Makefile.am b/Makefile.am index 9af4ce5..0f46f6f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -66,7 +66,7 @@ examples_uuid_LDADD = $(STROPHE_LIBS) ## Tests TESTS = tests/check_parser tests/test_sha1 tests/test_rand tests/test_scram \ - tests/test_base64 + tests/test_base64 tests/test_snprintf check_PROGRAMS = $(TESTS) tests_check_parser_SOURCES = tests/check_parser.c tests/test.h @@ -88,3 +88,6 @@ tests_test_scram_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src tests_test_sha1_SOURCES = tests/test_sha1.c src/sha1.c tests_test_sha1_CFLAGS = -I$(top_srcdir)/src + +tests_test_snprintf_SOURCES = tests/test_snprintf.c +tests_test_snprintf_CFLAGS = -I$(top_srcdir)/src diff --git a/src/snprintf.c b/src/snprintf.c index 61b038b..03d96f3 100644 --- a/src/snprintf.c +++ b/src/snprintf.c @@ -760,80 +760,4 @@ int xmpp_snprintf (va_alist) va_dcl } #endif /* !HAVE_SNPRINTF */ -#ifdef TEST_SNPRINTF -#ifndef LONG_STRING -#define LONG_STRING 1024 -#endif -int main (void) -{ - char buf1[LONG_STRING]; - char buf2[LONG_STRING]; - char *fp_fmt[] = { - "%-1.5f", - "%1.5f", - "%123.9f", - "%10.5f", - "% 10.5f", - "%+22.9f", - "%+4.9f", - "%01.3f", - "%4f", - "%3.1f", - "%3.2f", - "%.0f", - "%.1f", - NULL - }; - double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, - 0.9996, 1.996, 4.136, 0}; - char *int_fmt[] = { - "%-1.5d", - "%1.5d", - "%123.9d", - "%5.5d", - "%10.5d", - "% 10.5d", - "%+22.33d", - "%01.3d", - "%4d", - NULL - }; - long int_nums[] = { -1, 134, 91340, 341, 0203, 0}; - int x, y; - int fail = 0; - int num = 0; - - printf ("Testing xmpp_snprintf format codes against system sprintf...\n"); - - for (x = 0; fp_fmt[x] != NULL ; x++) - for (y = 0; fp_nums[y] != 0 ; y++) - { - xmpp_snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]); - sprintf (buf2, fp_fmt[x], fp_nums[y]); - if (strcmp (buf1, buf2)) - { - printf("xmpp_snprintf doesn't match Format: %s\n\txmpp_snprintf = %s\n\tsprintf = %s\n", - fp_fmt[x], buf1, buf2); - fail++; - } - num++; - } - - for (x = 0; int_fmt[x] != NULL ; x++) - for (y = 0; int_nums[y] != 0 ; y++) - { - xmpp_snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]); - sprintf (buf2, int_fmt[x], int_nums[y]); - if (strcmp (buf1, buf2)) - { - printf("xmpp_snprintf doesn't match Format: %s\n\txmpp_snprintf = %s\n\tsprintf = %s\n", - int_fmt[x], buf1, buf2); - fail++; - } - num++; - } - printf ("%d tests failed out of %d.\n", fail, num); -} -#endif /* SNPRINTF_TEST */ - #endif /* !HAVE_SNPRINTF */ diff --git a/tests/test_snprintf.c b/tests/test_snprintf.c new file mode 100644 index 0000000..b999a2a --- /dev/null +++ b/tests/test_snprintf.c @@ -0,0 +1,89 @@ +/* + * Copyright Patrick Powell 1995 + * This code is based on code written by Patrick Powell (papowell@astart.com) + * It may be used for any purpose as long as this notice remains intact + * on all source code distributions + */ + +#include + +/* We don't have separated header file, so include this instead of common.h */ +#include "snprintf.c" + +#ifndef LONG_STRING +#define LONG_STRING 1024 +#endif + +int main (void) +{ + char buf1[LONG_STRING]; + char buf2[LONG_STRING]; + char *fp_fmt[] = { + "%-1.5f", + "%1.5f", + "%123.9f", + "%10.5f", + "% 10.5f", + "%+22.9f", + "%+4.9f", + "%01.3f", + "%4f", + "%3.1f", + "%3.2f", + "%.0f", + "%.1f", + NULL + }; + double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, + 0.9996, 1.996, 4.136, 0}; + char *int_fmt[] = { + "%-1.5d", + "%1.5d", + "%123.9d", + "%5.5d", + "%10.5d", + "% 10.5d", + "%+22.33d", + "%01.3d", + "%4d", + "0x%x", + "0x%04x", + NULL + }; + long int_nums[] = { -1, 134, 91340, 341, 0203, 0x76543210, 0}; + int x, y; + int fail = 0; + int num = 0; + + printf ("Testing xmpp_snprintf format codes against system sprintf...\n"); + + for (x = 0; fp_fmt[x] != NULL ; x++) + for (y = 0; fp_nums[y] != 0 ; y++) + { + xmpp_snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]); + sprintf (buf2, fp_fmt[x], fp_nums[y]); + if (strcmp (buf1, buf2)) + { + printf("xmpp_snprintf doesn't match Format: %s\n\txmpp_snprintf = %s\n\tsprintf = %s\n", + fp_fmt[x], buf1, buf2); + fail++; + } + num++; + } + + for (x = 0; int_fmt[x] != NULL ; x++) + for (y = 0; int_nums[y] != 0 ; y++) + { + xmpp_snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]); + sprintf (buf2, int_fmt[x], int_nums[y]); + if (strcmp (buf1, buf2)) + { + printf("xmpp_snprintf doesn't match Format: %s\n\txmpp_snprintf = %s\n\tsprintf = %s\n", + int_fmt[x], buf1, buf2); + fail++; + } + num++; + } + printf ("%d tests failed out of %d.\n", fail, num); + return fail != 0 ? 1 : 0; +}