summaryrefslogtreecommitdiff
path: root/tests/test-nl_langinfo2.c
blob: 7f276da83d148e2c7669f8f19a8cddc6312f496a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Test of nl_langinfo replacement.
   Copyright (C) 2023 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2023.  */

#include <config.h>

#include <langinfo.h>

#include <locale.h>
#include <stdlib.h>
#include <string.h>

#include "c-strcase.h"
#include "c-strcasestr.h"
#include "macros.h"

int
main (int argc, char *argv[])
{
#if HAVE_WORKING_USELOCALE
  /* Check that nl_langinfo() uses the per-thread locale.  */
  int pass;
  bool skipped_all = true;

  /* Extract a few items from the C locale.  */
  /* nl_langinfo items of the LC_CTYPE category */
  const char *c_CODESET = nl_langinfo (CODESET);
  /* nl_langinfo items of the LC_NUMERIC category */
  const char *c_RADIXCHAR = nl_langinfo (RADIXCHAR);
  /* nl_langinfo items of the LC_TIME category */
  const char *c_T_FMT_AMPM = nl_langinfo (T_FMT_AMPM);
  const char *c_AM_STR = nl_langinfo (AM_STR);
  const char *c_PM_STR = nl_langinfo (PM_STR);
  /* nl_langinfo items of the LC_MONETARY category */
  const char *c_CRNCYSTR = nl_langinfo (CRNCYSTR);
  /* nl_langinfo items of the LC_MESSAGES category */
  const char *c_YESEXPR = nl_langinfo (YESEXPR);

  /* Sanity checks.  */
  (void) c_CODESET;
  ASSERT (strcmp (c_RADIXCHAR, ".") == 0);
  ASSERT (strlen (c_T_FMT_AMPM) > 0);
  ASSERT (strlen (c_AM_STR) > 0);
  ASSERT (strlen (c_PM_STR) > 0);
  ASSERT (strlen (c_CRNCYSTR) <= 1); /* "-", "+", ".", or "" */
  ASSERT (c_strcasestr (c_YESEXPR, "y" /* from "yes" */) != NULL);

  for (pass = 1; pass <= 2; pass++)
    {
      /* pass    locale
          1        traditional French locale
          2        French UTF-8 locale
       */
      const char *fr_locale_name =
        getenv (pass == 1 ? "LOCALE_FR" : "LOCALE_FR_UTF8");
      if (strcmp (fr_locale_name, "none") != 0)
        {
          /* Use a per-thread locale.  */
          locale_t fr_locale = newlocale (LC_ALL_MASK, fr_locale_name, NULL);
          if (fr_locale != NULL)
            {
              uselocale (fr_locale);

              /* Extract a few items from the current locale, and check the
                 values.  */

              /* nl_langinfo items of the LC_CTYPE category */
              const char *fr_CODESET = nl_langinfo (CODESET);
              if (pass == 1)
                ASSERT (strstr (fr_CODESET, "8859") != NULL);
              else if (pass == 2)
                ASSERT (c_strcasecmp (fr_CODESET, "UTF-8") == 0
                        || c_strcasecmp (fr_CODESET, "UTF8") == 0);

              /* nl_langinfo items of the LC_NUMERIC category */
              const char *fr_RADIXCHAR = nl_langinfo (RADIXCHAR);
              ASSERT (strcmp (fr_RADIXCHAR, ",") == 0);

              /* nl_langinfo items of the LC_TIME category */
              /* macOS and Solaris 11 don't get the LC_TIME values right.
                 Poor.  */
              #if !((defined __APPLE__ && defined __MACH__) || defined __sun)
              const char *fr_T_FMT_AMPM = nl_langinfo (T_FMT_AMPM);
              const char *fr_AM_STR = nl_langinfo (AM_STR);
              const char *fr_PM_STR = nl_langinfo (PM_STR);
              ASSERT (strlen (fr_T_FMT_AMPM) == 0
                      || strcmp (fr_T_FMT_AMPM, "%I:%M:%S") == 0);
              ASSERT (strlen (fr_AM_STR) == 0);
              ASSERT (strlen (fr_PM_STR) == 0);
              #endif

              /* nl_langinfo items of the LC_MONETARY category */
              /* macOS doesn't get the EUR currency symbol or abbreviation
                 right.  Very poor.  */
              #if !(defined __APPLE__ && defined __MACH__)
              const char *fr_CRNCYSTR = nl_langinfo (CRNCYSTR);
              if (pass == 2)
                ASSERT (strlen (fr_CRNCYSTR) >= 1
                        && strcmp (fr_CRNCYSTR + 1, "€") == 0);
              #endif

              /* nl_langinfo items of the LC_MESSAGES category */
              const char *fr_YESEXPR = nl_langinfo (YESEXPR);
              ASSERT (c_strcasestr (fr_YESEXPR, "o" /* from "oui" */) != NULL);

              skipped_all = false;
            }
        }
    }

  if (skipped_all)
    {
      fputs ("Skipping test: French locale is not installed\n", stderr);
      return 77;
    }

  return 0;
#else
  fputs ("Skipping test: uselocale() not available\n", stderr);
  return 77;
#endif
}