theftC 的屬性測(cè)試庫(kù)
theft 是基于屬性測(cè)試的 C 庫(kù)。theft 并不是使用定義特定的輸入,運(yùn)行代碼,然后測(cè)試和檢測(cè)結(jié)果的方式,而是進(jìn)行屬性斷言(“任意可能的輸入,[條件]應(yīng)該一直存在”),尋找反例。如果找到一個(gè)參數(shù)導(dǎo)致斷言失敗,將會(huì)繼續(xù)尋找更簡(jiǎn)單的參數(shù),如果仍然失敗,打印最小的失敗輸入。
安裝 & 依賴
theft 只依賴 C99。
構(gòu)建:
$ make
構(gòu)建和運(yùn)行測(cè)試:
$ make test
安裝 libtheft :
$ make install # using sudo, if necessary
用法
首先,定義屬性函數(shù):
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
}
然后,生成測(cè)試參數(shù):
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
};
最后,實(shí)例化:
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;
評(píng)論
圖片
表情
