/*
 * Copyright (C) Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the AUTHORS file distributed with this source
 * distribution
 *
 * This program 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.
 *
 * This program 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 program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <birb.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_birb_utf8_compare_valid(void) {
	g_assert_cmpint(birb_utf8_compare(NULL, NULL), ==, 0);
	g_assert_cmpint(birb_utf8_compare("foo", NULL), ==, -1);
	g_assert_cmpint(birb_utf8_compare(NULL, "bar"), ==, 1);

	g_assert_cmpint(birb_utf8_compare("bar", "foo"), ==, -1);
	g_assert_cmpint(birb_utf8_compare("foo", "bar"), ==, 1);

	g_assert_cmpint(birb_utf8_compare("baz", "baz"), ==, 0);
	g_assert_cmpint(birb_utf8_compare("BAZ", "baz"), ==, 0);
	g_assert_cmpint(birb_utf8_compare("baz", "BAZ"), ==, 0);
	g_assert_cmpint(birb_utf8_compare("BAZ", "BAZ"), ==, 0);
	g_assert_cmpint(birb_utf8_compare("bAz", "BaZ"), ==, 0);

	g_assert_cmpint(birb_utf8_compare("H₂O", "H2O"), ==, 0);
	g_assert_cmpint(birb_utf8_compare("E=mc²", "E=mc2"), ==, 0);
	g_assert_cmpint(birb_utf8_compare("E=mc²", "E=mc^2"), ==, -1);
}

static void
test_birb_utf8_compare_invalid(void) {
	/* Invalid sorts higher than null. */
	g_assert_cmpint(birb_utf8_compare(NULL, "foo\377bar"), ==, 1);
	g_assert_cmpint(birb_utf8_compare("foo\377bar", NULL), ==, -1);

	/* Invalids are all treated the same. */
	g_assert_cmpint(birb_utf8_compare("foo\377bar", "foo\377bar"), ==, 0);
	g_assert_cmpint(birb_utf8_compare("foo\377bar", "baz\377quux"), ==, 0);

	/* Basic valid vs invalid tests. */
	g_assert_cmpint(birb_utf8_compare("foo bar", "foo\377bar"), ==, -1);
	g_assert_cmpint(birb_utf8_compare("foo\377bar", "baz quux"), ==, 1);
	g_assert_cmpint(birb_utf8_compare("H₂O", "H\377O"), ==, -1);
	g_assert_cmpint(birb_utf8_compare("H\377O", "H₂O"), ==, 1);
}

static void
test_birb_utf8_equal_valid(void) {
	g_assert_true(birb_utf8_equal(NULL, NULL));
	g_assert_false(birb_utf8_equal("foo", NULL));
	g_assert_false(birb_utf8_equal(NULL, "bar"));

	g_assert_false(birb_utf8_equal("bar", "foo"));
	g_assert_false(birb_utf8_equal("foo", "bar"));

	g_assert_true(birb_utf8_equal("baz", "baz"));
	g_assert_true(birb_utf8_equal("BAZ", "baz"));
	g_assert_true(birb_utf8_equal("baz", "BAZ"));
	g_assert_true(birb_utf8_equal("BAZ", "BAZ"));
	g_assert_true(birb_utf8_equal("bAz", "BaZ"));

	g_assert_true(birb_utf8_equal("H₂O", "H2O"));
	g_assert_true(birb_utf8_equal("E=mc²", "E=mc2"));
	g_assert_false(birb_utf8_equal("E=mc²", "E=mc^2"));
}

static void
test_birb_utf8_equal_invalid(void) {
	/* Invalid sorts higher than null. */
	g_assert_false(birb_utf8_equal(NULL, "foo\377bar"));
	g_assert_false(birb_utf8_equal("foo\377bar", NULL));

	/* Invalids are all treated the same. */
	g_assert_true(birb_utf8_equal("foo\377bar", "foo\377bar"));
	g_assert_true(birb_utf8_equal("foo\377bar", "baz\377quux"));

	/* Basic valid vs invalid tests. */
	g_assert_false(birb_utf8_equal("foo bar", "foo\377bar"));
	g_assert_false(birb_utf8_equal("foo\377bar", "baz quux"));
	g_assert_false(birb_utf8_equal("H₂O", "H\377O"));
	g_assert_false(birb_utf8_equal("H\377O", "H₂O"));
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/birb/utf8/compare/valid", test_birb_utf8_compare_valid);
	g_test_add_func("/birb/utf8/compare/invalid",
	                test_birb_utf8_compare_invalid);

	g_test_add_func("/birb/utf8/equal/valid", test_birb_utf8_equal_valid);
	g_test_add_func("/birb/utf8/equal/invalid", test_birb_utf8_equal_invalid);

	return g_test_run();
}
