Appearance
边界类型
使用 boundary(type) 自动生成边界值测试用例。
支持的类型
| 类型标识 | C 类型 | 边界值个数 |
|---|---|---|
int8 / i8 | int8_t | 8 个 |
uint8 / u8 | uint8_t | 8 个 |
int16 / i16 | int16_t | 8 个 |
uint16 / u16 | uint16_t | 8 个 |
int32 / i32 | int32_t | 6 个 |
uint32 / u32 | uint32_t | 5 个 |
int64 / i64 | int64_t | 6 个 |
uint64 / u64 | uint64_t | 5 个 |
bool | bool | 4 个 |
ptr | void* | 3 个 |
边界值详情
uint8 边界值
c
params(n = boundary(uint8))
// 生成:0, 1, 127, 128, 254, 255, -1, 256| 值 | 说明 |
|---|---|
| 0 | 最小值 |
| 1 | 最小值 + 1 |
| 127 | 中间值 - 1 |
| 128 | 中间值 |
| 254 | 最大值 - 1 |
| 255 | 最大值 |
| -1 | 下溢出 |
| 256 | 上溢出 |
int8 边界值
c
params(n = boundary(int8))
// 生成:-128, -127, -1, 0, 1, 126, 127, 128| 值 | 说明 |
|---|---|
| -128 | 最小值 |
| -127 | 最小值 + 1 |
| -1 | 零下 |
| 0 | 零 |
| 1 | 零上 |
| 126 | 最大值 - 1 |
| 127 | 最大值 |
| 128 | 上溢出 |
bool 边界值
c
params(b = boundary(bool))
// 生成:0, 1, -1, 2| 值 | 说明 |
|---|---|
| 0 | false |
| 1 | true |
| -1 | 非标准值 |
| 2 | 非标准值 |
ptr 边界值
c
params(p = boundary(ptr))
// 生成:NULL, (void*)1, (void*)-1| 值 | 说明 |
|---|---|
| NULL | 空指针 |
| (void*)1 | 非法低地址 |
| (void*)-1 | 非法高地址 |
使用示例
验证 uint8 范围
c
/*
* 设计说明:测试字节值验证函数
* 预期结果:0-255 有效,其他无效
*/
__attribute__((test_method, params(n = boundary(uint8))))
int test_is_valid_byte(int n) {
int result = is_valid_byte(n);
int expected = (n >= 0 && n <= 255) ? 1 : 0;
return result == expected;
}验证有符号范围
c
/*
* 设计说明:测试有符号字节验证
* 预期结果:-128 到 127 有效
*/
__attribute__((test_method, params(n = boundary(int8))))
int test_is_valid_signed_byte(int n) {
int result = is_valid_signed_byte(n);
int expected = (n >= -128 && n <= 127) ? 1 : 0;
return result == expected;
}验证布尔参数
c
/*
* 设计说明:测试布尔标准化函数
* 预期结果:非零值应转为1,零应保持0
*/
__attribute__((test_method, params(b = boundary(bool))))
int test_normalize_bool(int b) {
int result = normalize_bool(b);
int expected = (b != 0) ? 1 : 0;
return result == expected;
}验证指针参数
c
/*
* 设计说明:测试指针有效性检查
* 预期结果:NULL 无效,其他需要进一步检查
*/
__attribute__((test_method, params(p = boundary(ptr))))
int test_is_valid_ptr(void *p) {
int result = is_valid_ptr(p);
if (p == NULL) {
return result == 0; // NULL 应该无效
}
// 非 NULL 指针可能有效也可能无效
return 1;
}多个边界参数
可以组合多个边界类型:
c
/*
* 设计说明:测试二维坐标验证
* 预期结果:两个参数都在有效范围内
*/
__attribute__((test_method,
params(x = boundary(uint8), y = boundary(uint8)),
strategy(pairwise)))
int test_valid_coords(int x, int y) {
int result = is_valid_coord(x, y);
int expected = (x >= 0 && x <= 255 && y >= 0 && y <= 255);
return result == expected;
}使用 Pairwise 减少组合数
两个 uint8 边界参数默认生成 8×8=64 个用例。使用 strategy(pairwise) 可以显著减少组合数量。
与显式值混合
可以混合使用边界类型和显式值:
c
/*
* 设计说明:混合测试策略
* 预期结果:涵盖边界和特殊值
*/
__attribute__((test_method,
params(
x = boundary(uint8),
mode = {0, 1, 2}
)))
int test_mixed(int x, int mode) {
return process(x, mode) >= 0;
}