Thread: Detailed asserts

  1. #1
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115

    Detailed asserts

    Greetings,

    Until now both clang/gcc compilers do not show detailed/verbose info for asserts. Consider:

    Code:
    // detailed asserts
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    
    int main(void)
    {
        int a=1;
        int b=2;
        assert(a==b);
        
        return 0;
    }
    Output:
    Code:
    clang:
    a.out: assert.c:11: int main(void): Assertion `a==b' failed.
    Aborted (core dumped)
    
    gcc:
    a.out: assert.c:11: main: Assertion `a==b' failed.
    Aborted (core dumped)
    I want such addition:
    Code:
    a is 1
    b is 2
    
    or:
    (1==2) failed
    Considering CUnit testing library:

    Code:
    // Detailed CUnit asserts
    
    
    // http://cunit.sourceforge.net/
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <stddef.h>
    #include <stdnoreturn.h>
    
    
    #include <CUnit/CUnit.h>
    #include <CUnit/Basic.h>
    #include <CUnit/Console.h>
    
    
    int maxi(int i1, int i2)
    {
        return (i1 > i2) ? -i1 : -i2; // wrong
    }
    
    
    void test_maxi(void)
    { 
        CU_ASSERT_EQUAL(maxi(0,2) , 2);
        CU_ASSERT_EQUAL(maxi(-1,-2) , -1);
        CU_ASSERT_EQUAL(maxi(2,2) , 2);
    }
    
    
    int main(void)
    {
        CU_initialize_registry();
    
    
        CU_pSuite suite1 = CU_add_suite("suite1", NULL, NULL);
    
    
        CU_add_test(suite1, "test_maxi", test_maxi);
    
    
        CU_basic_set_mode(CU_BRM_VERBOSE); 
    
    
        //CU_automated_run_tests();
        CU_basic_run_tests();
    
    
        CU_cleanup_registry();
    
    
        return 0;
    }
    Output:
    Code:
    
    
    
         CUnit - A unit testing framework for C - Version 2.1-3
         http://cunit.sourceforge.net/
    
    
    
    
    Suite: suite1
      Test: test_maxi ...FAILED
        1. cu_assert.c:21  - CU_ASSERT_EQUAL(maxi(0,2),2)
        2. cu_assert.c:22  - CU_ASSERT_EQUAL(maxi(-1,-2),-1)
        3. cu_assert.c:23  - CU_ASSERT_EQUAL(maxi(2,2),2)
    
    
    Run Summary:    Type  Total    Ran Passed Failed Inactive
                  suites      1      1    n/a      0        0
                   tests      1      1      0      1        0
                 asserts      3      3      0      3      n/a
    
    
    Elapsed time =    0.000 seconds
    I want computed values of maxi too. But they are not given in the above output. Some other testing libraries do it.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    788
    I imagine it would be difficult if not impossible for the assert function/macro to do such a thing. You could do something like this, though:

    Code:
    assert(a == b || (fprintf(stderr, "a is %d\nb is %d\n", a, b), 0));
    which should print out the "a is 1" and "b is 2" lines before the program is aborted. You could even wrap the whole (fprintf(...), 0) expression in a macro to make the assertion output look cleaner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to display detailed information of a directory ?
    By blackendstars in forum Linux Programming
    Replies: 3
    Last Post: 03-09-2013, 11:31 PM
  2. Errors, exceptions and asserts
    By Mario F. in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2006, 09:00 AM
  3. Asserts and their ranges
    By Matamoros123 in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2006, 12:32 PM
  4. Map editor asserts in line 175 of doctempl.cpp
    By VirtualAce in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2005, 09:07 PM
  5. Detailed newbie tutorials.
    By Hidden-Shadow in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2003, 03:07 AM

Tags for this Thread