1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h>
#define MAX_MALLOC 100
char *SortSaveAsCSV(struct Person *persons, int count);
void run_tests();
int main() { run_tests(); return 0; }
void run_tests() { printf("=== 排序后存储为CSV风格字符串测试 ===\n"); int pass_count = 0; int total_count = 0;
printf("\n测试用例1:正常情况\n"); struct Person people1[] = { (struct Person){"Zoe", 29, "Hangzhou"}, (struct Person){"Alice", 25, "Beijing"}, (struct Person){"Bob", 30, "Shanghai"} }; char *csv1 = SortSaveAsCSV(people1, 3);
if (csv1 != NULL && strcmp(csv1, "Alice,25,Beijing\nZoe,29,Hangzhou\nBob,30,Shanghai\n") == 0 && malloc_usable_size(csv1) < MAX_MALLOC) { printf(" ✔️ 通过\n"); pass_count++; } else { printf(" ❌ 失败:输出不正确or内存开辟过大\n"); } free(csv1); total_count++;
printf("\n测试用例2:空数组或NULL输入\n"); char *csv2 = SortSaveAsCSV(NULL, 0); if (strlen(csv2) == 0) { printf(" ✔️ 通过\n"); pass_count++; } else { printf(" ❌ 失败:应返回空字符串\n"); } total_count++;
printf("\n测试用例3:单个元素\n"); struct Person person3 = (struct Person){"Tom", 35, "Chengdu"}; char *csv3 = SortSaveAsCSV(&person3, 1); if (csv3 != NULL && strcmp(csv3, "Tom,35,Chengdu\n") == 0 && malloc_usable_size(csv3) < MAX_MALLOC) { printf(" ✔️ 通过\n"); pass_count++; } else { printf(" ❌ 失败:单行输出错误或开辟内存过大\n"); } free(csv3); total_count++;
printf("\n测试用例4:相同年龄(稳定排序)\n"); struct Person people4[] = { (struct Person){"Zoe", 25, "Hangzhou"}, (struct Person){"Alice", 25, "Beijing"}, (struct Person){"Bob", 26, "Shanghai"} }; char *csv4 = SortSaveAsCSV(people4, 3); if (csv4 != NULL && strcmp(csv4, "Zoe,25,Hangzhou\nAlice,25,Beijing\nBob,26,Shanghai\n") == 0 && malloc_usable_size(csv4) < MAX_MALLOC) { printf(" ✔️ 通过\n"); pass_count++; } else { printf(" ❌ 失败:排序不稳定或式格错误or开辟内存过大\n"); } free(csv4); total_count++;
printf("\n测试用例5:城市为空字符串\n"); struct Person people5[] = { (struct Person){"Zoe", 29, ""}, (struct Person){"Alice", 25, "Beijing"} }; char *csv5 = SortSaveAsCSV(people5, 2); if (csv5 != NULL && strcmp(csv5, "Alice,25,Beijing\nZoe,29,\n") == 0 && malloc_usable_size(csv5) < MAX_MALLOC) { printf(" ✔️ 通过\n"); pass_count++; } else { printf(" ❌ 失败:空城市未正确处理或内存开辟过大\n"); } free(csv5); total_count++;
printf("\n测试用例6:姓名/城市含特殊字符\n"); struct Person people6[] = { (struct Person){"Li_42", 29, "New-York"}, (struct Person){"OOP_Programmer", 25, "San-Francisco"} }; char *csv6 = SortSaveAsCSV(people6, 2); if (csv6 != NULL && strcmp(csv6, "OOP_Programmer,25,San-Francisco\nLi_42,29,New-York\n") == 0 && malloc_usable_size(csv6) < MAX_MALLOC) { printf(" ✔️ 通过\n"); pass_count++; } else { printf(" ❌ 失败:特殊字符未原样输出或内存开辟过大\n"); } free(csv6); total_count++;
printf("\n测试用例7:负年龄\n"); struct Person people7[] = { (struct Person){"Zoe", -5, "Hangzhou"}, (struct Person){"Alice", 100, "Beijing"} }; char *csv7 = SortSaveAsCSV(people7, 2); if (csv7 != NULL && strcmp(csv7, "Zoe,-5,Hangzhou\nAlice,100,Beijing\n") == 0 && malloc_usable_size(csv7) < MAX_MALLOC) { printf(" ✔️ 通过\n"); pass_count++; } else { printf(" ❌ 失败:负数年龄未正确输出或内存开辟过大\n"); } free(csv7); total_count++;
printf("\n=== 测试结果 ===\n"); printf("通过: %d/%d (%.1f%%)\n", pass_count, total_count, (float)pass_count / total_count * 100); }
|