1 /* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ecma-helpers.h" 17 #include "lit-strings.h" 18 #include "ecma-init-finalize.h" 19 #include "lit-char-helpers.h" 20 #include "js-parser-internal.h" 21 22 #include "test-common.h" 23 24 static lit_code_point_t lexer_hex_to_character(const uint8_t * source_p)25lexer_hex_to_character (const uint8_t *source_p) /**< current source position */ 26 { 27 lit_code_point_t result = 0; 28 29 do 30 { 31 uint32_t byte = *source_p++; 32 33 result <<= 4; 34 35 if (byte >= LIT_CHAR_0 && byte <= LIT_CHAR_9) 36 { 37 result += byte - LIT_CHAR_0; 38 } 39 else 40 { 41 byte = LEXER_TO_ASCII_LOWERCASE (byte); 42 if (byte >= LIT_CHAR_LOWERCASE_A && byte <= LIT_CHAR_LOWERCASE_F) 43 { 44 result += byte - (LIT_CHAR_LOWERCASE_A - 10); 45 } 46 else 47 { 48 return UINT32_MAX; 49 } 50 } 51 } 52 while (*source_p); 53 54 return result; 55 } /* lexer_hex_to_character */ 56 57 int main(void)58main (void) 59 { 60 TEST_INIT (); 61 62 jmem_init (); 63 ecma_init (); 64 65 const uint8_t _1_byte_long1[] = "007F"; 66 const uint8_t _1_byte_long2[] = "0000"; 67 const uint8_t _1_byte_long3[] = "0065"; 68 69 const uint8_t _2_byte_long1[] = "008F"; 70 const uint8_t _2_byte_long2[] = "00FF"; 71 const uint8_t _2_byte_long3[] = "07FF"; 72 73 const uint8_t _3_byte_long1[] = "08FF"; 74 const uint8_t _3_byte_long2[] = "0FFF"; 75 const uint8_t _3_byte_long3[] = "FFFF"; 76 77 const uint8_t _6_byte_long1[] = "10000"; 78 const uint8_t _6_byte_long2[] = "10FFFF"; 79 80 size_t length; 81 82 /* Test 1-byte-long unicode sequences. */ 83 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long1)); 84 TEST_ASSERT (length == 1); 85 86 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long2)); 87 TEST_ASSERT (length == 1); 88 89 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long3)); 90 TEST_ASSERT (length == 1); 91 92 /* Test 2-byte-long unicode sequences. */ 93 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long1)); 94 TEST_ASSERT (length == 2); 95 96 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long2)); 97 TEST_ASSERT (length == 2); 98 99 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long3)); 100 TEST_ASSERT (length == 2); 101 102 /* Test 3-byte-long unicode sequences. */ 103 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long1)); 104 TEST_ASSERT (length == 3); 105 106 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long2)); 107 TEST_ASSERT (length == 3); 108 109 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long3)); 110 TEST_ASSERT (length == 3); 111 112 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long1)); 113 TEST_ASSERT (length == 6); 114 115 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long2)); 116 TEST_ASSERT (length == 6); 117 118 ecma_finalize (); 119 jmem_finalize (); 120 121 return 0; 122 } /* main */ 123