-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.c
More file actions
126 lines (116 loc) · 3.26 KB
/
tests.c
File metadata and controls
126 lines (116 loc) · 3.26 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
#include "hashtable.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// tests
int htCreateDestroy() {
hashtable* t = hashtableCreate(sizeof(int), 14, 0);
if (t->elementLength != 14) {
printf("Failed test. hashtable.elementLength not correct expected %d. "
"Got %d",
14, t->elementLength);
return 0;
}
if (t->elementStride != sizeof(int)) {
printf("Failed test. hashtable.elementStride not correct expected %lu. "
"Got %d",
sizeof(int), t->elementStride);
return 0;
}
if (t->memory == 0) {
printf("Failed test. hashtable.memory equals zero. Not malloced "
"correctly.");
return 0;
}
hashtableDestroy(t);
return 1;
}
int htInt() {
hashtable* t = hashtableCreate(sizeof(int), 4, 1);
int v = 22;
hashtableSet(t, "Int", &v);
int x;
hashtableGet(t, "Int", &x);
if (x != v) {
printf("Failed test. expected: %d. Got %d\n", v, x);
return 0;
}
return 1;
}
typedef struct test2 {
int num2;
} test2;
typedef struct testStruct {
int num;
char letter[50];
float pi;
test2* myTest;
} testStruct;
int htStruct() {
testStruct ts;
ts.num = 14;
ts.pi = 3.14f;
strcpy(ts.letter, "This is actually a string");
test2 mt;
mt.num2 = 35;
ts.myTest = &mt;
hashtable* t = hashtableCreate(sizeof(testStruct), 4, 1);
hashtableSet(t, "my table", &ts);
testStruct* x = malloc(sizeof(testStruct));
hashtableGet(t, "my table", x);
if (x->num != 14) {
printf("Failed test num. Setting and Getting a struct pointer failed. "
"Expected %d. Got %d\n",
14, x->num);
return 0;
}
if (fabsf(x->pi - 3.14f) > .0000000001f) {
printf("Failed test pi. Setting and Getting a struct pointer failed. "
"Expected %f. Got %f\n",
3.14f, x->pi);
return 0;
}
if (strcmp(x->letter, "This is actually a string")) {
printf("Failed test letter. Setting and Getting a struct pointer "
"failed. Expected %s. Got %s\n",
"This is actually a string", x->letter);
return 0;
}
testStruct t2;
t2.num = 38;
t2.pi = 3.142;
strcpy(t2.letter, "C");
test2 mt2;
mt2.num2 = 11;
t2.myTest = &mt2;
hashtableSet(t, "other table", &t2);
testStruct* s = malloc(sizeof(testStruct));
hashtableGet(t, "other table", s);
if (s->num != 38) {
printf("Failed test num. Setting and Getting a struct pointer failed. "
"Expected %d. Got %d\n",
38, s->num);
return 0;
}
if (fabsf(s->pi - 3.142f) > 0.001f) {
printf("Failed test. Setting and Getting a struct pointer failed. "
"Expected %f. Got %f\n",
3.142f, s->pi);
return 0;
}
if (strcmp(s->letter, "C")) {
printf("Failed test other table. Setting and Getting a struct pointer "
"failed. Expected %s. Got %s\n",
"C", s->letter);
return 0;
}
return 1;
}
int main() {
printf("Starting tests...\n");
int x = htCreateDestroy();
int y = htInt();
int z = htStruct();
return 0;
}