2017-10-17 04:06:44

Hello guys!
I would like to test a program I did in c, but I would like something small and to show errors with accessibility.
Does anyone know of any tool for c so?

2017-10-17 04:55:50

If your talking about unit tests, I'd recommend Catch. It can be found at https://github.com/philsquared/Catch. It allows you to write unit tests and test individual procedures of your application, and reports failures. It even supports telling you duration's if you want to benchmark your application. An example follows:

#include <dlib/config_reader.h>
#include <iostream>
#include <fstream>
#include <vector>
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
using namespace std;
using namespace dlib;
/*
We'll assume we have a configuration file on disk named config.txt with the contents:
# This is an example config file.  Note that # is used to create a comment.

# At its most basic level a config file is just a bunch of key/value pairs.
# So for example:
key1 = value2
dlib = a C++ library

# You can also define "sub blocks" in your config files like so
user1 
{ 
    # Inside a sub block you can list more key/value pairs.  
    id = 42
    name = davis

    # you can also nest sub-blocks as deep as you want
    details 
    { 
        editor = vim
        home_dir = /home/davis
    }
}
user2 { 
    id = 1234
    name = joe
    details { 
        editor = emacs
        home_dir = /home/joe
    }
}

*/
TEST_CASE("Config reader test") {
config_reader cr("config.txt");
// We use the REQUIRE macro to require that a condition be true or false
REQUIRE(cr["key1"]!="" && cr["key1"] == "value2");
// And so on...
}

You'll have noticed that this program doesn't have a main() function like it's supposed to. This is because Catch defines it's own main() function that accepts command-line arguments. (That's what the CATCH_CONFIG_MAIN preprocessor definition does.) You should only define that in one .cpp file; defining it multiple times... won't go well for you big_smile. Next we use the TEST_CASE() macro to create test cases. These test cases can be executed manually via the command-line or will be executed sequentially when the program runs. The syntax of the macro is:
TEST_CASE( test name [, tags ] )
The [, tags] means that tags are optional (as clearly demonstrated here). Test cases can have sections, which then can have sub-sections and so on. You can do this with the SECTION() macro:
SECTION( section name )
The full documentation can be found at https://github.com/philsquared/Catch/bl … Readme.md. The tutorial can be found at https://github.com/philsquared/Catch/bl … torial.md.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2017-10-18 02:57:58 (edited by nyanchan 2017-10-18 16:08:33)

Show errors? Does that mean you still don't have a set of compiling environment? If that's the case, I recommend using MinGW compiler collection. You can just type "gcc filename.c <additional compiler options if any>" on the command prompt. We can of course use Visual Studio, but MinGW is much smaller and easier while you are testing basic C programs that don't heavily rely on the latest Win32 API or VC/VC++ specific macros.
Also, if you are sure you are writing in C, the code Ethin pasted above doesn't work because it's written for C++. I'm not sure if the testing library itself is actually providing C API's, so it may work with some tweeks though.

I don't speak as good as I write, and I don't listen as good as I speak.

2017-10-18 05:19:44

@3, actually, your slightly incorrect -- I wrote that code to fit the question. Catch will work for C or C++ though, though C++ is recommended. The only thing I pasted was the config file.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2017-10-18 16:07:22

Oh, I looked at it again and got it. Thanks for pointing that out. Glad to know that it does support C.

I don't speak as good as I write, and I don't listen as good as I speak.

2017-10-19 00:43:18

Hello guys!
Can someone send the direct link from Catch? The git hub link does not work

2017-10-19 03:49:07

It doesn't work? Odd... you can download it at https://github.com/philsquared/Catch/re … catch.hpp. If that doesn't work... check your firewall settings. You should have no problems accessing Git Hub. At all.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github