summaryrefslogtreecommitdiff
path: root/gmp/tests/cxx/t-locale.cc
blob: 01eacda9f23067f981f7848eadbded63dd0b6a80 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Test locale support in C++ functions.

Copyright 2001-2003, 2007 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite 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.

The GNU MP Library test suite 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
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include <clocale>
#include <iostream>
#include <cstdlib>

#include "gmp.h"
#include "gmp-impl.h"
#include "tests.h"

using namespace std;


extern "C" {
  char point_string[2];
}

#if HAVE_STD__LOCALE
// Like std::numpunct, but with decimal_point coming from point_string[].
class my_numpunct : public numpunct<char> {
 public:
  explicit my_numpunct (size_t r = 0) : numpunct<char>(r) { }
 protected:
  char do_decimal_point() const { return point_string[0]; }
};
#endif

void
set_point (char c)
{
  point_string[0] = c;

#if HAVE_STD__LOCALE
  locale loc (locale::classic(), new my_numpunct ());
  locale::global (loc);
#endif
}


void
check_input (void)
{
  static const struct {
    const char  *str1;
    const char  *str2;
    double      want;
  } data[] = {

    { "1","",   1.0 },
    { "1","0",  1.0 },
    { "1","00", 1.0 },

    { "","5",    0.5 },
    { "0","5",   0.5 },
    { "00","5",  0.5 },
    { "00","50", 0.5 },

    { "1","5",    1.5 },
    { "1","5e1", 15.0 },
  };

  static char point[] = {
    '.', ',', 'x', '\xFF'
  };

  mpf_t  got;
  mpf_init (got);

  for (size_t i = 0; i < numberof (point); i++)
    {
      set_point (point[i]);

      for (int neg = 0; neg <= 1; neg++)
        {
          for (size_t j = 0; j < numberof (data); j++)
            {
              string str = string(data[j].str1)+point[i]+string(data[j].str2);
              if (neg)
                str = "-" + str;

              istringstream is (str.c_str());

              mpf_set_ui (got, 123);   // dummy initial value

              if (! (is >> got))
                {
                  cout << "istream mpf_t operator>> error\n";
                  cout << "  point " << point[i] << "\n";
                  cout << "  str   \"" << str << "\"\n";
                  cout << "  localeconv point \""
                       << GMP_DECIMAL_POINT << "\"\n";
                  abort ();
                }

              double want = data[j].want;
              if (neg)
                want = -want;
              if (mpf_cmp_d (got, want) != 0)
                {
                  cout << "istream mpf_t operator>> wrong\n";
                  cout << "  point " << point[i] << "\n";
                  cout << "  str   \"" << str << "\"\n";
                  cout << "  got   " << got << "\n";
                  cout << "  want  " << want << "\n";
                  cout << "  localeconv point \""
                       << GMP_DECIMAL_POINT << "\"\n";
                  abort ();
                }
            }
        }
    }

  mpf_clear (got);
}

void
check_output (void)
{
  static char point[] = {
    '.', ',', 'x', '\xFF'
  };

  for (size_t i = 0; i < numberof (point); i++)
    {
      set_point (point[i]);
      ostringstream  got;

      mpf_t  f;
      mpf_init (f);
      mpf_set_d (f, 1.5);
      got << f;
      mpf_clear (f);

      string  want = string("1") + point[i] + string("5");

      if (want.compare (got.str()) != 0)
        {
          cout << "ostream mpf_t operator<< doesn't respect locale\n";
          cout << "  point " << point[i] << "\n";
          cout << "  got   \"" << got.str() << "\"\n";
          cout << "  want  \"" << want      << "\"\n";
          abort ();
        }
    }
}

int
replacement_works (void)
{
  set_point ('x');
  mpf_t  f;
  mpf_init (f);
  mpf_set_d (f, 1.5);
  ostringstream s;
  s << f;
  mpf_clear (f);

  return (s.str().compare("1x5") == 0);
}

int
main (void)
{
  tests_start ();

  if (replacement_works())
    {
      check_input ();
      check_output ();
    }
  else
    {
      cout << "Replacing decimal point didn't work, tests skipped\n";
    }

  tests_end ();
  return 0;
}