/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT 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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <pidgin.h>

typedef struct {
	const char *text;
	float hue;
	float red;
	float green;
	float blue;
} TestColorData;

/* This test data is from Section 13.1 of XEP-0392.
 * https://xmpp.org/extensions/attic/xep-0392-0.8.0.html#testvectors-fullrange
 */
static TestColorData test_data[] = {
	{
		.text = "Romeo",
		.hue = 327.255249,
		.red = 0.865,
		.green = 0.000,
		.blue = 0.686,
	}, {
		.text = "juliet@capulet.lit",
		.hue = 209.410400,
		.red = 0.000,
		.green = 0.515,
		.blue = 0.573,
	}, {
		.text = "😺",
		.hue = 331.199341,
		.red = 0.872,
		.green = 0.000,
		.blue = 0.659,
	}, {
		.text = "council",
		.hue = 359.994507,
		.red = 0.918,
		.green = 0.000,
		.blue = 0.394,
	}, {
		.text = "Board",
		.hue = 171.430664,
		.red = 0.000,
		.green = 0.527,
		.blue = 0.457,
	}
};

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_pidgin_color_hue_for_text(void) {
	for(guint i = 0; i < G_N_ELEMENTS(test_data); i++) {
		float hue = pidgin_color_hue_for_text(test_data[i].text);

		g_assert_cmpfloat_with_epsilon(hue, test_data[i].hue, 0.01);
	}
}

static void
test_pidgin_color_calculate_for_text(void) {
	for(guint i = 0; i < G_N_ELEMENTS(test_data); i++) {
		GdkRGBA color;

		pidgin_color_calculate_for_text(test_data[i].text, &color);

		g_assert_cmpfloat_with_epsilon(color.red, test_data[i].red, 0.01);
		g_assert_cmpfloat_with_epsilon(color.green, test_data[i].green, 0.01);
		g_assert_cmpfloat_with_epsilon(color.blue, test_data[i].blue, 0.01);
	}
}

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

	g_test_add_func("/pidgin/color/hue-for-text",
	                test_pidgin_color_hue_for_text);
	g_test_add_func("/pidgin/color/calculate-for-text",
	                test_pidgin_color_calculate_for_text);

	return g_test_run();
}
