Skip to content

Commit 8971797

Browse files
committed
Expand tests for array.new_data
Noticed that the existing tests didn't exercise out-of-bounds indexing of the data segment, so I added a test for that. At the same time, since I was already creating a dedicated `.wast` file for testing `array.new_data`, I also added a few general tests to round the file out. Some of these might technically duplicate things that already happen to be tested in `array.wast`, but I think that's probably fine, and that it is good to have dedicated tests in this new file. I can remove them if we'd rather not have these potential-duplicate tests.
1 parent 671b526 commit 8971797

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/core/gc/array_new_data.wast

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(module
2+
(type $arr (array (mut i8)))
3+
4+
(data $d "abcd")
5+
6+
(func (export "array-new-data") (param i32 i32) (result (ref $arr))
7+
(array.new_data $arr $d (local.get 0) (local.get 1))
8+
)
9+
)
10+
11+
;; In-bounds data segment accesses.
12+
(assert_return (invoke "array-new-data" (i32.const 0) (i32.const 0)) (ref.array))
13+
(assert_return (invoke "array-new-data" (i32.const 0) (i32.const 4)) (ref.array))
14+
(assert_return (invoke "array-new-data" (i32.const 1) (i32.const 2)) (ref.array))
15+
(assert_return (invoke "array-new-data" (i32.const 4) (i32.const 0)) (ref.array))
16+
17+
;; Out-of-bounds data segment accesses.
18+
(assert_trap (invoke "array-new-data" (i32.const 0) (i32.const 5)) "out of bounds memory access")
19+
(assert_trap (invoke "array-new-data" (i32.const 5) (i32.const 0)) "out of bounds memory access")
20+
21+
(module
22+
(type $arr (array (mut i8)))
23+
24+
(data $d "\aa\bb\cc\dd")
25+
26+
(func (export "array-new-data-contents") (result i32 i32)
27+
(local (ref null $arr))
28+
(local.set 0 (array.new_data $arr $d (i32.const 1) (i32.const 2)))
29+
(array.get_u $arr (local.get 0) (i32.const 0))
30+
(array.get_u $arr (local.get 0) (i32.const 1))
31+
)
32+
)
33+
34+
;; Array is initialized with the correct contents.
35+
(assert_return (invoke "array-new-data-contents") (i32.const 0xbb) (i32.const 0xcc))
36+
37+
(module
38+
(type $arr (array (mut i32)))
39+
40+
(data $d "\aa\bb\cc\dd")
41+
42+
(func (export "array-new-data-little-endian") (result i32)
43+
(array.get $arr
44+
(array.new_data $arr $d (i32.const 0) (i32.const 1))
45+
(i32.const 0))
46+
)
47+
)
48+
49+
;; Data segments are interpreted as little-endian.
50+
(assert_return (invoke "array-new-data-little-endian") (i32.const 0xddccbbaa))

0 commit comments

Comments
 (0)