-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact.c
246 lines (206 loc) · 6.02 KB
/
compact.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* idea for code from Circuit Cellar - Test Your EQ */
/*
* Copyright (c) 2001 Stephen Karg
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/* programmed in C - using some extreme programming techniques
1. Code the unit test first.
You create one test to define some small aspect of the
problem at hand. Then you create the simplest code that
will make that test pass. Then you create a second test.
Now you add to the code you just created to make this
new test pass, but no more! Not until you have yet a third
test. You continue until there is nothing left to test.
2. Always do the simplest thing that could possibly work
3. All code must pass all unit tests before it can be released.
4. When a bug is found new tests are created.
5. Acceptance tests are run often and the score is published
6. Leave optimization till last.
7. Keep things as simple as possible as long as possible by
never adding functionality before it is scheduled.
*/
#include <stdio.h>
#include <stdlib.h>
typedef int bool;
typedef struct
{
char *pName;
unsigned pass;
unsigned fail;
} UNIT_TEST;
/* allow us to check the unit tests for failure */
//#define UNIT_TEST_CHECK
/* take a sorted array with some elements duplicated,
and compact the array by removing the duplicates,
returning the new length of the array */
int compact(int *p,int size)
{
#ifdef UNIT_TEST_CHECK
(void)p;
#else
int index = 0;
int j; /* for loop index */
if (p && (size > 1))
{
while (index < (size - 1))
{
while (p[index] == p[index + 1])
{
/* move all entries down */
for (j = index + 1; j < (size - 1); j++)
{
p[j] = p[j + 1];
}
/* adjust new size */
size--;
if (index >= (size - 1))
break;
}
index++;
}
}
#endif
return (size);
}
/* generic unit test block */
static bool unit_test(UNIT_TEST *pTest,bool cond,char *pText)
{
if (!cond)
{
if (pTest && pTest->pName)
printf("%s - ",pTest->pName);
if (pText)
printf("%s\r\n",pText);
if (pTest)
pTest->fail++;
}
else if (pTest)
pTest->pass++;
return (cond);
}
/* unit tests for compact() */
static void testCompact(
UNIT_TEST *pTest,
int *array,
int *new_array,
int size,
int new_size)
{
int return_value;
int i;
/* test for our function */
return_value = compact(array,size);
if (unit_test(pTest,return_value == new_size,"wrong size returned!"))
{
for (i = 0; i < new_size; i++)
{
unit_test(pTest,array[i] == new_array[i],"compacted incorrectly!");
}
}
return;
}
/* our given example test */
static void testCompact1(UNIT_TEST *pTest)
{
int array[] = {1,3,7,7,8,9,9,9,10};
int new_array[] = {1,3,7,8,9,10};
int size = sizeof(array)/sizeof(int);
int new_size = sizeof(new_array)/sizeof(int);
testCompact(pTest,array,new_array,size,new_size);
return;
}
/* small single number test */
static void testCompact2(UNIT_TEST *pTest)
{
int array[] = {1,1};
int new_array[] = {1};
int size = sizeof(array)/sizeof(int);
int new_size = sizeof(new_array)/sizeof(int);
testCompact(pTest,array,new_array,size,new_size);
return;
}
/* larger single number test */
static void testCompact3(UNIT_TEST *pTest)
{
int array[] = {1,1,1,1};
int new_array[] = {1};
int size = sizeof(array)/sizeof(int);
int new_size = sizeof(new_array)/sizeof(int);
testCompact(pTest,array,new_array,size,new_size);
return;
}
/* end of array duplicate test */
static void testCompact4(UNIT_TEST *pTest)
{
int array[] = {1,2,2,2,2};
int new_array[] = {1,2};
int size = sizeof(array)/sizeof(int);
int new_size = sizeof(new_array)/sizeof(int);
testCompact(pTest,array,new_array,size,new_size);
return;
}
/* beginning of array duplicate test */
static void testCompact5(UNIT_TEST *pTest)
{
int array[] = {2,2,2,2,3,5,6};
int new_array[] = {2,3,5,6};
int size = sizeof(array)/sizeof(int);
int new_size = sizeof(new_array)/sizeof(int);
testCompact(pTest,array,new_array,size,new_size);
return;
}
/* verify that it doesn't mess up a perfectly good array */
static void testCompact6(UNIT_TEST *pTest)
{
int array[] = {1,3,7,8,9,10};
int new_array[] = {1,3,7,8,9,10};
int size = sizeof(array)/sizeof(int);
int new_size = sizeof(new_array)/sizeof(int);
testCompact(pTest,array,new_array,size,new_size);
return;
}
/* verify that it can handle bad data */
static void testCompact7(UNIT_TEST *pTest)
{
int *array = NULL;
int size = 40;
int return_value;
/* test for our function */
return_value = compact(array,size);
unit_test(pTest,return_value == size,"wrong size returned!");
return;
}
/* run the unit test */
int main(void)
{
UNIT_TEST test_data = {"compact",0,0};
testCompact1(&test_data);
testCompact2(&test_data);
testCompact3(&test_data);
testCompact4(&test_data);
testCompact5(&test_data);
testCompact6(&test_data);
testCompact7(&test_data);
printf("compact - %u passed, %u failed - testing complete\r\n",
test_data.pass,test_data.fail);
return (EXIT_SUCCESS);
}