Skip to content

边界类型

使用 boundary(type) 自动生成边界值测试用例。

支持的类型

类型标识C 类型边界值个数
int8 / i8int8_t8 个
uint8 / u8uint8_t8 个
int16 / i16int16_t8 个
uint16 / u16uint16_t8 个
int32 / i32int32_t6 个
uint32 / u32uint32_t5 个
int64 / i64int64_t6 个
uint64 / u64uint64_t5 个
boolbool4 个
ptrvoid*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
说明
0false
1true
-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;
}

本页面内容遵循 Luna 软件源代码授权条款 (LSLA) 发布