-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit_test.c
52 lines (42 loc) · 1.05 KB
/
unit_test.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "src/lib/utils.h"
void test_sort();
Country *test_data(int size);
int check_data(Country *array, int size);
int main (){
test_sort();
return 0;
}
void test_sort(){
int size = 10;
Country * array = test_data(size);
{
sort_by_density(array, size);
int result = check_data(array, size);
assert(result == 1);
}
{
int status = sort_by_density(NULL, size);
assert(status == 0);
}
free(array);
}
Country *test_data(int size){
Country *array = malloc(size * sizeof(Country));
for(int i = 0; i < size; i++){
array[i].square = rand() + 1;
array[i].population = rand() + 1;
array[i].density = (float)array[i].population / (float)array[i].square ;
}
return array;
}
int check_data(Country *array, int size) {
if (size > 1)
for(int i = 0, j = 1; i < (size - 1); i++, j++) {
if(array[i].density > array[j].density)
return 0;
}
return 1;
}