File: test-encoding-conversion.c

package info (click to toggle)
libgedit-gtksourceview 299.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,608 kB
  • sloc: ansic: 49,191; xml: 2,676; perl: 206; sh: 57; yacc: 45; makefile: 30; cobol: 20; objc: 19; javascript: 16; fortran: 14; python: 13; cpp: 8; ml: 3
file content (60 lines) | stat: -rw-r--r-- 1,448 bytes parent folder | download
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
/* SPDX-FileCopyrightText: 2025 - Sébastien Wilmet
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <gtksourceview/gtksource.h>
#include "gtksourceview/file-loading-and-saving/gtksourceencodingconversion.h"

static void
test_try_convert_success (void)
{
	GtkSourceEncoding *utf8_encoding;
	gboolean ret;

	utf8_encoding = gtk_source_encoding_new_utf8 ();

	ret = _gtk_source_encoding_conversion_try_convert (utf8_encoding, "a", 1);
	g_assert_true (ret);

	gtk_source_encoding_free (utf8_encoding);
}

static void
test_try_convert_illegal_sequence (void)
{
	GtkSourceEncoding *utf8_encoding;
	guint8 input_buffer[1];
	gboolean ret;

	utf8_encoding = gtk_source_encoding_new_utf8 ();

	// 255, aka "1111 1111" in binary and 0xFF in hexadecimal.
	// In UTF-8, this is an invalid byte.
	// See https://en.wikipedia.org/wiki/UTF-8
	input_buffer[0] = 255;

	ret = _gtk_source_encoding_conversion_try_convert (utf8_encoding,
							   (const gchar *) input_buffer,
							   1);
	g_assert_false (ret);

	gtk_source_encoding_free (utf8_encoding);
}

int
main (int    argc,
      char **argv)
{
	int exit_status;

	gtk_source_init ();
	g_test_init (&argc, &argv, NULL);

	g_test_add_func ("/EncodingConversion/try_convert_success", test_try_convert_success);
	g_test_add_func ("/EncodingConversion/try_convert_illegal_sequence", test_try_convert_illegal_sequence);

	exit_status = g_test_run ();
	gtk_source_finalize ();

	return exit_status;
}