aboutsummaryrefslogtreecommitdiff
path: root/tests/unit-core/test-lit-char-helpers.c
blob: 16009dacf9b639bec6ea4bccecede16de59894d1 (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
/* Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecma-helpers.h"
#include "ecma-init-finalize.h"

#include "js-parser-internal.h"
#include "lit-char-helpers.h"
#include "lit-strings.h"
#include "test-common.h"

static lit_code_point_t
lexer_hex_to_character (const uint8_t *source_p) /**< current source position */
{
  lit_code_point_t result = 0;

  do
  {
    uint32_t byte = *source_p++;

    result <<= 4;

    if (byte >= LIT_CHAR_0 && byte <= LIT_CHAR_9)
    {
      result += byte - LIT_CHAR_0;
    }
    else
    {
      byte = LEXER_TO_ASCII_LOWERCASE (byte);
      if (byte >= LIT_CHAR_LOWERCASE_A && byte <= LIT_CHAR_LOWERCASE_F)
      {
        result += byte - (LIT_CHAR_LOWERCASE_A - 10);
      }
      else
      {
        return UINT32_MAX;
      }
    }
  } while (*source_p);

  return result;
} /* lexer_hex_to_character */

int
main (void)
{
  TEST_INIT ();

  jmem_init ();
  ecma_init ();

  const uint8_t _1_byte_long1[] = "007F";
  const uint8_t _1_byte_long2[] = "0000";
  const uint8_t _1_byte_long3[] = "0065";

  const uint8_t _2_byte_long1[] = "008F";
  const uint8_t _2_byte_long2[] = "00FF";
  const uint8_t _2_byte_long3[] = "07FF";

  const uint8_t _3_byte_long1[] = "08FF";
  const uint8_t _3_byte_long2[] = "0FFF";
  const uint8_t _3_byte_long3[] = "FFFF";

  const uint8_t _6_byte_long1[] = "10000";
  const uint8_t _6_byte_long2[] = "10FFFF";

  size_t length;

  /* Test 1-byte-long unicode sequences. */
  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long1));
  TEST_ASSERT (length == 1);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long2));
  TEST_ASSERT (length == 1);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long3));
  TEST_ASSERT (length == 1);

  /* Test 2-byte-long unicode sequences. */
  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long1));
  TEST_ASSERT (length == 2);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long2));
  TEST_ASSERT (length == 2);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long3));
  TEST_ASSERT (length == 2);

  /* Test 3-byte-long unicode sequences. */
  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long1));
  TEST_ASSERT (length == 3);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long2));
  TEST_ASSERT (length == 3);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long3));
  TEST_ASSERT (length == 3);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long1));
  TEST_ASSERT (length == 6);

  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long2));
  TEST_ASSERT (length == 6);

  ecma_finalize ();
  jmem_finalize ();

  return 0;
} /* main */