You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
4.7 KiB
211 lines
4.7 KiB
/*
|
|
* 数组操作测试文件
|
|
* 用于测试LLM生成的数组边界检查和内存安全验证规范
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
|
|
// 数组初始化函数
|
|
void init_array(int *array, int size, int value) {
|
|
if (array == NULL || size <= 0) {
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
array[i] = value;
|
|
}
|
|
}
|
|
|
|
// 数组拷贝函数
|
|
void copy_array(int *dest, const int *src, int size) {
|
|
if (dest == NULL || src == NULL || size <= 0) {
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
dest[i] = src[i];
|
|
}
|
|
}
|
|
|
|
// 安全的数组拷贝函数
|
|
int safe_copy_array(int *dest, const int *src, int size, int dest_capacity) {
|
|
if (dest == NULL || src == NULL || size <= 0 || dest_capacity <= 0) {
|
|
return -1;
|
|
}
|
|
|
|
if (size > dest_capacity) {
|
|
return -2; // 目标数组容量不足
|
|
}
|
|
|
|
// 检查内存重叠
|
|
if ((src < dest && src + size > dest) || (dest < src && dest + size > src)) {
|
|
return -3; // 内存重叠
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
dest[i] = src[i];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// 数组查找函数
|
|
int find_index(const int *array, int size, int target) {
|
|
if (array == NULL || size <= 0) {
|
|
return -1; // 未找到
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
if (array[i] == target) {
|
|
return i; // 返回找到的索引
|
|
}
|
|
}
|
|
|
|
return -1; // 未找到
|
|
}
|
|
|
|
// 数组排序函数(简单的冒泡排序)
|
|
void bubble_sort(int *array, int size) {
|
|
if (array == NULL || size <= 1) {
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
for (int j = 0; j < size - i - 1; j++) {
|
|
if (array[j] > array[j + 1]) {
|
|
// 交换元素
|
|
int temp = array[j];
|
|
array[j] = array[j + 1];
|
|
array[j + 1] = temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 数组去重函数
|
|
int remove_duplicates(int *array, int size) {
|
|
if (array == NULL || size <= 1) {
|
|
return size;
|
|
}
|
|
|
|
// 先排序
|
|
bubble_sort(array, size);
|
|
|
|
int unique_count = 1;
|
|
for (int i = 1; i < size; i++) {
|
|
if (array[i] != array[unique_count - 1]) {
|
|
array[unique_count] = array[i];
|
|
unique_count++;
|
|
}
|
|
}
|
|
|
|
return unique_count;
|
|
}
|
|
|
|
// 数组旋转函数
|
|
void rotate_array(int *array, int size, int positions) {
|
|
if (array == NULL || size <= 1 || positions == 0) {
|
|
return;
|
|
}
|
|
|
|
// 标准化positions
|
|
positions = positions % size;
|
|
if (positions < 0) {
|
|
positions += size;
|
|
}
|
|
|
|
// 创建临时数组
|
|
int *temp = (int*)malloc(size * sizeof(int));
|
|
if (temp == NULL) {
|
|
return;
|
|
}
|
|
|
|
// 执行旋转
|
|
for (int i = 0; i < size; i++) {
|
|
temp[(i + positions) % size] = array[i];
|
|
}
|
|
|
|
// 复制回原数组
|
|
for (int i = 0; i < size; i++) {
|
|
array[i] = temp[i];
|
|
}
|
|
|
|
free(temp);
|
|
}
|
|
|
|
// 数组合并函数
|
|
int merge_arrays(const int *array1, int size1, const int *array2, int size2, int *result) {
|
|
if (array1 == NULL || array2 == NULL || result == NULL || size1 < 0 || size2 < 0) {
|
|
return -1;
|
|
}
|
|
|
|
int result_size = size1 + size2;
|
|
for (int i = 0; i < size1; i++) {
|
|
result[i] = array1[i];
|
|
}
|
|
for (int i = 0; i < size2; i++) {
|
|
result[size1 + i] = array2[i];
|
|
}
|
|
|
|
return result_size;
|
|
}
|
|
|
|
// 数组分区函数(用于快速排序)
|
|
int partition_array(int *array, int low, int high) {
|
|
if (array == NULL || low < 0 || high < low) {
|
|
return low;
|
|
}
|
|
|
|
int pivot = array[high];
|
|
int i = low - 1;
|
|
|
|
for (int j = low; j < high; j++) {
|
|
if (array[j] <= pivot) {
|
|
i++;
|
|
// 交换array[i]和array[j]
|
|
int temp = array[i];
|
|
array[i] = array[j];
|
|
array[j] = temp;
|
|
}
|
|
}
|
|
|
|
// 交换array[i+1]和array[high]
|
|
int temp = array[i + 1];
|
|
array[i + 1] = array[high];
|
|
array[high] = temp;
|
|
|
|
return i + 1;
|
|
}
|
|
|
|
// 数组验证函数
|
|
int is_sorted(const int *array, int size) {
|
|
if (array == NULL || size <= 1) {
|
|
return 1; // 空数组或单元素数组认为是已排序的
|
|
}
|
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
if (array[i] > array[i + 1]) {
|
|
return 0; // 未排序
|
|
}
|
|
}
|
|
|
|
return 1; // 已排序
|
|
}
|
|
|
|
// 数组统计函数
|
|
void array_statistics(const int *array, int size, int *min, int *max, int *sum) {
|
|
if (array == NULL || size <= 0 || min == NULL || max == NULL || sum == NULL) {
|
|
return;
|
|
}
|
|
|
|
*min = array[0];
|
|
*max = array[0];
|
|
*sum = 0;
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
if (array[i] < *min) *min = array[i];
|
|
if (array[i] > *max) *max = array[i];
|
|
*sum += array[i];
|
|
}
|
|
} |