Home > Blockchain >  Assert function in C using cassert function - How can I declare the variables in the test function
Assert function in C using cassert function - How can I declare the variables in the test function

Time:12-08

#include <iostream>
#include <string>
#include <cassert>

using namespace std;

void test();

void test() { //my attempted test function in order to ensure the program works correctly

string answ1 = "noooooob";
string answ2 = "saaaaaadie";
string answ3 = "trish";

assert(answer(answ1) == "nob"); //this is where i get the compile error that "answer" not declared
assert(answer(answ2) == "sadie");
assert(answer(answ3) == "trish");

cout << "All test cases passed!" << endl;

}

int main() {

    int i = 0;
    string output;
    string input;
    char x;

    getline(cin, input); //input

    output  = input[0];
    x = input[0];


    while (i < input.length()) {

        i  ;
        if (input [i] != x) {
            output  = input[i];
        }
        x = input[i];
    }

    cout << output << endl;

    test();

    return 0;
}

All I'm really asking is how to declare the "answer" variable in the assert function. Maybe I'm doing it completely wrong, and my apologies if so. I'm pretty new to the coding scene. Do I need to make a string that would loop through the current for loop in my main function? Any help is much appreciated.

CodePudding user response:

  1. Either you forgot to include a header where you define a class or a function with name answer
  2. or you don't need it and just comapre strings:
assert(answ1 == "nob"); // operator== kicks in
assert(answ2 == "sadie");
assert(answ3 == "trish");

You should be aware that assert is a debug-only feature. It compiles to nothing in release mode.
You should probably reevaluate your approach for unit testing as the comments suggest (GTest would be fine).

  •  Tags:  
  • c
  • Related