mirror of
https://github.com/jorenchik/testing-spring-2024.git
synced 2025-10-21 20:10:36 +00:00
Merge branch 'refactor'
This commit is contained in:
commit
9eb1bf48ff
@ -9,17 +9,17 @@ project(
|
|||||||
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
add_subdirectory(Cppunit)
|
add_subdirectory(src/cppunit)
|
||||||
add_subdirectory(Prep)
|
add_subdirectory(src/prep)
|
||||||
|
|
||||||
add_executable(main main.cpp)
|
add_executable(main src/main.cpp)
|
||||||
add_library(mainLib main_lib.cpp main_lib.h)
|
add_library(lib src/lib.cpp src/lib.hh)
|
||||||
target_link_libraries(main PUBLIC mainLib)
|
target_link_libraries(main PUBLIC lib)
|
||||||
target_link_libraries(mainLib PUBLIC prepLib)
|
target_link_libraries(lib PUBLIC prepLib)
|
||||||
target_include_directories(main PUBLIC "${PROJECT_BINARY_DIR}"
|
target_include_directories(main PUBLIC "${PROJECT_BINARY_DIR}"
|
||||||
"${PROJECT_SOURCE_DIR}/Prep")
|
"${PROJECT_SOURCE_DIR}/prep")
|
||||||
target_include_directories(mainLib PUBLIC "${PROJECT_BINARY_DIR}"
|
target_include_directories(lib PUBLIC "${PROJECT_BINARY_DIR}"
|
||||||
"${PROJECT_SOURCE_DIR}/Prep")
|
"${PROJECT_SOURCE_DIR}/prep")
|
||||||
|
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
@ -28,8 +28,8 @@ FetchContent_Declare(
|
|||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(googletest)
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
|
||||||
add_executable(runtests test.cpp)
|
add_executable(runtests src/test.cpp)
|
||||||
target_link_libraries(runtests PUBLIC mainLib gtest gtest_main)
|
target_link_libraries(runtests PUBLIC lib gtest gtest_main)
|
||||||
target_include_directories(runtests PUBLIC "${PROJECT_BINARY_DIR}")
|
target_include_directories(runtests PUBLIC "${PROJECT_BINARY_DIR}")
|
||||||
|
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
@ -1 +0,0 @@
|
|||||||
add_library(cppunit cppunit.cpp cppunit.h)
|
|
||||||
@ -1 +0,0 @@
|
|||||||
add_library(prepLib Prep.cpp Prep.h)
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
#include "Prep.h"
|
|
||||||
|
|
||||||
int add(int a, int b) { return a + b; }
|
|
||||||
1
src/cppunit/CMakeLists.txt
Normal file
1
src/cppunit/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
add_library(cppunit cppunit.cpp cppunit.hh)
|
||||||
@ -12,32 +12,32 @@
|
|||||||
// CPlusPlusUnit - C++ Unit testing TDD framework (github.com/cppunit/cppunit)
|
// CPlusPlusUnit - C++ Unit testing TDD framework (github.com/cppunit/cppunit)
|
||||||
class Cppunit {
|
class Cppunit {
|
||||||
public:
|
public:
|
||||||
#define CHECK(a, b) \
|
#define CHECK(a, b) check<long long>(a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__);
|
||||||
check<long long>(a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__);
|
#define CHECKT(a) check<bool>(a, true, #a, "true", __FILE__, __LINE__, __FUNCTION__);
|
||||||
#define CHECKT(a) \
|
|
||||||
check<bool>(a, true, #a, "true", __FILE__, __LINE__, __FUNCTION__);
|
|
||||||
#define CHECKS(a, b) check<cs>(a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__);
|
#define CHECKS(a, b) check<cs>(a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__);
|
||||||
typedef const std::string &cs;
|
typedef const std::string &cs;
|
||||||
int checks, fails;
|
int checks, fails;
|
||||||
std::ostringstream serr;
|
std::ostringstream serr;
|
||||||
std::istringstream *in;
|
std::istringstream *in;
|
||||||
Cppunit() { checks = fails = 0; }
|
|
||||||
|
Cppunit() {
|
||||||
|
checks = fails = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void test_cin(cs s) {
|
void test_cin(cs s) {
|
||||||
in = new std::istringstream(s);
|
in = new std::istringstream(s);
|
||||||
std::cin.rdbuf(in->rdbuf());
|
std::cin.rdbuf(in->rdbuf());
|
||||||
}
|
}
|
||||||
|
|
||||||
void fail_hdr(cs stra, cs strb, cs file, int line, cs func) {
|
void fail_hdr(cs stra, cs strb, cs file, int line, cs func) {
|
||||||
serr << "=================================================="
|
serr << "==================================================" << std::endl;
|
||||||
<< std::endl;
|
|
||||||
serr << "FAIL: " << func << std::endl;
|
serr << "FAIL: " << func << std::endl;
|
||||||
serr << "--------------------------------------------------"
|
serr << "--------------------------------------------------" << std::endl;
|
||||||
<< std::endl;
|
serr << "File \"" << file << "\", line " << line << " in " << func << std::endl;
|
||||||
serr << "File \"" << file << "\", line " << line << " in " << func
|
|
||||||
<< std::endl;
|
|
||||||
serr << " Checking " << stra << " == " << strb << std::endl;
|
serr << " Checking " << stra << " == " << strb << std::endl;
|
||||||
}
|
}
|
||||||
template <typename T>
|
|
||||||
void check(T a, T b, cs stra, cs strb, cs file, int line, cs func) {
|
template <typename T> void check(T a, T b, cs stra, cs strb, cs file, int line, cs func) {
|
||||||
checks++;
|
checks++;
|
||||||
if (a == b) {
|
if (a == b) {
|
||||||
std::cout << ".";
|
std::cout << ".";
|
||||||
@ -46,27 +46,32 @@ class Cppunit {
|
|||||||
fails++;
|
fails++;
|
||||||
std::cout << "F";
|
std::cout << "F";
|
||||||
fail_hdr(stra, strb, file, line, func);
|
fail_hdr(stra, strb, file, line, func);
|
||||||
serr << " Error: \"" << a << "\" ! = \"" << b << "\"" << std::endl
|
serr << " Error: \"" << a << "\" ! = \"" << b << "\"" << std::endl << std::endl;
|
||||||
<< std::endl;
|
|
||||||
}
|
}
|
||||||
virtual void single_test() {}
|
|
||||||
virtual void test_list() { single_test(); }
|
virtual void single_test() {
|
||||||
double dclock() { return double(clock()) / CLOCKS_PER_SEC; }
|
}
|
||||||
|
|
||||||
|
virtual void test_list() {
|
||||||
|
single_test();
|
||||||
|
}
|
||||||
|
|
||||||
|
double dclock() {
|
||||||
|
return double(clock()) / CLOCKS_PER_SEC;
|
||||||
|
}
|
||||||
|
|
||||||
int status() {
|
int status() {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
if (fails)
|
if (fails) std::cout << serr.str();
|
||||||
std::cout << serr.str();
|
std::cout << "--------------------------------------------------" << std::endl;
|
||||||
std::cout << "--------------------------------------------------"
|
std::cout << "Ran " << checks << " checks in " << dclock() << "s" << std::endl << std::endl;
|
||||||
<< std::endl;
|
|
||||||
std::cout << "Ran " << checks << " checks in " << dclock() << "s"
|
|
||||||
<< std::endl
|
|
||||||
<< std::endl;
|
|
||||||
if (fails)
|
if (fails)
|
||||||
std::cout << "FAILED (failures=" << fails << ")";
|
std::cout << "FAILED (failures=" << fails << ")";
|
||||||
else
|
else
|
||||||
std::cout << "OK" << std::endl;
|
std::cout << "OK" << std::endl;
|
||||||
return fails > 0;
|
return fails > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int run() {
|
int run() {
|
||||||
std::streambuf *ocin = std::cin.rdbuf();
|
std::streambuf *ocin = std::cin.rdbuf();
|
||||||
test_list();
|
test_list();
|
||||||
13
src/lib.cpp
Normal file
13
src/lib.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "lib.hh"
|
||||||
|
|
||||||
|
#include "prep/prep.hh"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
std::cout << add(2, 2) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int functionToTest(int a) {
|
||||||
|
return a * 2;
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
#include "main_lib.h"
|
#include "lib.hh"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|||||||
1
src/prep/CMakeLists.txt
Normal file
1
src/prep/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
add_library(prepLib prep.cpp prep.hh)
|
||||||
5
src/prep/prep.cpp
Normal file
5
src/prep/prep.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "prep.hh"
|
||||||
|
|
||||||
|
int add(int a, int b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
@ -11,10 +11,34 @@
|
|||||||
int add(int a, int b);
|
int add(int a, int b);
|
||||||
|
|
||||||
// All IDs are uint32_t
|
// All IDs are uint32_t
|
||||||
enum EventType { PHASE_CHANGE, ACTION, ROOM_STATE_CHANGE, PLAYER_STATE_CHANGE };
|
enum EventType {
|
||||||
enum Action { KILL, VOTE, INVESTIGATE, HEAL, PROTECT };
|
PHASE_CHANGE,
|
||||||
enum RoomStatus { AWAITING_START, IN_PROGRESS, STOPPED, ENDED };
|
ACTION,
|
||||||
enum PlayerStatus { KICKED, ALIVE, DEAD, VOTED_OUT };
|
ROOM_STATE_CHANGE,
|
||||||
|
PLAYER_STATE_CHANGE,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Action {
|
||||||
|
KILL,
|
||||||
|
VOTE,
|
||||||
|
INVESTIGATE,
|
||||||
|
HEAL,
|
||||||
|
PROTECT,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum RoomStatus {
|
||||||
|
AWAITING_START,
|
||||||
|
IN_PROGRESS,
|
||||||
|
STOPPED,
|
||||||
|
ENDED,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum PlayerStatus {
|
||||||
|
KICKED,
|
||||||
|
ALIVE,
|
||||||
|
DEAD,
|
||||||
|
VOTED_OUT,
|
||||||
|
};
|
||||||
|
|
||||||
struct Role;
|
struct Role;
|
||||||
struct Player;
|
struct Player;
|
||||||
18
src/test.cpp
18
src/test.cpp
@ -1,9 +1,17 @@
|
|||||||
#include "Prep.h"
|
#include "lib.hh"
|
||||||
#include "main_lib.h"
|
#include "prep/prep.hh"
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
// TEST(TestSuiteName, testName) {...}
|
// TEST(TestSuiteName, testName) {...}
|
||||||
TEST(ProgramTest, testFunction) { EXPECT_EQ(functionToTest(4), 8); }
|
TEST(ProgramTest, testFunction) {
|
||||||
TEST(ProgramTest, testFunctionShouldFail) { EXPECT_EQ(functionToTest(4), 12); }
|
EXPECT_EQ(functionToTest(4), 8);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PrepTest, testAddFunc) { EXPECT_EQ(add(4, 2), 6); }
|
TEST(ProgramTest, testFunctionShouldFail) {
|
||||||
|
EXPECT_EQ(functionToTest(4), 12);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PrepTest, testAddFunc) {
|
||||||
|
EXPECT_EQ(add(4, 2), 6);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user