summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/utf8proc01/init.c
blob: fda1444a372209ed2e69f1ab20ea958387c2655b (plain) (blame)
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
/*
 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#include "tmacros.h"

#include <string.h>
#include <utf8proc/utf8proc.h>

const char rtems_test_name[] = "UTF8PROC 1";

static void
test_utf8proc_errmsg ( void )
{
  int          error;
  size_t       string_length;
  const char*  err_msg;

  for ( error = 0; error >= UTF8PROC_ERROR_INVALIDOPTS - 1; --error ) {
    err_msg = utf8proc_errmsg ( error );
    rtems_test_assert ( err_msg != NULL );

    string_length = strlen (err_msg );
    rtems_test_assert ( string_length > 0 );
  }
}

static void
test_utf8proc_version ( void )
{
  const char*  version;
  size_t       string_length;

  version = utf8proc_version ( );
  rtems_test_assert ( version != NULL );

  string_length = strlen ( version );
  rtems_test_assert ( string_length > 0 );

  rtems_test_assert (0 == strcmp ( version, "1.1.5" ) );
}

static void
test_utf8proc_iterate ( void )
{
  char         utf8_str_simple[]    = "The quick brown.fox";
  uint8_t     *utf8_str_simple_ptr  = (uint8_t*)(&utf8_str_simple[0]);
  size_t       length_simple_string = strlen ( utf8_str_simple );
  int32_t      unicode_char;
  unsigned int index;
  ssize_t      bytes_read;

  for (index = 0; index < length_simple_string; ++index) {
    bytes_read = utf8proc_iterate (
      &utf8_str_simple_ptr[index],
      length_simple_string - index,
      &unicode_char );
    rtems_test_assert ( bytes_read == 1 );
    rtems_test_assert ( (uint8_t)unicode_char == utf8_str_simple_ptr[index]);
  }
}

static void
test_utf8proc_encode_char ( void )
{
  uint8_t utf8_str[4];
  int32_t unicode_char;
  ssize_t bytes_written;

  for ( unicode_char = 0; unicode_char < 128; ++unicode_char ) {
    bytes_written = utf8proc_encode_char ( unicode_char, utf8_str );

    rtems_test_assert ( bytes_written == 1 );
    rtems_test_assert ( utf8_str[0]   == (uint8_t)unicode_char );
  }
}

static void
test_utf8proc_get_property ( void )
{
  int32_t                    unicode_char;
  const utf8proc_property_t* properties;

  for ( unicode_char = 0x0000; unicode_char <= 0x10FFFF; ++unicode_char ) {
    properties = utf8proc_get_property ( unicode_char );
    rtems_test_assert ( NULL != properties );
  }
}

static void
test_utf8proc_decompose_char ( void )
{
  int32_t unicode_char;
  int32_t unicode_char_decomposed[4];
  ssize_t chars_written;

  for ( unicode_char = 0x0000; unicode_char <= 0x10FFFF; ++unicode_char ) {
    chars_written = utf8proc_decompose_char (
      unicode_char,
      unicode_char_decomposed,
      sizeof ( unicode_char_decomposed ) / sizeof ( unicode_char_decomposed[0] ),
      UTF8PROC_STABLE | UTF8PROC_DECOMPOSE,
      0);
    if ( unicode_char < 0x80 ) {
      rtems_test_assert ( chars_written == 1 );
      rtems_test_assert ( unicode_char_decomposed[0] == unicode_char);
    }
    else
     rtems_test_assert ( chars_written > 0 );
  }
}

static void
test_utf8proc_decompose ( void )
{
  char         string_simple[]    = "The quick brown.fox";
  uint8_t     *string_simple_utf8 = (uint8_t*)(&string_simple[0]);
  int32_t      string_decomposed[sizeof ( string_simple ) * 4];
  ssize_t      chars_written;
  unsigned int index;

  memset (&string_decomposed[0], 0, sizeof ( string_decomposed ) );

  chars_written = utf8proc_decompose (
    string_simple_utf8,
    sizeof ( string_simple ),
    &string_decomposed[0],
    sizeof ( string_decomposed ),
    UTF8PROC_NULLTERM | UTF8PROC_STABLE | UTF8PROC_DECOMPOSE );
  rtems_test_assert ( chars_written == strlen ( string_simple ) );
  /* Our source string contains only very simple characters. Thus the above
   * decomposition should result in exactly the same string
   */
  for ( index = 0; index < sizeof ( string_simple ); ++index ) {
    rtems_test_assert ( string_simple_utf8[index] == (uint8_t)string_decomposed[index] );
  }
}

static void
test_utf8proc_reencode ( void )
{
  char         string_simple[]    = "The quick brown.fox";
  uint8_t     *string_simple_utf8 = (uint8_t*)(&string_simple[0]);
  int32_t      string_decomposed[sizeof ( string_simple ) * 4];
  uint8_t     *string_reencoded   = (uint8_t*)(&string_decomposed[0]);
  ssize_t      chars_written;
  unsigned int index;

  memset (&string_decomposed[0], 0, sizeof ( string_decomposed ) );

  chars_written = utf8proc_decompose (
    string_simple_utf8,
    sizeof ( string_simple ),
    &string_decomposed[0],
    sizeof ( string_decomposed ),
    UTF8PROC_NULLTERM | UTF8PROC_STABLE | UTF8PROC_DECOMPOSE );
  rtems_test_assert ( chars_written == strlen ( string_simple ) );

  chars_written = utf8proc_reencode (
    &string_decomposed[0],
    chars_written,
    UTF8PROC_NULLTERM | UTF8PROC_STABLE | UTF8PROC_DECOMPOSE );
  rtems_test_assert ( chars_written == strlen ( string_simple ) );
  /* Our source string contains only very simple characters. Thus the above
   * decomposition should result in exactly the same string
   */
  for ( index = 0; index < sizeof ( string_simple ); ++index ) {
    rtems_test_assert ( string_simple_utf8[index] == string_reencoded[index] );
  }
}

static void
test_utf8proc_map ( void )
{
  char         string_simple[]    = "The quick brown.fox";
  uint8_t     *string_simple_utf8 = (uint8_t*)(&string_simple[0]);
  uint8_t     *dest               = NULL;
  ssize_t      chars_written;
  unsigned int index;

  chars_written = utf8proc_map(
    string_simple_utf8,
    sizeof ( string_simple ),
    &dest,
    UTF8PROC_NULLTERM | UTF8PROC_STABLE | UTF8PROC_DECOMPOSE );
  rtems_test_assert ( chars_written == strlen ( string_simple ) );
  rtems_test_assert ( dest != NULL);

  /* Our source string contains only very simple characters. Thus the above
   * decomposition should result in exactly the same string
   */
  for ( index = 0; index < chars_written; ++index ) {
    rtems_test_assert ( string_simple_utf8[index] == dest[index] );
  }
  free ( dest );
}

typedef uint8_t* (*normalization_method)(const uint8_t* str);

static void
test_utf8proc_normalize ( const normalization_method test_sample )
{
  char         string_simple[]    = "The quick brown.fox";
  uint8_t     *string_simple_utf8 = (uint8_t*)(&string_simple[0]);
  uint8_t     *dest               = NULL;
  unsigned int index;

  dest = test_sample ( string_simple_utf8 );
  rtems_test_assert ( dest != NULL);

  /* Our source string contains only very simple characters. Thus the above
   * decomposition should result in exactly the same string
   */
  for ( index = 0; index < sizeof ( string_simple ); ++index ) {
    rtems_test_assert ( string_simple_utf8[index] == dest[index] );
  }
  free ( dest );
}

static void test ( void )
{
  test_utf8proc_errmsg         ( );
  test_utf8proc_version        ( );
  test_utf8proc_iterate        ( );
  test_utf8proc_encode_char    ( );
  test_utf8proc_get_property   ( );
  test_utf8proc_decompose_char ( );
  test_utf8proc_decompose      ( );
  test_utf8proc_reencode       ( );
  test_utf8proc_map            ( );
  test_utf8proc_normalize      ( utf8proc_NFD  );
  test_utf8proc_normalize      ( utf8proc_NFC  );
  test_utf8proc_normalize      ( utf8proc_NFKD );
  test_utf8proc_normalize      ( utf8proc_NFKC );
}

static void Init ( rtems_task_argument arg )
{
  TEST_BEGIN();

  test ( );

  TEST_END();

  rtems_test_exit ( 0 );
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_MAXIMUM_TASKS 1

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>