Commit 38cc1faf authored by James Knee's avatar James Knee
Browse files

lab2 basic tests

parent 60a6f174
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+4 −0
Original line number Diff line number Diff line

.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/settings.json

ci_cd/lab2.yml

0 → 100644
+99 −0
Original line number Diff line number Diff line
### Lab 2
prebuild_lab_2:
  stage: lab1
  script:
    - echo "Changes Detected (if any):";
    - git diff --name-only $CI_COMMIT_SHA~ $CI_COMMIT_SHA || echo "No changes found";
    - |
      # Check if lab2.cpp exists
      if [ ! -f "lab2.cpp" ]; then
        echo "lab2.cpp does not exist";
        exit 1;
      fi
      
      # Check if lab0.cpp has been merged into master
      git fetch origin master  # Fetch the latest master branch
      if git ls-tree -r origin/master --name-only | grep -q 'lab0.cpp'; then
        echo "Lab 0 complete";
      else
        echo "Please have lab 0 merged first";
        exit 1;
      fi

      # Check if lab1.cpp has been merged into master
      if git ls-tree -r origin/master --name-only | grep -q 'lab1.cpp'; then
        echo "Lab 1 complete";
      else
        echo "Please have lab 1 merged first";
        exit 1;
      fi
    - git clone https://agile.bu.edu/gitlab/configs/ec327/lab-configs/current.git base_1
  artifacts:
    paths:
      - base_2
  rules:
    - if: '$CI_COMMIT_REF_NAME == "lab2"'
  tags: [c++-17]

prebuild_lab_2_SKIP:
  stage: lab2
  script:
    - echo "SKIPPING Lab 2 checks because the branch name is not 'lab2'"
  rules:
    - if: '$CI_COMMIT_REF_NAME != "lab2"'
  tags: [c++-17]

compile_lab_2:
  stage: lab2
  script:
    - echo "Compiling lab2.cpp"
    - g++ -c lab2.cpp
  artifacts:
    paths:
      - base_2
  dependencies:
    - prebuild_lab_2
  rules:
    - if: '$CI_COMMIT_REF_NAME == "lab2"'
  tags: [c++-17]

lint_lab_2:
  stage: lab2
  script:
    - echo "Static code check of lab2.cpp"
    - cppcheck --check-config --enable=all --inconclusive --error-exitcode=1 lab2.cpp tests/LabTwo.cpp
  allow_failure: false
  artifacts:
    paths:
      - base_2
  dependencies:
    - prebuild_lab_2
  rules:
    - if: '$CI_COMMIT_REF_NAME == "lab2"'
  tags: [cppcheck]

test_lab_2:
  stage: lab2
  script:
    - echo "Testing Lab 2";
    - git clone https://agile.bu.edu/gitlab/configs/ec327/lab-configs/current.git base_2
    - cp lab2.cpp base_2/tests
    - cd base_2/tests
    - make lab2
  dependencies:
    - compile_lab_2
  rules:
    - if: '$CI_COMMIT_REF_NAME == "lab2"'
  tags: [c++-17]

# trigger internal tests only upon a merge request into master
internal_tests_lab_2:
  stage: tests
  script:
    - git clone https://trachten-gitlab:$INTERNAL_LAB_TESTS@agile.bu.edu/gitlab/ec327/ec327-staff/lab-tests.git
    - cp -f lab2.cpp lab-tests/internal_tests/
    - cd lab-tests/internal_tests
    - make lab2
  rules:
    - if: '$CI_COMMIT_REF_NAME == "lab2" && $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
  tags: [ c++-17 ]
+12 −2
Original line number Diff line number Diff line
@@ -10,12 +10,12 @@
 * @return A pointer to the third largest value of [arr]
 *
 * @example 0
 * int A[] = {1,5,3,2}
 * double A[] = {1,5,3,2}
 * labTwo_pThree(A,4) returns a pointer to 2 (i.e., A+3) because it is
 * the third largest element in the array.
 *
 * @example 1
 * int B[] = {3,1,4,1,5,9,2,6,5,3,5,6}
 * double B[] = {3,1,4,1,5,9,2,6,5,3,5,6}
 * labTwo_pThree(A,12) returns a pointer to 6 (i.e., either A+7 or A+11).
 */
double* labTwo_pThree(double* arr, int size);
@@ -26,6 +26,16 @@ double* labTwo_pThree(double* arr, int size);
 * @param arr2 Another null-terminated array of characters.
 * @param result An unallocated array of characters.
 * 
 * @example 0
 * char *result0;
 * const char arr00[] = {'H','e','l','l','o','\0'};
 * const char arr01[] = {'W','o','r','l','d','\0'};
 * 
 * @example 1
 * char *result1;
 * const char arr10[] = {'B','o','s','t','o','n','\0'};
 * const char arr11[] = {'U','n','i','v','e','r','s','i','t','y','\0'};
 *
 * This method concatenates [arr1] and [arr2] into [result]. The array
 * [result] must be sufficiently allocated with "new" to contain the non-null
 * contents of [arr1], [arr2], and a terminating null character.

tests/LabTwo.cpp

0 → 100644
+201 −0
Original line number Diff line number Diff line
/**
** Created by James Knee on 10/21/24.
** With help from ChatGPT
*/


#include <iostream>
#include <string>

#include "../problems/LabtWO_ChapterSix.h"
#include "../problems/LabTwo_ChapterSeven.h"

using namespace std;

bool test_labOne_pZero() {
    // simple, but incomplete tests

    int arr0 [] = {1,2,3};

    int arr1 [] = {1,2,3,4};

    //test 0
    if(labTwo_pZero(arr0,3) != 9){
        cout << "Test 0a failed" << endl;
        return false;
    }
    //test 1
    if(labTwo_pZero(arr1,4) != 14){
        cout << "Test 0ab failed" << endl;
        return false;
    }
    // All tests passed
    return true;
}

bool test_labOne_pOne() {
    

    int A0[] = {1,4,9,16,9,7,4,9,11};
    int A1[] = {11,1,4,9,16,9,7,4,9};

    int B0[] = {1,4,9,16,9,7,4,9,11};
    int B1[] = {11,11,7,9,16,4,1,4,9};

    //test 0
    if (!labTwo_pOne(A0,A1,9)){
        cout << "Test 1a failed" << endl;
        return false;
    }
    //test 1
    if(labTwo_pOne(B0,B1,9)){
        cout << "Test 1b failed" << endl;
        return false;
    }
    // All tests passed
    return true;
}

bool test_labOne_pTwo() {
    
    int arr0[] = {1,2,3,5};
    int arr1[] = {2,7,6,9,5,1,4,3,8};
    int arr2[] = {16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1};

    //test 0
    if (labTwo_pTwo(arr0,4)){
        cout << "Test 2a failed" << endl;
        return false;
    }
    //test 1
    if (!labTwo_pTwo(arr1,9)){
        cout << "Test 2b failed" << endl;
        return false;
    }

    //test 2
    if (!labTwo_pTwo(arr2,16)){
        cout << "Test 2c failed" << endl;
        return false;
    }

    // All tests passed
    return true;
}

bool test_labOne_pThree() {
    
    double arr0[] = {1,5,3,2};
    double arr1[] = {3,1,4,1,5,9,2,6,5,3,5,6};

    double *test0 = labTwo_pThree(arr0,4);
    double *test1 = labTwo_pThree(arr1,12);

    //test 0
    if (*test0 != 2){
        cout << "Test 3a failed" << endl;
        return false;
    }

    //test 1
    if (*test1 != 6){
        cout << "Test 3b failed" << endl;
        return false;
    }

    // All tests passed
    return true;
}

bool test_labOne_pFour() {

    char *result0;
    const char arr00[] = {'H','e','l','l','o','\0'};
    const char arr01[] = {'W','o','r','l','d','\0'};

    char *result1;
    const char arr10[] = {'B','o','s','t','o','n','\0'};
    const char arr11[] = {'U','n','i','v','e','r','s','i','t','y','\0'};

    labTwo_pFour(arr00,arr01,result0);
    labTwo_pFour(arr10,arr11,result1);

    //test 0
    if (strcmp(result0,"HelloWorld") != 0){
        cout << "Test 4a failed" << endl;
        return false;
    }

    //test 1
    if (strcmp(result1,"BostonUniversity") != 0){
        cout << "Test 4b failed" << endl;
        return false;
    }
    
    //All tests passed
    return true;
}

bool test_labOne_pFive() {

    const char *src0 = "Hatty";
    const char *seek0 = "t";
    const char *replace0 = "p";

    const char *src1 = "Hatty";
    const char *seek1 = "tt";
    const char *replace1 = "ppil";

    const char *src2 = "Happy";
    const char *seek2 = "p";
    const char *replace2 = "";

    
    char *test0 = labTwo_pFive(src0,seek0,replace0);
    char *test1 = labTwo_pFive(src1,seek1,replace1);
    char *test2 = labTwo_pFive(src2,seek2,replace2);

    //test 0
    if (strcmp(test0,"Happy") != 0){
        cout << "Test 5a failed" << endl;
        return false;
    }
    
    //test 1
    if (strcmp(test1,"Happily") != 0){
        cout << "Test 5b failed" << endl;
        return false;
    }

    //test 2
    if (strcmp(test2,"Hay") != 0){
        cout << "Test 5c failed" << endl;
        return false;
    }

    //All tests passed
    return true;
}


int main() {
    bool t0 = test_labOne_pZero();
    bool t1 = test_labOne_pOne();
    bool t2 = test_labOne_pTwo();
    bool t3 = test_labOne_pThree();
    bool t4 = test_labOne_pFour();
    bool t5 = test_labOne_pFive();

    cout << "pZero: " << (t0 ? "passed" : "failed") << endl;
    cout << "pOne: " << (t1 ? "passed" : "failed") << endl;
    cout << "pTwo: " << (t2 ? "passed" : "failed") << endl;
    cout << "pThree: " << (t3 ? "passed" : "failed") << endl;
    cout << "pFour: " << (t4 ? "passed" : "failed") << endl;
    cout << "pFive: " << (t5 ? "passed" : "failed") << endl;

    if (t0 && t1 && t2 && t3 && t4 && t5) { // all tests passed
        exit(0);  // passed
    } else {
        exit(-1); // failed
    }
}
 No newline at end of file
+9 −2
Original line number Diff line number Diff line
@@ -8,20 +8,23 @@ CXXFLAGS = -Wall -std=c++17
COMMON_SRCS = Support/Common.cpp
LAB0_SRCS = LabZero.cpp lab0.cpp $(COMMON_SRCS)
LAB1_SRCS = LabOne.cpp lab1.cpp $(COMMON_SRCS)
LAB2_SRCS = LabTwo.cpp lab2.cpp 

# Object files
COMM_OBJS = Support/Common.o
LAB0_OBJS = LabZero.o lab0.o $(COMM_OBJS)
LAB1_OBJS = LabOne.o lab1.o $(COMM_OBJS)
LAB2_OBJS = LabTwo.o lab2.o 
OBJS = $(COMM_OBJS) $(LAB0_OBJS) $(LAB1_OBJS)

# Executable names
LAB0_EXEC = lab0
LAB1_EXEC = lab1
EXEC = $(LAB0_EXEC) $(LAB1_EXEC)
LAB2_EXEC = lab2
EXEC = $(LAB0_EXEC) $(LAB1_EXEC) $(LAB2_EXEC)

# Default target to build the executable
all: $(LAB0_EXEC) $(LAB1_EXEC)
all: $(LAB0_EXEC) $(LAB1_EXEC) $(LAB2_EXEC)

# Rule to build the executable from object files
$(LAB0_EXEC): $(LAB0_OBJS) Makefile
@@ -32,6 +35,10 @@ $(LAB1_EXEC): $(LAB1_OBJS) Makefile
	$(CXX) $(CXXFLAGS) -o $(LAB1_EXEC) $(LAB1_OBJS)
	./$(LAB1_EXEC)

$(LAB2_EXEC): $(LAB2_OBJS) Makefile
	$(CXX) $(CXXFLAGS) -o $(LAB2_EXEC) $(LAB2_OBJS)
	./$(LAB2_EXEC)

# Rules to build object files from source files, with dependency on the Common.h header
%.o: %.cpp Support/Common.h Makefile
	$(CXX) $(CXXFLAGS) -c $< -o $@