aboutsummaryrefslogtreecommitdiffstats
path: root/tools/node_modules/expresso/deps/jscoverage/tests/encodings.c
blob: 26f87072dd067c0a3f6a041cd35f73d4482ecb04 (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
/*
    encodings.c - test handling different character encodings
    Copyright (C) 2008 siliconforks.com

    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 2 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, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <config.h>

#include <assert.h>
#include <string.h>

#include "encoding.h"
#include "stream.h"

int main(void) {
  jschar * characters;
  size_t num_characters; 
  int result;

  /* e, e grave, e acute, e circumflex */
  uint8_t utf8[] = {
    'e',
    0xc3,
    0xa8,
    0xc3,
    0xa9,
    0xc3,
    0xaa,
  };

  result = jscoverage_bytes_to_characters("UTF-8", utf8, 7, &characters, &num_characters);

#if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR
  assert(result == 0);
  assert(num_characters == 4);
  assert(characters[0] == 'e');
  assert(characters[1] == 0xe8);
  assert(characters[2] == 0xe9);
  assert(characters[3] == 0xea);

  free(characters);
#else
  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
#endif

  /*
  BOM is 0xfeff
  = 1111 1110 1111 1111
  UTF: 1110---- 10------ 10------
     = 11101111 10111011 10111111
     = EF BB BF
  */
  uint8_t utf8_with_bom[] = {
    0xef,
    0xbb,
    0xbf,
    'e',
    0xc3,
    0xa8,
    0xc3,
    0xa9,
    0xc3,
    0xaa,
  };

  result = jscoverage_bytes_to_characters("UTF-8", utf8_with_bom, 10, &characters, &num_characters);

#if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR
  assert(result == 0);
  assert(num_characters == 4);
  assert(characters[0] == 'e');
  assert(characters[1] == 0xe8);
  assert(characters[2] == 0xe9);
  assert(characters[3] == 0xea);

  free(characters);
#else
  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
#endif

  uint8_t utf16be[] = {
    0, 'e',
    0, 0xe8,
    0, 0xe9,
    0, 0xea,
  };

  result = jscoverage_bytes_to_characters("UTF-16BE", utf16be, 8, &characters, &num_characters);

#ifdef HAVE_ICONV
  assert(result == 0);
  assert(num_characters == 4);
  assert(characters[0] == 'e');
  assert(characters[1] == 0xe8);
  assert(characters[2] == 0xe9);
  assert(characters[3] == 0xea);

  free(characters);
#else
  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
#endif

  uint8_t utf16be_with_bom[] = {
    0xfe, 0xff,
    0, 'e',
    0, 0xe8,
    0, 0xe9,
    0, 0xea,
  };

  result = jscoverage_bytes_to_characters("UTF-16BE", utf16be_with_bom, 10, &characters, &num_characters);

#ifdef HAVE_ICONV
  assert(result == 0);
  assert(num_characters == 4);
  assert(characters[0] == 'e');
  assert(characters[1] == 0xe8);
  assert(characters[2] == 0xe9);
  assert(characters[3] == 0xea);

  free(characters);
#else
  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
#endif

  uint8_t utf16le[] = {
    'e', 0,
    0xe8, 0,
    0xe9, 0,
    0xea, 0,
  };

  result = jscoverage_bytes_to_characters("UTF-16LE", utf16le, 8, &characters, &num_characters);

#ifdef HAVE_ICONV
  assert(result == 0);
  assert(num_characters == 4);
  assert(characters[0] == 'e');
  assert(characters[1] == 0xe8);
  assert(characters[2] == 0xe9);
  assert(characters[3] == 0xea);

  free(characters);
#else
  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
#endif

  uint8_t utf16le_with_bom[] = {
    0xff, 0xfe,
    'e', 0,
    0xe8, 0,
    0xe9, 0,
    0xea, 0,
  };

  result = jscoverage_bytes_to_characters("UTF-16LE", utf16le_with_bom, 10, &characters, &num_characters);

#ifdef HAVE_ICONV
  assert(result == 0);
  assert(num_characters == 4);
  assert(characters[0] == 'e');
  assert(characters[1] == 0xe8);
  assert(characters[2] == 0xe9);
  assert(characters[3] == 0xea);

  free(characters);
#else
  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
#endif

  /* bogus encoding */
  uint8_t bogus[] = {'b', 'o', 'g', 'u', 's'};

  result = jscoverage_bytes_to_characters("BOGUS", bogus, 5, &characters, &num_characters);

  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);

#ifdef HAVE_ICONV
  /* malformed US-ASCII */
  /* NOTE: Windows simply discards the high bit */
  uint8_t malformed_ascii[] = {
    'e',
    0xe8,
    0xe9,
    0xea,
  };

  result = jscoverage_bytes_to_characters("US-ASCII", malformed_ascii, 4, &characters, &num_characters);

  assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE);
#endif

  /* malformed UTF-8 */
  uint8_t malformed_utf8[] = {
    'e',
    0xe8,
    0xe9,
    0xea,
  };

  result = jscoverage_bytes_to_characters("UTF-8", malformed_utf8, 4, &characters, &num_characters);

#if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR
  assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE);
#else
  assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
#endif

  return 0;
}