-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinf-test-text-cleanup.c
418 lines (356 loc) · 9.79 KB
/
inf-test-text-cleanup.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* libinfinity - a GObject-based infinote implementation
* Copyright (C) 2007, 2008 Armin Burgmeier <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "util/inf-test-util.h"
#include <libinftext/inf-text-session.h>
#include <libinftext/inf-text-default-insert-operation.h>
#include <libinftext/inf-text-insert-operation.h>
#include <libinftext/inf-text-delete-operation.h>
#include <libinftext/inf-text-default-buffer.h>
#include <libinftext/inf-text-user.h>
#include <libinfinity/common/inf-user-table.h>
#include <libinfinity/common/inf-standalone-io.h>
#include <libinfinity/common/inf-xml-util.h>
#include <string.h>
typedef struct {
guint total;
guint passed;
} test_result;
typedef enum {
INF_TEST_TEXT_CLEANUP_USER_UNAVAILABLE,
INF_TEST_TEXT_CLEANUP_UNSUPPORTED,
INF_TEST_TEXT_CLEANUP_VERIFY_FAILED
} InfTestTextCleanupError;
static GQuark
inf_test_text_cleanup_error_quark()
{
return g_quark_from_static_string("INF_TEST_TEXT_CLEANUP_ERROR");
}
static gboolean
perform_test(guint max_total_log_size,
InfTextChunk* initial,
GSList* users,
GSList* requests,
GError** error)
{
InfTextBuffer* buffer;
InfConnectionManager* manager;
InfIo* io;
InfTextSession* session;
InfAdoptedAlgorithm* algorithm;
InfUserTable* user_table;
InfTextUser* user;
gchar* user_name;
GSList* item;
xmlNodePtr request;
gboolean result;
GError* local_error;
guint verify_user_id;
InfAdoptedUser* verify_user;
guint verify_log_size;
gint verify_can_undo;
gint verify_can_redo;
InfAdoptedRequestLog* log;
guint log_size;
buffer = INF_TEXT_BUFFER(inf_text_default_buffer_new("UTF-8"));
inf_text_buffer_insert_chunk(buffer, 0, initial, NULL);
manager = inf_connection_manager_new();
io = INF_IO(inf_standalone_io_new());
user_table = inf_user_table_new();
local_error = NULL;
for(item = users; item != NULL; item = g_slist_next(item))
{
user_name = g_strdup_printf("User_%u", GPOINTER_TO_UINT(item->data));
user = INF_TEXT_USER(
g_object_new(
INF_TEXT_TYPE_USER,
"id", GPOINTER_TO_UINT(item->data),
"name", user_name,
"status", INF_USER_ACTIVE,
"flags", 0,
NULL
)
);
g_free(user_name);
inf_user_table_add_user(user_table, INF_USER(user));
g_object_unref(user);
}
session = INF_TEXT_SESSION(
g_object_new(
INF_TEXT_TYPE_SESSION,
"connection-manager", manager,
"buffer", buffer,
"io", io,
"user_table", user_table,
"max-total-log-size", max_total_log_size,
NULL
)
);
algorithm = inf_adopted_session_get_algorithm(INF_ADOPTED_SESSION(session));
g_object_unref(io);
g_object_unref(manager);
g_object_unref(user_table);
g_object_unref(buffer);
for(item = requests; item != NULL; item = item->next)
{
request = (xmlNodePtr)item->data;
if(strcmp((const char*)request->name, "request") == 0)
{
/* Request */
result = inf_net_object_received(
INF_NET_OBJECT(session),
NULL,
request,
&local_error
);
if(local_error != NULL)
goto fail;
}
else
{
/* TODO: Make an extra function out of this: */
/* Verify */
result = inf_xml_util_get_attribute_uint_required(
request,
"user",
&verify_user_id,
&local_error
);
if(result == FALSE)
goto fail;
verify_user = INF_ADOPTED_USER(
inf_user_table_lookup_user_by_id(user_table, verify_user_id)
);
if(verify_user == NULL)
{
g_set_error(
error,
inf_test_text_cleanup_error_quark(),
INF_TEST_TEXT_CLEANUP_USER_UNAVAILABLE,
"User ID '%u' not available",
verify_user_id
);
goto fail;
}
result = inf_xml_util_get_attribute_uint(
request,
"log-size",
&verify_log_size,
&local_error
);
if(local_error) goto fail;
if(result)
{
log = inf_adopted_user_get_request_log(INF_ADOPTED_USER(verify_user));
log_size = inf_adopted_request_log_get_end(log) -
inf_adopted_request_log_get_begin(log);
if(verify_log_size != log_size)
{
g_set_error(
error,
inf_test_text_cleanup_error_quark(),
INF_TEST_TEXT_CLEANUP_VERIFY_FAILED,
"Log size does not match; got %u, but expected %u",
log_size,
verify_log_size
);
goto fail;
}
}
result = inf_xml_util_get_attribute_int(
request,
"can-undo",
&verify_can_undo,
&local_error
);
if(local_error) goto fail;
if(result)
{
result = inf_adopted_algorithm_can_undo(algorithm, verify_user);
if(result != verify_can_undo)
{
g_set_error(
error,
inf_test_text_cleanup_error_quark(),
INF_TEST_TEXT_CLEANUP_VERIFY_FAILED,
"can-undo does not match; got %d, but expected %d",
(guint)result,
verify_can_undo
);
goto fail;
}
}
result = inf_xml_util_get_attribute_int(
request,
"can-redo",
&verify_can_redo,
&local_error
);
if(local_error) goto fail;
if(result)
{
result = inf_adopted_algorithm_can_redo(algorithm, verify_user);
if(result != verify_can_redo)
{
g_set_error(
error,
inf_test_text_cleanup_error_quark(),
INF_TEST_TEXT_CLEANUP_VERIFY_FAILED,
"can-redo does not match; got %d, but expected %d",
(guint)result,
verify_can_redo
);
goto fail;
}
}
}
}
g_object_unref(session);
return TRUE;
fail:
g_object_unref(session);
if(local_error) g_propagate_error(error, local_error);
return FALSE;
}
static void
foreach_test_func(const gchar* testfile,
gpointer user_data)
{
test_result* result;
xmlDocPtr doc;
xmlNodePtr root;
xmlNodePtr child;
GSList* requests;
InfTextChunk* initial;
GSList* users;
guint max_total_log_size;
GError* error;
gboolean res;
/* Only process XML files, not the Makefiles or other stuff */
if(!g_str_has_suffix(testfile, ".xml"))
return;
result = (test_result*)user_data;
doc = xmlParseFile(testfile);
requests = NULL;
initial = NULL;
users = NULL;
max_total_log_size = 0;
error = NULL;
printf("%s... ", testfile);
fflush(stdout);
++ result->total;
if(doc != NULL)
{
root = xmlDocGetRootElement(doc);
for(child = root->children; child != NULL; child = child->next)
{
if(child->type != XML_ELEMENT_NODE) continue;
if(strcmp((const char*)child->name, "log") == 0)
{
res = inf_xml_util_get_attribute_uint_required(
child,
"size",
&max_total_log_size,
&error
);
if(!res)
break;
}
else if(strcmp((const char*)child->name, "initial-buffer") == 0)
{
if(initial != NULL) inf_text_chunk_free(initial);
initial = inf_test_util_parse_buffer(child, &error);
if(initial == NULL) break;
}
else if(strcmp((const char*)child->name, "user") == 0)
{
if(inf_test_util_parse_user(child, &users, &error) == FALSE)
break;
}
else if(strcmp((const char*)child->name, "request") == 0 ||
strcmp((const char*)child->name, "verify") == 0)
{
requests = g_slist_prepend(requests, child);
}
else
{
g_set_error(
&error,
inf_test_util_parse_error_quark(),
INF_TEST_UTIL_PARSE_ERROR_UNEXPECTED_NODE,
"Node '%s' unexpected",
(const gchar*)child->name
);
break;
}
}
if(error != NULL)
{
printf("Failed to parse: %s\n", error->message);
g_error_free(error);
xmlFreeDoc(doc);
g_slist_free(requests);
if(initial != NULL) inf_text_chunk_free(initial);
g_slist_free(users);
}
else
{
g_assert(initial != NULL);
requests = g_slist_reverse(requests);
if(perform_test(max_total_log_size, initial, users, requests, &error) ==
TRUE)
{
++ result->passed;
printf("OK\n");
}
else
{
printf("FAILED (%s)\n", error->message);
g_error_free(error);
}
xmlFreeDoc(doc);
g_slist_free(requests);
inf_text_chunk_free(initial);
g_slist_free(users);
}
}
}
int main(int argc, char* argv[])
{
const char* dir;
GError* error;
test_result result;
g_type_init();
if(argc > 1)
dir = argv[1];
else
dir = "cleanup";
result.total = 0;
result.passed = 0;
error = NULL;
if(inf_test_util_dir_foreach(dir, foreach_test_func, &result, &error) ==
FALSE)
{
fprintf(stderr, "%s\n", error->message);
g_error_free(error);
return -1;
}
printf("%u out of %u tests passed\n", result.passed, result.total);
if(result.passed < result.total)
return -1;
return 0;
}