#include <Account.h>
#include <gtest/gtest.h>
TEST(Account, Banking){
Account test(0,0);
ASSERT_EQ(test.GetBalance(), 0);
ASSERT_THROW(test.ChangeBalance(100), std::runtime_error);
test.Lock();
ASSERT_NO_THROW(test.ChangeBalance(100));
ASSERT_EQ(test.GetBalance(), 100);
ASSERT_THROW(test.Lock(), std::runtime_error);
test.Unlock();
ASSERT_THROW(test.ChangeBalance(100), std::runtime_error);
}
#include <Account.h>
#include <Transaction.h>
#include <gtest/gtest.h>
TEST(Transaction, Banking){
const int base_A = 5000, base_B = 5000, base_fee = 100;
Account Alice(0,base_A), Bob(1,base_B);
Transaction test_tran;
ASSERT_EQ(test_tran.fee(), 1);
test_tran.set_fee(base_fee);
ASSERT_EQ(test_tran.fee(), base_fee);
ASSERT_THROW(test_tran.Make(Alice, Alice, 1000), std::logic_error);
ASSERT_THROW(test_tran.Make(Alice, Bob, -50), std::invalid_argument);
ASSERT_THROW(test_tran.Make(Alice, Bob, 50), std::logic_error);
if (test_tran.fee()*2-1 >= 100)
ASSERT_EQ(test_tran.Make(Alice, Bob, test_tran.fee()*2-1), false);
Alice.Lock();
ASSERT_THROW(test_tran.Make(Alice, Bob, 1000), std::runtime_error);
Alice.Unlock();
ASSERT_EQ(test_tran.Make(Alice, Bob, 1000), true);
ASSERT_EQ(Bob.GetBalance(), base_B+1000);
ASSERT_EQ(Alice.GetBalance(), base_A-1000-base_fee);
ASSERT_EQ(test_tran.Make(Alice, Bob, 3900), false);
ASSERT_EQ(Bob.GetBalance(), base_B+1000);
ASSERT_EQ(Alice.GetBalance(), base_A-1000-base_fee);
}
build.yml
cmake_minimum_required(VERSION 3.22)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(BUILD_TESTS "Build tests" OFF)
if(BUILD_TESTS)
add_compile_options(--coverage)
endif()
project (banking)
add_library(banking STATIC ${CMAKE_CURRENT_SOURCE_DIR}/banking/Transaction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/banking/Account.cpp)
target_include_directories(banking PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/banking )
target_link_libraries(banking gcov)
if(BUILD_TESTS)
enable_testing()
add_subdirectory(third-party/gtest)
file(GLOB BANKING_TEST_SOURCES tests/*.cpp)
add_executable(check ${BANKING_TEST_SOURCES})
target_link_libraries(check banking gtest_main)
add_test(NAME check COMMAND check)
endif()