diff --git a/src/contracts/Flipper.h b/src/contracts/Flipper.h new file mode 100644 index 000000000..ea4fce502 --- /dev/null +++ b/src/contracts/Flipper.h @@ -0,0 +1,23 @@ +// src/contracts/Flipper.h + +struct FLIPPER { + bit value; +}; + +// Input/output para a função flip +struct Flip_input {}; +struct Flip_output { + bit value; +}; + +// Função pública para inverter o valor salvo +PUBLIC_FUNCTION(flip) +{ + state.value = !state.value; + output.value = state.value; +} + +// Registro das interfaces +void REGISTER_USER_FUNCTIONS_AND_PROCEDURES() { + REGISTER_USER_FUNCTION(flip, 1); +} \ No newline at end of file diff --git a/src/contracts/HashStore.h b/src/contracts/HashStore.h new file mode 100644 index 000000000..98c2d044d --- /dev/null +++ b/src/contracts/HashStore.h @@ -0,0 +1,39 @@ +// src/contracts/HashStore.h + +struct HASHSTORE { + Array hash; +}; + +// Input/output para o procedimento de setar o hash +struct SetHash_input { + Array hash; +}; +struct SetHash_output {}; + +// Input/output para a função de consultar o hash +struct GetHash_input {}; +struct GetHash_output { + Array hash; +}; + +// Procedimento público para setar o hash +PUBLIC_PROCEDURE(SetHash) +{ + for (uint32 i = 0; i < 32; i += 1) { + state.hash[i] = input.hash[i]; + } +} + +// Função pública para consultar o hash +PUBLIC_FUNCTION(GetHash) +{ + for (uint32 i = 0; i < 32; i += 1) { + output.hash[i] = state.hash[i]; + } +} + +// Registro das interfaces +void REGISTER_USER_FUNCTIONS_AND_PROCEDURES() { + REGISTER_USER_PROCEDURE(SetHash, 1); + REGISTER_USER_FUNCTION(GetHash, 2); +} \ No newline at end of file diff --git a/test/contract_flipper.cpp b/test/contract_flipper.cpp new file mode 100644 index 000000000..5b91b3181 --- /dev/null +++ b/test/contract_flipper.cpp @@ -0,0 +1,27 @@ +#include "gtest/gtest.h" +#include "contracts/Flipper.h" + +TEST(Flipper, FlipFunction) +{ + // Instancia o estado do contrato + FLIPPER state = {}; + state.value = 0; // começa como falso + + // Chama flip pela primeira vez + Flip_input input = {}; + Flip_output output = {}; + QpiContextFunctionCall qpi; + flip(state, input, output, qpi); + EXPECT_EQ(state.value, 1); + EXPECT_EQ(output.value, 1); + + // Chama flip novamente + flip(state, input, output, qpi); + EXPECT_EQ(state.value, 0); + EXPECT_EQ(output.value, 0); + + // Chama flip mais uma vez + flip(state, input, output, qpi); + EXPECT_EQ(state.value, 1); + EXPECT_EQ(output.value, 1); +} \ No newline at end of file diff --git a/test/contract_hashstore.cpp b/test/contract_hashstore.cpp new file mode 100644 index 000000000..de4057635 --- /dev/null +++ b/test/contract_hashstore.cpp @@ -0,0 +1,30 @@ +#include "gtest/gtest.h" +#include "contracts/HashStore.h" + +TEST(HashStore, SetAndGetHash) +{ + // Instancia o estado do contrato + HASHSTORE state = {}; + + // Cria um hash de teste + SetHash_input setInput = {}; + for (uint32_t i = 0; i < 32; ++i) + setInput.hash[i] = static_cast(i); + + SetHash_output setOutput = {}; + + // Chama o procedimento para setar o hash + // Simula o contexto do QPI (pode ser vazio para teste simples) + QpiContextProcedureCall qpi_proc; + SetHash(state, setInput, setOutput, qpi_proc); + + // Consulta o hash salvo + GetHash_input getInput = {}; + GetHash_output getOutput = {}; + QpiContextFunctionCall qpi_func; + GetHash(state, getInput, getOutput, qpi_func); + + // Verifica se o hash retornado é igual ao que foi setado + for (uint32_t i = 0; i < 32; ++i) + EXPECT_EQ(getOutput.hash[i], setInput.hash[i]); +} \ No newline at end of file