openat: don’t close (-1)
[gnulib.git] / tests / test-c-strtof-mt.c
blob5f18c917d2e4a5a5b9f12453aa49c1c5b44ecb2d
1 /* Multithread-safety test for c_strtof().
2 Copyright (C) 2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
19 #include <config.h>
21 /* Work around GCC bug 44511. */
22 #if _GL_GNUC_PREREQ (4, 3)
23 # pragma GCC diagnostic ignored "-Wreturn-type"
24 #endif
26 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
28 /* Specification. */
29 #include "c-strtod.h"
31 #include <errno.h>
32 #include <locale.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <time.h>
37 #include "glthread/thread.h"
39 static void *
40 thread1_func (void *arg)
42 const char input[] = "1,5";
44 for (;;)
46 char *ptr;
47 float result;
48 errno = 0;
49 result = c_strtof (input, &ptr);
50 if (!(result == 1.0f && ptr == input + 1 && errno == 0))
52 fprintf (stderr, "thread1 malfunction!\n"); fflush (stderr);
53 abort ();
57 /*NOTREACHED*/
60 static void *
61 thread2_func (void *arg)
63 const char input[] = "1,5";
65 for (;;)
67 char *ptr;
68 float result;
69 errno = 0;
70 result = strtof (input, &ptr);
71 if (!(result == 1.5f && ptr == input + 3 && errno == 0))
73 fprintf (stderr, "thread2 disturbed by thread1!\n"); fflush (stderr);
74 abort ();
78 /*NOTREACHED*/
81 static void *
82 thread3_func (void *arg)
84 for (;;)
86 char pointbuf[5];
87 sprintf (pointbuf, "%#.0f", 1.0);
88 if (!(pointbuf[1] == ','))
90 fprintf (stderr, "thread3 disturbed by thread1!\n"); fflush (stderr);
91 abort ();
95 /*NOTREACHED*/
98 int
99 main (int argc, char *argv[])
101 /* Try to set the locale by implicitly looking at the LC_ALL environment
102 variable.
103 configure should already have checked that the locale is supported. */
104 if (setlocale (LC_ALL, "") == NULL)
105 return 1;
107 /* Create the threads. */
108 gl_thread_create (thread1_func, NULL);
109 gl_thread_create (thread2_func, NULL);
110 gl_thread_create (thread3_func, NULL);
112 /* Let them run for 1 second. */
114 struct timespec duration;
115 duration.tv_sec = (argc > 1 ? atoi (argv[1]) : 1);
116 duration.tv_nsec = 0;
118 nanosleep (&duration, NULL);
121 return 0;
124 #else
126 /* No multithreading available. */
128 #include <stdio.h>
131 main ()
133 fputs ("Skipping test: multithreading not enabled\n", stderr);
134 return 77;
137 #endif