key_value.c
#include <stddef.h>#include <stdlib.h>#include <string.h>typedef struct KeyValue {unsigned int key;const char* value;} KeyValue;static KeyValue *key_values = NULL;static unsigned int number_of_key_values = 0;void set_key_values(KeyValue * const new_key_values,const unsigned int new_number_of_key_values) {key_values = new_key_values;number_of_key_values = new_number_of_key_values;}// Compare two key members of KeyValue structures.int key_value_compare_keys(const void *a, const void *b) {return (int)((KeyValue*)a)->key - (int)((KeyValue*)b)->key;}// Search an array of key value pairs for the item with the specified value.KeyValue* find_item_by_value(const char * const value) {unsigned int i;for (i = 0; i < number_of_key_values; i++) {if (strcmp(key_values[i].value, value) == 0) {return &key_values[i];}}return NULL;}// Sort an array of key value pairs by key.void sort_items_by_key() {qsort(key_values, number_of_key_values, sizeof(*key_values),key_value_compare_keys);}
该文件中有4个待测试函数以及一个结构体
set_key_values函数实现给静态全局变量进行赋值成为一个键值对。
key_value_compare_keys函数用于比较键值对集合中的两个键值的大笑
find_item_by_value用于在键值对集合中根据特定的键值返回所对应的数值的键值对,未找到则返回NULL。
sort_items_by_key根据键值调用qsort对集合进行排序。
key_value_test.c
#include <stdarg.h>#include <stddef.h>#include <setjmp.h>#include <string.h>#include "cmockery.h"/* This is duplicated here from the module setup_teardown.c to reduce the* number of files used in this test. */typedef struct KeyValue {unsigned int key;const char* value;} KeyValue;void set_key_values(KeyValue * const new_key_values,const unsigned int new_number_of_key_values);extern KeyValue* find_item_by_value(const char * const value);extern void sort_items_by_key();static KeyValue key_values[] = {{ 10, "this" },{ 52, "test" },{ 20, "a" },{ 13, "is" },};void create_key_values(void **state) {KeyValue * const items = (KeyValue*)test_malloc(sizeof(key_values));memcpy(items, key_values, sizeof(key_values));*state = (void*)items;set_key_values(items, sizeof(key_values) / sizeof(key_values[0]));}void destroy_key_values(void **state) {test_free(*state);set_key_values(NULL, 0);}void test_find_item_by_value(void **state) {unsigned int i;for (i = 0; i < sizeof(key_values) / sizeof(key_values[0]); i++) {KeyValue * const found = find_item_by_value(key_values[i].value);assert_true(found);assert_int_equal(found->key, key_values[i].key);assert_string_equal(found->value, key_values[i].value);}}void test_sort_items_by_key(void **state) {unsigned int i;KeyValue * const kv = *state;sort_items_by_key();for (i = 1; i < sizeof(key_values) / sizeof(key_values[0]); i++) {assert_true(kv[i - 1].key < kv[i].key);}}int main(int argc, char* argv[]) {const UnitTest tests[] = {unit_test_setup_teardown(test_find_item_by_value, create_key_values,destroy_key_values),unit_test_setup_teardown(test_sort_items_by_key, create_key_values,destroy_key_values),};return run_tests(tests);}
该文件里面声明了一组测试用的键值对数组
static KeyValue key_values[] = {{ 10, "this" },{ 52, "test" },{ 20, "a" },{ 13, "is" },};
create_key_values使用这组测试的数据,赋值了一个键值对集合,调用set_key_values赋值到全局静态变量里,进行测试前的准备
destroy_key_values释放create_key_values函数中申请的内存并且将全局静态变量设置为0
test_find_item_by_value函数里使用assert函数对于从全局静态变量里面查找到的值与上面的测试键值对组的值挨个进行比较,以确认能够正常的找到
test_sort_items_by_key函数会使用sort_items_by_key函数将这个上面的键值对组进行排序,然后挨个比较确认排序是成功的。
在main函数里面向UnitTest数组进行注册单元测试的时候使用的unit_test_setup_teardown这个函数,实际上这个是一个宏定义如下:
// 初始化一个单元测试结构体.#define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }#define unit_test_setup(test, setup){ #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }#define unit_test_teardown(test, teardown){ #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }/* Initialize an array of UnitTest structures with a setup function for a test* and a teardown function. Either setup or teardown can be NULL.*/#define unit_test_setup_teardown(test, setup, teardown)unit_test_setup(test, setup),unit_test(test),unit_test_teardown(test, teardown)
即分别调用了unit_test_setup,unit_test,unit_test_teardown三个宏进行展开成为UnitTest的三个成员,且他们的类型也都做了赋值。
两个测试单元分别测试了上面的两个函数:test_find_item_by_value,test_sort_items_by_key。
在这里使用unit_test_setup_teardown将每一组setup test teardown进行注册进去就是为了保证其执行的先后顺序。实际上就是在UnitTest按照顺序初始化了值。然后再执行run_tests函数的时候就是按照UnitTest数组里面的顺序进行执行的。借助于assert类函数,保证测试的目的可以达到。
最终的执行结果如下:
