#include <cutter.h>

#include <glib/gmacros.h>
#include "goffice/utils/regutf8.h"
#include "goffice/utils/datetime.h"

void test_head_character (void);
void test_tail_character (void);

static GORegexp re;

void
setup (void)
{
	re.ppcre = NULL;
}

void
teardown (void)
{
	go_regfree (&re);
}

static gchar *
get_matched_string (const gchar *text, GORegmatch match) 
{
	return g_strndup (text + match.rm_so, match.rm_eo - match.rm_so);
}

void
test_head_character (void)
{
	GORegmatch match[10];
	const gchar *text;

	cut_assert_equal_int (REG_NOERROR, go_regcomp (&re, "^a", REG_ICASE));

	text = "abc";
	cut_assert_equal_int (REG_NOERROR, go_regexec (&re, text, G_N_ELEMENTS (match), match, 0));
	cut_assert_equal_string_with_free ("a", get_matched_string (text, match[0]));
	cut_assert_equal_int (-1, match[1].rm_so);
	cut_assert_equal_int (-1, match[1].rm_eo);
}

void
test_tail_character (void)
{
	GORegmatch match[10];
	const gchar *text;

	cut_assert_equal_int (REG_NOERROR, go_regcomp (&re, "c$", REG_ICASE));

	text = "abc";
	cut_assert_equal_int (REG_NOERROR, go_regexec (&re, text, G_N_ELEMENTS (match), match, 0));
	cut_assert_equal_string_with_free ("c", get_matched_string (text, match[0]));
	cut_assert_equal_int (-1, match[1].rm_so);
	cut_assert_equal_int (-1, match[1].rm_eo);
}

