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

#include <gfls/gfls.h>

static void
test_empty (void)
{
	GflsBytesRegionBuilder *builder;
	GflsBytesRegion *region;
	gsize total_size;
	gchar *str;

	builder = gfls_bytes_region_builder_new ();
	total_size = gfls_bytes_region_builder_get_current_size (builder);
	g_assert_cmpuint (total_size, ==, 0);

	region = gfls_bytes_region_builder_free (builder, FALSE);
	builder = NULL;

	str = gfls_bytes_region_to_string (region);
	g_assert_nonnull (str);
	g_assert_cmpstr (str, ==, "");

	g_free (str);
	gfls_bytes_region_free (region);
}

static void
test_single_sub_region_part_of_region (void)
{
	GflsBytesRegionBuilder *builder;
	GflsBytesRegion *region;
	gsize total_size;
	gchar *str;

	builder = gfls_bytes_region_builder_new ();
	gfls_bytes_region_builder_append (builder, 3, TRUE);

	total_size = gfls_bytes_region_builder_get_current_size (builder);
	g_assert_cmpuint (total_size, ==, 3);

	region = gfls_bytes_region_builder_free (builder, FALSE);
	builder = NULL;

	str = gfls_bytes_region_to_string (region);
	g_assert_nonnull (str);
	g_assert_cmpstr (str, ==, "[0, 3, 1]\n");

	g_free (str);
	gfls_bytes_region_free (region);
}

static void
test_single_sub_region_not_part_of_region (void)
{
	GflsBytesRegionBuilder *builder;
	GflsBytesRegion *region;
	gsize total_size;
	gchar *str;

	builder = gfls_bytes_region_builder_new ();
	gfls_bytes_region_builder_append (builder, 4, FALSE);

	total_size = gfls_bytes_region_builder_get_current_size (builder);
	g_assert_cmpuint (total_size, ==, 4);

	region = gfls_bytes_region_builder_free (builder, FALSE);
	builder = NULL;

	str = gfls_bytes_region_to_string (region);
	g_assert_nonnull (str);
	g_assert_cmpstr (str, ==, "[0, 4, 0]\n");

	g_free (str);
	gfls_bytes_region_free (region);
}

static void
test_merge (void)
{
	GflsBytesRegionBuilder *builder;
	GflsBytesRegion *region;
	gsize total_size;
	gchar *str;

	builder = gfls_bytes_region_builder_new ();
	gfls_bytes_region_builder_append (builder, 5, TRUE);
	gfls_bytes_region_builder_append (builder, 5, TRUE);
	gfls_bytes_region_builder_append (builder, 2, FALSE);
	gfls_bytes_region_builder_append (builder, 2, FALSE);

	total_size = gfls_bytes_region_builder_get_current_size (builder);
	g_assert_cmpuint (total_size, ==, 14);

	region = gfls_bytes_region_builder_free (builder, FALSE);
	builder = NULL;

	str = gfls_bytes_region_to_string (region);
	g_assert_nonnull (str);
	g_assert_cmpstr (str, ==,
			 "[0, 10, 1]\n"
			 "[10, 4, 0]\n");

	g_free (str);
	gfls_bytes_region_free (region);
}

static void
test_match_bytes (void)
{
	GflsBytesRegionBuilder *builder;
	GflsBytesRegion *region;
	GBytes *bytes;

	builder = gfls_bytes_region_builder_new ();
	gfls_bytes_region_builder_append (builder, 2, TRUE);
	gfls_bytes_region_builder_append (builder, 1, FALSE);

	region = gfls_bytes_region_builder_free (builder, FALSE);
	builder = NULL;

	bytes = g_bytes_new_static ("abc", 3);
	g_assert_true (gfls_bytes_region_match_bytes (region, bytes));
	g_bytes_unref (bytes);

	bytes = g_bytes_new_static ("a", 1);
	g_assert_false (gfls_bytes_region_match_bytes (region, bytes));
	g_bytes_unref (bytes);

	gfls_bytes_region_free (region);
}

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

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

	g_test_add_func ("/BytesRegion/empty", test_empty);
	g_test_add_func ("/BytesRegion/single_sub_region_part_of_region", test_single_sub_region_part_of_region);
	g_test_add_func ("/BytesRegion/single_sub_region_not_part_of_region", test_single_sub_region_not_part_of_region);
	g_test_add_func ("/BytesRegion/merge", test_merge);
	g_test_add_func ("/BytesRegion/match_bytes", test_match_bytes);

	exit_status = g_test_run ();
	gfls_finalize ();

	return exit_status;
}
