vendredi 15 juin 2018

How to setup cmocka example with arm-none-eabi-gcc + cmake?

I am developing firmware for stm32f4xx based systems. For that I setup a toolchain based on the arm-none-eabi-gcc toolchain form ARM and cmake. This toolchain works on Ubuntu. I can x-compile and debug(via openocd + eclipse IDE). Now I like to add do some functional testing for my code. I was checking and it seems that cmocka is a good tool to use for embedded software testing.

I am now looking for an example/template that integrates a test into the cmake build.

let's assume a simple function at myfunc.c

#include "myFunc.h"

int buffer[10];

void myFunc(int i, int val) {
    buffer[i] = val;
}

if I got it right I can do a test in a separate c file like "test.c"

#include "myFunc.h"
#include <cmocka.h>

// function success
static void test_myFunc_positive() {
    for(int i = 0; i < 10; i++) {
        myFunc(i,i);
    }
}

static void test_myFunc_outofbounds() {
    myFunc(100,44);
}


int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(test_myFunc_positive),
        cmocka_unit_test(test_myFunc_outofbounds),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}

Usually I run

cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE="cmake/STM32Toolchain.cmake"

My question contains some sub questions: 1.) I installed libcmocka-dev. This is for my host system. Do I need to install cmocka for my arm-none-eabi-gcc compiler? 2.) How to setup cmake to pick the cmocka lib, build the test and run it on the host system? Think my toolchain file needs to be ignored.

Aucun commentaire:

Enregistrer un commentaire