-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmove.c
406 lines (332 loc) · 14.6 KB
/
move.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
/* move.c
*
* Copyright 2022 Matthew Jakeman <[email protected]>
*
* This file is dual-licensed under the terms of the Mozilla Public
* License 2.0 and the Lesser General Public License 2.1 (or any
* later version).
*
* SPDX-License-Identifier: MPL-2.0 OR LGPL-2.1-or-later
*/
#include <glib.h>
#include <locale.h>
#include <model/document.h>
#include <model/paragraph.h>
#include <model/run.h>
#include <editor/editor.h>
typedef struct {
TextDocument *doc;
TextEditor *editor;
TextRun *run1;
TextRun *run2;
TextRun *run3;
TextRun *run4;
} MoveFixture;
#define RUN1 "Once upon a time there was a little dog, "
#define RUN2 "AND HIS NAME WAS ROVER."
#define RUN3 "By J. R. R. Tolkien"
#define RUN4 "Roverandom, 1920s"
static void
move_fixture_set_up_single (MoveFixture *fixture,
gconstpointer user_data)
{
TextFrame *frame;
TextParagraph *para1;
TextRun *run1;
frame = text_frame_new ();
para1 = text_paragraph_new ();
run1 = text_run_new (RUN1);
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run1));
text_frame_append_block (frame, TEXT_BLOCK (para1));
fixture->doc = text_document_new ();
fixture->doc->frame = frame;
fixture->editor = text_editor_new (fixture->doc);
text_editor_move_first (fixture->editor, TEXT_EDITOR_CURSOR);
fixture->run1 = run1;
fixture->run2 = NULL;
fixture->run3 = NULL;
fixture->run4 = NULL;
}
#define RUN5 "This is some text that is pa"
#define RUN6 "RT OF TWO DIFFE"
#define RUN7 "rent runs"
static void
move_fixture_set_up_runs (MoveFixture *fixture,
gconstpointer user_data)
{
TextFrame *frame;
TextParagraph *para1;
TextRun *run1, *run2, *run3;
frame = text_frame_new ();
para1 = text_paragraph_new ();
run1 = text_run_new (RUN5);
run2 = text_run_new (RUN6);
run3 = text_run_new (RUN7);
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run1));
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run2));
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run3));
text_frame_append_block (frame, TEXT_BLOCK (para1));
fixture->doc = text_document_new ();
fixture->doc->frame = frame;
fixture->editor = text_editor_new (fixture->doc);
text_editor_move_first (fixture->editor, TEXT_EDITOR_CURSOR);
fixture->run1 = run1;
fixture->run2 = run2;
fixture->run3 = run3;
fixture->run4 = NULL;
}
static void
move_fixture_set_up_paragraphs (MoveFixture *fixture,
gconstpointer user_data)
{
TextFrame *frame;
TextParagraph *para1, *para2, *para3;
TextRun *run1, *run2, *run3, *run4;
frame = text_frame_new ();
para1 = text_paragraph_new ();
run1 = text_run_new (RUN1);
run2 = text_run_new (RUN2);
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run1));
text_paragraph_append_fragment(para1, TEXT_FRAGMENT (run2));
text_frame_append_block (frame, TEXT_BLOCK (para1));
para2 = text_paragraph_new ();
run3 = text_run_new (RUN3);
text_paragraph_append_fragment(para2, TEXT_FRAGMENT (run3));
text_frame_append_block (frame, TEXT_BLOCK (para2));
para3 = text_paragraph_new ();
run4 = text_run_new (RUN4);
text_paragraph_append_fragment(para3, TEXT_FRAGMENT (run4));
text_frame_append_block (frame, TEXT_BLOCK (para3));
fixture->doc = text_document_new ();
fixture->doc->frame = frame;
fixture->editor = text_editor_new (fixture->doc);
text_editor_move_first (fixture->editor, TEXT_EDITOR_CURSOR);
fixture->run1 = run1;
fixture->run2 = run2;
fixture->run3 = run3;
fixture->run4 = run4;
}
static void
move_fixture_tear_down (MoveFixture *fixture,
gconstpointer user_data)
{
g_object_unref (fixture->editor);
g_object_unref (fixture->doc);
}
static void
test_left_guard (MoveFixture *fixture,
gconstpointer user_data)
{
int amount;
amount = (int)user_data;
// test moving left at the start of the document
text_editor_move_left (fixture->editor, TEXT_EDITOR_CURSOR, amount);
// cursor position unchanged
g_assert_cmpint (fixture->doc->cursor->index, ==, 0);
}
static void
test_right_guard (MoveFixture *fixture,
gconstpointer user_data)
{
int amount;
amount = (int)user_data;
// go to end
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 41);
// test moving right at the end of the document
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, amount);
// cursor position unchanged
g_assert_cmpint (fixture->doc->cursor->index, ==, 41);
}
// We have three runs in a single paragraph. Capitalisation indicates
// a different run for these test cases:
//
// index 28
// /
// `This is some text that is paRT OF TWO DIFFErent runs`
// ^
// index 29 /
//
// When traversing leftwards from index 29, we cross into a new
// run at index 28.
static void
test_left_traversal_across_run (MoveFixture *fixture,
gconstpointer user_data)
{
int amount;
amount = (int)user_data;
// go to index 29 (run two)
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 29);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run2));
// test moving left by amount
text_editor_move_left (fixture->editor, TEXT_EDITOR_CURSOR, amount);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run1));
g_assert_cmpint (fixture->doc->cursor->index, ==, 29 - amount);
}
static void
test_right_traversal_across_run (MoveFixture *fixture,
gconstpointer user_data)
{
int amount;
amount = (int)user_data;
// go to index 28 (run one)
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 28);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run1));
// test moving right by amount
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, amount);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run2));
g_assert_cmpint (fixture->doc->cursor->index, ==, 28 + amount);
}
// We have four runs across three paragraphs. Again, capitalisation
// represents the start of a new run.
//
// first index in run 2 index 64
// \ \
// p1: `Once upon a time there was a little dog, AND HIS NAME WAS ROVER.`
// p2: `By J. R. R. Tolkien`
// p3: `Roverandom, 1920s`
//
// The end of p1 is index 64, after the full stop. The start of p2 is
// naturally index 0. When traversing one character from index 64, the
// cursor should move to index 0. Traversing 10 characters would move to
// index 9, etc.
//
// Traversing 21 characters should place the cursor at p3 index 0.
static void
test_left_traversal_across_paragraph (MoveFixture *fixture,
gconstpointer user_data)
{
int amount;
amount = (int)user_data;
// move to start of p2 (run3)
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 65);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run3));
g_assert_cmpint (fixture->doc->cursor->index, ==, 0);
// move backwards by amount
text_editor_move_left (fixture->editor, TEXT_EDITOR_CURSOR, amount);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run2));
// check index
g_assert_cmpint (fixture->doc->cursor->index, ==, (64 - (amount - 1)));
}
static void
test_right_traversal_across_paragraph (MoveFixture *fixture,
gconstpointer user_data)
{
int amount;
amount = (int)user_data;
// move to end of p1 (run2)
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 64);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run2));
// move forwards by amount
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, amount);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run3));
// check index
g_assert_cmpint (fixture->doc->cursor->index, ==, amount - 1);
}
static void
test_left_traversal_across_several_paragraphs (MoveFixture *fixture,
gconstpointer user_data)
{
// move to start of p3 (run4)
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 85);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run4));
// move left by 62 characters (to run1)
text_editor_move_left (fixture->editor, TEXT_EDITOR_CURSOR, 62);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run1));
// check index is 23 in p1
g_assert_cmpint (fixture->doc->cursor->index, ==, 23);
}
static void
test_right_traversal_across_several_paragraphs (MoveFixture *fixture,
gconstpointer user_data)
{
// move to p3, index 2 (run4)
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, 87);
g_assert_true (text_editor_get_item (fixture->editor, TEXT_EDITOR_CURSOR) == TEXT_FRAGMENT (fixture->run4));
// check index is 2 in p3
g_assert_cmpint (fixture->doc->cursor->index, ==, 2);
}
static void
test_balanced_traversal (MoveFixture *fixture,
gconstpointer user_data)
{
int amount;
int old_index;
TextParagraph *old_paragraph;
amount = (int)user_data;
old_index = fixture->doc->cursor->index;
old_paragraph = fixture->doc->cursor->paragraph;
// ensure that equal left and right movements return to the same position
text_editor_move_right (fixture->editor, TEXT_EDITOR_CURSOR, amount);
text_editor_move_left (fixture->editor, TEXT_EDITOR_CURSOR, amount);
g_assert_true (old_index == fixture->doc->cursor->index);
g_assert_true (old_paragraph == fixture->doc->cursor->paragraph);
}
int
main (int argc, char *argv[])
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
// Document guard tests
g_test_add ("/text-engine/editor/move/test-left-guard-one", MoveFixture, (void*)1,
move_fixture_set_up_single, test_left_guard,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-left-guard-ten", MoveFixture, (void*)10,
move_fixture_set_up_single, test_left_guard,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-right-guard-one", MoveFixture, (void*)1,
move_fixture_set_up_single, test_right_guard,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-right-guard-ten", MoveFixture, (void*)10,
move_fixture_set_up_single, test_right_guard,
move_fixture_tear_down);
// Run boundary tests
g_test_add ("/text-engine/editor/move/test-left-traversal-across-run-one", MoveFixture, (void*)1,
move_fixture_set_up_runs, test_left_traversal_across_run,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-left-traversal-across-run-ten", MoveFixture, (void*)10,
move_fixture_set_up_runs, test_left_traversal_across_run,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-right-traversal-across-run-one", MoveFixture, (void*)1,
move_fixture_set_up_runs, test_right_traversal_across_run,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-right-traversal-across-run-ten", MoveFixture, (void*)10,
move_fixture_set_up_runs, test_right_traversal_across_run,
move_fixture_tear_down);
// Paragraph boundary tests
g_test_add ("/text-engine/editor/move/test-left-traversal-across-paragraph-one", MoveFixture, (void*)1,
move_fixture_set_up_paragraphs, test_left_traversal_across_paragraph,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-right-traversal-across-paragraph-one", MoveFixture, (void*)1,
move_fixture_set_up_paragraphs, test_right_traversal_across_paragraph,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-left-traversal-across-paragraph-five", MoveFixture, (void*)5,
move_fixture_set_up_paragraphs, test_left_traversal_across_paragraph,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-right-traversal-across-paragraph-five", MoveFixture, (void*)5,
move_fixture_set_up_paragraphs, test_right_traversal_across_paragraph,
move_fixture_tear_down);
// Multi-paragraph boundary tests
g_test_add ("/text-engine/editor/move/test-left-traversal-across-several-paragraphs", MoveFixture, NULL,
move_fixture_set_up_paragraphs, test_left_traversal_across_several_paragraphs,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-right-traversal-across-several-paragraphs", MoveFixture, NULL,
move_fixture_set_up_paragraphs, test_right_traversal_across_several_paragraphs,
move_fixture_tear_down);
// Balance tests
g_test_add ("/text-engine/editor/move/test-balanced-traversal-one", MoveFixture, (void*)1,
move_fixture_set_up_paragraphs, test_balanced_traversal,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-balanced-traversal-five", MoveFixture, (void*)5,
move_fixture_set_up_paragraphs, test_balanced_traversal,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-balanced-traversal-ten", MoveFixture, (void*)10,
move_fixture_set_up_paragraphs, test_balanced_traversal,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-balanced-traversal-fifty", MoveFixture, (void*)50,
move_fixture_set_up_paragraphs, test_balanced_traversal,
move_fixture_tear_down);
g_test_add ("/text-engine/editor/move/test-balanced-traversal-hundred", MoveFixture, (void*)100,
move_fixture_set_up_paragraphs, test_balanced_traversal,
move_fixture_tear_down);
return g_test_run ();
}