/* SPDX-FileCopyrightText: 2025-2026 - Sébastien Wilmet
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <gfls/gfls.h>

static void
test_try_convert_success (void)
{
	GBytes *input_bytes;
	gboolean ret;

	input_bytes = g_bytes_new_static ("a", 1);

	ret = gfls_encoding_try_convert (input_bytes, "UTF-8", "UTF-8");
	g_assert_true (ret);

	g_bytes_unref (input_bytes);
}

static void
test_try_convert_illegal_sequence (void)
{
	guint8 input_buffer[1];
	GBytes *input_bytes;
	gboolean ret;

	// 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;

	input_bytes = g_bytes_new (input_buffer, 1);

	ret = gfls_encoding_try_convert (input_bytes, "UTF-8", "UTF-8");
	g_assert_false (ret);

	g_bytes_unref (input_bytes);
}

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

	gfls_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 ();
	gfls_finalize ();

	return exit_status;
}
