/*
 * 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_str_is_empty(void) {
	g_assert_true(birb_str_is_empty(NULL));
	g_assert_true(birb_str_is_empty(""));

	g_assert_false(birb_str_is_empty("birb"));
}

static void
test_birb_str_equal(void) {
	g_assert_true(birb_str_equal(NULL, NULL));
	g_assert_true(birb_str_equal("", ""));
	g_assert_true(birb_str_equal("birb", "birb"));

	g_assert_false(birb_str_equal(NULL, "birb"));
	g_assert_false(birb_str_equal("birb", NULL));
}

static void
test_birb_str_to_uint(void) {
	g_assert_cmpuint(birb_str_to_uint(NULL), ==, 0);
	g_assert_cmpuint(birb_str_to_uint(""), ==, 0);
	g_assert_cmpuint(birb_str_to_uint("10"), ==, 10);
	g_assert_cmpuint(birb_str_to_uint("birb"), ==, 0);
	g_assert_cmpuint(birb_str_to_uint("3.14"), ==, 3);
}

static void
test_birb_str_matches_null(void) {
	g_assert_false(birb_str_matches("abc", NULL));
}

static void
test_birb_str_matches_adjacent(void) {
	g_assert_true(birb_str_matches("abc", "abc"));
	g_assert_true(birb_str_matches("abc", "123abc"));
	g_assert_true(birb_str_matches("abc", "abcxyz"));
	g_assert_true(birb_str_matches("abc", "123abcxyz"));
}

static void
test_birb_str_matches_sparse(void) {
	g_assert_true(birb_str_matches("adf", "abcdefg"));
	g_assert_true(birb_str_matches("jon", "john"));
	g_assert_true(birb_str_matches("lce", "alice"));
}

static void
test_birb_str_matches_iterates_correctly(void) {
	/* These tests are to make sure that we are iterating both the pattern and
	* the string to check properly.
	*/

	/* since we have 2 e's in the pattern, this test makes sure that we iterate
	* past the first matching e in the string we're checking.
	*/
	g_assert_false(birb_str_matches("beer", "berry"));
}

static void
test_birb_str_matches_unicode(void) {
	g_assert_true(birb_str_matches("✊✋✌",
	                               "Rock ✊, Paper ✋, Scissor ✌!"));
}

/******************************************************************************
 * Wipe tests
 *****************************************************************************/
static void
test_birb_str_wipe_null(void) {
	birb_str_wipe(NULL);
}

static void
test_birb_str_wipe_empty(void) {
	birb_str_wipe(g_strdup(""));
}

static void
test_birb_str_wipe_normal(void) {
	birb_str_wipe(g_strdup("hunter2"));
}

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

	g_test_add_func("/birb/str/is-empty", test_birb_str_is_empty);
	g_test_add_func("/birb/str/equal", test_birb_str_equal);
	g_test_add_func("/birb/str/to-uint", test_birb_str_to_uint);

	g_test_add_func("/birb/str/matches/null", test_birb_str_matches_null);
	g_test_add_func("/birb/str/matches/adjacent", test_birb_str_matches_adjacent);
	g_test_add_func("/birb/str/matches/sparse", test_birb_str_matches_sparse);
	g_test_add_func("/birb/str/matches/iterates_correctly",
	                test_birb_str_matches_iterates_correctly);
	g_test_add_func("/birb/str/matches/unicode",
	                test_birb_str_matches_unicode);

	g_test_add_func("/birb/str/wipe/null", test_birb_str_wipe_null);
	g_test_add_func("/birb/str/wipe/empty", test_birb_str_wipe_empty);
	g_test_add_func("/birb/str/wipe/normal", test_birb_str_wipe_normal);

	return g_test_run();
}
