-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_diff_main.c
More file actions
97 lines (92 loc) · 2.42 KB
/
array_diff_main.c
File metadata and controls
97 lines (92 loc) · 2.42 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
/*
6 kyu
Array.diff
https://www.codewars.com/kata/523f5d21c841566fde000009
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* array_diff(const int arr1[],
size_t n1,
const int arr2[],
size_t n2,
size_t* z);
static void print_array(size_t length, const int array[length]) {
printf("{ ");
for (size_t i = 0; i < length; i++)
printf("%d%s", array[i], (i == length - 1) ? "" : ", ");
printf(" }");
}
static void do_test(size_t n1,
const int arr1[n1],
size_t n2,
const int arr2[n2],
size_t nexp,
const int expected[nexp]) {
size_t nact = 42;
int* actual = array_diff(arr1, n1, arr2, n2, &nact);
const size_t mem_size = nexp * sizeof *expected;
printf("arr1 = ");
print_array(n1, arr1);
printf("\narr2 = ");
print_array(n2, arr2);
printf("\nexpected = ");
print_array(nexp, expected);
printf("\nactual = ");
print_array(nact, actual);
printf("\n%s\n\n", (nact == nexp && memcmp(actual, expected, mem_size) == 0)
? "OK"
: "FAIL");
free(actual);
}
int main(void) {
{
const int arr1[2] = {1, 2};
const int arr2[1] = {1};
const int expected[1] = {2};
do_test(2, arr1, 1, arr2, 1, expected);
}
{
const int arr1[3] = {1, 2, 2};
const int arr2[1] = {1};
const int expected[2] = {2, 2};
do_test(3, arr1, 1, arr2, 2, expected);
}
{
const int arr1[3] = {1, 2, 2};
const int arr2[1] = {2};
const int expected[1] = {1};
do_test(3, arr1, 1, arr2, 1, expected);
}
{
const int arr1[3] = {1, 2, 2};
const int* arr2 = NULL;
const int expected[3] = {1, 2, 2};
do_test(3, arr1, 0, arr2, 3, expected);
}
{
const int arr1[3] = {1, 2, 3};
const int arr2[2] = {1, 2};
const int expected[1] = {3};
do_test(3, arr1, 2, arr2, 1, expected);
}
{
const int* arr1 = NULL;
const int arr2[2] = {1, 2};
const int* expected = NULL;
do_test(0, arr1, 2, arr2, 0, expected);
}
{
const int arr1[5] = {1, 2, 3, 4, 5};
const int arr2[3] = {1, 3, 4};
const int expected[2] = {2, 5};
do_test(5, arr1, 3, arr2, 2, expected);
}
{
const int arr1[5] = {1, 2, 3, 4, 5};
const int arr2[5] = {1, 2, 3, 4, 5};
const int* expected = NULL;
do_test(5, arr1, 5, arr2, 0, expected);
}
return 0;
}