From d81829652057d63b9971b9217996438ee41788ca Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 18 Aug 2018 20:09:52 +0100 Subject: Implement XEP-0392: Consistent Color Generation --- xmpp-vala/tests/color.vala | 50 +++++++++++++++++++++++++++++++++++++++++++++ xmpp-vala/tests/common.vala | 17 +++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 xmpp-vala/tests/color.vala (limited to 'xmpp-vala/tests') diff --git a/xmpp-vala/tests/color.vala b/xmpp-vala/tests/color.vala new file mode 100644 index 00000000..ded67d53 --- /dev/null +++ b/xmpp-vala/tests/color.vala @@ -0,0 +1,50 @@ +using Xmpp.Xep; + +namespace Xmpp.Test { + +class ColorTest : Gee.TestCase { + + public ColorTest() { + base("color"); + + add_test("xep-vectors-angle", () => { text_xep_vectors_angle(); }); + add_test("xep-vectors-rgbf", () => { test_xep_vectors_rgbf(); }); + add_test("rgb-to-angle", () => { test_rgb_to_angle(); }); + } + + public void text_xep_vectors_angle() { + fail_if_not_eq_double(ConsistentColor.string_to_angle("Romeo"), 327.255249); + fail_if_not_eq_double(ConsistentColor.string_to_angle("juliet@capulet.lit"), 209.410400); + fail_if_not_eq_double(ConsistentColor.string_to_angle("😺"), 331.199341); + fail_if_not_eq_double(ConsistentColor.string_to_angle("council"), 359.994507); + fail_if_not_eq_double(ConsistentColor.string_to_angle("Board"), 171.430664); + } + + private bool fail_if_not_eq_rgbf(float[] left, float[] right) { + bool failed = false; + for (int i = 0; i < 3; i++) { + failed = fail_if_not_eq_float(left[i], right[i]) || failed; + } + return failed; + } + + public void test_xep_vectors_rgbf() { + fail_if_not_eq_rgbf(ConsistentColor.string_to_rgbf("Romeo"), {0.865f,0.000f,0.686f}); + fail_if_not_eq_rgbf(ConsistentColor.string_to_rgbf("juliet@capulet.lit"), {0.000f,0.515f,0.573f}); + fail_if_not_eq_rgbf(ConsistentColor.string_to_rgbf("😺"), {0.872f,0.000f,0.659f}); + fail_if_not_eq_rgbf(ConsistentColor.string_to_rgbf("council"), {0.918f,0.000f,0.394f}); + fail_if_not_eq_rgbf(ConsistentColor.string_to_rgbf("Board"), {0.000f,0.527f,0.457f}); + } + + public void test_rgb_to_angle() { + string[] colors = {"e57373", "f06292", "ba68c8", "9575cd", "7986cb", "64b5f6", "4fc3f7", "4dd0e1", "4db6ac", "81c784", "aed581", "dce775", "fff176", "ffd54f", "ffb74d", "ff8a65"}; + foreach(string hex_color in colors) { + uint8 r = (uint8) ((double) hex_color.substring(0, 2).to_long(null, 16)); + uint8 g = (uint8) ((double) hex_color.substring(2, 2).to_long(null, 16)); + uint8 b = (uint8) ((double) hex_color.substring(4, 2).to_long(null, 16)); + //print(@"$hex_color, $r, $g, $b, $(ConsistentColor.rgb_to_angle(r, g, b))\n"); + } + } +} + +} \ No newline at end of file diff --git a/xmpp-vala/tests/common.vala b/xmpp-vala/tests/common.vala index 47dbce0e..dc1c8e50 100644 --- a/xmpp-vala/tests/common.vala +++ b/xmpp-vala/tests/common.vala @@ -6,6 +6,7 @@ int main(string[] args) { TestSuite.get_root().add_suite(new Xmpp.Test.StanzaTest().get_suite()); TestSuite.get_root().add_suite(new Xmpp.Test.UtilTest().get_suite()); TestSuite.get_root().add_suite(new Xmpp.Test.JidTest().get_suite()); + TestSuite.get_root().add_suite(new Xmpp.Test.ColorTest().get_suite()); return GLib.Test.run(); } @@ -68,6 +69,22 @@ bool fail_if_not_eq_int(int left, int right, string? reason = null) { return fail_if_not(left == right, @"$(reason + ": " ?? "")$left != $right"); } +private float float_to_accuracy(float f, float accuracy) { + return (float) (Math.round(f * Math.pow(10, accuracy)) / Math.pow(10, accuracy)); +} + +private float double_to_accuracy(double f, float accuracy) { + return (float) (Math.round(f * Math.pow(10, accuracy)) / Math.pow(10, accuracy)); +} + +bool fail_if_not_eq_float(float left, float right, float accuracy = 3, string? reason = null) { + return fail_if_not(float_to_accuracy(left, accuracy) == float_to_accuracy(right, accuracy), @"$(reason + ": " ?? "")$left != $right"); +} + +bool fail_if_not_eq_double(double left, double right, float accuracy = 3, string? reason = null) { + return fail_if_not(double_to_accuracy(left, accuracy) == double_to_accuracy(right, accuracy), @"$(reason + ": " ?? "")$left != $right"); +} + bool fail_if_not_eq_str(string? left, string? right, string? reason = null) { bool nullcheck = (left == null || right == null) && (left != null && right != null); if (left == null) left = "(null)"; -- cgit v1.2.3-70-g09d2