File: test-buffer-output-stream.c

package info (click to toggle)
gtksourceview4 4.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,840 kB
  • sloc: ansic: 49,715; sh: 4,917; xml: 1,302; makefile: 1,010; python: 230; perl: 52; yacc: 45; cobol: 20; objc: 19; sed: 16; fortran: 14; cpp: 8; ml: 3
file content (424 lines) | stat: -rw-r--r-- 12,467 bytes parent folder | download | duplicates (2)
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
419
420
421
422
423
424
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/*
 * This file is part of GtkSourceView
 *
 * Copyright (C) 2010 - Ignacio Casal Quinteiro
 * Copyright (C) 2014 - S├ębastien Wilmet
 *
 * GtkSourceView 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.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView 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, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include <string.h>
#include <gtksourceview/gtksource.h>
#include "gtksourceview/gtksourcebufferoutputstream.h"

static void
test_consecutive_write (const gchar          *inbuf,
			const gchar          *outbuf,
			gsize                 write_chunk_len,
			GtkSourceNewlineType  newline_type)
{
	GtkSourceBuffer *source_buffer;
	GtkSourceBufferOutputStream *out;
	gsize len;
	gssize n, w;
	GError *err = NULL;
	gchar *b;
	GtkSourceNewlineType type;
	GSList *encodings = NULL;

	source_buffer = gtk_source_buffer_new (NULL);
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_utf8 ());
	out = gtk_source_buffer_output_stream_new (source_buffer, encodings, TRUE);

	n = 0;

	do
	{
		len = MIN (write_chunk_len, strlen (inbuf + n));
		w = g_output_stream_write (G_OUTPUT_STREAM (out), inbuf + n, len, NULL, &err);
		g_assert_cmpint (w, >=, 0);
		g_assert_no_error (err);

		n += w;
	} while (w != 0);

	g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err);

	g_assert_no_error (err);

	type = gtk_source_buffer_output_stream_detect_newline_type (out);
	g_assert (type == newline_type);

	g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err);
	g_assert_no_error (err);

	g_object_get (G_OBJECT (source_buffer), "text", &b, NULL);

	g_assert_cmpstr (outbuf, ==, b);
	g_free (b);

	g_assert (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (source_buffer)) == FALSE);

	g_object_unref (source_buffer);
	g_object_unref (out);
}

static void
test_empty (void)
{
	test_consecutive_write ("", "", 10, GTK_SOURCE_NEWLINE_TYPE_DEFAULT);
	test_consecutive_write ("\r\n", "", 10, GTK_SOURCE_NEWLINE_TYPE_CR_LF);
	test_consecutive_write ("\r", "", 10, GTK_SOURCE_NEWLINE_TYPE_CR);
	test_consecutive_write ("\n", "", 10, GTK_SOURCE_NEWLINE_TYPE_LF);
}

static void
test_consecutive (void)
{
	test_consecutive_write ("hello\nhow\nare\nyou", "hello\nhow\nare\nyou", 2,
				GTK_SOURCE_NEWLINE_TYPE_LF);
	test_consecutive_write ("hello\rhow\rare\ryou", "hello\rhow\rare\ryou", 2,
				GTK_SOURCE_NEWLINE_TYPE_CR);
	test_consecutive_write ("hello\r\nhow\r\nare\r\nyou", "hello\r\nhow\r\nare\r\nyou", 2,
				GTK_SOURCE_NEWLINE_TYPE_CR_LF);
}

static void
test_consecutive_tnewline (void)
{
	test_consecutive_write ("hello\nhow\nare\nyou\n", "hello\nhow\nare\nyou", 2,
				GTK_SOURCE_NEWLINE_TYPE_LF);
	test_consecutive_write ("hello\rhow\rare\ryou\r", "hello\rhow\rare\ryou", 2,
				GTK_SOURCE_NEWLINE_TYPE_CR);
	test_consecutive_write ("hello\r\nhow\r\nare\r\nyou\r\n", "hello\r\nhow\r\nare\r\nyou", 2,
				GTK_SOURCE_NEWLINE_TYPE_CR_LF);
}

static void
test_big_char (void)
{
	test_consecutive_write ("\343\203\200\343\203\200", "\343\203\200\343\203\200", 2,
				GTK_SOURCE_NEWLINE_TYPE_LF);
}

static void
test_boundary (void)
{
	GtkSourceBuffer *source_buffer;
	GtkSourceBufferOutputStream *out;
	gint line_count;
	GError *err = NULL;
	GSList *encodings = NULL;

	source_buffer = gtk_source_buffer_new (NULL);
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_utf8 ());
	out = gtk_source_buffer_output_stream_new (source_buffer, encodings, TRUE);

	g_output_stream_write (G_OUTPUT_STREAM (out), "\r", 1, NULL, NULL);
	g_output_stream_write (G_OUTPUT_STREAM (out), "\n", 1, NULL, NULL);

	g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err);
	g_assert_no_error (err);

	line_count = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (source_buffer));

	g_assert_cmpint (line_count, ==, 2);

	g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err);
	g_assert_no_error (err);

	g_object_unref (source_buffer);
	g_object_unref (out);
}

#if 0
static void
test_invalid_utf8 (void)
{
	test_consecutive_write ("foobar\n\xef\xbf\xbe", "foobar\n\\EF\\BF\\BE", 10,
	                        GTK_SOURCE_NEWLINE_TYPE_LF);
	test_consecutive_write ("foobar\n\xef\xbf\xbezzzzzz\n", "foobar\n\\EF\\BF\\BEzzzzzz", 10,
	                        GTK_SOURCE_NEWLINE_TYPE_LF);
	test_consecutive_write ("\xef\xbf\xbezzzzzz\n", "\\EF\\BF\\BEzzzzzz", 10,
	                        GTK_SOURCE_NEWLINE_TYPE_LF);
}
#endif

/* SMART CONVERSION */

#define TEXT_TO_CONVERT "this is some text to make the tests"
#define TEXT_TO_GUESS "hello \xe6\x96\x87 world"

static gchar *
get_encoded_text (const gchar             *text,
		  gint                     nread,
		  const GtkSourceEncoding *to,
		  const GtkSourceEncoding *from,
		  gsize                   *bytes_written_aux,
		  gboolean                 care_about_error)
{
	GCharsetConverter *converter;
	gchar *out, *out_aux;
	gsize bytes_read, bytes_read_aux;
	gsize bytes_written;
	GConverterResult res;
	GError *err;

	converter = g_charset_converter_new (gtk_source_encoding_get_charset (to),
					     gtk_source_encoding_get_charset (from),
					     NULL);

	out = g_malloc (200);
	out_aux = g_malloc (200);
	err = NULL;
	bytes_read_aux = 0;
	*bytes_written_aux = 0;

	if (nread == -1)
	{
		nread = strlen (text);
	}

	do
	{
		res = g_converter_convert (G_CONVERTER (converter),
		                           text + bytes_read_aux,
		                           nread,
		                           out_aux,
		                           200,
		                           G_CONVERTER_INPUT_AT_END,
		                           &bytes_read,
		                           &bytes_written,
		                           &err);
		memcpy (out + *bytes_written_aux, out_aux, bytes_written);
		bytes_read_aux += bytes_read;
		*bytes_written_aux += bytes_written;
		nread -= bytes_read;
	} while (res != G_CONVERTER_FINISHED && res != G_CONVERTER_ERROR);

	if (care_about_error)
	{
		g_assert_no_error (err);
	}
	else if (err)
	{
		g_printf ("** You don't care, but there was an error: %s", err->message);
		return NULL;
	}

	out[*bytes_written_aux] = '\0';

	if (!g_utf8_validate (out, *bytes_written_aux, NULL) && !care_about_error)
	{
		if (!care_about_error)
		{
			return NULL;
		}
		else
		{
			g_assert_not_reached ();
		}
	}

	return out;
}

static gchar *
do_test (const gchar              *inbuf,
	 const gchar              *enc,
	 GSList                   *encodings,
	 gsize                     len,
	 gsize                     write_chunk_len,
	 const GtkSourceEncoding **guessed)
{
	GtkSourceBuffer *source_buffer;
	GtkSourceBufferOutputStream *out;
	GError *err = NULL;
	GtkTextIter start, end;
	gchar *text;
	gsize to_write;
	gssize n, w;

	if (enc != NULL)
	{
		encodings = NULL;
		encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_from_charset (enc));
	}

	source_buffer = gtk_source_buffer_new (NULL);
	out = gtk_source_buffer_output_stream_new (source_buffer, encodings, TRUE);

	n = 0;

	do
	{
		to_write = MIN (len, write_chunk_len);
		w = g_output_stream_write (G_OUTPUT_STREAM (out), inbuf + n, to_write, NULL, &err);
		g_assert_cmpint (w, >=, 0);
		g_assert_no_error (err);

		len -= w;
		n += w;
	} while (len != 0);

	g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err);
	g_assert_no_error (err);

	g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err);
	g_assert_no_error (err);

	if (guessed != NULL)
	{
		*guessed = gtk_source_buffer_output_stream_get_guessed (out);
	}

	gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (source_buffer), &start, &end);
	text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (source_buffer),
	                                 &start,
	                                 &end,
	                                 FALSE);

	g_object_unref (source_buffer);
	g_object_unref (out);

	return text;
}

static void
test_utf8_utf8 (void)
{
	gchar *aux;

	aux = do_test (TEXT_TO_CONVERT, "UTF-8", NULL, strlen (TEXT_TO_CONVERT), strlen (TEXT_TO_CONVERT), NULL);
	g_assert_cmpstr (aux, ==, TEXT_TO_CONVERT);
	g_free (aux);

	aux = do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 18, 18, NULL);
	g_assert_cmpstr (aux, ==, "foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz");
	g_free (aux);

	/* small chunk */
	aux = do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 18, 2, NULL);
	g_assert_cmpstr (aux, ==, "foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz");
	g_free (aux);
}

static void
test_empty_conversion (void)
{
	const GtkSourceEncoding *guessed;
	gchar *out;
	GSList *encodings = NULL;

	/* testing the case of an empty file and list of encodings with no
	   utf-8. In this case, the smart converter cannot determine the right
	   encoding (because there is no input), but should still default to
	   utf-8 for the detection */
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_from_charset ("UTF-16"));
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_from_charset ("ISO-8859-15"));

	out = do_test ("", NULL, encodings, 0, 0, &guessed);

	g_assert_cmpstr (out, ==, "");
	g_free (out);

	g_assert (guessed == gtk_source_encoding_get_utf8 ());
}

static void
test_guessed (void)
{
	GSList *encs = NULL;
	gchar *aux, *aux2, *fail;
	gsize aux_len, fail_len;
	const GtkSourceEncoding *guessed;

	aux = get_encoded_text (TEXT_TO_GUESS, -1,
	                        gtk_source_encoding_get_from_charset ("UTF-16"),
	                        gtk_source_encoding_get_from_charset ("UTF-8"),
	                        &aux_len,
	                        TRUE);

	fail = get_encoded_text (aux, aux_len,
	                         gtk_source_encoding_get_from_charset ("UTF-8"),
	                         gtk_source_encoding_get_from_charset ("ISO-8859-15"),
	                         &fail_len,
	                         FALSE);

	g_assert (fail == NULL);

	/* ISO-8859-15 should fail */
	encs = g_slist_append (encs, (gpointer)gtk_source_encoding_get_from_charset ("ISO-8859-15"));
	encs = g_slist_append (encs, (gpointer)gtk_source_encoding_get_from_charset ("UTF-16"));

	aux2 = do_test (aux, NULL, encs, aux_len, aux_len, &guessed);
	g_free (aux);
	g_free (aux2);

	g_assert (guessed == gtk_source_encoding_get_from_charset ("UTF-16"));
}

static void
test_utf16_utf8 (void)
{
	gchar *text, *aux;
	gsize aux_len;

	text = get_encoded_text ("\xe2\xb4\xb2", -1,
	                         gtk_source_encoding_get_from_charset ("UTF-16"),
	                         gtk_source_encoding_get_from_charset ("UTF-8"),
	                         &aux_len,
	                         TRUE);

	aux = do_test (text, "UTF-16", NULL, aux_len, aux_len, NULL);
	g_assert_cmpstr (aux, ==, "\xe2\xb4\xb2");
	g_free (aux);

	aux = do_test (text, "UTF-16", NULL, aux_len, 1, NULL);
	g_assert_cmpstr (aux, ==, "\xe2\xb4\xb2");
	g_free (aux);
}

gint
main (gint   argc,
      gchar *argv[])
{
	g_test_init (&argc, &argv, NULL);

	g_test_add_func ("/buffer-output-stream/empty", test_empty);

	g_test_add_func ("/buffer-output-stream/consecutive", test_consecutive);
	g_test_add_func ("/buffer-output-stream/consecutive_tnewline", test_consecutive_tnewline);
	g_test_add_func ("/buffer-output-stream/big-char", test_big_char);
	g_test_add_func ("/buffer-output-stream/test-boundary", test_boundary);


	/* This broke after https://bugzilla.gnome.org/show_bug.cgi?id=694669 We
	 * need to revisit the test to pick something that is actually invalid
	 * utf8.
	 */
#if 0
	g_test_add_func ("/buffer-output-stream/test-invalid-utf8", test_invalid_utf8);
#endif
	g_test_add_func ("/buffer-output-stream/smart conversion: utf8-utf8", test_utf8_utf8);
	g_test_add_func ("/buffer-output-stream/smart conversion: empty", test_empty_conversion);
	g_test_add_func ("/buffer-output-stream/smart conversion: guessed", test_guessed);
	g_test_add_func ("/buffer-output-stream/smart conversion: utf16-utf8", test_utf16_utf8);

	return g_test_run ();
}