From 2dc6d26f8db22b0899718bd57413445a27e8b527 Mon Sep 17 00:00:00 2001 From: Ari Trachtenberg Date: Sun, 6 Oct 2024 13:50:26 -0400 Subject: [PATCH] Added tests --- ci_cd/.gitlab-ci.yml | 12 ++++++++ ci_cd/problem2.yml | 70 ++++++++++++++++++++++++++++++++++++++++++ ci_cd/problem3.yml | 52 +++++++++++++++++++++++++++++++ ci_cd/problem4.yml | 52 +++++++++++++++++++++++++++++++ tests/Makefile | 37 ++++++++++++++++++++++ tests/testProblem2.cpp | 41 +++++++++++++++++++++++++ tests/testProblem3.cpp | 40 ++++++++++++++++++++++++ tests/testProblem4.cpp | 30 ++++++++++++++++++ 8 files changed, 334 insertions(+) create mode 100644 ci_cd/.gitlab-ci.yml create mode 100644 ci_cd/problem2.yml create mode 100644 ci_cd/problem3.yml create mode 100644 ci_cd/problem4.yml create mode 100644 tests/Makefile create mode 100644 tests/testProblem2.cpp create mode 100644 tests/testProblem3.cpp create mode 100644 tests/testProblem4.cpp diff --git a/ci_cd/.gitlab-ci.yml b/ci_cd/.gitlab-ci.yml new file mode 100644 index 0000000..dfe74e5 --- /dev/null +++ b/ci_cd/.gitlab-ci.yml @@ -0,0 +1,12 @@ +stages: + problem2 + problem3 + problem4 + +include: + - local: 'ci_cd/problem2.yml' # hw3, problem 1 + - local: 'ci_cd/problem3.yml' # hw3, problem 2 + - local: 'ci_cd/problem4.yml' # hw3, problem 2 + +default: + timeout: 5m diff --git a/ci_cd/problem2.yml b/ci_cd/problem2.yml new file mode 100644 index 0000000..c0a09d0 --- /dev/null +++ b/ci_cd/problem2.yml @@ -0,0 +1,70 @@ +prebuild_problem_2: + stage: problem2 + script: + - | + # Check if problem2.cpp exists + if [ ! -f "problem2.cpp" ]; then + echo "problem2.cpp does not exist"; + exit 1; + fi + rules: + - if: '$CI_COMMIT_REF_NAME == "problem2"' + tags: [shell] + +prebuild_problem_2_SKIP: + stage: problem2 + script: + - echo "SKIPPING problem 2 checks because the branch name is not 'problem2'" + rules: + - if: '$CI_COMMIT_REF_NAME != "problem2"' + tags: [shell] + +compile_problem_2: + stage: problem2 + script: + - echo "Compiling problem2.cpp" + - g++ -c problem2.cpp + rules: + - if: '$CI_COMMIT_REF_NAME == "problem2"' + tags: [c++-17] + +lint_problem_2: + stage: problem2 + script: + - echo "Static code check of problem2.cpp" + - cppcheck --std=c++17 --check-config --enable=all --inconclusive --error-exitcode=1 problem2.cpp + allow_failure: false + rules: + - if: '$CI_COMMIT_REF_NAME == "problem2"' + tags: [cppcheck] + +test_problem_2: + stage: problem2 + script: + - echo "Testing problem 2"; + - git clone https://agile.bu.edu/gitlab/configs/ec327/hw-configs/hw2.git base_2 + - cp problem2.cpp base_2/tests + - cd base_2/tests + - make problem2 + - ./problem2 + rules: + - if: '$CI_COMMIT_REF_NAME == "problem2"' + tags: [c++-17] + +#internal_testing_problem_2: +# stage: problem2 +# script: +# - echo "Internal testing problem 2"; +# - git clone https://trachten-gitlab:${INTERNAL_HW_TESTS}@agile.bu.edu/gitlab/ec327/ec327-staff/hw-tests/homework-three-internal.git +# - cp -f problem2.cpp homework-three-internal/internal_tests/ +# - cd homework-three-internal/internal_tests/ +# - make internalTestProblem2 +# - ./internalTestProblem2 > /dev/null 2>&1 +# - RESULT=$(cat output.txt | tail -n 1) +# - curl "https://agile.bu.edu/ec327_scripts/bestScores.pl?key=$MAGIC_KEY&name=$GITLAB_USER_LOGIN&score=$RESULT" +# rules: +# - if: '$CI_COMMIT_REF_NAME == "problem2"' +# after_script: +# - cat homework-two-internal/internal_tests/output.txt +# tags: [c++-17] +# timeout: 12m diff --git a/ci_cd/problem3.yml b/ci_cd/problem3.yml new file mode 100644 index 0000000..6c9859d --- /dev/null +++ b/ci_cd/problem3.yml @@ -0,0 +1,52 @@ +prebuild_problem_3: + stage: problem3 + script: + - | + # Check if problem3.cpp exists + if [ ! -f "problem3.cpp" ]; then + echo "problem3.cpp does not exist"; + exit 1; + fi + rules: + - if: '$CI_COMMIT_REF_NAME == "problem3"' + tags: [shell] + +prebuild_problem_3_SKIP: + stage: problem3 + script: + - echo "SKIPPING problem 3 checks because the branch name is not 'problem3'" + rules: + - if: '$CI_COMMIT_REF_NAME != "problem3"' + tags: [shell] + +compile_problem_3: + stage: problem3 + script: + - echo "Compiling problem3.cpp" + - g++ -c problem3.cpp + rules: + - if: '$CI_COMMIT_REF_NAME == "problem3"' + tags: [c++-17] + +lint_problem_3: + stage: problem3 + script: + - echo "Static code check of problem3.cpp" + - cppcheck --std=c++17 --check-config --enable=all --inconclusive --error-exitcode=1 problem3.cpp + allow_failure: false + rules: + - if: '$CI_COMMIT_REF_NAME == "problem3"' + tags: [cppcheck] + +test_problem_3: + stage: problem3 + script: + - echo "Testing problem 3"; + - git clone https://agile.bu.edu/gitlab/configs/ec337/hw-configs/hw3.git base_3 + - cp problem3.cpp base_3/tests + - cd base_3/tests + - make problem3 + - ./problem3 + rules: + - if: '$CI_COMMIT_REF_NAME == "problem3"' + tags: [c++-17] diff --git a/ci_cd/problem4.yml b/ci_cd/problem4.yml new file mode 100644 index 0000000..30f5e88 --- /dev/null +++ b/ci_cd/problem4.yml @@ -0,0 +1,52 @@ +prebuild_problem_4: + stage: problem4 + script: + - | + # Check if problem4.cpp exists + if [ ! -f "problem4.cpp" ]; then + echo "problem4.cpp does not exist"; + exit 1; + fi + rules: + - if: '$CI_COMMIT_REF_NAME == "problem4"' + tags: [shell] + +prebuild_problem_4_SKIP: + stage: problem4 + script: + - echo "SKIPPING problem 4 checks because the branch name is not 'problem4'" + rules: + - if: '$CI_COMMIT_REF_NAME != "problem4"' + tags: [shell] + +compile_problem_4: + stage: problem4 + script: + - echo "Compiling problem4.cpp" + - g++ -c problem4.cpp + rules: + - if: '$CI_COMMIT_REF_NAME == "problem4"' + tags: [c++-17] + +lint_problem_4: + stage: problem4 + script: + - echo "Static code check of problem4.cpp" + - cppcheck --std=c++17 --check-config --enable=all --inconclusive --error-exitcode=1 problem4.cpp + allow_failure: false + rules: + - if: '$CI_COMMIT_REF_NAME == "problem4"' + tags: [cppcheck] + +test_problem_4: + stage: problem4 + script: + - echo "Testing problem 4"; + - git clone https://agile.bu.edu/gitlab/configs/ec447/hw-configs/hw4.git base_4 + - cp problem4.cpp base_4/tests + - cd base_4/tests + - make problem4 + - ./problem4 + rules: + - if: '$CI_COMMIT_REF_NAME == "problem4"' + tags: [c++-17] diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..b36f473 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,37 @@ +# Makefile, generated with support of ChatGPT + +# Compiler and flags +CXX = g++ +CXXFLAGS = -Wall -std=c++17 + +# Source files +PROBLEM2_SRCS = testProblem2.cpp problem2.cpp +PROBLEM3_SRCS = testProblem3.cpp problem3.cpp +SRCS = $(PROBLEM2_SRCS) $(PROBLEM3_SRCS) + +# Object files +PROBLEM2_OBJS = testProblem2.o problem2.o +PROBLEM3_OBJS = testProblem3.o problem3.o +OBJS = $(PROBLEM2_OBJS) $(PROBLEM3_OBJS) + +# Executable names +PROBLEM2_EXEC = problem2 +PROBLEM3_EXEC = problem3 + +# Default target to build the executable +all: $(PROBLEM2_EXEC) $(PROBLEM3_EXEC) + +# Rule to build the executable from object files +$(PROBLEM2_EXEC): $(PROBLEM2_OBJS) Makefile + $(CXX) $(CXXFLAGS) -o $(PROBLEM2_EXEC) $(PROBLEM2_OBJS) + +$(PROBLEM3_EXEC): $(PROBLEM3_OBJS) Makefile + $(CXX) $(CXXFLAGS) -o $(PROBLEM3_EXEC) $(PROBLEM3_OBJS) + +# Rules to build object files from source files, with dependency on the Common.h header +%.o: %.cpp %.h Makefile + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# Clean target to remove compiled files +clean: + rm -f $(OBJS) $(EXEC) diff --git a/tests/testProblem2.cpp b/tests/testProblem2.cpp new file mode 100644 index 0000000..427922c --- /dev/null +++ b/tests/testProblem2.cpp @@ -0,0 +1,41 @@ +// +// Created by Ari on 10/6/24. +// + +#include +#include +using namespace std; + +// prototype +int longestStreak(string gameBoard); + +bool test_2a() { + string boardA = "x--------\n-x-------\n--x------\n"; + if (longestStreak(boardA)!=3) + return false; + return true; +} + +bool test_2b() { + string boardB = "xxxx----x\n-yyyyy--y\n"; + if (longestStreak(boardB)!=5) + return false; + return true; +} + +int main() { + bool t2a = test_2a(); + bool t2b = test_2b(); + + cout << "Test (a) of problem 2: " << (t2a ? "passed" : "failed") << endl; + cout << "Test (b) of problem 2: " << (t2b ? "passed" : "failed") << endl; + + if (t2a && t2b) { + // all tests passed + exit(0); + } + else { + // at least one test was failed + exit (-1); + } +} diff --git a/tests/testProblem3.cpp b/tests/testProblem3.cpp new file mode 100644 index 0000000..f10fab4 --- /dev/null +++ b/tests/testProblem3.cpp @@ -0,0 +1,40 @@ +// +// Created by Ari on 10/6/24. +// + +#include +#include +#include "../ttt_interface/ttt.h" +using namespace std; + +// prototype +void randomNextMove(string board, SQUARE sideToMove, int& nextMoveRow, int& nextMoveCol); + +bool test_3a() { + int nmr=-1, nmc=-1; + randomNextMove( + "---------\n---------\n---------\n---------\n---------\n", + 'y', + nmr, + nmc); + + if (nmr==-1 || nmc==-1) // i.e., they were not updated + return false; + + return true; +} + +int main() { + bool t3a = test_3a(); + + cout << "Testing problem 3: " << (t3a ? "passed" : "failed") << endl; + + if (t3a) { + // all tests passed + exit(0); + } + else { + // at least one test was failed + exit (-1); + } +} \ No newline at end of file diff --git a/tests/testProblem4.cpp b/tests/testProblem4.cpp new file mode 100644 index 0000000..be04bbe --- /dev/null +++ b/tests/testProblem4.cpp @@ -0,0 +1,30 @@ +// +// Created by Ari on 10/6/24. +// + +#include +#include +#include "../ttt_interface/ttt.h" +using namespace std; + +// prototype declaration +void playGame(); + +bool test_4a() { + // no tests for now +} + +int main() { + bool t4a = test_4a(); + + cout << "Testing problem 4: " << (t4a ? "passed" : "failed") << endl; + + if (t4a) { + // all tests passed + exit(0); + } + else { + // at least one test was failed + exit (-1); + } +} \ No newline at end of file -- GitLab