Its my first time testing C programs. I have this header file which I would like to test:
#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif
int add(int num1, int num2) {
return num1 + num2;
}
I am using the framework CUnit to test it. I am using Netbeans as an IDE. The following is the code.
#include <stdio.h>
#include <stdlib.h>
#include "CUnit/Basic.h"
#include "calculator_helper.h"
/*
* CUnit Test Suite
*/
int init_suite(void) {
return 0;
}
int clean_suite(void) {
return 0;
}
/* IMPORTANT PART: */
void testAdd() {
int num1 = 2;
int num2 = 2;
int result = add(num1, num2);
if (result == 4) {
CU_ASSERT(0);
}
}
int main() {
CU_pSuite pSuite = NULL;
/* Initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* Add a suite to the registry */
pSuite = CU_add_suite("newcunittest", init_suite, clean_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add the tests to the suite */
if ((NULL == CU_add_test(pSuite, "testAdd", testAdd))) {
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}
PROBLEM
When I am building the test, I am getting a BUILD TESTS FAILED. More specifically, I get this:
In function `add': NetBeans/Calculator/calculator_helper.h:12: multiple definition of `add' build/Debug/GNU-Linux-x86/tests/tests/newcunittest.o:NetBeans/Calculator/./calculator_helper.h:12: first defined here collect2: error: ld returned 1 exit status
Can anybody tell me why I am getting this error. I tried searching on google but I found no luck.
I have this header file which I would like to test:
You're defining a function in a header file:
int add(int num1, int num2) {
return num1 + num2;
}
Declare it in the header:
#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
int add(int num1, int num2);
#endif /* the endif goes at the end of the file */
...and define it in a source file:
#include "helper.h"
int add(int num1, int num2) {
return num1 + num2;
}
Recommended reading: