-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeanstester.c
64 lines (57 loc) · 1.41 KB
/
kmeanstester.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
#include <stdio.h>
#include <stdlib.h>
#define INSTRUCTION_FILE_PATH "kmeans_tester/instruction.txt"
#define ALLOC_CALLS_FILE_PATH "kmeans_tester/alloc_calls.txt"
int alloc_to_fail = -2;
int curr_alloc = 0;
void read_alloc_to_fail() {
FILE *fptr;
if (alloc_to_fail != -2) {
return;
}
fptr = fopen(INSTRUCTION_FILE_PATH, "r");
if (fptr == NULL) {
alloc_to_fail = -1;
return;
}
fscanf(fptr, "%d", &alloc_to_fail);
fclose(fptr);
}
void write_alloc_call(char type) {
FILE *fptr = fopen(ALLOC_CALLS_FILE_PATH, "a");
fputc(type, fptr);
fclose(fptr);
}
void *tester_malloc(size_t Size) {
read_alloc_to_fail();
if (alloc_to_fail == curr_alloc) {
return NULL;
}
else if (alloc_to_fail == -1) {
write_alloc_call('m');
}
curr_alloc++;
return malloc(Size);
}
void *tester_calloc(size_t NumOfElements, size_t SizeOfElement) {
read_alloc_to_fail();
if (alloc_to_fail == curr_alloc) {
return NULL;
}
else if (alloc_to_fail == -1) {
write_alloc_call('c');
}
curr_alloc++;
return calloc(NumOfElements, SizeOfElement);
}
void *tester_realloc(void *Memory, size_t NewSize) {
read_alloc_to_fail();
if (alloc_to_fail == curr_alloc) {
return NULL;
}
else if (alloc_to_fail == -1) {
write_alloc_call('r');
}
curr_alloc++;
return realloc(Memory, NewSize);
}