Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use assert to test for string

Tags:

c

assert

I'm currently trying to test a strcat() function that I wrote myself. Instead of printing the outputs and checking them line by line manually, I've decided to use assert from assert.h. The problem is that assert is showing errors even though the outputs look totally fine. The following is my code:

void mystrcat_test()
{
    char str[BUFSIZ];
    assert(strcmp(str, ""));
    mystrcat(str, "hello");
    assert(strcmo(str, "hello"));
}
like image 296
jon2512chua Avatar asked Oct 01 '10 08:10

jon2512chua


2 Answers

strcmp returns 0 if both strings are same. assert takes 0 (false) as an indication that the test failed and will report error. So the test should be:

assert(strcmp(str, "") == 0);
like image 57
Vijay Mathew Avatar answered Oct 04 '22 08:10

Vijay Mathew


strcmp returns 0 when strings match, so your tests need to be inverted, e.g.

assert(strcmp(str, "hello") == 0);

like image 32
Paul R Avatar answered Oct 04 '22 06:10

Paul R