0

I am trying to work on a C++ tutorial for my college. I tried everything possible, such as importing Catch library in a catch.hpp file, but I always get the error of TEST_CASE not found in scope. Can anyone help me here? I really need to get this to work.

Here's my code,

#include <iostream>
#include <fstream>
#include <string>

std::string FizzBuzz(int number) {
    if (number % 3 == 0 && number % 5 == 0) {
        return "FizzBuzz";
    } else if (number % 3 == 0) {
        return "Fizz";
    } else if (number % 5 == 0) {
        return "Buzz";
    } else {
        return std::to_string(number);
    }
}

int main() {
    TEST_CASE("FIZZ if div by 3", "[Fizz]") {
        REQUIRE(FizzBuzz(3) == "Fizz");
        REQUIRE(FizzBuzz(9) == "Fizz"); 
        REQUIRE(FizzBuzz(15) == "Fizz");    
    }
    
    TEST_CASE("BUZZ if div by 7", "[Buzz]") {
        REQUIRE(FizzBuzz(7) == "Buzz");
        REQUIRE(FizzBuzz(14) == "Buzz");    
        REQUIRE(FizzBuzz(35) == "Buzz");    
    }

    TEST_CASE("FIZZBUZZ if div by 3,7", "[Buzz]") {
        REQUIRE(FizzBuzz(21) == "FizzBuzz");
        REQUIRE(FizzBuzz(42) == "FizzBuzz");    
        REQUIRE(FizzBuzz(63) == "FizzBuzz");    
    }

    TEST_CASE("Return Num if div by 3,7", "[ReturnNum]") {
        REQUIRE(FizzBuzz(13) == "13");
        REQUIRE(FizzBuzz(17) == "17");  
        REQUIRE(FizzBuzz(24) == "24");  
    }

    return 0;
}

And here is a screenshot of the error:

enter image description here

prapin
  • 6,395
  • 5
  • 26
  • 44
James Fang
  • 61
  • 3
  • I don't think so I am just following the powerpoint in this link: https://drive.google.com/file/d/1ge0YS98ntxTD0uBk--vEHf8p28f_Epdz/view . Is TEST_CASE a native method in C++? – James Fang Aug 02 '21 at 20:25
  • Alright, I got rid of the int main() {} constructor imported the catch.hpp, then test_case is spitting out a bunch of nonsense errors: /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x24): undefined reference to `main' /usr/bin/ld: /tmp/ccRrpDzF.o: in function `____C_A_T_C_H____T_E_S_T____0()' – James Fang Aug 02 '21 at 20:31
  • 2
    Be sure to `#define CATCH_CONFIG_MAIN` (see my answer). – Fred Larson Aug 02 '21 at 20:33
  • just did that, that worked well. Thank you! – James Fang Aug 02 '21 at 20:34

1 Answers1

3

TEST_CASE is not something you use within a function, including main(). It is a Catch preprocessor macro that creates a test case, which might be a function or a class or something. And Catch can create a main() for you that runs all of your test cases. Do this by defining CATCH_CONFIG_MAIN in only one translation unit. Your posted code does not show the Catch header, either.

I'd also recommend that you prefer the CHECK macro for testing rather than REQUIRE. REQUIRE aborts the test case immediately at that point, so it is useful for things like null pointer checks. But in cases like this, it obscures further failures. That gets you into fixing failures serially instead of potentially fixing a whole case at once.

Here's what your code should look like:

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include <string>

std::string FizzBuzz(int number) {
    if (number % 3 == 0 && number % 5 == 0) {
        return "FizzBuzz";
    } else if (number % 3 == 0) {
        return "Fizz";
    } else if (number % 5 == 0) {
        return "Buzz";
    } else {
        return std::to_string(number);
    }
}

    TEST_CASE("FIZZ if divisible by 3", "[Fizz]") {
        CHECK(FizzBuzz(3) == "Fizz");
        CHECK(FizzBuzz(9) == "Fizz"); 
        CHECK(FizzBuzz(12) == "Fizz");    
    }
    
    TEST_CASE("BUZZ if divisible by 5", "[Buzz]") {
        CHECK(FizzBuzz(5) == "Buzz");
        CHECK(FizzBuzz(25) == "Buzz");    
        CHECK(FizzBuzz(35) == "Buzz");    
    }

    TEST_CASE("FIZZBUZZ if divisible by 3 and 5", "[FizzBuzz]") {
        CHECK(FizzBuzz(15) == "FizzBuzz");
        CHECK(FizzBuzz(45) == "FizzBuzz");    
        CHECK(FizzBuzz(60) == "FizzBuzz");    
    }

    TEST_CASE("Return Num if not divisible by 3 or 5", "[ReturnNum]") {
        CHECK(FizzBuzz(13) == "13");
        CHECK(FizzBuzz(17) == "17");  
        CHECK(FizzBuzz(26) == "26");  
    }

Try the Catch tutorial for more information.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174