3
3
// SPDX-License-Identifier: Apache-2.0
4
4
5
5
#include " parser.hpp"
6
- #include < gtest/gtest .h>
6
+ #include < gmock/gmock .h>
7
7
#include < test/utils/asserts.hpp>
8
8
#include < test/utils/hex.hpp>
9
9
#include < test/utils/wasm_binary.hpp>
10
10
11
11
using namespace fizzy ;
12
12
using namespace fizzy ::test;
13
+ using namespace testing ;
13
14
14
15
namespace
15
16
{
@@ -27,28 +28,28 @@ TEST(parser_expr, instr_loop)
27
28
{
28
29
const auto loop_void = " 03400b0b" _bytes;
29
30
const auto [code1, pos1] = parse_expr (loop_void);
30
- EXPECT_EQ (code1.instructions , (std::vector{ Instr::loop, Instr::end, Instr::end} ));
31
+ EXPECT_THAT (code1.instructions , ElementsAre ( Instr::loop, Instr::end, Instr::end));
31
32
EXPECT_EQ (code1.immediates .size (), 0 );
32
33
EXPECT_EQ (code1.max_stack_height , 0 );
33
34
34
35
const auto loop_i32 = " 037f41000b1a0b" _bytes;
35
36
const auto [code2, pos2] = parse_expr (loop_i32);
36
- EXPECT_EQ (code2.instructions ,
37
- (std::vector{ Instr::loop, Instr::i32_const, Instr::end, Instr::drop, Instr::end} ));
37
+ EXPECT_THAT (code2.instructions ,
38
+ ElementsAre ( Instr::loop, Instr::i32_const, Instr::end, Instr::drop, Instr::end));
38
39
EXPECT_EQ (code2.immediates .size (), 4 );
39
40
EXPECT_EQ (code2.max_stack_height , 1 );
40
41
41
42
const auto loop_f32 = " 037d43000000000b1a0b" _bytes;
42
43
const auto [code3, pos3] = parse_expr (loop_f32);
43
- EXPECT_EQ (code3.instructions ,
44
- (std::vector{ Instr::loop, Instr::f32_const, Instr::end, Instr::drop, Instr::end} ));
44
+ EXPECT_THAT (code3.instructions ,
45
+ ElementsAre ( Instr::loop, Instr::f32_const, Instr::end, Instr::drop, Instr::end));
45
46
EXPECT_EQ (code3.immediates .size (), 4 );
46
47
EXPECT_EQ (code3.max_stack_height , 1 );
47
48
48
49
const auto loop_f64 = " 037c4400000000000000000b1a0b" _bytes;
49
50
const auto [code4, pos4] = parse_expr (loop_f64);
50
- EXPECT_EQ (code4.instructions ,
51
- (std::vector{ Instr::loop, Instr::f64_const, Instr::end, Instr::drop, Instr::end} ));
51
+ EXPECT_THAT (code4.instructions ,
52
+ ElementsAre ( Instr::loop, Instr::f64_const, Instr::end, Instr::drop, Instr::end));
52
53
EXPECT_EQ (code4.immediates .size (), 8 );
53
54
EXPECT_EQ (code4.max_stack_height , 1 );
54
55
}
@@ -67,20 +68,20 @@ TEST(parser_expr, instr_block)
67
68
68
69
const auto empty = " 010102400b0b" _bytes;
69
70
const auto [code1, pos1] = parse_expr (empty);
70
- EXPECT_EQ (code1.instructions ,
71
- (std::vector{ Instr::nop, Instr::nop, Instr::block, Instr::end, Instr::end} ));
71
+ EXPECT_THAT (code1.instructions ,
72
+ ElementsAre ( Instr::nop, Instr::nop, Instr::block, Instr::end, Instr::end));
72
73
EXPECT_TRUE (code1.immediates .empty ());
73
74
74
75
const auto block_i64 = " 027e42000b1a0b" _bytes;
75
76
const auto [code2, pos2] = parse_expr (block_i64);
76
- EXPECT_EQ (code2.instructions ,
77
- (std::vector{ Instr::block, Instr::i64_const, Instr::end, Instr::drop, Instr::end} ));
77
+ EXPECT_THAT (code2.instructions ,
78
+ ElementsAre ( Instr::block, Instr::i64_const, Instr::end, Instr::drop, Instr::end));
78
79
EXPECT_EQ (code2.immediates , " 0000000000000000" _bytes);
79
80
80
81
const auto block_f64 = " 027c4400000000000000000b1a0b" _bytes;
81
82
const auto [code3, pos3] = parse_expr (block_f64);
82
- EXPECT_EQ (code3.instructions ,
83
- (std::vector{ Instr::block, Instr::f64_const, Instr::end, Instr::drop, Instr::end} ));
83
+ EXPECT_THAT (code3.instructions ,
84
+ ElementsAre ( Instr::block, Instr::f64_const, Instr::end, Instr::drop, Instr::end));
84
85
EXPECT_EQ (code2.immediates , " 0000000000000000" _bytes);
85
86
}
86
87
@@ -99,8 +100,8 @@ TEST(parser_expr, loop_br)
99
100
const auto wasm = from_hex (" 0061736d01000000010401600000030201000a0901070003400c000b0b" );
100
101
const auto module = parse (wasm);
101
102
102
- EXPECT_EQ (module->codesec [0 ].instructions ,
103
- (std::vector{ Instr::loop, Instr::br, Instr::end, Instr::end} ));
103
+ EXPECT_THAT (module->codesec [0 ].instructions ,
104
+ ElementsAre ( Instr::loop, Instr::br, Instr::end, Instr::end));
104
105
EXPECT_EQ (module->codesec [0 ].immediates ,
105
106
" 00000000" // code_offset
106
107
" 00000000" // imm_offset
@@ -118,9 +119,8 @@ TEST(parser_expr, loop_br)
118
119
from_hex (" 0061736d01000000010401600000030201000a0c010a00410003400c000b1a0b" );
119
120
const auto module_parent_stack = parse (wasm_parent_stack);
120
121
121
- EXPECT_EQ (module_parent_stack->codesec [0 ].instructions ,
122
- (std::vector{
123
- Instr::i32_const, Instr::loop, Instr::br, Instr::end, Instr::drop, Instr::end}));
122
+ EXPECT_THAT (module_parent_stack->codesec [0 ].instructions ,
123
+ ElementsAre (Instr::i32_const, Instr::loop, Instr::br, Instr::end, Instr::drop, Instr::end));
124
124
EXPECT_EQ (module_parent_stack->codesec [0 ].immediates ,
125
125
" 00000000" // i32.const
126
126
" 00000000" // arity
@@ -141,9 +141,8 @@ TEST(parser_expr, loop_br)
141
141
from_hex (" 0061736d01000000010401600000030201000a0c010a00037f41000c000b1a0b" );
142
142
const auto module_arity = parse (wasm_arity);
143
143
144
- EXPECT_EQ (module_arity->codesec [0 ].instructions ,
145
- (std::vector{
146
- Instr::loop, Instr::i32_const, Instr::br, Instr::end, Instr::drop, Instr::end}));
144
+ EXPECT_THAT (module_arity->codesec [0 ].instructions ,
145
+ ElementsAre (Instr::loop, Instr::i32_const, Instr::br, Instr::end, Instr::drop, Instr::end));
147
146
EXPECT_EQ (module_arity->codesec [0 ].immediates ,
148
147
" 00000000" // i32.const
149
148
" 00000000" // arity - always 0 for loop
@@ -160,8 +159,8 @@ TEST(parser_expr, loop_return)
160
159
const auto wasm = from_hex (" 0061736d01000000010401600000030201000a0801060003400f0b0b" );
161
160
const auto module = parse (wasm);
162
161
163
- EXPECT_EQ (module->codesec [0 ].instructions ,
164
- (std::vector{ Instr::loop, Instr::return_, Instr::end, Instr::end} ));
162
+ EXPECT_THAT (module->codesec [0 ].instructions ,
163
+ ElementsAre ( Instr::loop, Instr::return_, Instr::end, Instr::end));
165
164
EXPECT_EQ (module->codesec [0 ].immediates ,
166
165
" 00000000" // arity
167
166
" 03000000" // code_offset
@@ -185,10 +184,10 @@ TEST(parser_expr, block_br)
185
184
186
185
const auto code_bin = " 010240410a21010c00410b21010b20011a0b" _bytes;
187
186
const auto [code, pos] = parse_expr (code_bin, 0 , {{2 , ValType::i32}});
188
- EXPECT_EQ (
189
- code.instructions , (std::vector{ Instr::nop, Instr::block, Instr::i32_const,
190
- Instr::local_set , Instr::br , Instr::i32_const , Instr::local_set ,
191
- Instr::end, Instr:: local_get, Instr::drop, Instr::end} ));
187
+ EXPECT_THAT (
188
+ code.instructions , ElementsAre ( Instr::nop, Instr::block, Instr::i32_const, Instr::local_set ,
189
+ Instr::br , Instr::i32_const , Instr::local_set , Instr::end ,
190
+ Instr::local_get, Instr::drop, Instr::end));
192
191
EXPECT_EQ (code.immediates ,
193
192
" 0a000000"
194
193
" 01000000"
@@ -212,9 +211,9 @@ TEST(parser_expr, block_br)
212
211
from_hex (" 0061736d01000000010401600000030201000a0c010a00410002400c000b1a0b" );
213
212
const auto module_parent_stack = parse (wasm_parent_stack);
214
213
215
- EXPECT_EQ (module_parent_stack->codesec [0 ].instructions ,
216
- (std::vector{
217
- Instr::i32_const, Instr::block, Instr::br, Instr::end, Instr::drop, Instr::end} ));
214
+ EXPECT_THAT (module_parent_stack->codesec [0 ].instructions ,
215
+ ElementsAre (
216
+ Instr::i32_const, Instr::block, Instr::br, Instr::end, Instr::drop, Instr::end));
218
217
EXPECT_EQ (module_parent_stack->codesec [0 ].immediates ,
219
218
" 00000000" // i32.const
220
219
" 00000000" // arity
@@ -235,9 +234,9 @@ TEST(parser_expr, block_br)
235
234
from_hex (" 0061736d01000000010401600000030201000a0c010a00027f41000c000b1a0b" );
236
235
const auto module_arity = parse (wasm_arity);
237
236
238
- EXPECT_EQ (module_arity-> codesec [ 0 ]. instructions ,
239
- (std::vector{
240
- Instr::block, Instr::i32_const, Instr::br, Instr::end, Instr::drop, Instr::end} ));
237
+ EXPECT_THAT (
238
+ module_arity-> codesec [ 0 ]. instructions , ElementsAre (Instr::block, Instr::i32_const,
239
+ Instr::br, Instr::end, Instr::drop, Instr::end));
241
240
EXPECT_EQ (module_arity->codesec [0 ].immediates ,
242
241
" 00000000" // i32.const
243
242
" 01000000" // arity
@@ -254,8 +253,8 @@ TEST(parser_expr, block_return)
254
253
const auto wasm = from_hex (" 0061736d01000000010401600000030201000a0801060002400f0b0b" );
255
254
const auto module = parse (wasm);
256
255
257
- EXPECT_EQ (module->codesec [0 ].instructions ,
258
- (std::vector{ Instr::block, Instr::return_, Instr::end, Instr::end} ));
256
+ EXPECT_THAT (module->codesec [0 ].instructions ,
257
+ ElementsAre ( Instr::block, Instr::return_, Instr::end, Instr::end));
259
258
EXPECT_EQ (module->codesec [0 ].immediates ,
260
259
" 00000000" // arity
261
260
" 03000000" // code_offset
@@ -274,8 +273,8 @@ TEST(parser_expr, if_br)
274
273
const auto wasm = from_hex (" 0061736d01000000010401600000030201000a0b010900410004400c000b0b" );
275
274
const auto module = parse (wasm);
276
275
277
- EXPECT_EQ (module->codesec [0 ].instructions ,
278
- (std::vector{ Instr::i32_const, Instr::if_, Instr::br, Instr::end, Instr::end} ));
276
+ EXPECT_THAT (module->codesec [0 ].instructions ,
277
+ ElementsAre ( Instr::i32_const, Instr::if_, Instr::br, Instr::end, Instr::end));
279
278
EXPECT_EQ (module->codesec [0 ].immediates ,
280
279
" 00000000" // i32.const
281
280
" 04000000" // else code offset
@@ -297,9 +296,9 @@ TEST(parser_expr, if_br)
297
296
from_hex (" 0061736d01000000010401600000030201000a0e010c004100410004400c000b1a0b" );
298
297
const auto module_parent_stack = parse (wasm_parent_stack);
299
298
300
- EXPECT_EQ (module_parent_stack->codesec [0 ].instructions ,
301
- (std::vector{ Instr::i32_const, Instr::i32_const, Instr::if_, Instr::br, Instr::end,
302
- Instr::drop, Instr::end} ));
299
+ EXPECT_THAT (module_parent_stack->codesec [0 ].instructions ,
300
+ ElementsAre ( Instr::i32_const, Instr::i32_const, Instr::if_, Instr::br, Instr::end,
301
+ Instr::drop, Instr::end));
303
302
EXPECT_EQ (module_parent_stack->codesec [0 ].immediates ,
304
303
" 00000000" // i32.const
305
304
" 00000000" // i32.const
@@ -342,12 +341,12 @@ TEST(parser_expr, instr_br_table)
342
341
ASSERT_EQ (module->codesec .size (), 1 );
343
342
const auto & code = module->codesec [0 ];
344
343
345
- EXPECT_EQ (code.instructions ,
346
- (std::vector{ Instr::block, Instr::block, Instr::block, Instr::block, Instr::block,
344
+ EXPECT_THAT (code.instructions ,
345
+ ElementsAre ( Instr::block, Instr::block, Instr::block, Instr::block, Instr::block,
347
346
Instr::local_get, Instr::br_table, Instr::i32_const, Instr::return_, Instr::end,
348
347
Instr::i32_const, Instr::return_, Instr::end, Instr::i32_const, Instr::return_,
349
348
Instr::end, Instr::i32_const, Instr::return_, Instr::end, Instr::i32_const,
350
- Instr::return_, Instr::end, Instr::i32_const, Instr::end} ));
349
+ Instr::return_, Instr::end, Instr::i32_const, Instr::end));
351
350
352
351
// 1 local_get before br_table
353
352
const auto br_table_imm_offset = 4 ;
@@ -404,9 +403,9 @@ TEST(parser_expr, instr_br_table_empty_vector)
404
403
ASSERT_EQ (module->codesec .size (), 1 );
405
404
const auto & code = module->codesec [0 ];
406
405
407
- EXPECT_EQ (code.instructions ,
408
- (std::vector{ Instr::block, Instr::local_get, Instr::br_table, Instr::i32_const,
409
- Instr::return_, Instr::end, Instr::i32_const, Instr::end} ));
406
+ EXPECT_THAT (code.instructions ,
407
+ ElementsAre ( Instr::block, Instr::local_get, Instr::br_table, Instr::i32_const,
408
+ Instr::return_, Instr::end, Instr::i32_const, Instr::end));
410
409
411
410
// local_get before br_table
412
411
const auto br_table_imm_offset = 4 ;
@@ -429,7 +428,7 @@ TEST(parser_expr, instr_br_table_as_return)
429
428
430
429
const auto code_bin = " 41000e00000b" _bytes;
431
430
const auto [code, _] = parse_expr (code_bin);
432
- EXPECT_EQ (code.instructions , (std::vector{ Instr::i32_const, Instr::br_table, Instr::end} ));
431
+ EXPECT_THAT (code.instructions , ElementsAre ( Instr::i32_const, Instr::br_table, Instr::end));
433
432
EXPECT_EQ (code.max_stack_height , 1 );
434
433
}
435
434
@@ -465,7 +464,7 @@ TEST(parser_expr, call_indirect_table_index)
465
464
466
465
const auto code1_bin = i32_const (0 ) + " 1100000b" _bytes;
467
466
const auto [code, pos] = parse_expr (code1_bin, 0 , {}, module);
468
- EXPECT_EQ (code.instructions , (std::vector{ Instr::i32_const, Instr::call_indirect, Instr::end} ));
467
+ EXPECT_THAT (code.instructions , ElementsAre ( Instr::i32_const, Instr::call_indirect, Instr::end));
469
468
470
469
const auto code2_bin = i32_const (0 ) + " 1100010b" _bytes;
471
470
EXPECT_THROW_MESSAGE (parse_expr (code2_bin, 0 , {}, module), parser_error,
0 commit comments