• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ecma-literal-storage.h"
18 #include "test-common.h"
19 
20 /* Iterations count. */
21 #define test_iters 64
22 
23 /* Subiterations count. */
24 #define test_sub_iters 64
25 
26 /* Max characters in a string. */
27 #define max_characters_in_string 256
28 
29 static void
generate_string(lit_utf8_byte_t * str,lit_utf8_size_t len)30 generate_string (lit_utf8_byte_t *str, lit_utf8_size_t len)
31 {
32   static const lit_utf8_byte_t bytes[] = "!@#$%^&*()_+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
33   static const lit_utf8_size_t length = (lit_utf8_size_t) (sizeof (bytes) - 1);
34   for (lit_utf8_size_t i = 0; i < len; ++i)
35   {
36     str[i] = bytes[(unsigned long) rand () % length];
37   }
38 } /* generate_string */
39 
40 static ecma_number_t
generate_number(void)41 generate_number (void)
42 {
43   ecma_number_t num = ((ecma_number_t) rand () / 32767.0);
44   if (rand () % 2)
45   {
46     num = -num;
47   }
48   int power = rand () % 30;
49   while (power-- > 0)
50   {
51     num *= 10;
52   }
53   return num;
54 } /* generate_number */
55 
56 int
main(void)57 main (void)
58 {
59   TEST_INIT ();
60 
61   const lit_utf8_byte_t *ptrs[test_sub_iters];
62   ecma_number_t numbers[test_sub_iters];
63   lit_utf8_byte_t strings[test_sub_iters][max_characters_in_string + 1];
64   lit_utf8_size_t lengths[test_sub_iters];
65 
66   jmem_init ();
67 
68   for (uint32_t i = 0; i < test_iters; i++)
69   {
70     memset (numbers, 0, sizeof (ecma_number_t) * test_sub_iters);
71     memset (lengths, 0, sizeof (lit_utf8_size_t) * test_sub_iters);
72     memset (ptrs, 0, sizeof (lit_utf8_byte_t *) * test_sub_iters);
73 
74     for (uint32_t j = 0; j < test_sub_iters; j++)
75     {
76       int type = rand () % 3;
77       if (type == 0)
78       {
79         lengths[j] = (lit_utf8_size_t) (rand () % max_characters_in_string + 1);
80         generate_string (strings[j], lengths[j]);
81         ecma_find_or_create_literal_string (strings[j], lengths[j]);
82         strings[j][lengths[j]] = '\0';
83         ptrs[j] = strings[j];
84         TEST_ASSERT (ptrs[j]);
85       }
86       else if (type == 1)
87       {
88         lit_magic_string_id_t msi = (lit_magic_string_id_t) (rand () % LIT_NON_INTERNAL_MAGIC_STRING__COUNT);
89         ptrs[j] = lit_get_magic_string_utf8 (msi);
90         TEST_ASSERT (ptrs[j]);
91         lengths[j] = (lit_utf8_size_t) lit_zt_utf8_string_size (ptrs[j]);
92         ecma_find_or_create_literal_string (ptrs[j], lengths[j]);
93       }
94       else
95       {
96         ecma_number_t num = generate_number ();
97         lengths[j] = ecma_number_to_utf8_string (num, strings[j], max_characters_in_string);
98         ecma_find_or_create_literal_number (num);
99       }
100     }
101 
102     /* Add empty string. */
103     ecma_find_or_create_literal_string (NULL, 0);
104 
105     for (uint32_t j = 0; j < test_sub_iters; j++)
106     {
107       ecma_value_t lit1;
108       ecma_value_t lit2;
109       if (ptrs[j])
110       {
111         lit1 = ecma_find_or_create_literal_string (ptrs[j], lengths[j]);
112         lit2 = ecma_find_or_create_literal_string (ptrs[j], lengths[j]);
113         TEST_ASSERT (ecma_is_value_string (lit1));
114         TEST_ASSERT (ecma_is_value_string (lit2));
115         TEST_ASSERT (lit1 == lit2);
116       }
117       else
118       {
119         lit1 = ecma_find_or_create_literal_number (numbers[j]);
120         lit2 = ecma_find_or_create_literal_number (numbers[j]);
121         TEST_ASSERT (ecma_is_value_number (lit1));
122         TEST_ASSERT (ecma_is_value_number (lit2));
123         TEST_ASSERT (lit1 == lit2);
124       }
125     }
126 
127     /* Check empty string exists. */
128     TEST_ASSERT (ecma_find_or_create_literal_string (NULL, 0) != JMEM_CP_NULL);
129   }
130 
131   ecma_finalize_lit_storage ();
132   jmem_finalize ();
133   return 0;
134 } /* main */
135