Skip to content

Commit

Permalink
4495 - use some matchers gtest functionalities to improve unite tests in
Browse files Browse the repository at this point in the history
AvmMini_execution.test.cpp
  • Loading branch information
jeanmon committed Feb 8, 2024
1 parent 808efb8 commit ef4ba2e
Showing 1 changed file with 40 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include "barretenberg/vm/avm_trace/AvmMini_helper.hpp"
#include "barretenberg/vm/avm_trace/AvmMini_opcode.hpp"
#include "barretenberg/vm/tests/helpers.test.hpp"
#include "gmock/gmock.h"
#include <cstdint>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <utility>

using namespace bb;
using namespace testing;
namespace {
void gen_proof_and_validate(std::vector<uint8_t> const& bytecode,
std::vector<Row>&& trace,
Expand Down Expand Up @@ -65,17 +66,17 @@ TEST_F(AvmMiniExecutionTests, basicAddReturn)
auto instructions = Deserialization::parse(bytecode);

// 2 instructions
EXPECT_THAT(instructions, testing::SizeIs(2));
EXPECT_THAT(instructions, SizeIs(2));

// ADD
EXPECT_EQ(instructions.at(0).op_code, OpCode::ADD);

auto operands = instructions.at(0).operands;
EXPECT_EQ(operands.size(), 4);
EXPECT_EQ(std::get<AvmMemoryTag>(operands.at(0)), AvmMemoryTag::U8);
EXPECT_EQ(std::get<uint32_t>(operands.at(1)), 7);
EXPECT_EQ(std::get<uint32_t>(operands.at(2)), 9);
EXPECT_EQ(std::get<uint32_t>(operands.at(3)), 1);
EXPECT_THAT(operands,
ElementsAre(VariantWith<AvmMemoryTag>(AvmMemoryTag::U8),
VariantWith<uint32_t>(7),
VariantWith<uint32_t>(9),
VariantWith<uint32_t>(1)));

// RETURN
EXPECT_EQ(instructions.at(1).op_code, OpCode::RETURN);
Expand Down Expand Up @@ -113,35 +114,34 @@ TEST_F(AvmMiniExecutionTests, setAndSubOpcodes)
auto bytecode = hex_to_bytes(bytecode_hex);
auto instructions = Deserialization::parse(bytecode);

EXPECT_EQ(instructions.size(), 4);
EXPECT_THAT(instructions, SizeIs(4));

// SET
EXPECT_EQ(instructions.at(0).op_code, OpCode::SET);

auto operands = instructions.at(0).operands;
EXPECT_EQ(operands.size(), 3);
EXPECT_EQ(std::get<AvmMemoryTag>(operands.at(0)), AvmMemoryTag::U16);
EXPECT_EQ(std::get<uint16_t>(operands.at(1)), 47123);
EXPECT_EQ(std::get<uint32_t>(operands.at(2)), 170);
EXPECT_THAT(operands,
ElementsAre(VariantWith<AvmMemoryTag>(AvmMemoryTag::U16),
VariantWith<uint16_t>(47123),
VariantWith<uint32_t>(170)));

// SET
EXPECT_EQ(instructions.at(1).op_code, OpCode::SET);

operands = instructions.at(1).operands;
EXPECT_EQ(operands.size(), 3);
EXPECT_EQ(std::get<AvmMemoryTag>(operands.at(0)), AvmMemoryTag::U16);
EXPECT_EQ(std::get<uint16_t>(operands.at(1)), 37123);
EXPECT_EQ(std::get<uint32_t>(operands.at(2)), 51);

EXPECT_THAT(operands,
ElementsAre(VariantWith<AvmMemoryTag>(AvmMemoryTag::U16),
VariantWith<uint16_t>(37123),
VariantWith<uint32_t>(51)));
// SUB
EXPECT_EQ(instructions.at(2).op_code, OpCode::SUB);

operands = instructions.at(2).operands;
EXPECT_EQ(operands.size(), 4);
EXPECT_EQ(std::get<AvmMemoryTag>(operands.at(0)), AvmMemoryTag::U16);
EXPECT_EQ(std::get<uint32_t>(operands.at(1)), 170);
EXPECT_EQ(std::get<uint32_t>(operands.at(2)), 51);
EXPECT_EQ(std::get<uint32_t>(operands.at(3)), 1);
EXPECT_THAT(operands,
ElementsAre(VariantWith<AvmMemoryTag>(AvmMemoryTag::U16),
VariantWith<uint32_t>(170),
VariantWith<uint32_t>(51),
VariantWith<uint32_t>(1)));

auto trace = Execution::gen_trace(instructions);

Expand Down Expand Up @@ -191,35 +191,33 @@ TEST_F(AvmMiniExecutionTests, powerWithMulOpcodes)
auto bytecode = hex_to_bytes(bytecode_hex);
auto instructions = Deserialization::parse(bytecode);

EXPECT_EQ(instructions.size(), 15);
EXPECT_THAT(instructions, SizeIs(15));

// MUL first pos
EXPECT_EQ(instructions.at(2).op_code, OpCode::MUL);

auto operands = instructions.at(2).operands;
EXPECT_EQ(operands.size(), 4);
EXPECT_EQ(std::get<AvmMemoryTag>(operands.at(0)), AvmMemoryTag::U64);
EXPECT_EQ(std::get<uint32_t>(operands.at(1)), 0);
EXPECT_EQ(std::get<uint32_t>(operands.at(2)), 1);
EXPECT_EQ(std::get<uint32_t>(operands.at(3)), 1);
EXPECT_THAT(operands,
ElementsAre(VariantWith<AvmMemoryTag>(AvmMemoryTag::U64),
VariantWith<uint32_t>(0),
VariantWith<uint32_t>(1),
VariantWith<uint32_t>(1)));

// MUL last pos
EXPECT_EQ(instructions.at(13).op_code, OpCode::MUL);

operands = instructions.at(13).operands;
EXPECT_EQ(operands.size(), 4);
EXPECT_EQ(std::get<AvmMemoryTag>(operands.at(0)), AvmMemoryTag::U64);
EXPECT_EQ(std::get<uint32_t>(operands.at(1)), 0);
EXPECT_EQ(std::get<uint32_t>(operands.at(2)), 1);
EXPECT_EQ(std::get<uint32_t>(operands.at(3)), 1);
EXPECT_THAT(operands,
ElementsAre(VariantWith<AvmMemoryTag>(AvmMemoryTag::U64),
VariantWith<uint32_t>(0),
VariantWith<uint32_t>(1),
VariantWith<uint32_t>(1)));

// RETURN
EXPECT_EQ(instructions.at(14).op_code, OpCode::RETURN);
operands = instructions.at(14).operands;

EXPECT_EQ(operands.size(), 2);
EXPECT_EQ(std::get<uint32_t>(operands.at(0)), 0);
EXPECT_EQ(std::get<uint32_t>(operands.at(1)), 0);
EXPECT_THAT(operands, ElementsAre(VariantWith<uint32_t>(0), VariantWith<uint32_t>(0)));

auto trace = Execution::gen_trace(instructions);

Expand Down Expand Up @@ -266,14 +264,13 @@ TEST_F(AvmMiniExecutionTests, simpleInternalCall)
auto bytecode = hex_to_bytes(bytecode_hex);
auto instructions = Deserialization::parse(bytecode);

EXPECT_EQ(instructions.size(), 6);
EXPECT_THAT(instructions, SizeIs(6));

// We test parsing step for INTERNALCALL and INTERNALRETURN.

// INTERNALCALL
EXPECT_EQ(instructions.at(1).op_code, OpCode::INTERNALCALL);
EXPECT_EQ(instructions.at(1).operands.size(), 1);
EXPECT_EQ(std::get<uint32_t>(instructions.at(1).operands.at(0)), 4);
EXPECT_THAT(instructions.at(1).operands, ElementsAre(VariantWith<uint32_t>(4)));

// INTERNALRETURN
EXPECT_EQ(instructions.at(5).op_code, OpCode::INTERNALRETURN);
Expand Down Expand Up @@ -343,7 +340,7 @@ TEST_F(AvmMiniExecutionTests, nestedInternalCalls)
auto bytecode = hex_to_bytes(bytecode_hex);
auto instructions = Deserialization::parse(bytecode);

EXPECT_EQ(instructions.size(), 12);
EXPECT_THAT(instructions, SizeIs(12));

// Expected sequence of opcodes
std::vector<OpCode> const opcode_sequence{ OpCode::SET, OpCode::SET,
Expand Down Expand Up @@ -406,23 +403,19 @@ TEST_F(AvmMiniExecutionTests, jumpAndCalldatacopy)
auto bytecode = hex_to_bytes(bytecode_hex);
auto instructions = Deserialization::parse(bytecode);

EXPECT_EQ(instructions.size(), 5);
EXPECT_THAT(instructions, SizeIs(5));

// We test parsing steps for CALLDATACOPY and JUMP.

// CALLDATACOPY
EXPECT_EQ(instructions.at(0).op_code, OpCode::CALLDATACOPY);
EXPECT_EQ(instructions.at(0).operands.size(), 3);

auto operands = instructions.at(0).operands;
EXPECT_EQ(std::get<uint32_t>(operands.at(0)), 0);
EXPECT_EQ(std::get<uint32_t>(operands.at(1)), 2);
EXPECT_EQ(std::get<uint32_t>(operands.at(2)), 10);
EXPECT_THAT(operands, ElementsAre(VariantWith<uint32_t>(0), VariantWith<uint32_t>(2), VariantWith<uint32_t>(10)));

// JUMP
EXPECT_EQ(instructions.at(1).op_code, OpCode::JUMP);
EXPECT_EQ(instructions.at(1).operands.size(), 1);
EXPECT_EQ(std::get<uint32_t>(instructions.at(1).operands.at(0)), 3);
EXPECT_THAT(instructions.at(1).operands, ElementsAre(VariantWith<uint32_t>(3)));

auto trace = Execution::gen_trace(instructions, std::vector<FF>{ 13, 156 });

Expand Down

0 comments on commit ef4ba2e

Please sign in to comment.