theftC 的属性测试库
theft 是基于属性测试的 C 库。theft 并不是使用定义特定的输入,运行代码,然后测试和检测结果的方式,而是进行属性断言(“任意可能的输入,[条件]应该一直存在”),寻找反例。如果找到一个参数导致断言失败,将会继续寻找更简单的参数,如果仍然失败,打印最小的失败输入。
安装 & 依赖
theft 只依赖 C99。
构建:
$ make
构建和运行测试:
$ make test
安装 libtheft :
$ make install # using sudo, if necessary
用法
首先,定义属性函数:
static theft_trial_res
prop_encoded_and_decoded_data_should_match(buffer *input) {
// [compress & uncompress input, compare output & original input]
// return THEFT_TRIAL_PASS, FAIL, SKIP, or ERROR
}
然后,生成测试参数:
static struct theft_type_info random_buffer_info = {
.alloc = random_buffer_alloc_cb, // allocate random instance
.free = random_buffer_free_cb, // free instance
.hash = random_buffer_hash_cb, // get hash of instance
.shrink = random_buffer_shrink_cb, // simplify instance
.print = random_buffer_print_cb, // print instance
};
最后,实例化:
struct theft *t = theft_init(0); // 0 -> auto-size bloom filter
// Configuration for the property test
struct theft_cfg cfg = {
// name of the property, used for failure messages (optional)
.name = __func__,
// the property function under test
.fun = prop_encoded_and_decoded_data_should_match,
// list of structs with argument info; the property function
// will be called with this many arguments
.type_info = { &random_buffer_info },
// number of trials to run; defaults to 100
.trials = 1000,
};
// Run the property test. Any failures will be printed, with seeds.
theft_run_res result = theft_run(t, &cfg);
theft_free(t);
return result == THEFT_RUN_PASS;
评论
