Skip to content

Commit d13addb

Browse files
committed
test: Use ElementsAre() to check arrays of instructions
1 parent 5f593a6 commit d13addb

File tree

2 files changed

+66
-80
lines changed

2 files changed

+66
-80
lines changed

test/unittests/parser_expr_test.cpp

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
#include "parser.hpp"
6-
#include <gtest/gtest.h>
6+
#include <gmock/gmock.h>
77
#include <test/utils/asserts.hpp>
88
#include <test/utils/hex.hpp>
99
#include <test/utils/wasm_binary.hpp>
1010

1111
using namespace fizzy;
1212
using namespace fizzy::test;
13+
using namespace testing;
1314

1415
namespace
1516
{
@@ -27,28 +28,28 @@ TEST(parser_expr, instr_loop)
2728
{
2829
const auto loop_void = "03400b0b"_bytes;
2930
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));
3132
EXPECT_EQ(code1.immediates.size(), 0);
3233
EXPECT_EQ(code1.max_stack_height, 0);
3334

3435
const auto loop_i32 = "037f41000b1a0b"_bytes;
3536
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));
3839
EXPECT_EQ(code2.immediates.size(), 4);
3940
EXPECT_EQ(code2.max_stack_height, 1);
4041

4142
const auto loop_f32 = "037d43000000000b1a0b"_bytes;
4243
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));
4546
EXPECT_EQ(code3.immediates.size(), 4);
4647
EXPECT_EQ(code3.max_stack_height, 1);
4748

4849
const auto loop_f64 = "037c4400000000000000000b1a0b"_bytes;
4950
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));
5253
EXPECT_EQ(code4.immediates.size(), 8);
5354
EXPECT_EQ(code4.max_stack_height, 1);
5455
}
@@ -67,20 +68,20 @@ TEST(parser_expr, instr_block)
6768

6869
const auto empty = "010102400b0b"_bytes;
6970
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));
7273
EXPECT_TRUE(code1.immediates.empty());
7374

7475
const auto block_i64 = "027e42000b1a0b"_bytes;
7576
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));
7879
EXPECT_EQ(code2.immediates, "0000000000000000"_bytes);
7980

8081
const auto block_f64 = "027c4400000000000000000b1a0b"_bytes;
8182
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));
8485
EXPECT_EQ(code2.immediates, "0000000000000000"_bytes);
8586
}
8687

@@ -99,8 +100,8 @@ TEST(parser_expr, loop_br)
99100
const auto wasm = from_hex("0061736d01000000010401600000030201000a0901070003400c000b0b");
100101
const auto module = parse(wasm);
101102

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));
104105
EXPECT_EQ(module->codesec[0].immediates,
105106
"00000000" // code_offset
106107
"00000000" // imm_offset
@@ -118,9 +119,8 @@ TEST(parser_expr, loop_br)
118119
from_hex("0061736d01000000010401600000030201000a0c010a00410003400c000b1a0b");
119120
const auto module_parent_stack = parse(wasm_parent_stack);
120121

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));
124124
EXPECT_EQ(module_parent_stack->codesec[0].immediates,
125125
"00000000" // i32.const
126126
"00000000" // arity
@@ -141,9 +141,8 @@ TEST(parser_expr, loop_br)
141141
from_hex("0061736d01000000010401600000030201000a0c010a00037f41000c000b1a0b");
142142
const auto module_arity = parse(wasm_arity);
143143

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));
147146
EXPECT_EQ(module_arity->codesec[0].immediates,
148147
"00000000" // i32.const
149148
"00000000" // arity - always 0 for loop
@@ -160,8 +159,8 @@ TEST(parser_expr, loop_return)
160159
const auto wasm = from_hex("0061736d01000000010401600000030201000a0801060003400f0b0b");
161160
const auto module = parse(wasm);
162161

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));
165164
EXPECT_EQ(module->codesec[0].immediates,
166165
"00000000" // arity
167166
"03000000" // code_offset
@@ -185,10 +184,10 @@ TEST(parser_expr, block_br)
185184

186185
const auto code_bin = "010240410a21010c00410b21010b20011a0b"_bytes;
187186
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));
192191
EXPECT_EQ(code.immediates,
193192
"0a000000"
194193
"01000000"
@@ -212,9 +211,9 @@ TEST(parser_expr, block_br)
212211
from_hex("0061736d01000000010401600000030201000a0c010a00410002400c000b1a0b");
213212
const auto module_parent_stack = parse(wasm_parent_stack);
214213

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));
218217
EXPECT_EQ(module_parent_stack->codesec[0].immediates,
219218
"00000000" // i32.const
220219
"00000000" // arity
@@ -235,9 +234,9 @@ TEST(parser_expr, block_br)
235234
from_hex("0061736d01000000010401600000030201000a0c010a00027f41000c000b1a0b");
236235
const auto module_arity = parse(wasm_arity);
237236

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));
241240
EXPECT_EQ(module_arity->codesec[0].immediates,
242241
"00000000" // i32.const
243242
"01000000" // arity
@@ -254,8 +253,8 @@ TEST(parser_expr, block_return)
254253
const auto wasm = from_hex("0061736d01000000010401600000030201000a0801060002400f0b0b");
255254
const auto module = parse(wasm);
256255

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));
259258
EXPECT_EQ(module->codesec[0].immediates,
260259
"00000000" // arity
261260
"03000000" // code_offset
@@ -274,8 +273,8 @@ TEST(parser_expr, if_br)
274273
const auto wasm = from_hex("0061736d01000000010401600000030201000a0b010900410004400c000b0b");
275274
const auto module = parse(wasm);
276275

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));
279278
EXPECT_EQ(module->codesec[0].immediates,
280279
"00000000" // i32.const
281280
"04000000" // else code offset
@@ -297,9 +296,9 @@ TEST(parser_expr, if_br)
297296
from_hex("0061736d01000000010401600000030201000a0e010c004100410004400c000b1a0b");
298297
const auto module_parent_stack = parse(wasm_parent_stack);
299298

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));
303302
EXPECT_EQ(module_parent_stack->codesec[0].immediates,
304303
"00000000" // i32.const
305304
"00000000" // i32.const
@@ -342,12 +341,12 @@ TEST(parser_expr, instr_br_table)
342341
ASSERT_EQ(module->codesec.size(), 1);
343342
const auto& code = module->codesec[0];
344343

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,
347346
Instr::local_get, Instr::br_table, Instr::i32_const, Instr::return_, Instr::end,
348347
Instr::i32_const, Instr::return_, Instr::end, Instr::i32_const, Instr::return_,
349348
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));
351350

352351
// 1 local_get before br_table
353352
const auto br_table_imm_offset = 4;
@@ -404,9 +403,9 @@ TEST(parser_expr, instr_br_table_empty_vector)
404403
ASSERT_EQ(module->codesec.size(), 1);
405404
const auto& code = module->codesec[0];
406405

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));
410409

411410
// local_get before br_table
412411
const auto br_table_imm_offset = 4;
@@ -429,7 +428,7 @@ TEST(parser_expr, instr_br_table_as_return)
429428

430429
const auto code_bin = "41000e00000b"_bytes;
431430
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));
433432
EXPECT_EQ(code.max_stack_height, 1);
434433
}
435434

@@ -465,7 +464,7 @@ TEST(parser_expr, call_indirect_table_index)
465464

466465
const auto code1_bin = i32_const(0) + "1100000b"_bytes;
467466
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));
469468

470469
const auto code2_bin = i32_const(0) + "1100010b"_bytes;
471470
EXPECT_THROW_MESSAGE(parse_expr(code2_bin, 0, {}, module), parser_error,

test/unittests/parser_test.cpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
#include "instructions.hpp"
66
#include "parser.hpp"
7-
#include <gtest/gtest.h>
7+
#include <gmock/gmock.h>
88
#include <test/utils/asserts.hpp>
99
#include <test/utils/hex.hpp>
1010
#include <test/utils/leb128_encode.hpp>
1111
#include <test/utils/wasm_binary.hpp>
1212

1313
using namespace fizzy;
1414
using namespace fizzy::test;
15+
using namespace testing;
1516

1617
namespace
1718
{
@@ -1026,8 +1027,7 @@ TEST(parser, code_with_empty_expr_2_locals)
10261027
ASSERT_EQ(module->codesec.size(), 1);
10271028
const auto& code_obj = module->codesec[0];
10281029
EXPECT_EQ(code_obj.local_count, 2);
1029-
ASSERT_EQ(code_obj.instructions.size(), 1);
1030-
EXPECT_EQ(code_obj.instructions[0], Instr::end);
1030+
EXPECT_THAT(code_obj.instructions, ElementsAre(Instr::end));
10311031
EXPECT_EQ(code_obj.immediates.size(), 0);
10321032
}
10331033

@@ -1043,8 +1043,7 @@ TEST(parser, code_with_empty_expr_5_locals)
10431043
ASSERT_EQ(module->codesec.size(), 1);
10441044
const auto& code_obj = module->codesec[0];
10451045
EXPECT_EQ(code_obj.local_count, 5);
1046-
ASSERT_EQ(code_obj.instructions.size(), 1);
1047-
EXPECT_EQ(code_obj.instructions[0], Instr::end);
1046+
EXPECT_THAT(code_obj.instructions, ElementsAre(Instr::end));
10481047
EXPECT_EQ(code_obj.immediates.size(), 0);
10491048
}
10501049

@@ -1062,11 +1061,9 @@ TEST(parser, code_section_with_2_trivial_codes)
10621061
EXPECT_EQ(module->typesec[0].outputs.size(), 0);
10631062
ASSERT_EQ(module->codesec.size(), 2);
10641063
EXPECT_EQ(module->codesec[0].local_count, 0);
1065-
ASSERT_EQ(module->codesec[0].instructions.size(), 1);
1066-
EXPECT_EQ(module->codesec[0].instructions[0], Instr::end);
1064+
EXPECT_THAT(module->codesec[0].instructions, ElementsAre(Instr::end));
10671065
EXPECT_EQ(module->codesec[1].local_count, 0);
1068-
ASSERT_EQ(module->codesec[1].instructions.size(), 1);
1069-
EXPECT_EQ(module->codesec[1].instructions[0], Instr::end);
1066+
EXPECT_THAT(module->codesec[1].instructions, ElementsAre(Instr::end));
10701067
}
10711068

10721069
TEST(parser, code_section_with_basic_instructions)
@@ -1091,15 +1088,10 @@ TEST(parser, code_section_with_basic_instructions)
10911088
EXPECT_EQ(module->typesec[0].outputs.size(), 0);
10921089
ASSERT_EQ(module->codesec.size(), 1);
10931090
EXPECT_EQ(module->codesec[0].local_count, 4);
1094-
ASSERT_EQ(module->codesec[0].instructions.size(), 7);
1095-
EXPECT_EQ(module->codesec[0].instructions[0], Instr::local_get);
1096-
EXPECT_EQ(module->codesec[0].instructions[1], Instr::i32_const);
1097-
EXPECT_EQ(module->codesec[0].instructions[2], Instr::i32_add);
1098-
EXPECT_EQ(module->codesec[0].instructions[3], Instr::local_set);
1099-
EXPECT_EQ(module->codesec[0].instructions[4], Instr::nop);
1100-
EXPECT_EQ(module->codesec[0].instructions[5], Instr::unreachable);
1101-
EXPECT_EQ(module->codesec[0].instructions[6], Instr::end);
1102-
ASSERT_EQ(module->codesec[0].immediates.size(), 3 * 4);
1091+
1092+
EXPECT_THAT(module->codesec[0].instructions,
1093+
ElementsAre(Instr::local_get, Instr::i32_const, Instr::i32_add, Instr::local_set,
1094+
Instr::nop, Instr::unreachable, Instr::end));
11031095
EXPECT_EQ(module->codesec[0].immediates, "010000000200000003000000"_bytes);
11041096
}
11051097

@@ -1116,9 +1108,7 @@ TEST(parser, code_section_with_memory_size)
11161108
const auto module = parse(bin);
11171109
ASSERT_EQ(module->codesec.size(), 1);
11181110
EXPECT_EQ(module->codesec[0].local_count, 0);
1119-
ASSERT_EQ(module->codesec[0].instructions.size(), 2);
1120-
EXPECT_EQ(module->codesec[0].instructions[0], Instr::memory_size);
1121-
EXPECT_EQ(module->codesec[0].instructions[1], Instr::end);
1111+
EXPECT_THAT(module->codesec[0].instructions, ElementsAre(Instr::memory_size, Instr::end));
11221112
EXPECT_TRUE(module->codesec[0].immediates.empty());
11231113

11241114
const auto func_bin_invalid =
@@ -1146,11 +1136,8 @@ TEST(parser, code_section_with_memory_grow)
11461136
const auto module = parse(bin);
11471137
ASSERT_EQ(module->codesec.size(), 1);
11481138
EXPECT_EQ(module->codesec[0].local_count, 0);
1149-
ASSERT_EQ(module->codesec[0].instructions.size(), 4);
1150-
EXPECT_EQ(module->codesec[0].instructions[0], Instr::i32_const);
1151-
EXPECT_EQ(module->codesec[0].instructions[1], Instr::memory_grow);
1152-
EXPECT_EQ(module->codesec[0].instructions[2], Instr::drop);
1153-
EXPECT_EQ(module->codesec[0].instructions[3], Instr::end);
1139+
EXPECT_THAT(module->codesec[0].instructions,
1140+
ElementsAre(Instr::i32_const, Instr::memory_grow, Instr::drop, Instr::end));
11541141
EXPECT_EQ(module->codesec[0].immediates, "00000000"_bytes);
11551142

11561143
const auto func_bin_invalid = "00"_bytes + // vec(locals)
@@ -1465,15 +1452,15 @@ TEST(parser, milestone1)
14651452
const auto m = parse(wasm);
14661453

14671454
ASSERT_EQ(m->typesec.size(), 1);
1468-
EXPECT_EQ(m->typesec[0].inputs, (std::vector{ValType::i32, ValType::i32}));
1469-
EXPECT_EQ(m->typesec[0].outputs, (std::vector{ValType::i32}));
1455+
EXPECT_THAT(m->typesec[0].inputs, ElementsAre(ValType::i32, ValType::i32));
1456+
EXPECT_THAT(m->typesec[0].outputs, ElementsAre(ValType::i32));
14701457

14711458
ASSERT_EQ(m->codesec.size(), 1);
14721459
const auto& c = m->codesec[0];
14731460
EXPECT_EQ(c.local_count, 1);
1474-
EXPECT_EQ(c.instructions,
1475-
(std::vector{Instr::local_get, Instr::local_get, Instr::i32_add, Instr::local_get,
1476-
Instr::i32_add, Instr::local_tee, Instr::local_get, Instr::i32_add, Instr::end}));
1461+
EXPECT_THAT(c.instructions,
1462+
ElementsAre(Instr::local_get, Instr::local_get, Instr::i32_add, Instr::local_get,
1463+
Instr::i32_add, Instr::local_tee, Instr::local_get, Instr::i32_add, Instr::end));
14771464
EXPECT_EQ(c.immediates,
14781465
"00000000"
14791466
"01000000"

0 commit comments

Comments
 (0)