2 # Copyright (C) 2010, Parrot Foundation.
7 t/pmc/bytebuffer.t - ByteBuffer PMC
11 % prove t/pmc/bytebuffer.t
15 Tests C<ByteBuffer> PMC..
19 .include 'iglobals.pasm'
20 .include 'iterator.pasm'
21 .include 'except_types.pasm'
24 .include 'test_more.pir'
38 ################################################################
43 $P1 = $P0[.IGLOBALS_CONFIG_HASH]
50 $P1 = $P0[.IGLOBALS_CONFIG_HASH]
51 $I0 = $P1['bigendian']
55 ################################################################
60 bb = new ['ByteBuffer']
62 is(n, 0, "size of a new buffer is 0")
64 bb = new ['ByteBuffer'], 42
66 is(n, 42, "size of a new buffer with initial size is correct")
74 bb = new ['ByteBuffer']
78 # Exercise mark vtable
82 is(n, 2, "size is the same as the source string bytelength")
85 is(n, c, "first byte is the same as the source string")
88 is(n, c, "second byte is the same as the source string")
90 is(n, 0, "byte out of size is 0")
92 is(n, 0, "byte at negative index is 0")
98 bb = new ['ByteBuffer']
101 is(n, 256, "setting a byte resize empty buffer")
108 is(n, 3, "setting a byte resize buffer initialized from string")
110 bb = new ['ByteBuffer'], 42
113 is(n, 42, "setting a byte within range does not resize")
116 is(n, 43, "setting a byte resize buffer with initial size")
118 is(n, 9, "resized buffer preserve old value")
122 ok(0, "setting a byte with negative index should throw")
126 ok(1, "setting a byte with negative index throws")
136 bb = new ['ByteBuffer']
137 s = bb.'get_string'('ascii')
139 is(s, 0, "getting from unitialized buffer gives empty string")
142 s = bb.'get_string'('ascii')
144 is(n, 4, "getting ascii from buffer gives correct length")
145 is(s, "abcd", "getting ascii from buffer gives correct content")
148 unless $I0 goto skip_it
150 bb = new ['ByteBuffer']
152 # Upper case n tilde: codepoint 0xD1, utf8 encoding 0xC3, 0x91
153 #bb = utf16:unicode:"\x{D1}"
154 # Can't do that, or the program can't be compiled without ICU.
155 # Fill the buffer with bytes instead.
157 # Get endianess to set the bytes in the appropiate order.
158 # *** XXX *** Need report from big endian platforms.
168 s = bb.'get_string'('utf16')
170 is(n, 1, "getting utf16 from buffer gives correct length")
172 is(n, 0xD1, "getting utf16 from buffer gives correct codepoint")
173 bb = new ['ByteBuffer']
176 s = bb.'get_string_as'(utf8:unicode:"")
178 is(n, 1, "getting utf8 from buffer gives correct length")
180 is(n, 0xD1, "getting utf8 from buffer gives correct codepoint")
183 skip(4, "this test needs ICU")
190 bb = new ['ByteBuffer']
197 is(n, m, "push increments size")
199 s = bb.'get_string_as'(ascii:"")
200 is(s, 'hello', "push gives expected string result")
207 bb = new ['ByteBuffer']
211 is(n, 723, 'resize from empty')
215 is(n, 42, 'reduce size')
219 is(n, 999, 'increase size')
223 is(n, 0, 'resize to 0')
228 is(n, 3, 'reduce size from string content')
230 s = bb.'get_string_as'(ascii:"")
231 is(s, 'foo', 'resized string content has correct value')
236 is(n, 7, 'increase size from string content')
238 # This test is for code coverage, zero filling is not a feature
239 # you should expect, it can be changed for performance reasons.
240 s = bb.'get_string_as'(binary:"")
241 is(s, binary:"foobar\x{0}", 'resized from string content is zero filled')
246 is(n, 0, 'resize to zero from string content')
251 is(n, 0, 'resize to zero from allocated content')
254 eh = new ['ExceptionHandler']
255 eh.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS)
256 set_addr eh, catch_negative
266 ok(n, 'negative size throws')
270 # Exercise buffer reallocation building a utf16 string with the
273 .local int i, big, pos, b0, b1, c
276 unless $I0 goto skip_it
278 # Get endianess to set the bytes in the appropiate order.
279 # *** XXX *** Need report from big endian platforms.
282 bb = new ['ByteBuffer']
301 if i < 8192 goto loopset
304 s = bb.'get_string'('utf16')
308 if i != 8160 goto failed
310 # Check string content
315 if c != i goto failed
318 if i < 8192 goto loopcheck
319 ok(1, "reallocation")
323 ok(0, "reallocation")
326 skip(1, "this test needs ICU")
331 .local pmc bb, it, arr
334 bb = new ['ByteBuffer']
337 it = .ITERATE_FROM_START
338 arr = new ['ResizableStringArray']
340 unless it goto donearray
348 is(r, s, 'iterate buffer content')
354 bb = new ['ByteBuffer']
356 push_eh catch_encoding
357 s = bb.'get_string'('ascii', '???INVALID eNCODING===')
359 ok(0, "get_string with invalid encoding should throw")
365 ok(1, "get_string with invalid encoding throws")
367 bb[0] = 128 # Out of ascii range
368 push_eh catch_content
369 s = bb.'get_string'('ascii', 'fixed_8')
371 ok(0, "get_string with invalid content should throw")
377 ok(1, "get_string with invalid content throws")
385 # vim: expandtab shiftwidth=4 ft=pir: