This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v2 3/3] Add tests for strfrom functions
- From: "Gabriel F. T. Gomes" <gftg at linux dot vnet dot ibm dot com>
- To: libc-alpha at sourceware dot org
- Date: Tue, 30 Aug 2016 12:07:44 -0300
- Subject: [PATCH v2 3/3] Add tests for strfrom functions
- Authentication-results: sourceware.org; auth=none
- References: <1472569664-12402-1-git-send-email-gftg@linux.vnet.ibm.com>
From: Rajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
2016-08-29 Rajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
* stdlib/Makefile (tests): Add tst-strfrom.
* stdlib/tst-strfrom.c: New file.
---
stdlib/Makefile | 3 +-
stdlib/tst-strfrom.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 153 insertions(+), 1 deletion(-)
create mode 100644 stdlib/tst-strfrom.c
diff --git a/stdlib/Makefile b/stdlib/Makefile
index 687e40b..ea1a4bf 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -78,7 +78,7 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \
tst-tininess tst-strtod-underflow tst-tls-atexit \
tst-setcontext3 tst-tls-atexit-nodelete \
tst-strtol-locale tst-strtod-nan-locale tst-strfmon_l \
- tst-quick_exit tst-thread-quick_exit
+ tst-quick_exit tst-thread-quick_exit tst-strfrom
tests-static := tst-secure-getenv
ifeq ($(have-cxx-thread_local),yes)
CFLAGS-tst-quick_exit.o = -std=c++11
@@ -157,6 +157,7 @@ $(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)
endif
# Testdir has to be named stdlib and needs to be writable
diff --git a/stdlib/tst-strfrom.c b/stdlib/tst-strfrom.c
new file mode 100644
index 0000000..719dceb
--- /dev/null
+++ b/stdlib/tst-strfrom.c
@@ -0,0 +1,151 @@
+/* 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"
+
+struct ltest
+{
+ const char *expect;
+ double val;
+ const char *fmt;
+ int size;
+ int rc;
+};
+static const struct ltest ltests[] = {
+ {"12345.3", 12345.345, "%g", 50, 7},
+ {"1.000001", 0x80000Ap-23, "%f", 50, 8},
+ {"inf", HUGE_VAL, "%f", 50, 3},
+ {"-inf", -HUGE_VAL, "%g", 50, 4},
+ {"12.345", 12.345, "%g", 50, 6},
+ {"1.2345e+20", 12.345e19, "%g", 50, 10},
+ {"-1e+08", -.1e+9, "%g", 50, 6},
+ {"0.125000", .125, "%f", 50, 8},
+ {"100000", 1e5, "%g", 50, 6},
+ {"9.999", 9.999, "%.3f", 50, 5},
+ {"0.000000", 0.0, "%f", 50, 8},
+ {"0", 0.0, "%g", 50, 1},
+ {"8.000000", 0x10p-1, "%f", 50, 8},
+ {"8", 0x10p-1, "%g", 50, 1},
+ {"INF", DBL_MAX * DBL_MAX, "%F", 50, 3},
+ {"-INF", -DBL_MAX * DBL_MAX, "%G", 50, 4},
+ {"9.900000", 9.9, "%f", 50, 8},
+ {"9.900", 9.9, "%.3f", 50, 5},
+ {"1", 10.000e-1, "%.f", 50, 1},
+ {"2", 200.000e-2, "%g", 50, 1},
+ {"9.912346", 9.91234567812345678, "%f", 50, 8},
+ {"7.987654e+01", 79.87654321, "%e", 50, 12},
+ {"7.987654E+01", 79.87654321, "%E", 50, 12},
+ {"7.988e+01", 79.87654321, "%.3e", 50, 9},
+ {"7.987654e+01", 79.87654321, "%.6e", 50, 12},
+ {"79.8765", 79.87654321, "%g", 50, 7},
+ {"79.8765", 79.87654321, "%G", 50, 7},
+ {"5.070602e+30", 0x1p102, "%e", 50, 12},
+ {"1.038459e+34", 0x1000p101, "%e", 50, 12},
+ {"2.097152e+06", 0x2p020, "%e", 50, 12},
+ {"1.000000e+38", 1e+38, "%e", 50, 12},
+ {"1.000000e+38", 1e38, "%e", 50, 12},
+ {"1.000000e-37", 1e-37, "%e", 50, 12},
+ {"1.000000e-37", 0.00000001e-29, "%e", 50, 12},
+ {"1.000000e-37", 1.000000e-37, "%e", 50, 12},
+ {"3.761582e-37", 0x1p-121, "%e", 50, 12},
+ {"3.761582e-37", 0x0.0001p-105, "%e", 50, 12},
+ {"5.900000e-16", 5.9e-16, "%e", 50, 12},
+ {"1.000001", 0x80000Ap-23, "%f", 50, 8},
+ {"inf", HUGE_VAL, "%f", 50, 3},
+ {"-INF", -HUGE_VAL, "%G", 50, 4},
+ {"1.2345e+20", 12.345e19, "%g", 50, 10},
+ {"0.125000", .125, "%f", 50, 8},
+ {"100000", 1e5, "%g", 50, 6},
+ {"0x1.8p-11", 0x0.0030p+0, "%a", 50, 9},
+ {"0x1p-10", 0x0.0040p+0, "%a", 50, 7},
+ {"0x1.08p+56", 0x1.08p56, "%a", 50, 10},
+ {"0x1p+61", 0x1p61, "%a", 50, 7},
+ {"0x1.0001p+61", 0x1.0001p61, "%a", 50, 12},
+ {"0x1.4p+3", 10.0, "%a", 50, 8},
+ {"0X1.4P+3", 10.0, "%A", 50, 8},
+ {"nan", NAN, "%e", 50, 3},
+ {"-NAN", -NAN, "%G", 50, 4},
+ {"1.79769e+38", 1.79769e+38, "%g", 50, 11},
+ {"nan", __builtin_nan (""), "%f", 50, 3},
+ {"NAN", __builtin_nans (""), "%G", 50, 3},
+ {"9.1", 9.123456, "%.5f", 4, 7},
+ {NULL, 0, "%f", 50, 0},
+};
+
+#define NTESTS (sizeof (tests) / sizeof (tests[0]))
+
+#define TEST_STRFROM(STRTOF, FTYPE, FTOSTR, LSUF, CSUF) \
+static int \
+test_ ## STRTOF (void) \
+{ \
+ char buf[50]; \
+ const struct ltest *lt; \
+ int status = 0; \
+ int rc = 0, rc1 = 0; \
+ for (lt = ltests; lt->expect != NULL; ++lt) \
+ { \
+ rc = FTOSTR (buf, lt->size, lt->fmt, lt->val); \
+ if (strcmp (lt->fmt, "%a") == 0 || strcmp (lt->fmt, "%A") == 0) \
+ rc1 = (strtold (lt->expect, NULL) == strtold (buf, NULL)) \
+ ? 0 : 1; \
+ else \
+ 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++; \
+ } \
+ } \
+ 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_CTYPE, 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 ("de_DE.UTF-8");
+ result += test_locale ("en_US.ISO-8859-1");
+ result += test_locale ("en_US.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"
--
2.4.11