Skip to content

Instantly share code, notes, and snippets.

@groyoh
Last active September 1, 2021 22:45
Show Gist options
  • Save groyoh/a3d8ec21d6c98ec0182ee704c75e6c99 to your computer and use it in GitHub Desktop.
Save groyoh/a3d8ec21d6c98ec0182ee704c75e6c99 to your computer and use it in GitHub Desktop.
CMocka example

Installation

$ make install

This will copy libcmoacka static library to lib/libcmocka.a and two .h files into include.

Compilation

$ make test

Running test

$ ./cmockatest
[==========] Running 4 test(s).
[ RUN      ] test_replace_char_null_ptr
[  ERROR   ] --- 0xffffffffffffffff == 0xffffffffffffffff
[   LINE   ] --- test.c:10: error: Failure!
[  FAILED  ] test_replace_char_null_ptr
[ RUN      ] test_replace_char_a_to_b
[       OK ] test_replace_char_a_to_b
[ RUN      ] test_replace_char_empty_str
[       OK ] test_replace_char_empty_str
[ RUN      ] test_replace_char_no_change
[       OK ] test_replace_char_no_change
[==========] 4 test(s) run.
[  PASSED  ] 3 test(s).
[  FAILED  ] 1 test(s), listed below:
[  FAILED  ] test_replace_char_null_ptr

 1 FAILED TEST(S)
.PHONY: test install
CMOCKA_DIR=$(shell pwd)/cmocka-1.1.0
CMOCKA_BUILD_DIR=$(CMOCKA_DIR)/build
CMOCKA_OUT_DIR=$(CMOCKA_BUILD_DIR)/output
all: test
test:
gcc test.c my_function.c -L./lib -I./ -I./include -lcmocka -o cmockatest
install:
curl https://cmocka.org/files/1.1/cmocka-1.1.0.tar.xz -o cmocka.tar
tar xf cmocka.tar
rm cmocka.tar
mkdir -p $(CMOCKA_OUT_DIR)
cd $(CMOCKA_BUILD_DIR) && \
cmake -DWITH_STATIC_LIB=1 -DCMAKE_INSTALL_PREFIX=$(CMOCKA_OUT_DIR) .. && \
make && \
make install
mkdir -p lib include
cp $(CMOCKA_OUT_DIR)/lib/libcmocka.a lib
cp $(CMOCKA_OUT_DIR)/include/*.h include
rm -R $(CMOCKA_DIR)
#include <string.h>
#include "my_function.h"
int replace_char(char * const input, const char old, const char new){
int len;
int i, changed_count = 0;
if(input == NULL){
return REPLACE_CHAR_NULL_PTR;
}
len = strlen(input);
for(i = 0; i < len; i++){
if(input[i] == old){
input[i] = new;
changed_count++;
}
}
return changed_count;
}
#define REPLACE_CHAR_NULL_PTR -1
int replace_char(char * const input, const char old, const char new);
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "my_function.h"
static void test_replace_char_null_ptr(void **state)
{
int result = replace_char(NULL, 'a', 'b');
assert_int_not_equal(result, REPLACE_CHAR_NULL_PTR); /* Will fail on purpose */
}
static void test_replace_char_a_to_b(void **state)
{
char str[] = "aabc122";
int result = replace_char(str, 'a', 'b');
assert_int_equal(result, 2);
assert_string_equal(str, "bbbc122");
}
static void test_replace_char_no_change(void **state)
{
char str[] = "bbbbbbedsefge";
int result = replace_char(str, 'a', 'b');
assert_int_equal(result, 0);
assert_string_equal(str, "bbbbbbedsefge");
}
static void test_replace_char_empty_str(void **state)
{
char str[] = "";
int result = replace_char(str, 'a', 'b');
assert_int_equal(result, 0);
assert_string_equal(str, "");
}
int main(void){
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_replace_char_null_ptr),
cmocka_unit_test(test_replace_char_a_to_b),
cmocka_unit_test(test_replace_char_empty_str),
cmocka_unit_test(test_replace_char_no_change)
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment