This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v8 3/3] Add tests for strfrom functions
On Fri, 14 Oct 2016 14:31:25 -0300
"Gabriel F. T. Gomes" <gftg@linux.vnet.ibm.com> wrote:
> From: Rajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
>
> 2016-10-13 Rajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
>
> * stdlib/Makefile (tests): Add tst-strfrom and tst-strfrom-locale.
> * stdlib/tst-strfrom.c: New file.
> * stdlib/tst-strfrom-locale.c: New file.
>
> wi: http://dagger1.rch.stglabs.ibm.com:5678/b/tqzrQAkzP337NcJ5P/corelibs/uqHc2aKRqa6hNni2P
> Change-Id: I7c5975a608c2a8085db544a7f9de31c921f545ca
Please ignore these two lines... I sent them by accident. :(
I already remove them in my local branch.
> ---
> stdlib/Makefile | 4 +-
> stdlib/tst-strfrom-locale.c | 178 ++++++++++++++++++++++++++++++++++++++++++++
> stdlib/tst-strfrom.c | 178 ++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 359 insertions(+), 1 deletion(-)
> create mode 100644 stdlib/tst-strfrom-locale.c
> create mode 100644 stdlib/tst-strfrom.c
>
> diff --git a/stdlib/Makefile b/stdlib/Makefile
> index 3cacb8b..3cce9d9 100644
> --- a/stdlib/Makefile
> +++ b/stdlib/Makefile
> @@ -79,7 +79,7 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \
> tst-setcontext3 tst-tls-atexit-nodelete \
> tst-strtol-locale tst-strtod-nan-locale tst-strfmon_l \
> tst-quick_exit tst-thread-quick_exit tst-width \
> - tst-width-stdint
> + tst-width-stdint tst-strfrom tst-strfrom-locale
> tests-static := tst-secure-getenv
> ifeq ($(have-cxx-thread_local),yes)
> CFLAGS-tst-quick_exit.o = -std=c++11
> @@ -158,6 +158,8 @@ $(objpfx)tst-strtod5.out: $(gen-locales)
> $(objpfx)tst-strtol-locale.out: $(gen-locales)
> $(objpfx)tst-strtod-nan-locale.out: $(gen-locales)
> $(objpfx)tst-strfmon_l.out: $(gen-locales)
> +$(objpfx)tst-strfrom.out: $(gen-locales)
> +$(objpfx)tst-strfrom-locale.out: $(gen-locales)
> endif
>
> # Testdir has to be named stdlib and needs to be writable
> diff --git a/stdlib/tst-strfrom-locale.c b/stdlib/tst-strfrom-locale.c
> new file mode 100644
> index 0000000..09945a9
> --- /dev/null
> +++ b/stdlib/tst-strfrom-locale.c
> @@ -0,0 +1,178 @@
> +/* Tests for strfromf, strfromd, strfroml functions.
> + Copyright (C) 2016 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <http://www.gnu.org/licenses/>. */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <float.h>
> +#include <math.h>
> +#include <locale.h>
> +
> +#include "tst-strtod.h"
> +
> +/* Hexadecimal tests. */
> +struct htest
> +{
> + double val;
> + const char *fmt;
> + const char *exp[4];
> +};
> +static const struct htest htests[] = {
> + {0x1.ffp+6, "%a", { "0x1,ffp+6", "0x3,fep+5", "0x7,fcp+4", "0xf,f8p+3" } },
> + {0x1.88p+4, "%a", { "0x1,88p+4", "0x3,1p+3", "0x6,2p+2", "0xc,4p+1" } },
> + {-0x1.88p+5, "%A", { "-0X1,88P+5", "-0X3,1P+4", "-0X6,2P+3", "-0XC,4P+2" } },
> + {0x1.44p+10, "%a", { "0x1,44p+10", "0x2,88p+9", "0x5,1p+8", "0xa,2p+7"} },
> + {0x0.0040p+0, "%a", { "0x1p-10", "0x2p-11", "0x4p-12", "0x8p-13"} },
> + {10.0, "%a", { "0x1,4p+3", "0x2,8p+2", "0x5p+1", "0xap+0"} },
> + {0, NULL, {NULL}},
> +};
> +
> +/* Tests with buffer size small. */
> +struct stest
> +{
> + const char *expect;
> + double val;
> + const char *fmt;
> + int size;
> + int rc;
> +};
> +static const struct stest stests[] = {
> + {"1234", 12345.345, "%g", 5, 7},
> + {"0,12", .125, "%f", 5, 8},
> + {"9,99", 9.999, "%.3f", 5, 5},
> + {"100", 1e2, "%g", 5, 3},
> + {NULL, 0, "%f", 50, 0},
> +};
> +
> +struct val
> +{
> + float f;
> + double d;
> + long double ld;
> +};
> +struct ltest
> +{
> + const char *expect;
> + struct val values;
> + const char *fmt;
> + int size;
> + int rc;
> +};
> +static const struct ltest ltests[] = {
> + {"12,345000", { 12.345, 12.345, 12.345}, "%f", 50, 9},
> + {"9,999", { 9.999, 9.999, 9.999}, "%.3f", 50, 5},
> + {"0,125000", { .125, .125, .125}, "%f", 50, 8},
> + {"0,000000", { .0, .0, .0}, "%f", 50, 8},
> + {"0", { .0, .0, .0}, "%g", 50, 1},
> + {"9,900000", { 9.9, 9.9, 9.9}, "%f", 50, 8},
> + {"9,1", { 9.123456, 9.123456, 9.123456}, "%.5f", 4, 7},
> + {"9,91235", { 9.9123456, 9.91234567812345678, 9.91234567812345678}, "%g", 50, 7},
> + {"79,8765", { 79.876543, 79.8765432111, 79.8765432111}, "%G", 50, 7},
> + {"79,9", { 79.876543, 79.8765432111, 79.8765432111}, "%.3g", 50, 4},
> + {"1,000000e+38", { 1e+38, 1e+38, 1e+38}, "%e", 50, 12},
> + {"1,000000e+38", { 1e38, 1e38, 1e38}, "%e", 50, 12},
> + {"-1,000000e-37", { -1e-37, -1e-37, -1e-37}, "%e", 50, 13},
> + {"1,000000e-37", { 0.00000001e-29, 0.00000001e-29, 0.00000001e-29}, "%e", 50, 12},
> + {"1,000000e-37", { 1.000000e-37, 1.000000e-37, 1.000000e-37}, "%e", 50, 12},
> + {"5,900000e-16", { 5.9e-16, 5.9e-16, 5.9e-16}, "%e", 50, 12},
> + {"1,234500e+20", { 12.345e19, 12.345e19, 12.345e19}, "%e", 50, 12},
> + {"1,000000e+05", { 1e5, 1e5, 1e5}, "%e", 50, 12},
> + {"inf", { HUGE_VAL, HUGE_VAL, HUGE_VAL}, "%f", 50, 3},
> + {"-inf", { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL}, "%g", 50, 4},
> + {"nan", { __builtin_nanf (""), __builtin_nan (""), __builtin_nanl ("")}, "%f", 50, 3},
> + {"NAN", { __builtin_nansf (""), __builtin_nans (""), __builtin_nansl ("")}, "%G", 50, 3},
> + {"-NAN", { -NAN, -NAN, -NAN},"%G", 50, 4},
> + {"-INF", { -FLT_MAX * FLT_MAX, -DBL_MAX * DBL_MAX, -DBL_MAX * DBL_MAX}, "%G", 50, 4},
> + {NULL, {0}, "%e", 50, 0},
> +};
> +
> +#define TEST_STRFROM(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
> +static int \
> +test_ ## FSUF (void) \
> +{ \
> + char buf[50], sbuf[5]; \
> + const struct ltest *lt; \
> + const struct htest *ht; \
> + const struct stest *st; \
> + int status = 0; \
> + int rc = 0, rc1 = 0; \
> + for (st = stests; st->expect != NULL; ++st) \
> + { \
> + rc = FTOSTR (sbuf, st->size, st->fmt, st->val); \
> + rc1 = (strcmp (sbuf, st->expect) != 0) || (rc != st->rc); \
> + if (rc1) \
> + { \
> + printf (#FTOSTR ": got %s (%d), expected %s (%d)\n", \
> + sbuf, rc, st->expect, st->rc); \
> + status++; \
> + } \
> + } \
> + for (lt = ltests; lt->expect != NULL; ++lt) \
> + { \
> + rc = FTOSTR (buf, lt->size, lt->fmt, lt->values.FSUF); \
> + rc1 = (strcmp (buf, lt->expect) != 0) || (rc != lt->rc); \
> + if (rc1) \
> + { \
> + printf (#FTOSTR ": got %s (%d), expected %s (%d)\n", \
> + buf, rc, lt->expect, lt->rc); \
> + status++; \
> + } \
> + } \
> + for (ht = htests; ht->val != 0; ++ht) \
> + { \
> + rc = FTOSTR (buf, 50, ht->fmt, ht->val); \
> + if (strcmp (buf, ht->exp[0]) == 0 || \
> + strcmp (buf, ht->exp[1]) == 0 || \
> + strcmp (buf, ht->exp[2]) == 0 || \
> + strcmp (buf, ht->exp[3]) == 0) \
> + continue; \
> + else \
> + { \
> + printf (#FTOSTR ": got %s (%d), expected %s or %s or %s " \
> + "or %s\n", buf, rc, ht->exp[0], ht->exp[1], \
> + ht->exp[2], ht->exp[3]); \
> + status++; \
> + } \
> + } \
> + return status; \
> +}
> +
> +GEN_TEST_STRTOD_FOREACH (TEST_STRFROM)
> +
> +static int
> +test_locale (const char *locale)
> +{
> + printf ("Testing in locale: %s\n", locale);
> + if (setlocale (LC_ALL, locale) == NULL)
> + {
> + printf ("Cannot set locale %s\n", locale);
> + }
> + return STRTOD_TEST_FOREACH (test_);
> +}
> +static int
> +do_test (void)
> +{
> + int result = 0;
> + result += test_locale ("de_DE.UTF-8");
> + result += test_locale ("tr_TR.ISO-8859-9");
> + result += test_locale ("tr_TR.UTF-8");
> + return result;
> +}
> +
> +#define TEST_FUNCTION do_test ()
> +#include "../test-skeleton.c"
> diff --git a/stdlib/tst-strfrom.c b/stdlib/tst-strfrom.c
> new file mode 100644
> index 0000000..bcb6f29
> --- /dev/null
> +++ b/stdlib/tst-strfrom.c
> @@ -0,0 +1,178 @@
> +/* Tests for strfromf, strfromd, strfroml functions.
> + Copyright (C) 2016 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <http://www.gnu.org/licenses/>. */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <float.h>
> +#include <math.h>
> +#include <locale.h>
> +
> +#include "tst-strtod.h"
> +
> +/* Hexadecimal tests. */
> +struct htest
> +{
> + double val;
> + const char *fmt;
> + const char *exp[4];
> +};
> +static const struct htest htests[] = {
> + {0x1.ffp+6, "%a", { "0x1.ffp+6", "0x3.fep+5", "0x7.fcp+4", "0xf.f8p+3" } },
> + {0x1.88p+4, "%a", { "0x1.88p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" } },
> + {-0x1.88p+5, "%A", { "-0X1.88P+5", "-0X3.1P+4", "-0X6.2P+3", "-0XC.4P+2" } },
> + {0x1.44p+10, "%a", { "0x1.44p+10", "0x2.88p+9", "0x5.1p+8", "0xa.2p+7"} },
> + {0x0.0040p+0, "%a", { "0x1p-10", "0x2p-11", "0x4p-12", "0x8p-13"} },
> + {10.0, "%a", { "0x1.4p+3", "0x2.8p+2", "0x5p+1", "0xap+0"} },
> + {0, NULL, {NULL}},
> +};
> +
> +/* Tests with buffer size small. */
> +struct stest
> +{
> + const char *expect;
> + double val;
> + const char *fmt;
> + int size;
> + int rc;
> +};
> +static const struct stest stests[] = {
> + {"1234", 12345.345, "%g", 5, 7},
> + {"0.12", .125, "%f", 5, 8},
> + {"9.99", 9.999, "%.3f", 5, 5},
> + {"100", 1e2, "%g", 5, 3},
> + {NULL, 0, "%f", 50, 0},
> +};
> +
> +struct val
> +{
> + float f;
> + double d;
> + long double ld;
> +};
> +struct ltest
> +{
> + const char *expect;
> + struct val values;
> + const char *fmt;
> + int size;
> + int rc;
> +};
> +static const struct ltest ltests[] = {
> + {"12.345000", { 12.345, 12.345, 12.345}, "%f", 50, 9},
> + {"9.999", { 9.999, 9.999, 9.999}, "%.3f", 50, 5},
> + {"0.125000", { .125, .125, .125}, "%f", 50, 8},
> + {"0.000000", { .0, .0, .0}, "%f", 50, 8},
> + {"0", { .0, .0, .0}, "%g", 50, 1},
> + {"9.900000", { 9.9, 9.9, 9.9}, "%f", 50, 8},
> + {"9.1", { 9.123456, 9.123456, 9.123456}, "%.5f", 4, 7},
> + {"9.91235", { 9.9123456, 9.91234567812345678, 9.91234567812345678}, "%g", 50, 7},
> + {"79.8765", { 79.876543, 79.8765432111, 79.8765432111}, "%G", 50, 7},
> + {"79.9", { 79.876543, 79.8765432111, 79.8765432111}, "%.3g", 50, 4},
> + {"1.000000e+38", { 1e+38, 1e+38, 1e+38}, "%e", 50, 12},
> + {"1.000000e+38", { 1e38, 1e38, 1e38}, "%e", 50, 12},
> + {"-1.000000e-37", { -1e-37, -1e-37, -1e-37}, "%e", 50, 13},
> + {"1.000000e-37", { 0.00000001e-29, 0.00000001e-29, 0.00000001e-29}, "%e", 50, 12},
> + {"1.000000e-37", { 1.000000e-37, 1.000000e-37, 1.000000e-37}, "%e", 50, 12},
> + {"5.900000e-16", { 5.9e-16, 5.9e-16, 5.9e-16}, "%e", 50, 12},
> + {"1.234500e+20", { 12.345e19, 12.345e19, 12.345e19}, "%e", 50, 12},
> + {"1.000000e+05", { 1e5, 1e5, 1e5}, "%e", 50, 12},
> + {"inf", { HUGE_VAL, HUGE_VAL, HUGE_VAL}, "%f", 50, 3},
> + {"-inf", { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL}, "%g", 50, 4},
> + {"nan", { __builtin_nanf (""), __builtin_nan (""), __builtin_nanl ("")}, "%f", 50, 3},
> + {"NAN", { __builtin_nansf (""), __builtin_nans (""), __builtin_nansl ("")}, "%G", 50, 3},
> + {"-NAN", { -NAN, -NAN, -NAN},"%G", 50, 4},
> + {"-INF", { -FLT_MAX * FLT_MAX, -DBL_MAX * DBL_MAX, -DBL_MAX * DBL_MAX}, "%G", 50, 4},
> + {NULL, {0}, "%e", 50, 0},
> +};
> +
> +#define TEST_STRFROM(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
> +static int \
> +test_ ## FSUF (void) \
> +{ \
> + char buf[50], sbuf[5]; \
> + const struct ltest *lt; \
> + const struct htest *ht; \
> + const struct stest *st; \
> + int status = 0; \
> + int rc = 0, rc1 = 0; \
> + for (st = stests; st->expect != NULL; ++st) \
> + { \
> + rc = FTOSTR (sbuf, st->size, st->fmt, st->val); \
> + rc1 = (strcmp (sbuf, st->expect) != 0) || (rc != st->rc); \
> + if (rc1) \
> + { \
> + printf (#FTOSTR ": got %s (%d), expected %s (%d)\n", \
> + sbuf, rc, st->expect, st->rc); \
> + status++; \
> + } \
> + } \
> + for (lt = ltests; lt->expect != NULL; ++lt) \
> + { \
> + rc = FTOSTR (buf, lt->size, lt->fmt, lt->values.FSUF); \
> + rc1 = (strcmp (buf, lt->expect) != 0) || (rc != lt->rc); \
> + if (rc1) \
> + { \
> + printf (#FTOSTR ": got %s (%d), expected %s (%d)\n", \
> + buf, rc, lt->expect, lt->rc); \
> + status++; \
> + } \
> + } \
> + for (ht = htests; ht->val != 0; ++ht) \
> + { \
> + rc = FTOSTR (buf, 50, ht->fmt, ht->val); \
> + if (strcmp (buf, ht->exp[0]) == 0 || \
> + strcmp (buf, ht->exp[1]) == 0 || \
> + strcmp (buf, ht->exp[2]) == 0 || \
> + strcmp (buf, ht->exp[3]) == 0) \
> + continue; \
> + else \
> + { \
> + printf (#FTOSTR ": got %s (%d), expected %s or %s or %s " \
> + "or %s\n", buf, rc, ht->exp[0], ht->exp[1], \
> + ht->exp[2], ht->exp[3]); \
> + status++; \
> + } \
> + } \
> + return status; \
> +}
> +
> +GEN_TEST_STRTOD_FOREACH (TEST_STRFROM)
> +
> +static int
> +test_locale (const char *locale)
> +{
> + printf ("Testing in locale: %s\n", locale);
> + if (setlocale (LC_ALL, locale) == NULL)
> + {
> + printf ("Cannot set locale %s\n", locale);
> + }
> + return STRTOD_TEST_FOREACH (test_);
> +}
> +static int
> +do_test (void)
> +{
> + int result = 0;
> + result += test_locale ("C");
> + result += test_locale ("en_US.ISO-8859-1");
> + result += test_locale ("en_US.UTF-8");
> + return result;
> +}
> +
> +#define TEST_FUNCTION do_test ()
> +#include "../test-skeleton.c"