-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dynarray.c
More file actions
146 lines (126 loc) · 3.61 KB
/
test_dynarray.c
File metadata and controls
146 lines (126 loc) · 3.61 KB
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
/*
* This file contains executable code for testing your dynamic array
* implementation.
*/
#include <stdio.h>
#include <stdlib.h>
#include "dynarray.h"
#include "test_data.h"
/*
* Function to run tests on dynamic array implementation.
*/
void test_dynarray(struct student** students, int n)
{
struct dynarray* da;
struct student* repl, * s;
int i, k, n_removed = 0;
/*
* Create a dynamic array.
*/
da = dynarray_create();
printf("Checking that array is not NULL... ");
fflush(stdout);
if(da == NULL)
printf("FAILED\n");
else
printf("OK\n");
/*
* Add students to dynamic array.
*/
printf("\n");
for(i = 0; i < n; i++){
printf("Adding students[%d] to the array... ", i);
fflush(stdout);
dynarray_insert(da, students[i]);
printf("OK (check for correct array contents below)\n");
}
/*
* Check dynamic array size.
*/
printf("\nChecking array size (%d == %d?)... ", n, dynarray_size(da));
fflush(stdout);
if(n == dynarray_size(da))
printf("OK\n");
else
printf("FAILED\n");
/*
* Check that we can fetch values from the dynamic array.
*/
printf("\nArray contents:\n");
for (i = 0; i < dynarray_size(da); i++){
s = dynarray_get(da, i);
if(s)
printf(" %-16s\t%d\t%f\n", s->name, s->id, s->gpa);
else
printf(" NULL\n");
}
/*
* Test updating values stored in the dynamic array.
*/
repl = malloc(sizeof(struct student));
repl->name = "Kylo Ren";
repl->id = 933999999;
repl->gpa = 0.75;
for(i = n - 1; i > 0; i /= 2){
printf("\nReplacing element %d... ", i);
fflush(stdout);
dynarray_set(da, i, repl);
printf("OK (check that values below match)\n");
printf("Inserted: %-16s\t%d\t%f\n", repl->name, repl->id, repl->gpa);
s = dynarray_get(da, i);
if(s)
printf("Array @ %1d: %-16s\t%d\t%f\n", i, s->name, s->id, s->gpa);
else
printf("Array @ %1d: %-16s\n", i, "NULL");
}
/*
* Test removing elements from the dynamic array by removing the copies of
* repl that were inserted above.
*/
printf("\n");
for(i = n - 1; i > 0; i /= 2){
printf("Removing element %d... ", i);
fflush(stdout);
dynarray_remove(da, i);
printf("OK (check updated array contents below)\n");
n_removed++;
}
printf("\nNew array size (should be %d): %d\n", n - n_removed, dynarray_size(da));
printf("New array contents (should have no %s):\n", repl->name);
for(i = 0; i < dynarray_size(da); i++){
s = dynarray_get(da, i);
if(s)
printf(" %-16s\t%d\t%f\n", s->name, s->id, s->gpa);
else
printf(" NULL\n");
}
free(repl);
printf("\nFreeing array... ");
fflush(stdout);
dynarray_free(da);
printf("OK (check valgrind output to ensure no memory leaks)\n");
}
int main(int argc, char** argv)
{
struct student** students;
int i;
/*
* Create and fill an array of student structs.
*/
students = malloc(NUM_TESTING_STUDENTS * sizeof(struct student*));
for (i = 0; i < NUM_TESTING_STUDENTS; i++){
students[i] = malloc(sizeof(struct student));
students[i]->name = TESTING_NAMES[i];
students[i]->id = TESTING_IDS[i];
students[i]->gpa = TESTING_GPAS[i];
}
test_dynarray(students, NUM_TESTING_STUDENTS);
/*
* Free the array of student structs.
*/
for(i = 0; i < NUM_TESTING_STUDENTS; i++){
free(students[i]);
}
free(students);
return 0;
}