• 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-init-finalize.h"
18 #include "lit-char-helpers.h"
19 #include "lit-strings.h"
20 
21 #include "test-common.h"
22 
23 int
main(void)24 main (void)
25 {
26   TEST_INIT ();
27 
28   jmem_init ();
29   ecma_init ();
30 
31   {
32     static const lit_utf8_byte_t string_data[] = "A simple string";
33 
34     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
35     ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1);
36     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
37 
38     ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1);
39     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
40     ecma_deref_ecma_string (result_p);
41     ecma_deref_ecma_string (str_p);
42   }
43 
44   {
45     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
46     ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING);
47     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
48 
49     ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STRING);
50     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
51   }
52 
53   {
54     static const lit_utf8_byte_t string_data[] = "a";
55 
56     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
57     ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_A);
58     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
59 
60     ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1);
61     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
62     ecma_deref_ecma_string (result_p);
63     ecma_deref_ecma_string (str_p);
64   }
65 
66   {
67     static const lit_utf8_byte_t string_data[] = "A simple string";
68     ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1);
69 
70     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
71     ecma_stringbuilder_append (&builder, str_p);
72     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
73 
74     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
75     ecma_deref_ecma_string (result_p);
76     ecma_deref_ecma_string (str_p);
77   }
78 
79   {
80     ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
81 
82     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
83     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
84 
85     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
86   }
87 
88   {
89     static const lit_utf8_byte_t string_data[] = "abc";
90 
91     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
92     ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_A);
93     ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_B);
94     ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_C);
95     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
96 
97     ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1);
98     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
99     ecma_deref_ecma_string (result_p);
100     ecma_deref_ecma_string (str_p);
101   }
102 
103   {
104     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
105     ecma_stringbuilder_append_char (&builder, LIT_CHAR_1);
106     ecma_stringbuilder_append_char (&builder, LIT_CHAR_2);
107     ecma_stringbuilder_append_char (&builder, LIT_CHAR_3);
108     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
109 
110     ecma_string_t *str_p = ecma_new_ecma_string_from_uint32 (123);
111     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
112     ecma_deref_ecma_string (result_p);
113     ecma_deref_ecma_string (str_p);
114   }
115 
116   {
117     static const lit_utf8_byte_t string_data[] = "abc";
118     ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234);
119 
120     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
121     ecma_stringbuilder_append_char (&builder, LIT_CHAR_1);
122     ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1);
123     ecma_stringbuilder_append (&builder, uint_str_p);
124     ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING);
125     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
126 
127     static const lit_utf8_byte_t expected_data[] = "1abc234string";
128     ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (expected_data, sizeof (expected_data) - 1);
129     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
130     ecma_deref_ecma_string (result_p);
131     ecma_deref_ecma_string (str_p);
132   }
133 
134   {
135     static const lit_utf8_byte_t string_data[] = "abc";
136     ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234);
137 
138     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
139     ecma_stringbuilder_append_char (&builder, LIT_CHAR_1);
140     ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1);
141     ecma_stringbuilder_append (&builder, uint_str_p);
142     ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING);
143     /* Test that we do not leak. */
144     ecma_stringbuilder_destroy (&builder);
145   }
146 
147   {
148     static const lit_utf8_byte_t string_data[] = "abcdefghijklmnop";
149     const size_t count = UINT16_MAX / (sizeof (string_data) - 1) + 1;
150 
151     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
152     for (size_t i = 0; i < count; i++)
153     {
154       ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1);
155     }
156     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
157 
158     ecma_string_t *expected_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
159     for (size_t i = 0; i < count; i++)
160     {
161       expected_p = ecma_append_chars_to_string (expected_p,
162                                                 string_data,
163                                                 sizeof (string_data) - 1,
164                                                 sizeof (string_data) - 1);
165     }
166 
167     TEST_ASSERT (ecma_compare_ecma_strings (result_p, expected_p));
168     ecma_deref_ecma_string (result_p);
169     ecma_deref_ecma_string (expected_p);
170   }
171 
172   {
173     static const lit_utf8_byte_t string_data[] = "abc";
174     ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234);
175 
176     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
177     ecma_stringbuilder_append_char (&builder, LIT_CHAR_1);
178     ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1);
179 
180     ecma_string_t *another_string = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1);
181 
182     ecma_stringbuilder_append (&builder, uint_str_p);
183     ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING);
184     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
185 
186     static const lit_utf8_byte_t expected_data[] = "1abc234string";
187     ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (expected_data, sizeof (expected_data) - 1);
188     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
189     ecma_deref_ecma_string (result_p);
190     ecma_deref_ecma_string (str_p);
191     ecma_deref_ecma_string (another_string);
192   }
193 
194   {
195     static const lit_utf8_byte_t string_data[] = "abc";
196     ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234);
197 
198     ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (uint_str_p);
199     ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1);
200     ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING);
201     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
202 
203     static const lit_utf8_byte_t expected_data[] = "234abcstring";
204     ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (expected_data, sizeof (expected_data) - 1);
205     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
206     ecma_deref_ecma_string (result_p);
207     ecma_deref_ecma_string (str_p);
208   }
209 
210   {
211     ecma_stringbuilder_t builder = ecma_stringbuilder_create ();
212     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
213 
214     ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
215     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
216     ecma_deref_ecma_string (result_p);
217     ecma_deref_ecma_string (str_p);
218   }
219 
220   {
221     ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
222     ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (str_p);
223     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
224 
225     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
226     ecma_deref_ecma_string (result_p);
227     ecma_deref_ecma_string (str_p);
228   }
229 
230   {
231     ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STRING);
232     ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (str_p);
233     ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder);
234 
235     TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p));
236     ecma_deref_ecma_string (result_p);
237     ecma_deref_ecma_string (str_p);
238   }
239 
240   ecma_finalize ();
241   jmem_finalize ();
242 
243   return 0;
244 } /* main */
245