forked from Loures/objstore_sol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
144 lines (134 loc) · 3.66 KB
/
client.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <objstore.h>
//#define ERRSTR objstore_errstr
#define test_str "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis arcu eu tempor cras amet.\n"
#define test_strlen 100
static ssize_t failed_tests = 0;
//Create 20 strings, with sizes ranging from 55 * 100 to
char *repeat_str(int n) {
char *repeat = (char*)calloc(test_strlen * n, sizeof(char));
size_t seek = 0;
while (seek < test_strlen * n - 100) {
memcpy(repeat + seek, test_str, 100);
seek = seek + test_strlen;
}
memcpy(repeat + seek, test_str, 99);
return repeat;
}
int test1() {
if (!os_store("Object1", test_str, test_strlen)) {
#ifdef ERRSTR
printf("%s", ERRSTR);
#endif
failed_tests++;
return 0;
}
for (int i = 1; i <= 18; i++) {
char name[128];
memset(name, 0, 128);
sprintf(name, "Object%d", i + 1);
char *str = repeat_str(i * 55);
if (!os_store(name, str, i * test_strlen * 55)) {
free(str);
#ifdef ERRSTR
printf("%s", ERRSTR);
#endif
failed_tests++;
return 0;
}
free(str);
}
char *str = repeat_str(1000);
if (!os_store("Object20", str, 1000 * test_strlen)) {
free(str);
#ifdef ERRSTR
printf("%s", ERRSTR);
#endif
failed_tests++;
return 0;
}
free(str);
os_disconnect();
return 1;
}
int test2() {
char *str_first = os_retrieve("Object1");
if (!(str_first && strlen(str_first) == 100)) {
#ifdef ERRSTR
printf("%s", ERRSTR);
#endif
failed_tests++;
free(str_first);
return 0;
}
free(str_first);
for (int i = 2; i <= 19; i++) {
char name[128];
memset(name, 0, 128);
sprintf(name, "Object%d", i);
char *str = os_retrieve(name);
if (!(str && strlen(str) + 1 == (i - 1) * test_strlen * 55)) {
#ifdef ERRSTR
printf("%s", ERRSTR);
#endif
failed_tests++;
free(str);
return 0;
}
free(str);
}
char *str_last = os_retrieve("Object20");
if (!(str_last && strlen(str_last) + 1 == 100000)) {
#ifdef ERRSTR
printf("%s", ERRSTR);
#endif
failed_tests++;
free(str_last);
return 0;
}
free(str_last);
os_disconnect();
return 1;
}
int test3() {
for (int i = 1; i <= 20; i++) {
char name[128];
memset(name, 0, 128);
sprintf(name, "Object%d", i);
if (!os_delete(name)) {
#ifdef ERRSTR
printf("%s", ERRSTR);
#endif
failed_tests++;
os_disconnect();
return 0;
};
}
os_disconnect();
return 1;
}
int main(int argc, char *argv[]) {
if (os_connect(argv[1]) && argv[2]) {
switch (atoi(argv[2])) {
case 1:
printf("Test passati: %ld\n", 20 - failed_tests);
printf("Test fallito: %ld\n", failed_tests);
return !test1();
break;
case 2:
printf("Test passati: %ld\n", 20 - failed_tests);
printf("Test fallito: %ld\n", failed_tests);
return !test2();
break;
case 3:
printf("Test passati: %ld\n", 20 - failed_tests);
printf("Test fallito: %ld\n", failed_tests);
return !test3();
break;
default:
break;
}
} else return 1;
}