~ubuntu-branches/ubuntu/karmic/opensync/karmic

« back to all changes in this revision

Viewing changes to tests/check_vnote.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Collins
  • Date: 2005-12-18 00:59:13 UTC
  • Revision ID: james.westby@ubuntu.com-20051218005913-8keii9amu8dfkfnp
Tags: upstream-0.18
ImportĀ upstreamĀ versionĀ 0.18

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "support.h"
 
2
#include <time.h>
 
3
 
 
4
static void conv_vnote(const char *filename)
 
5
{
 
6
        char *command = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s .", g_get_current_dir(), filename);
 
7
        char *testbed = setup_testbed(NULL);
 
8
        system(command);
 
9
        g_free(command);
 
10
        
 
11
        
 
12
        OSyncError *error = NULL;
 
13
        OSyncEnv *env = init_env();
 
14
        
 
15
        OSyncFormatEnv *conv_env = osync_conv_env_new(env);
 
16
        fail_unless(conv_env != NULL, NULL);
 
17
 
 
18
        char *buffer;
 
19
        int size;
 
20
        
 
21
        char *file = g_path_get_basename(filename);
 
22
        fail_unless(osync_file_read(file, &buffer, &size, &error), NULL);
 
23
        
 
24
        OSyncChange *change = osync_change_new();
 
25
        osync_change_set_uid(change, file);             
 
26
        osync_change_set_data(change, buffer, size + 1, TRUE);
 
27
        osync_change_set_conv_env(change, conv_env);
 
28
        
 
29
        osync_change_set_objformat_string(change, "plain");
 
30
 
 
31
        OSyncObjFormat *sourceformat = osync_change_detect_objformat(conv_env, change, &error);
 
32
        fail_unless(sourceformat != NULL, NULL);
 
33
        osync_change_set_objformat(change, sourceformat);
 
34
        osync_change_set_objtype(change, osync_objformat_get_objtype(sourceformat));
 
35
        
 
36
        OSyncObjFormat *targetformat = osync_conv_find_objformat(conv_env, "xml-note");
 
37
        fail_unless(targetformat != NULL, NULL);
 
38
        
 
39
        OSyncChange *newchange = osync_change_copy(change, &error);
 
40
        fail_unless(newchange != NULL, NULL);
 
41
        
 
42
        //Convert to
 
43
        fail_unless(osync_change_convert(conv_env, change, targetformat, &error), NULL);
 
44
        
 
45
        //Compare old to new
 
46
        fail_unless(osync_change_compare(newchange, change) == CONV_DATA_SAME, NULL);
 
47
        
 
48
        //Convert back
 
49
        fail_unless(osync_change_convert(conv_env, change, sourceformat, &error), NULL);
 
50
 
 
51
        //Detect the output again
 
52
        osync_change_set_objformat_string(change, "plain");
 
53
        fail_unless(osync_change_detect_objformat(conv_env, change, &error) == sourceformat, NULL);
 
54
        
 
55
        //Compare again
 
56
        fail_unless(osync_change_compare(newchange, change) == CONV_DATA_SAME, NULL);
 
57
        
 
58
        osync_conv_env_free(conv_env);
 
59
        osync_env_finalize(env, NULL);
 
60
        osync_env_free(env);
 
61
        
 
62
        destroy_testbed(testbed);
 
63
}
 
64
 
 
65
static void compare_vnote(const char *lfilename, const char *rfilename, OSyncConvCmpResult result)
 
66
{
 
67
        char *command1 = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s lfile", g_get_current_dir(), lfilename);
 
68
        char *command2 = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s rfile", g_get_current_dir(), rfilename);
 
69
        char *testbed = setup_testbed(NULL);
 
70
        system(command1);
 
71
        g_free(command1);
 
72
        system(command2);
 
73
        g_free(command2);
 
74
        
 
75
        OSyncError *error = NULL;
 
76
        OSyncEnv *env = init_env();
 
77
        
 
78
        OSyncFormatEnv *conv_env = osync_conv_env_new(env);
 
79
        fail_unless(conv_env != NULL, NULL);
 
80
 
 
81
        char *buffer;
 
82
        int size;
 
83
        
 
84
        fail_unless(osync_file_read("lfile", &buffer, &size, &error), NULL);
 
85
        
 
86
        OSyncChange *lchange = osync_change_new();
 
87
        osync_change_set_uid(lchange, "lfile");
 
88
        osync_change_set_data(lchange, buffer, size + 1, TRUE);
 
89
        osync_change_set_conv_env(lchange, conv_env);
 
90
        osync_change_set_objformat_string(lchange, "plain");
 
91
 
 
92
        OSyncObjFormat *sourceformat = osync_change_detect_objformat(conv_env, lchange, &error);
 
93
        fail_unless(sourceformat != NULL, NULL);
 
94
        osync_change_set_objformat(lchange, sourceformat);
 
95
        osync_change_set_objtype(lchange, osync_objformat_get_objtype(sourceformat));
 
96
        
 
97
        fail_unless(osync_file_read("rfile", &buffer, &size, &error), NULL);
 
98
        
 
99
        OSyncChange *rchange = osync_change_new();
 
100
        osync_change_set_uid(rchange, "rfile");
 
101
        osync_change_set_data(rchange, buffer, size + 1, TRUE);
 
102
        osync_change_set_conv_env(rchange, conv_env);
 
103
        osync_change_set_objformat_string(rchange, "plain");
 
104
 
 
105
        sourceformat = osync_change_detect_objformat(conv_env, rchange, &error);
 
106
        fail_unless(sourceformat != NULL, NULL);
 
107
        osync_change_set_objformat(rchange, sourceformat);
 
108
        osync_change_set_objtype(rchange, osync_objformat_get_objtype(sourceformat));
 
109
        
 
110
        fail_unless(osync_change_compare(lchange, rchange) == result, NULL);
 
111
        
 
112
        osync_conv_env_free(conv_env);
 
113
        osync_env_finalize(env, NULL);
 
114
        osync_env_free(env);
 
115
        destroy_testbed(testbed);
 
116
}
 
117
 
 
118
static time_t vnote_get_revision(const char *filename)
 
119
{
 
120
        char *command = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s .", g_get_current_dir(), filename);
 
121
        char *testbed = setup_testbed(NULL);
 
122
        system(command);
 
123
        g_free(command);
 
124
        
 
125
        
 
126
        OSyncError *error = NULL;
 
127
        OSyncEnv *env = init_env();
 
128
        
 
129
        OSyncFormatEnv *conv_env = osync_conv_env_new(env);
 
130
        fail_unless(conv_env != NULL, NULL);
 
131
 
 
132
        char *buffer;
 
133
        int size;
 
134
        
 
135
        char *file = g_path_get_basename(filename);
 
136
        fail_unless(osync_file_read(file, &buffer, &size, &error), NULL);
 
137
        
 
138
        OSyncChange *change = osync_change_new();
 
139
        osync_change_set_uid(change, file);
 
140
        g_free(file);
 
141
        osync_change_set_data(change, buffer, size + 1, TRUE);
 
142
        osync_change_set_conv_env(change, conv_env);
 
143
        
 
144
        osync_change_set_objformat_string(change, "plain");
 
145
 
 
146
        OSyncObjFormat *sourceformat = osync_change_detect_objformat(conv_env, change, &error);
 
147
        fail_unless(sourceformat != NULL, NULL);
 
148
        osync_change_set_objformat(change, sourceformat);
 
149
        
 
150
        OSyncObjFormat *targetformat = osync_conv_find_objformat(conv_env, "xml-note");
 
151
        fail_unless(targetformat != NULL, NULL);
 
152
        
 
153
        fail_unless(osync_change_convert(conv_env, change, targetformat, &error), NULL);
 
154
        
 
155
        time_t time = osync_change_get_revision(change, &error);
 
156
        
 
157
        osync_conv_env_free(conv_env);
 
158
        osync_env_finalize(env, NULL);
 
159
        osync_env_free(env);
 
160
        
 
161
        destroy_testbed(testbed);
 
162
        return time;
 
163
}
 
164
 
 
165
START_TEST (conv_vnote1)
 
166
{
 
167
        conv_vnote("data/vnotes/vnote1.vnt");
 
168
}
 
169
END_TEST
 
170
 
 
171
START_TEST (conv_vnote2)
 
172
{
 
173
        conv_vnote("data/vnotes/vnote2.vnt");
 
174
}
 
175
END_TEST
 
176
 
 
177
START_TEST (conv_vnote3)
 
178
{
 
179
        conv_vnote("data/vnotes/vnote3.vnt");
 
180
}
 
181
END_TEST
 
182
 
 
183
START_TEST (conv_vnote_minimal)
 
184
{
 
185
        conv_vnote("data/vnotes/vnote-minimal.vnt");
 
186
}
 
187
END_TEST
 
188
 
 
189
START_TEST (get_revision1)
 
190
{
 
191
        struct tm testtm = {0, 0, 0, 6, 4 - 1, 2005 - 1900, 0, 0, 0};
 
192
        fail_unless(vnote_get_revision("data/vnotes/vnote1.vnt") == mktime(&testtm), NULL);
 
193
}
 
194
END_TEST
 
195
 
 
196
START_TEST (get_revision2)
 
197
{
 
198
        struct tm testtm = {1, 1, 1, 6, 4 - 1, 2005 - 1900, 0, 0, 0};
 
199
        fail_unless(vnote_get_revision("data/vnotes/vnote2.vnt") == mktime(&testtm), NULL);
 
200
}
 
201
END_TEST
 
202
 
 
203
START_TEST (get_revision3)
 
204
{
 
205
        struct tm testtm = {0, 0, 0, 6, 4 - 1, 2005 - 1900, 0, 0, 0};
 
206
        fail_unless(vnote_get_revision("data/vnotes/vnote3.vnt") == mktime(&testtm), NULL);
 
207
}
 
208
END_TEST
 
209
 
 
210
START_TEST (get_revision4)
 
211
{
 
212
        fail_unless(vnote_get_revision("data/vnotes/vnote-minimal.vnt") == -1, NULL);
 
213
}
 
214
END_TEST
 
215
 
 
216
START_TEST (compare_vnote_same1)
 
217
{
 
218
        compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote1.vnt", CONV_DATA_SAME);
 
219
}
 
220
END_TEST
 
221
 
 
222
START_TEST (compare_vnote_same2)
 
223
{
 
224
        compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote1-same.vnt", CONV_DATA_SAME);
 
225
}
 
226
END_TEST
 
227
 
 
228
START_TEST (compare_vnote_similar1)
 
229
{
 
230
        compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote1-similar.vnt", CONV_DATA_SIMILAR);
 
231
}
 
232
END_TEST
 
233
 
 
234
START_TEST (compare_vnote_mismatch1)
 
235
{
 
236
        compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote2.vnt", CONV_DATA_MISMATCH);
 
237
}
 
238
END_TEST
 
239
 
 
240
START_TEST (compare_vnote_mismatch2)
 
241
{
 
242
        compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote-minimal.vnt", CONV_DATA_MISMATCH);
 
243
}
 
244
END_TEST
 
245
 
 
246
Suite *vnote_suite(void)
 
247
{
 
248
        Suite *s = suite_create("VNote");
 
249
        //Suite *s2 = suite_create("VNote");
 
250
        
 
251
        create_case(s, "conv_vnote1", conv_vnote1);
 
252
        create_case(s, "conv_vnote2", conv_vnote2);
 
253
        create_case(s, "conv_vnote3", conv_vnote3);
 
254
        create_case(s, "conv_vnote_minimal", conv_vnote_minimal);
 
255
        
 
256
        create_case(s, "get_revision1", get_revision1);
 
257
        create_case(s, "get_revision2", get_revision2);
 
258
        create_case(s, "get_revision3", get_revision3);
 
259
        create_case(s, "get_revision4", get_revision4);
 
260
        
 
261
        create_case(s, "compare_vnote_same1", compare_vnote_same1);
 
262
        create_case(s, "compare_vnote_same2", compare_vnote_same2);
 
263
        create_case(s, "compare_vnote_similar1", compare_vnote_similar1);
 
264
        create_case(s, "compare_vnote_mismatch1", compare_vnote_mismatch1);
 
265
        create_case(s, "compare_vnote_mismatch2", compare_vnote_mismatch2);
 
266
        
 
267
        return s;
 
268
}
 
269
 
 
270
int main(void)
 
271
{
 
272
        int nf;
 
273
 
 
274
        Suite *s = vnote_suite();
 
275
        
 
276
        SRunner *sr;
 
277
        sr = srunner_create(s);
 
278
        srunner_run_all(sr, CK_NORMAL);
 
279
        nf = srunner_ntests_failed(sr);
 
280
        srunner_free(sr);
 
281
        return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 
282
}