1 /*
2  * Copyright (C) 2017-2021 Free Software Foundation, Inc.
3  *
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.
8  *
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.
13  *
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/>.  */
16 
17 #include <config.h>
18 
19 #if HAVE_MONETARY_H
20 # include <monetary.h>
21 #endif
22 
23 #include "signature.h"
24 #if HAVE_STRFMON_L
25 SIGNATURE_CHECK (strfmon_l, ssize_t, (char *s, size_t maxsize, locale_t locale,
26                                       const char *format, ...));
27 #endif
28 
29 #include <locale.h>
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include "macros.h"
34 
35 int
main(void)36 main (void)
37 {
38 #if HAVE_STRFMON_L
39   /* Simple test in the C locale.  */
40   {
41     char buf[80];
42     locale_t loc;
43     ssize_t ret;
44 
45     loc = newlocale (LC_ALL_MASK, "C", NULL);
46     ASSERT (loc != NULL);
47     ret = strfmon_l (buf, sizeof (buf), loc, "%^#5.0n", 123.4);
48     ASSERT (   (ret == 5 && strcmp (buf,  "  123") == 0) /* AIX, Solaris */
49             || (ret == 6 && strcmp (buf, "   123") == 0) /* glibc */
50             || (ret == 7 && strcmp (buf, "   123 ") == 0) /* Mac OS X */
51            );
52   }
53 
54   /* Test whether the decimal point comes from the right locale:
55      glibc bug <https://sourceware.org/bugzilla/show_bug.cgi?id=19633>.  */
56   if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
57     {
58       fprintf (stderr, "Skipping test: English Unicode locale is not installed\n");
59       return 77;
60     }
61   if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
62     {
63       fprintf (stderr, "Skipping test: English Unicode locale is not installed\n");
64       return 77;
65     }
66   {
67     char expected_buf[80];
68     locale_t loc;
69     char buf[80];
70 
71     setlocale (LC_ALL, "en_US.UTF-8");
72     ASSERT (strfmon (expected_buf, sizeof (expected_buf), "%.2n", 123.5) >= 0);
73     setlocale (LC_ALL, "de_DE.UTF-8");
74     loc = newlocale (LC_ALL_MASK, "en_US.UTF-8", NULL);
75     ASSERT (strfmon_l (buf, sizeof (buf), loc, "%.2n", 123.5) >= 0);
76     ASSERT (strcmp (buf, expected_buf) == 0);
77     freelocale (loc);
78   }
79   {
80     char expected_buf[80];
81     locale_t loc;
82     char buf[80];
83 
84     setlocale (LC_ALL, "de_DE.UTF-8");
85     ASSERT (strfmon (expected_buf, sizeof (expected_buf), "%.2n", 123.5) >= 0);
86     setlocale (LC_ALL, "en_US.UTF-8");
87     loc = newlocale (LC_ALL_MASK, "de_DE.UTF-8", NULL);
88     ASSERT (strfmon_l (buf, sizeof (buf), loc, "%.2n", 123.5) >= 0);
89     ASSERT (strcmp (buf, expected_buf) == 0);
90     freelocale (loc);
91   }
92 #endif
93 
94   return 0;
95 }
96