|
|
"""
|
|
|
简单C/C++测试用例集合
|
|
|
|
|
|
本模块提供了一系列渐进式复杂度的C/C++测试用例,用于验证LLM生成管道的
|
|
|
不同方面。每个测试用例包含源代码、预期的元数据特征、验证目标和质量标准。
|
|
|
"""
|
|
|
|
|
|
from typing import List, Dict, Any
|
|
|
from ..test_llm_generation import TestCase
|
|
|
|
|
|
|
|
|
# 基础算术函数测试用例
|
|
|
BASIC_ARITHMETIC_CASES = [
|
|
|
TestCase(
|
|
|
name="add_two_numbers",
|
|
|
description="基本的两个整数相加函数",
|
|
|
source_code="""
|
|
|
int add(int a, int b) {
|
|
|
return a + b;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'functional_correctness',
|
|
|
'integer_overflow',
|
|
|
'return_value'
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'return == a + b'
|
|
|
],
|
|
|
complexity_level="basic",
|
|
|
tags=['arithmetic', 'basic', 'function']
|
|
|
),
|
|
|
|
|
|
TestCase(
|
|
|
name="calculate_average",
|
|
|
description="计算两个整数的平均值",
|
|
|
source_code="""
|
|
|
int average(int a, int b) {
|
|
|
return (a + b) / 2;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'functional_correctness',
|
|
|
'integer_overflow',
|
|
|
'division_safety'
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'return == (a + b) / 2'
|
|
|
],
|
|
|
complexity_level="basic",
|
|
|
tags=['arithmetic', 'division', 'function']
|
|
|
),
|
|
|
|
|
|
TestCase(
|
|
|
name="simple_multiply",
|
|
|
description="基本的整数乘法函数",
|
|
|
source_code="""
|
|
|
int multiply(int a, int b) {
|
|
|
return a * b;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'functional_correctness',
|
|
|
'integer_overflow',
|
|
|
'return_value'
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'return == a * b'
|
|
|
],
|
|
|
complexity_level="basic",
|
|
|
tags=['arithmetic', 'multiplication', 'function']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 指针操作测试用例
|
|
|
POINTER_OPERATION_CASES = [
|
|
|
TestCase(
|
|
|
name="simple_pointer_swap",
|
|
|
description="使用指针交换两个整数值",
|
|
|
source_code="""
|
|
|
void swap(int *a, int *b) {
|
|
|
int temp = *a;
|
|
|
*a = *b;
|
|
|
*b = temp;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0 # 只计算全局变量,不计算局部变量
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'pointer_validity',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"确保指针参数有效",
|
|
|
"检查空指针情况",
|
|
|
"验证交换逻辑正确性"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid(a)',
|
|
|
'\\valid(b)'
|
|
|
],
|
|
|
complexity_level="intermediate",
|
|
|
tags=['pointer', 'swap', 'memory']
|
|
|
),
|
|
|
|
|
|
TestCase(
|
|
|
name="pointer_arithmetic",
|
|
|
description="基本的指针算术操作",
|
|
|
source_code="""
|
|
|
int sum_array(int *array, int size) {
|
|
|
int sum = 0;
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
sum += array[i];
|
|
|
}
|
|
|
return sum;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'array_bounds',
|
|
|
'pointer_validity',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证数组边界",
|
|
|
"检查指针有效性",
|
|
|
"确保size为正数"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid_read(array + (0..size-1))',
|
|
|
'size > 0'
|
|
|
],
|
|
|
complexity_level="intermediate",
|
|
|
tags=['pointer', 'array', 'arithmetic', 'loop']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 数组操作测试用例
|
|
|
ARRAY_OPERATION_CASES = [
|
|
|
TestCase(
|
|
|
name="array_initialization",
|
|
|
description="数组初始化函数",
|
|
|
source_code="""
|
|
|
void init_array(int *array, int size, int value) {
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
array[i] = value;
|
|
|
}
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'array_bounds',
|
|
|
'pointer_validity',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证数组边界",
|
|
|
"检查指针有效性",
|
|
|
"确保size为非负数"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid(array + (0..size-1))',
|
|
|
'size >= 0'
|
|
|
],
|
|
|
complexity_level="intermediate",
|
|
|
tags=['array', 'initialization', 'loop', 'pointer']
|
|
|
),
|
|
|
|
|
|
TestCase(
|
|
|
name="array_copy",
|
|
|
description="数组拷贝函数",
|
|
|
source_code="""
|
|
|
void copy_array(int *dest, int *src, int size) {
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
dest[i] = src[i];
|
|
|
}
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'array_bounds',
|
|
|
'pointer_validity',
|
|
|
'no_aliasing'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证源和目标数组边界",
|
|
|
"检查指针有效性",
|
|
|
"确保内存不重叠"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid_read(src + (0..size-1))',
|
|
|
'\\valid(dest + (0..size-1))'
|
|
|
],
|
|
|
complexity_level="intermediate",
|
|
|
tags=['array', 'copy', 'memory', 'pointer']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 字符串操作测试用例
|
|
|
STRING_OPERATION_CASES = [
|
|
|
TestCase(
|
|
|
name="string_length",
|
|
|
description="计算字符串长度的函数",
|
|
|
source_code="""
|
|
|
#include <stddef.h>
|
|
|
|
|
|
size_t string_length(const char *str) {
|
|
|
size_t len = 0;
|
|
|
while (str[len] != '\\0') {
|
|
|
len++;
|
|
|
}
|
|
|
return len;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'pointer_validity',
|
|
|
'null_termination',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证字符串以null结尾",
|
|
|
"检查指针有效性",
|
|
|
"防止缓冲区溢出"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid_read(str)',
|
|
|
'str != \\\\0'
|
|
|
],
|
|
|
complexity_level="intermediate",
|
|
|
tags=['string', 'length', 'pointer', 'null_termination']
|
|
|
),
|
|
|
|
|
|
TestCase(
|
|
|
name="string_copy",
|
|
|
description="安全的字符串拷贝函数",
|
|
|
source_code="""
|
|
|
#include <stddef.h>
|
|
|
|
|
|
size_t string_copy(char *dest, const char *src, size_t dest_size) {
|
|
|
size_t i;
|
|
|
for (i = 0; i < dest_size - 1 && src[i] != '\\0'; i++) {
|
|
|
dest[i] = src[i];
|
|
|
}
|
|
|
dest[i] = '\\0';
|
|
|
return i;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'buffer_overflow',
|
|
|
'pointer_validity',
|
|
|
'null_termination',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证目标缓冲区大小",
|
|
|
"确保字符串正确终止",
|
|
|
"防止缓冲区溢出"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid_read(src)',
|
|
|
'\\valid(dest + (0..dest_size-1))',
|
|
|
'dest_size > 0'
|
|
|
],
|
|
|
complexity_level="advanced",
|
|
|
tags=['string', 'copy', 'buffer_safety', 'pointer']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 内存分配测试用例
|
|
|
MEMORY_ALLOCATION_CASES = [
|
|
|
TestCase(
|
|
|
name="dynamic_array_create",
|
|
|
description="动态数组创建函数",
|
|
|
source_code="""
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
int* create_array(int size) {
|
|
|
if (size <= 0) {
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
int *array = (int*)malloc(size * sizeof(int));
|
|
|
if (array == NULL) {
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
array[i] = 0;
|
|
|
}
|
|
|
|
|
|
return array;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'memory_leak',
|
|
|
'allocation_success',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证内存分配成功",
|
|
|
"检查size参数有效性",
|
|
|
"处理内存分配失败情况"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'size > 0',
|
|
|
'return != \\\\0 ==> \\valid(return + (0..size-1))'
|
|
|
],
|
|
|
complexity_level="advanced",
|
|
|
tags=['memory', 'allocation', 'dynamic', 'error_handling']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# FreeRTOS特定测试用例
|
|
|
FREERTOS_CASES = [
|
|
|
TestCase(
|
|
|
name="freertos_task_function",
|
|
|
description="FreeRTOS任务函数示例",
|
|
|
source_code="""
|
|
|
#include "FreeRTOS.h"
|
|
|
#include "task.h"
|
|
|
|
|
|
void vTaskFunction(void *pvParameters) {
|
|
|
int *param_value = (int*)pvParameters;
|
|
|
|
|
|
for (;;) {
|
|
|
if (param_value != NULL) {
|
|
|
(*param_value)++;
|
|
|
}
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
}
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'pointer_validity',
|
|
|
'task_safety',
|
|
|
'concurrency'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证任务参数有效性",
|
|
|
"检查指针访问安全性",
|
|
|
"考虑并发访问问题"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid_read(pvParameters)'
|
|
|
],
|
|
|
complexity_level="advanced",
|
|
|
tags=['freertos', 'task', 'concurrency', 'pointer']
|
|
|
),
|
|
|
|
|
|
TestCase(
|
|
|
name="freertos_queue_operations",
|
|
|
description="FreeRTOS队列操作函数",
|
|
|
source_code="""
|
|
|
#include "FreeRTOS.h"
|
|
|
#include "queue.h"
|
|
|
|
|
|
BaseType_t send_to_queue(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait) {
|
|
|
if (xQueue == NULL || pvItemToQueue == NULL) {
|
|
|
return pdFAIL;
|
|
|
}
|
|
|
|
|
|
return xQueueSend(xQueue, pvItemToQueue, xTicksToWait);
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'pointer_validity',
|
|
|
'queue_operation',
|
|
|
'error_handling'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证队列句柄有效性",
|
|
|
"检查数据项指针有效性",
|
|
|
"处理错误返回情况"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'xQueue != \\\\0',
|
|
|
'pvItemToQueue != \\\\0'
|
|
|
],
|
|
|
complexity_level="advanced",
|
|
|
tags=['freertos', 'queue', 'pointer', 'error_handling']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 错误处理测试用例
|
|
|
ERROR_HANDLING_CASES = [
|
|
|
TestCase(
|
|
|
name="safe_division",
|
|
|
description="安全的除法函数,包含错误处理",
|
|
|
source_code="""
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
bool safe_divide(int a, int b, int *result) {
|
|
|
if (b == 0 || result == NULL) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
*result = a / b;
|
|
|
return true;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'pointer_validity',
|
|
|
'division_by_zero',
|
|
|
'error_handling',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"检查除数不为零",
|
|
|
"验证结果指针有效性",
|
|
|
"正确处理错误情况"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid(result)',
|
|
|
'b != 0'
|
|
|
],
|
|
|
complexity_level="intermediate",
|
|
|
tags=['error_handling', 'division', 'pointer', 'safety']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 边界条件测试用例
|
|
|
BOUNDARY_CONDITION_CASES = [
|
|
|
TestCase(
|
|
|
name="max_value_handler",
|
|
|
description="处理最大值边界条件的函数",
|
|
|
source_code="""
|
|
|
#include <limits.h>
|
|
|
|
|
|
int safe_add(int a, int b, bool *overflow) {
|
|
|
if (overflow == NULL) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
if (a > 0 && b > INT_MAX - a) {
|
|
|
*overflow = true;
|
|
|
return 0;
|
|
|
}
|
|
|
if (a < 0 && b < INT_MIN - a) {
|
|
|
*overflow = true;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
*overflow = false;
|
|
|
return a + b;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 0
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'integer_overflow',
|
|
|
'pointer_validity',
|
|
|
'boundary_conditions',
|
|
|
'error_handling',
|
|
|
'functional_correctness'
|
|
|
],
|
|
|
hints=[
|
|
|
"检查整数溢出条件",
|
|
|
"验证溢出标志指针",
|
|
|
"处理正负数边界情况"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'\\valid(overflow)'
|
|
|
],
|
|
|
complexity_level="advanced",
|
|
|
tags=['boundary', 'overflow', 'safety', 'pointer']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 复杂数据结构测试用例
|
|
|
COMPLEX_STRUCTURE_CASES = [
|
|
|
TestCase(
|
|
|
name="linked_list_node",
|
|
|
description="链表节点操作函数",
|
|
|
source_code="""
|
|
|
typedef struct Node {
|
|
|
int data;
|
|
|
struct Node *next;
|
|
|
} Node;
|
|
|
|
|
|
Node* create_node(int value) {
|
|
|
Node *new_node = (Node*)malloc(sizeof(Node));
|
|
|
if (new_node == NULL) {
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
new_node->data = value;
|
|
|
new_node->next = NULL;
|
|
|
return new_node;
|
|
|
}
|
|
|
""",
|
|
|
expected_metadata={
|
|
|
'functions_count': 1,
|
|
|
'variables_count': 1 # Node结构体定义
|
|
|
},
|
|
|
verification_goals=[
|
|
|
'memory_safety',
|
|
|
'allocation_success',
|
|
|
'data_structure_integrity',
|
|
|
'pointer_initialization'
|
|
|
],
|
|
|
hints=[
|
|
|
"验证内存分配",
|
|
|
"确保指针正确初始化",
|
|
|
"检查结构体字段设置"
|
|
|
],
|
|
|
expected_spec_patterns=[
|
|
|
'\\requires',
|
|
|
'\\ensures',
|
|
|
'return != \\\\0 ==> \\valid(return)'
|
|
|
],
|
|
|
complexity_level="advanced",
|
|
|
tags=['linked_list', 'struct', 'memory', 'pointer']
|
|
|
)
|
|
|
]
|
|
|
|
|
|
# 测试用例集合
|
|
|
ALL_TEST_CASES = (
|
|
|
BASIC_ARITHMETIC_CASES +
|
|
|
POINTER_OPERATION_CASES +
|
|
|
ARRAY_OPERATION_CASES +
|
|
|
STRING_OPERATION_CASES +
|
|
|
MEMORY_ALLOCATION_CASES +
|
|
|
FREERTOS_CASES +
|
|
|
ERROR_HANDLING_CASES +
|
|
|
BOUNDARY_CONDITION_CASES +
|
|
|
COMPLEX_STRUCTURE_CASES
|
|
|
)
|
|
|
|
|
|
# 按复杂度分组的测试用例
|
|
|
TEST_CASES_BY_COMPLEXITY = {
|
|
|
'basic': [case for case in ALL_TEST_CASES if case.complexity_level == 'basic'],
|
|
|
'intermediate': [case for case in ALL_TEST_CASES if case.complexity_level == 'intermediate'],
|
|
|
'advanced': [case for case in ALL_TEST_CASES if case.complexity_level == 'advanced']
|
|
|
}
|
|
|
|
|
|
# 按类别分组的测试用例
|
|
|
TEST_CASES_BY_CATEGORY = {
|
|
|
'arithmetic': [case for case in ALL_TEST_CASES if 'arithmetic' in case.tags],
|
|
|
'pointer': [case for case in ALL_TEST_CASES if 'pointer' in case.tags],
|
|
|
'array': [case for case in ALL_TEST_CASES if 'array' in case.tags],
|
|
|
'string': [case for case in ALL_TEST_CASES if 'string' in case.tags],
|
|
|
'memory': [case for case in ALL_TEST_CASES if 'memory' in case.tags],
|
|
|
'freertos': [case for case in ALL_TEST_CASES if 'freertos' in case.tags],
|
|
|
'error_handling': [case for case in ALL_TEST_CASES if 'error_handling' in case.tags],
|
|
|
'safety': [case for case in ALL_TEST_CASES if 'safety' in case.tags]
|
|
|
}
|
|
|
|
|
|
# 获取特定子集的测试用例
|
|
|
def get_test_cases_by_complexity(complexity_level: str) -> List[TestCase]:
|
|
|
"""根据复杂度级别获取测试用例"""
|
|
|
return TEST_CASES_BY_COMPLEXITY.get(complexity_level, [])
|
|
|
|
|
|
def get_test_cases_by_category(category: str) -> List[TestCase]:
|
|
|
"""根据类别获取测试用例"""
|
|
|
return TEST_CASES_BY_CATEGORY.get(category, [])
|
|
|
|
|
|
def get_test_cases_by_tags(tags: List[str]) -> List[TestCase]:
|
|
|
"""根据标签获取测试用例"""
|
|
|
return [case for case in ALL_TEST_CASES if any(tag in case.tags for tag in tags)]
|
|
|
|
|
|
def get_basic_test_suite() -> List[TestCase]:
|
|
|
"""获取基础测试套件(用于快速验证)"""
|
|
|
return TEST_CASES_BY_COMPLEXITY['basic'][:3] # 前3个基础用例
|
|
|
|
|
|
def get_comprehensive_test_suite() -> List[TestCase]:
|
|
|
"""获取全面测试套件"""
|
|
|
return ALL_TEST_CASES
|
|
|
|
|
|
def get_performance_test_suite() -> List[TestCase]:
|
|
|
"""获取性能测试套件(中等复杂度)"""
|
|
|
return TEST_CASES_BY_COMPLEXITY['intermediate'] |