更正函数名称
增加测试函数
This commit is contained in:
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,3 +1,6 @@
|
||||
{
|
||||
"C_Cpp.default.compilerPath": "/usr/bin/g++-14"
|
||||
"C_Cpp.default.compilerPath": "/usr/bin/g++-14",
|
||||
"files.associations": {
|
||||
"stdio.h": "c"
|
||||
}
|
||||
}
|
@@ -141,7 +141,7 @@ bool vector_remove_at(vector_t *vec, size_t index){
|
||||
* @param vec
|
||||
* @return size_t
|
||||
*/
|
||||
size_t vector_used(const vector_t *vec){
|
||||
size_t vector_size(const vector_t *vec){
|
||||
if (!vec) return 0; // 检查vec是否为NULL
|
||||
return vec->size;
|
||||
}
|
||||
@@ -309,6 +309,7 @@ bool vector_reserve(vector_t *vec, size_t new_capacity){
|
||||
}
|
||||
return true; // 成功保留数据并扩容
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 排序容器中数据
|
||||
*
|
||||
@@ -364,7 +365,6 @@ bool vector_for_each(const vector_t *vec, void (*func)(void *,size_t index)){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 删除与 elem 匹配的元素(通过 compare 函数判断)。
|
||||
*
|
||||
|
@@ -28,6 +28,14 @@ vector_t *vector_create(size_t elem_size, size_t initial_capacity);
|
||||
|
||||
bool vector_destroy(vector_t *vec);
|
||||
|
||||
bool vector_add_at(vector_t *vec, size_t index, const void *elem);
|
||||
|
||||
bool vector_swap(vector_t *vec, size_t index1, size_t index2);
|
||||
|
||||
bool vector_remove_at(vector_t *vec, size_t index);
|
||||
|
||||
size_t vector_size(const vector_t *vec);
|
||||
|
||||
bool vector_push_back(vector_t *vec, const void *elem);
|
||||
|
||||
bool vector_pop_back(vector_t *vec);
|
||||
|
164
src/main.c
164
src/main.c
@@ -8,10 +8,170 @@
|
||||
* @copyright Copyright (c) 2025
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "lib/vector/vector.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// 测试用的结构体
|
||||
typedef struct {
|
||||
int id;
|
||||
char name[20];
|
||||
float score;
|
||||
} Student;
|
||||
|
||||
// 比较函数
|
||||
int compare_students(const void *a, const void *b) {
|
||||
const Student *sa = (const Student *)a;
|
||||
const Student *sb = (const Student *)b;
|
||||
return sa->id - sb->id;
|
||||
}
|
||||
|
||||
// 谓词函数
|
||||
int is_high_score(const void *a) {
|
||||
const Student *s = (const Student *)a;
|
||||
return s->score >= 90.0f;
|
||||
}
|
||||
|
||||
// 遍历打印函数
|
||||
void print_student(void *elem, size_t index) {
|
||||
Student *s = (Student *)elem;
|
||||
printf("[%zu] ID: %d, Name: %s, Score: %.1f\n",
|
||||
index, s->id, s->name, s->score);
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector_t *vec = vector_create(sizeof(int), 10);
|
||||
printf("=== 开始测试 vector 实现 ===\n\n");
|
||||
|
||||
// 测试 vector_create
|
||||
printf("1. 测试 vector_create...\n");
|
||||
vector_t *students = vector_create(sizeof(Student), 2);
|
||||
if (!students) {
|
||||
printf("创建 vector 失败!\n");
|
||||
return -1;
|
||||
}
|
||||
printf("创建成功, 初始容量: %zu\n", vector_get_capacity(students));
|
||||
|
||||
// 测试 vector_push_back
|
||||
printf("\n2. 测试 vector_push_back...\n");
|
||||
Student s1 = {101, "Alice", 85.5f};
|
||||
Student s2 = {102, "Bob", 92.0f};
|
||||
Student s3 = {103, "Charlie", 78.5f};
|
||||
Student s4 = {104, "David", 95.0f};
|
||||
|
||||
vector_push_back(students, &s1);
|
||||
vector_push_back(students, &s2);
|
||||
vector_push_back(students, &s3); // 这里应该触发扩容
|
||||
printf("添加3个学生后, 容量: %zu, 大小: %zu\n",
|
||||
vector_get_capacity(students), vector_size(students));
|
||||
|
||||
// 测试 vector_get
|
||||
printf("\n3. 测试 vector_get...\n");
|
||||
Student *ps = (Student *)vector_get(students, 1);
|
||||
printf("索引1的学生: ID=%d, Name=%s\n", ps->id, ps->name);
|
||||
|
||||
// 测试 vector_set
|
||||
printf("\n4. 测试 vector_set...\n");
|
||||
strcpy(s2.name, "Robert");
|
||||
vector_set(students, 1, &s2);
|
||||
ps = (Student *)vector_get(students, 1);
|
||||
printf("修改后索引1的学生: Name=%s\n", ps->name);
|
||||
|
||||
// 测试 vector_add_at
|
||||
printf("\n5. 测试 vector_add_at...\n");
|
||||
vector_add_at(students, 1, &s4);
|
||||
printf("在索引1插入新学生后:\n");
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_remove_at
|
||||
printf("\n6. 测试 vector_remove_at...\n");
|
||||
vector_remove_at(students, 0);
|
||||
printf("删除索引0后:\n");
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_swap
|
||||
printf("\n7. 测试 vector_swap...\n");
|
||||
vector_swap(students, 0, 1);
|
||||
printf("交换索引0和1后:\n");
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_pop_back
|
||||
printf("\n8. 测试 vector_pop_back...\n");
|
||||
vector_pop_back(students);
|
||||
printf("弹出最后一个元素后:\n");
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_sort
|
||||
printf("\n9. 测试 vector_sort...\n");
|
||||
vector_push_back(students, &s1);
|
||||
vector_push_back(students, &s3);
|
||||
printf("排序前:\n");
|
||||
vector_for_each(students, print_student);
|
||||
vector_sort(students, compare_students);
|
||||
printf("按ID排序后:\n");
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_reverse
|
||||
printf("\n10. 测试 vector_reverse...\n");
|
||||
vector_reverse(students);
|
||||
printf("反转后:\n");
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_find
|
||||
printf("\n11. 测试 vector_find...\n");
|
||||
size_t index;
|
||||
if (vector_find(students, &s1, compare_students, &index)) {
|
||||
printf("找到ID为%d的学生在索引%zu\n", s1.id, index);
|
||||
} else {
|
||||
printf("未找到ID为%d的学生\n", s1.id);
|
||||
}
|
||||
|
||||
// 测试 vector_find_if
|
||||
printf("\n12. 测试 vector_find_if...\n");
|
||||
if (vector_find_if(students, is_high_score, &index)) {
|
||||
Student *high_score = (Student *)vector_get(students, index);
|
||||
printf("找到高分学生: %s (%.1f)\n", high_score->name, high_score->score);
|
||||
} else {
|
||||
printf("未找到高分学生\n");
|
||||
}
|
||||
|
||||
// 测试 vector_remove
|
||||
printf("\n13. 测试 vector_remove...\n");
|
||||
vector_remove(students, &s1, compare_students);
|
||||
printf("删除ID为%d的学生后:\n", s1.id);
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_resize
|
||||
printf("\n14. 测试 vector_resize...\n");
|
||||
vector_resize(students, 5);
|
||||
printf("调整大小为5后:\n");
|
||||
vector_for_each(students, print_student);
|
||||
|
||||
// 测试 vector_clear
|
||||
printf("\n15. 测试 vector_clear...\n");
|
||||
vector_clear(students);
|
||||
printf("清空后大小: %zu\n", vector_size(students));
|
||||
|
||||
// 测试 vector_shrink_to_fit
|
||||
printf("\n16. 测试 vector_shrink_to_fit...\n");
|
||||
vector_push_back(students, &s1);
|
||||
vector_push_back(students, &s2);
|
||||
printf("添加2个学生后, 容量: %zu\n", vector_get_capacity(students));
|
||||
vector_shrink_to_fit(students);
|
||||
printf("shrink_to_fit后, 容量: %zu\n", vector_get_capacity(students));
|
||||
|
||||
// 测试 vector_reserve
|
||||
printf("\n17. 测试 vector_reserve...\n");
|
||||
vector_reserve(students, 10);
|
||||
printf("预留10个容量后, 容量: %zu\n", vector_get_capacity(students));
|
||||
|
||||
// 测试 vector_destroy
|
||||
printf("\n18. 测试 vector_destroy...\n");
|
||||
if (vector_destroy(students)) {
|
||||
printf("销毁成功\n");
|
||||
} else {
|
||||
printf("销毁失败\n");
|
||||
}
|
||||
|
||||
printf("\n=== 测试完成 ===\n");
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user