list_t.c (3381B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <assert.h> 4 5 #include "../list.h" 6 #include "test.h" 7 8 struct Item { 9 int id; 10 }; 11 12 /* utils for testing */ 13 void iprint(struct Item *); 14 struct Item* icreate(int); 15 void ifree(struct Item *); 16 int icmp(struct Item *, struct Item *); 17 int ieq(struct Item *, struct Item *); 18 struct List * lcreate_from_array(int nums[], int n); 19 void aswap(int nums[], int i, int j); 20 21 /* tests */ 22 void test_laddfront(void); 23 void test_lswap(void); 24 25 void 26 iprint(struct Item *p) 27 { 28 printf("[%d]", p->id); 29 } 30 31 struct Item* 32 icreate(int id) 33 { 34 struct Item *p; 35 p = (struct Item *)malloc(sizeof(struct Item)); 36 p->id = id; 37 return p; 38 } 39 40 void 41 ifree(struct Item *p) 42 { 43 free(p); 44 } 45 46 int 47 icmp(struct Item *p, struct Item *q) 48 { 49 if (p->id < q->id) 50 return -1; 51 else if (p->id == q->id) 52 return 0; 53 else 54 return 1; 55 } 56 57 int 58 ieq(struct Item *p, struct Item *q) 59 { 60 return !icmp(p, q); 61 } 62 63 struct List * 64 lcreate_from_array(int nums[], int n) 65 { 66 struct List *p, *tmp; 67 p = NULL; 68 for (int i = n - 1; i >= 0; i--) { 69 tmp = laddfront(p, icreate(nums[i])); 70 if (tmp == NULL) { 71 fprintf(stderr, "lcreate_from_array(): Fatal. Laddfront() failed.\n"); 72 exit(1); 73 } 74 p = tmp; 75 } 76 return p; 77 } 78 79 void 80 aswap(int nums[], int i, int j) 81 { 82 int tmp; 83 tmp = nums[i]; 84 nums[i] = nums[j]; 85 nums[j] = tmp; 86 } 87 88 89 void 90 test_laddfront(void) 91 { 92 int n = 3; 93 int num[n]; 94 struct Item *item[n]; 95 struct List *p = NULL, *tmp; 96 97 if ((tmp = laddfront(p, NULL)) != NULL) 98 fprintf(stderr, "test_laddfront: Adding NULL item should return" 99 "NULL\n"); 100 101 for (int i = 0; i < n; i++) 102 item[i] = icreate(num[i]); 103 104 for (int i = 0; i < n; i++) { 105 if ((tmp = laddfront(p, item[i])) == NULL) 106 fprintf(stderr, "test_laddfront: Adding item failed.\n"); 107 p = tmp; 108 } 109 lfreei(p, (void (*)(void *))&ifree); 110 } 111 112 /* 113 * Test for lswap(). 114 * Assume that laddfront, leqa, leqi work correctly. 115 */ 116 void 117 test_lswap(void) 118 { 119 struct List *p = NULL, *q = NULL, *tmp; 120 121 /* test for NULL List. */ 122 lswap(p, p); 123 if (p != NULL) 124 fprintf(stderr, "test_lswap: Test for NULL List failed.\n"); 125 126 /* test for one element List. */ 127 struct Item *item; 128 item = icreate(0); 129 130 tmp = laddfront(p, item); 131 if (tmp == NULL) { 132 fprintf(stderr, "test_lswap: Fatal. laddfront() failed.\n"); 133 exit(1); 134 } 135 p = tmp; 136 tmp = laddfront(q, item); 137 if (tmp == NULL) { 138 fprintf(stderr, "test_lswap: Fatal. laddfront() failed.\n"); 139 exit(1); 140 } 141 q = tmp; 142 lswap(p, p); 143 if (! leqa(p, q)) 144 fprintf(stderr, "test_lswap: Test for one element List failed.\n"); 145 lfree(p); 146 lfree(q); 147 ifree(item); 148 149 /* test for random numbers */ 150 /* swaps every pairs with lswap and aswap, then checks both are same */ 151 struct List *li, *lj; 152 int i, j; 153 int lmin = 5; 154 int lmax = 15; 155 p = q = NULL; 156 157 int n = (unsigned int)rand() % (lmax - lmin) + lmin; 158 assert(lmin <= n && n < lmax); 159 int nums[n]; 160 161 for (int i = 0; i < n; i++) { 162 nums[i] = rand(); 163 } 164 p = lcreate_from_array(nums, n); 165 for (i = 0, li = p; i < n && li != NULL; i++, li = li->next) { 166 for (j = 0, lj = p; j < n && lj != NULL; j++, lj = lj->next) { 167 lswap(li, lj); 168 aswap(nums, i, j); 169 q = lcreate_from_array(nums, n); 170 if (!leqi(p, q, (int (*)(void *, void*))&ieq)) 171 fprintf(stderr, "test_lswap: Test for longer List failed.\n"); 172 lfreei(q, (void (*))(void *)&ifree); 173 } 174 } 175 if (i != j) 176 fprintf(stderr, "test_lswap: Index mismatch.\n"); 177 178 lfreei(p, (void (*))(void *)&ifree); 179 }