summaryrefslogtreecommitdiff
path: root/tests/uninorm/test-u32-normalize-big.c
blob: c1e4f6d5c67a60f339f30c70fe88a1336d3fc4d0 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Test of Unicode compliance of normalization of UTF-32 strings.
   Copyright (C) 2009-2016 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 <http://www.gnu.org/licenses/>.  */

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

#include <config.h>

/* Specification.  */
#include "test-u32-normalize-big.h"

#if GNULIB_TEST_UNINORM_U32_NORMALIZE

#include <stdio.h>
#include <stdlib.h>

#include "xalloc.h"
#include "unistr.h"
#include "macros.h"

#define ASSERT_WITH_LINE(expr, file, line) \
  do                                                                         \
    {                                                                        \
      if (!(expr))                                                           \
        {                                                                    \
          fprintf (stderr, "%s:%d: assertion failed for %s:%u\n",            \
                   __FILE__, __LINE__, file, line);                          \
          fflush (stderr);                                                   \
          abort ();                                                          \
        }                                                                    \
    }                                                                        \
  while (0)

static int
cmp_ucs4_t (const void *a, const void *b)
{
  ucs4_t a_value = *(const ucs4_t *)a;
  ucs4_t b_value = *(const ucs4_t *)b;
  return (a_value < b_value ? -1 : a_value > b_value ? 1 : 0);
}

void
read_normalization_test_file (const char *filename,
                              struct normalization_test_file *file)
{
  FILE *stream;
  unsigned int lineno;
  int part_index;
  struct normalization_test_line *lines;
  size_t lines_length;
  size_t lines_allocated;

  stream = fopen (filename, "r");
  if (stream == NULL)
    {
      fprintf (stderr, "error during fopen of '%s'\n", filename);
      exit (1);
    }

  for (part_index = 0; part_index < 4; part_index++)
    {
      file->parts[part_index].lines = NULL;
      file->parts[part_index].lines_length = 0;
    }

  lineno = 0;

  part_index = -1;
  lines = NULL;
  lines_length = 0;
  lines_allocated = 0;

  for (;;)
    {
      char buf[1000+1];
      char *ptr;
      int c;
      struct normalization_test_line line;
      size_t sequence_index;

      lineno++;

      /* Read a line.  */
      ptr = buf;
      do
        {
          c = getc (stream);
          if (c == EOF || c == '\n')
            break;
          *ptr++ = c;
        }
      while (ptr < buf + 1000);
      *ptr = '\0';
      if (c == EOF)
        break;

      /* Ignore empty lines and comment lines.  */
      if (buf[0] == '\0' || buf[0] == '#')
        continue;

      /* Handle lines that introduce a new part.  */
      if (buf[0] == '@')
        {
          /* Switch to the next part.  */
          if (part_index >= 0)
            {
              lines =
                (struct normalization_test_line *)
                xnrealloc (lines, lines_length, sizeof (struct normalization_test_line));
              file->parts[part_index].lines = lines;
              file->parts[part_index].lines_length = lines_length;
            }
          part_index++;
          lines = NULL;
          lines_length = 0;
          lines_allocated = 0;
          continue;
        }

      /* It's a line containing 5 sequences of Unicode characters.
         Parse it and append it to the current part.  */
      if (!(part_index >= 0 && part_index < 4))
        {
          fprintf (stderr, "unexpected structure of '%s'\n", filename);
          exit (1);
        }
      ptr = buf;
      line.lineno = lineno;
      for (sequence_index = 0; sequence_index < 5; sequence_index++)
        line.sequences[sequence_index] = NULL;
      for (sequence_index = 0; sequence_index < 5; sequence_index++)
        {
          uint32_t *sequence = XNMALLOC (1, uint32_t);
          size_t sequence_length = 0;

          for (;;)
            {
              char *endptr;
              unsigned int uc;

              uc = strtoul (ptr, &endptr, 16);
              if (endptr == ptr)
                break;
              ptr = endptr;

              /* Append uc to the sequence.  */
              sequence =
                (uint32_t *)
                xnrealloc (sequence, sequence_length + 2, sizeof (uint32_t));
              sequence[sequence_length] = uc;
              sequence_length++;

              if (*ptr == ' ')
                ptr++;
            }
          if (sequence_length == 0)
            {
              fprintf (stderr, "empty character sequence in '%s'\n", filename);
              exit (1);
            }
          sequence[sequence_length] = 0; /* terminator */

          line.sequences[sequence_index] = sequence;

          if (*ptr != ';')
            {
              fprintf (stderr, "error parsing '%s'\n", filename);
              exit (1);
            }
          ptr++;
        }

      /* Append the line to the current part.  */
      if (lines_length == lines_allocated)
        {
          lines_allocated = 2 * lines_allocated;
          if (lines_allocated < 7)
            lines_allocated = 7;
          lines =
            (struct normalization_test_line *)
            xnrealloc (lines, lines_allocated, sizeof (struct normalization_test_line));
        }
      lines[lines_length] = line;
      lines_length++;
    }

  if (part_index >= 0)
    {
      lines =
        (struct normalization_test_line *)
        xnrealloc (lines, lines_length, sizeof (struct normalization_test_line));
      file->parts[part_index].lines = lines;
      file->parts[part_index].lines_length = lines_length;
    }

  {
    /* Collect all c1 values from the part 1 in an array.  */
    const struct normalization_test_part *p = &file->parts[1];
    ucs4_t *c1_array = XNMALLOC (p->lines_length + 1, ucs4_t);
    size_t line_index;

    for (line_index = 0; line_index < p->lines_length; line_index++)
      {
        const uint32_t *sequence = p->lines[line_index].sequences[0];
        /* In part 1, every sequences[0] consists of a single character.  */
        if (!(sequence[0] != 0 && sequence[1] == 0))
          abort ();
        c1_array[line_index] = sequence[0];
      }

    /* Sort this array.  */
    qsort (c1_array, p->lines_length, sizeof (ucs4_t), cmp_ucs4_t);

    /* Add the sentinel at the end.  */
    c1_array[p->lines_length] = 0x110000;

    file->part1_c1_sorted = c1_array;
  }

  file->filename = xstrdup (filename);

  if (ferror (stream) || fclose (stream))
    {
      fprintf (stderr, "error reading from '%s'\n", filename);
      exit (1);
    }
}

void
test_specific (const struct normalization_test_file *file,
               int (*check) (const uint32_t *c1, size_t c1_length,
                             const uint32_t *c2, size_t c2_length,
                             const uint32_t *c3, size_t c3_length,
                             const uint32_t *c4, size_t c4_length,
                             const uint32_t *c5, size_t c5_length))
{
  size_t part_index;

  for (part_index = 0; part_index < 4; part_index++)
    {
      const struct normalization_test_part *p = &file->parts[part_index];
      size_t line_index;

      for (line_index = 0; line_index < p->lines_length; line_index++)
        {
          const struct normalization_test_line *l = &p->lines[line_index];

          ASSERT_WITH_LINE (check (l->sequences[0], u32_strlen (l->sequences[0]),
                                   l->sequences[1], u32_strlen (l->sequences[1]),
                                   l->sequences[2], u32_strlen (l->sequences[2]),
                                   l->sequences[3], u32_strlen (l->sequences[3]),
                                   l->sequences[4], u32_strlen (l->sequences[4]))
                            == 0,
                            file->filename, l->lineno);
        }
    }
}

void
test_other (const struct normalization_test_file *file, uninorm_t nf)
{
  /* Check that for every character not listed in part 1 of the
     NormalizationTest.txt file, the character maps to itself in each
     of the four normalization forms.  */
  const ucs4_t *p = file->part1_c1_sorted;
  ucs4_t uc;

  for (uc = 0; uc < 0x110000; uc++)
    {
      if (uc >= 0xD800 && uc < 0xE000)
        {
          /* A surrogate, not a character.  Skip uc.  */
        }
      else if (uc == *p)
        {
          /* Skip uc.  */
          p++;
        }
      else
        {
          uint32_t input[1];
          size_t length;
          uint32_t *result;

          input[0] = uc;
          result = u32_normalize (nf, input, 1, NULL, &length);
          ASSERT (result != NULL && length == 1 && result[0] == uc);
        }
    }
}

#endif