Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Last active October 22, 2019 07:58
Show Gist options
  • Save aerostitch/faa106d7534416c4dd9dc7e57dbfd209 to your computer and use it in GitHub Desktop.
Save aerostitch/faa106d7534416c4dd9dc7e57dbfd209 to your computer and use it in GitHub Desktop.
Testing out cunit
all:
gcc -g -Wall -c search.c -lcunit -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
gcc -g -Wall test_search.c search.o -lcunit -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0 -o test_search
./test_search && rm search.o test_search
#include <ctype.h>
#include <glib.h>
/**
* @brief Replaces ',' and '/' by ' ', deduplicates spaces within the string
* and strips spaces from both ends of the string
*
* @param pointer to the string to cleanup
* @return pointer to the cleaned up string
*/
char *search_fix_spaces(const char *str) {
int i;
int len=strlen(str);
char c,*s,*d,*ret=g_strdup(str);
if (len == 0) {
return ret;
}
for (i = 0 ; i < len ; i++) {
if (ret[i] == ',' || ret[i] == '/')
ret[i]=' ';
}
s=ret;
d=ret;
len=0;
do {
c=*s++;
// When the space is at the beginning of the line, just go to the next iteration to move the pointer further.
if (0 != isspace(c) && len == 0){
continue;
}else{
*d++=c;
len++;
}
while (0 != isspace(c) && 0 != isspace(*s))
s++;
// This trims the last space of the string as it has already been de-duplicated.
if (0 != isspace(c) && *s == '\0') {
d--;
len--;
}
} while (c);
return ret;
}
#ifndef NAVIT_SEARCH_H
#define NAVIT_SEARCH_H
#ifdef __cplusplus
extern "C" {
#endif
/* prototypes */
char *search_fix_spaces(const char *str);
/* end of prototypes */
#ifdef __cplusplus
}
#endif
#endif
#include <CUnit/Basic.h>
// #include <CUnit/Console.h>
#include "search.h"
int init_suite_search(void) { return 0; }
int clean_suite_search(void) { return 0; }
void test_search_fix_spaces(void)
{
const char *in = " Hello, world/! ";
CU_ASSERT_STRING_EQUAL(search_fix_spaces(in), "Hello world !");
char *non_const = "Spaces after string... ";
CU_ASSERT_STRING_EQUAL(search_fix_spaces(non_const), "Spaces after string...");
non_const = " Spaces before string.";
CU_ASSERT_STRING_EQUAL(search_fix_spaces(non_const), "Spaces before string.");
non_const = "Don't change a thing!";
CU_ASSERT_STRING_EQUAL(search_fix_spaces(non_const), "Don't change a thing!");
non_const = "";
CU_ASSERT_STRING_EQUAL(search_fix_spaces(non_const), "");
non_const = ", / ";
CU_ASSERT_STRING_EQUAL(search_fix_spaces(non_const), "");
non_const = ", */ ";
CU_ASSERT_STRING_EQUAL(search_fix_spaces(non_const), "*");
}
/* The main() function for setting up and running the tests.
* Returns a CUE_SUCCESS on successful running, another
* CUnit error code on failure.
*/
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("Search_Suite", init_suite_search, clean_suite_search);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if (NULL == CU_add_test(pSuite, "test of search_fix_spaces()", test_search_fix_spaces)) {
CU_cleanup_registry();
return CU_get_error();
}
// CU_console_run_tests();
/* 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment