2 # Copyright (C) 2010, Parrot Foundation.
7 t/pmc/stringbuilder.t - StringBuilder
11 % prove t/pmc/stringbuilder.t
15 Tests the C<StringBuilder> PMC.
21 .include 'test_more.pir'
23 test_create() # 3 tests
26 test_push_pmc() # 4 tests
27 test_push_string_unicode() # 1 test
28 test_i_concatenate() # 1 test
29 test_set_string_native() # 4 tests
30 test_set_string_native_with_hash() # 2 tests
35 emit_with_percent_args()
36 emit_with_named_args()
37 emit_with_pos_and_named_args()
39 test_unicode_conversion_tt1665()
49 sb = new ['StringBuilder']
53 ok( $I0, 'StringBuilder created' )
56 is( $S0, '', '... with empty content')
59 ar = new ['FixedStringArray']
60 sb = new ['StringBuilder'], ar
63 ok( $I0, 'StringBuilder created from empty array' )
66 .sub 'test_push_string'
68 sb = new ["StringBuilder"]
72 is( $S0, "foo", "First string pushed")
76 is( $S1, "foobar", "Second string pushed")
78 is( $S0, "foo", "... without clobbering first string")
80 $I0 = sb.'get_string_length'()
81 is( $I0, 6, "... and string length is correct")
83 # Push large string which will cause reallocate
84 $S99 = repeat "x", 128
87 $S0 = concat "foobar", $S99
89 is( $S0, $S1, "Push 128 chars string works")
91 $S99 = repeat "x", 1000
94 $S0 = concat $S0, $S99
96 is( $S0, $S1, "Push 1000 chars string works")
98 $S99 = repeat "x", 12000
101 $S0 = concat $S0, $S99
103 is( $S0, $S1, "Push 10000 chars string works")
109 is( $S0, $S1, "push a null string does nothing" )
114 sb = new ["StringBuilder"]
119 is( $S0, "foo", "First string pushed")
124 is( $S1, "foobar", "Second string pushed")
126 is( $S0, "foo", "... without clobbering first string")
129 is( $I0, 128, "... and capacity still 128" )
132 .sub 'test_push_string_unicode'
134 sb = new ["StringBuilder"]
137 push sb, unicode:"o "
138 push sb, iso-8859-1:"tötsch"
141 is( $S0, iso-8859-1:"leo tötsch", "Unicode strings appened")
144 .sub 'test_i_concatenate'
146 sb = new ["StringBuilder"]
157 is( $S0, "foobarbaz", "StringBuilder handles concat properly")
160 .sub 'test_set_string_native'
162 sb = new ["StringBuilder"]
168 is( $S0, "foo", "Assignment works")
172 is( $S0, "foobar", "... with appending string after")
173 is( $S99, "foo", "... without touching of original string")
175 # Assumed that the previous operations does not reach initial
176 # capacity of the buffer, the next test should cause a
177 # reallocation, ensuring full coverage of the set_string_native
179 $S1 = repeat 'x', 4096
181 $I0 = sb.'get_string_length'()
182 is( $I0, 4096, "... with a big size change")
185 .sub 'test_set_string_native_with_hash'
187 sb = new ["StringBuilder"]
201 is ( $S99, "foo", "First string stored in hash" )
204 is ( $S99, "foobar", "Second string stored in hash" )
210 sb = new ["StringBuilder"]
211 i = new ["Integer"], 17
215 is( $I0, 1, "set_pmc gives the pmc string value")
220 sb = new ["StringBuilder"]
222 $S0 = substr sb, 2, 3
223 is( $S0, 'oba', "substr result is correct")
226 .sub emit_with_pos_args
228 code = new ["StringBuilder"]
229 code."append_format"("label_%0:\n", 1234)
230 code."append_format"(" say '%0, %1'\n", "Hello", "World")
231 code."append_format"(" %0 = %2\n", "$I0", 24, 48)
232 is(code, <<'CODE', "code string with positional args looks fine")
239 .sub emit_with_percent_args
241 code = new ['StringBuilder']
242 code."append_format"("label_%0:\n", 1234)
243 code."append_format"(" say '%,'\n", "Hello")
244 code."append_format"(" say '%,'\n", "Hello", "World", "of", "Parrot")
245 code."append_format"(" say '%%0'\n")
246 is(code, <<'CODE', "code string with % args looks fine")
249 say 'Hello, World, of, Parrot'
254 .sub emit_with_named_args
256 code = new ['StringBuilder']
257 code."append_format"("label_%a:\n", "a"=>1234)
258 code."append_format"(" say '%b, %c'\n", "b"=>"Hello", "c"=>"World")
259 code."append_format"(" say '%d'\n", "b"=>"Hello", "c"=>"World")
260 is(code, <<'CODE', "emit with named args looks fine")
267 .sub emit_with_pos_and_named_args
269 code = new ['StringBuilder']
270 code."append_format"("label_%a:\n", "a"=>1234)
271 code."append_format"(" %0 '%b, %c'\n", "say", "print", "b"=>"H", "c"=>"W")
272 code."append_format"(" say '%,, %c'\n", "alpha", "beta", "b"=>"H", "c"=>"W")
273 is(code, <<'CODE', "emit with pos + named args")
280 .sub "test_unicode_conversion_tt1665"
282 list = new 'ResizablePMCArray'
289 sb = new 'StringBuilder'
292 unless iterator goto done
301 ok( $S0, "Pushing unicode strings doesn't kill StringBuilder")
306 ar = new ['ResizableStringArray']
311 $S99 = repeat "x", 12
313 $S1 = 'foobar' . $S99
315 $S99 = repeat "y", 13
319 $S99 = repeat "z", 14
327 sb = new ["StringBuilder"], ar
329 is( $S0, $S1, 'init_pmc() should join all passed strings' )
332 .sub 'test_encodings'
334 sb = new ["StringBuilder"]
337 push sb, iso-8859-1:"\x{E4}\x{F6}\x{FC}"
338 push sb, utf8:unicode:"БДЖ"
342 is( $S0, utf8:unicode:"fooäöüБДЖbar", 'push strings with different encodings' )
349 # vim: expandtab shiftwidth=4 ft=pir: