refactor: make filenames consistent

Use cpp header files
This commit is contained in:
Kristofers Solo 2024-03-10 18:22:55 +02:00
parent 6df34b5529
commit 6ace621748
15 changed files with 104 additions and 58 deletions

View File

@ -9,17 +9,17 @@ project(
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_subdirectory(Cppunit) add_subdirectory(cppunit)
add_subdirectory(Prep) add_subdirectory(prep)
add_executable(main main.cpp) add_executable(main main.cpp)
add_library(mainLib main_lib.cpp main_lib.h) add_library(lib lib.cpp 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(
@ -29,7 +29,7 @@ FetchContent_Declare(
FetchContent_MakeAvailable(googletest) FetchContent_MakeAvailable(googletest)
add_executable(runtests test.cpp) add_executable(runtests 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)

View File

@ -1 +0,0 @@
add_library(cppunit cppunit.cpp cppunit.h)

View File

@ -1 +0,0 @@
add_library(prepLib Prep.cpp Prep.h)

View File

@ -1,3 +0,0 @@
#include "Prep.h"
int add(int a, int b) { return a + b; }

View File

@ -0,0 +1 @@
add_library(cppunit cppunit.cpp cppunit.hh)

View File

@ -11,33 +11,33 @@
// 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();
@ -75,4 +80,4 @@ class Cppunit {
} }
}; };
#endif // CPPUNIT_H #endif // CPPUNIT_H

13
src/lib.cpp Normal file
View 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;
}

View File

@ -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[]) {

View File

@ -1,7 +0,0 @@
#include "main_lib.h"
#include "Prep.h"
#include <iostream>
void run() { std::cout << add(2, 2) << std::endl; }
int functionToTest(int a) { return a * 2; }

1
src/prep/CMakeLists.txt Normal file
View File

@ -0,0 +1 @@
add_library(prepLib prep.cpp prep.hh)

5
src/prep/prep.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "prep.hh"
int add(int a, int b) {
return a + b;
}

View File

@ -10,10 +10,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;

View File

@ -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);
}