diff options
-rw-r--r-- | src/gui/gui-nick.c | 30 | ||||
-rw-r--r-- | tests/unit/gui/test-gui-nick.cpp | 344 |
2 files changed, 310 insertions, 64 deletions
diff --git a/src/gui/gui-nick.c b/src/gui/gui-nick.c index f1b39760d..db1f825c8 100644 --- a/src/gui/gui-nick.c +++ b/src/gui/gui-nick.c @@ -105,12 +105,15 @@ gui_nick_hash_sum_32 (const char *nickname, uint32_t *color_32) /* * Hashes a nickname to find color. * - * Returns a number which is the index of color in the nicks colors of option + * Returns a number which is between 0 and num_colors - 1 (inclusive). + * + * num_colors is commonly the number of colors in the option * "weechat.color.chat_nick_colors". + * If num_colors is < 0, the hash itself is returned (64-bit unsigned number). */ -int -gui_nick_hash_color (const char *nickname) +uint64_t +gui_nick_hash_color (const char *nickname, int num_colors) { const char *ptr_salt; uint64_t color_64; @@ -119,10 +122,7 @@ gui_nick_hash_color (const char *nickname) if (!nickname || !nickname[0]) return 0; - if (!config_nick_colors) - config_set_nick_colors (); - - if (config_num_nick_colors == 0) + if (num_colors == 0) return 0; ptr_salt = CONFIG_STRING(config_look_nick_color_hash_salt); @@ -132,26 +132,26 @@ gui_nick_hash_color (const char *nickname) switch (CONFIG_INTEGER(config_look_nick_color_hash)) { case CONFIG_LOOK_NICK_COLOR_HASH_DJB2: - /* variant of djb2 hash */ + /* variant of djb2 hash, using 64-bit integer */ color_64 = 5381; gui_nick_hash_djb2_64 (ptr_salt, &color_64); gui_nick_hash_djb2_64 (nickname, &color_64); break; case CONFIG_LOOK_NICK_COLOR_HASH_SUM: - /* sum of letters */ + /* sum of letters, using 64-bit integer */ color_64 = 0; gui_nick_hash_sum_64 (ptr_salt, &color_64); gui_nick_hash_sum_64 (nickname, &color_64); break; case CONFIG_LOOK_NICK_COLOR_HASH_DJB2_32: - /* variant of djb2 hash (using 32-bit integer) */ + /* variant of djb2 hash, using 32-bit integer */ color_32 = 5381; gui_nick_hash_djb2_32 (ptr_salt, &color_32); gui_nick_hash_djb2_32 (nickname, &color_32); color_64 = color_32; break; case CONFIG_LOOK_NICK_COLOR_HASH_SUM_32: - /* sum of letters (using 32-bit integer) */ + /* sum of letters, using 32-bit integer */ color_32 = 0; gui_nick_hash_sum_32 (ptr_salt, &color_32); gui_nick_hash_sum_32 (nickname, &color_32); @@ -159,7 +159,7 @@ gui_nick_hash_color (const char *nickname) break; } - return (color_64 % config_num_nick_colors); + return (num_colors > 0) ? color_64 % num_colors : color_64; } /* @@ -280,7 +280,8 @@ gui_nick_find_color (const char *nickname) } /* hash nickname to get color */ - color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname); + color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname, + config_num_nick_colors); if (nickname2) free (nickname2); @@ -326,7 +327,8 @@ gui_nick_find_color_name (const char *nickname) } /* hash nickname to get color */ - color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname); + color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname, + config_num_nick_colors); if (nickname2) free (nickname2); diff --git a/tests/unit/gui/test-gui-nick.cpp b/tests/unit/gui/test-gui-nick.cpp index 865555a25..6423635e9 100644 --- a/tests/unit/gui/test-gui-nick.cpp +++ b/tests/unit/gui/test-gui-nick.cpp @@ -25,8 +25,18 @@ extern "C" { #include "src/core/wee-config.h" #include "src/core/wee-string.h" +#include "src/gui/gui-color.h" #include "src/gui/gui-nick.h" +extern void gui_nick_hash_djb2_64 (const char *nickname, uint64_t *color_64); +extern void gui_nick_hash_djb2_32 (const char *nickname, uint32_t *color_32); +extern void gui_nick_hash_sum_64 (const char *nickname, uint64_t *color_64); +extern void gui_nick_hash_sum_32 (const char *nickname, uint32_t *color_32); +extern uint64_t gui_nick_hash_color (const char *nickname, int num_colors); +extern const char *gui_nick_get_forced_color (const char *nickname); +extern char *gui_nick_strdup_for_color (const char *nickname); +} + #define NICK_COLORS "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20," \ "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43," \ "44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66," \ @@ -42,8 +52,14 @@ extern "C" "229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245," \ "246,247,248,249,250,251,252,253,254,255" -extern int gui_nick_hash_color (const char *nickname); -} +#define WEE_NICK_STRDUP_FOR_COLOR(__result, __nickname) \ + nick = gui_nick_strdup_for_color (__nickname); \ + if (__result) \ + STRCMP_EQUAL(__result, nick); \ + else \ + POINTERS_EQUAL(NULL, nick); \ + if (nick) \ + free (nick); TEST_GROUP(GuiNick) { @@ -51,74 +67,207 @@ TEST_GROUP(GuiNick) /* * Tests functions: + * gui_nick_hash_djb2_64 + */ + +TEST(GuiNick, HashDbj264) +{ + uint64_t hash; + + hash = 0; + gui_nick_hash_djb2_64 (NULL, NULL); + gui_nick_hash_djb2_64 ("", NULL); + gui_nick_hash_djb2_64 ("", &hash); + UNSIGNED_LONGS_EQUAL(0, hash); + + gui_nick_hash_djb2_64 ("a", &hash); + UNSIGNED_LONGS_EQUAL(97, hash); + + hash = 0; + gui_nick_hash_djb2_64 ("abcdef", &hash); + UNSIGNED_LONGS_EQUAL(4013083373, hash); + + hash = 0; + gui_nick_hash_djb2_64 ("abcdefghijklmnopqrstuvwxyz", &hash); + UNSIGNED_LONGS_EQUAL(16315903832110220128, hash); + + hash = 0; + gui_nick_hash_djb2_64 ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz", &hash); + UNSIGNED_LONGS_EQUAL(16109708650384405235, hash); +} + +/* + * Tests functions: + * gui_nick_hash_djb2_32 + */ + +TEST(GuiNick, HashDbj232) +{ + uint32_t hash; + + hash = 0; + gui_nick_hash_djb2_32 (NULL, NULL); + gui_nick_hash_djb2_32 ("", NULL); + gui_nick_hash_djb2_32 ("", &hash); + UNSIGNED_LONGS_EQUAL(0, hash); + + gui_nick_hash_djb2_32 ("a", &hash); + UNSIGNED_LONGS_EQUAL(97, hash); + + hash = 0; + gui_nick_hash_djb2_32 ("abcdef", &hash); + UNSIGNED_LONGS_EQUAL(4013083373, hash); + + hash = 0; + gui_nick_hash_djb2_32 ("abcdefghijklmnopqrstuvwxyz", &hash); + UNSIGNED_LONGS_EQUAL(3683976572, hash); +} + +/* + * Tests functions: + * gui_nick_hash_sum_64 + */ + +TEST(GuiNick, HashSum64) +{ + uint64_t hash; + + hash = 0; + gui_nick_hash_sum_64 (NULL, NULL); + gui_nick_hash_sum_64 ("", NULL); + gui_nick_hash_sum_64 ("", &hash); + UNSIGNED_LONGS_EQUAL(0, hash); + + gui_nick_hash_sum_64 ("a", &hash); + UNSIGNED_LONGS_EQUAL(97, hash); + + hash = 0; + gui_nick_hash_sum_64 ("abcdef", &hash); + UNSIGNED_LONGS_EQUAL(597, hash); + + hash = 0; + gui_nick_hash_sum_64 ("abcdefghijklmnopqrstuvwxyz", &hash); + UNSIGNED_LONGS_EQUAL(2847, hash); + + hash = 0; + gui_nick_hash_sum_64 ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz", &hash); + UNSIGNED_LONGS_EQUAL(5694, hash); +} + +/* + * Tests functions: + * gui_nick_hash_sum_32 + */ + +TEST(GuiNick, HashSum32) +{ + uint32_t hash; + + hash = 0; + gui_nick_hash_sum_32 (NULL, NULL); + gui_nick_hash_sum_32 ("", NULL); + gui_nick_hash_sum_32 ("", &hash); + UNSIGNED_LONGS_EQUAL(0, hash); + + gui_nick_hash_sum_32 ("a", &hash); + UNSIGNED_LONGS_EQUAL(97, hash); + + hash = 0; + gui_nick_hash_sum_32 ("abcdef", &hash); + UNSIGNED_LONGS_EQUAL(597, hash); + + hash = 0; + gui_nick_hash_sum_32 ("abcdefghijklmnopqrstuvwxyz", &hash); + UNSIGNED_LONGS_EQUAL(2847, hash); + + hash = 0; + gui_nick_hash_sum_32 ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz", &hash); + UNSIGNED_LONGS_EQUAL(5694, hash); +} + +/* + * Tests functions: * gui_nick_hash_color */ TEST(GuiNick, HashColor) { - config_file_option_set (config_color_chat_nick_colors, NICK_COLORS, 1); - /* hash without salt */ /* test hash: djb2 */ config_file_option_set (config_look_nick_color_hash, "djb2", 1); - LONGS_EQUAL(0, gui_nick_hash_color (NULL)); - LONGS_EQUAL(0, gui_nick_hash_color ("")); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color (NULL, 256)); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color ("", 256)); - LONGS_EQUAL(71, gui_nick_hash_color ("a")); - LONGS_EQUAL(108, gui_nick_hash_color ("abc")); - LONGS_EQUAL(146, gui_nick_hash_color ("abcdef")); - LONGS_EQUAL(73, gui_nick_hash_color ("abcdefghi")); - LONGS_EQUAL(170, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(124, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" - "abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(94, gui_nick_hash_color ("zzzzzz")); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color ("abcdef", 0)); + + UNSIGNED_LONGS_EQUAL(6006552168338, gui_nick_hash_color ("abcdef", -1)); + + UNSIGNED_LONGS_EQUAL(71, gui_nick_hash_color ("a", 256)); + UNSIGNED_LONGS_EQUAL(108, gui_nick_hash_color ("abc", 256)); + UNSIGNED_LONGS_EQUAL(146, gui_nick_hash_color ("abcdef", 256)); + UNSIGNED_LONGS_EQUAL(73, gui_nick_hash_color ("abcdefghi", 256)); + UNSIGNED_LONGS_EQUAL(170, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(124, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(94, gui_nick_hash_color ("zzzzzz", 256)); /* test hash: sum */ config_file_option_set (config_look_nick_color_hash, "sum", 1); - LONGS_EQUAL(0, gui_nick_hash_color (NULL)); - LONGS_EQUAL(0, gui_nick_hash_color ("")); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color (NULL, 256)); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color ("", 256)); - LONGS_EQUAL(97, gui_nick_hash_color ("a")); - LONGS_EQUAL(38, gui_nick_hash_color ("abc")); - LONGS_EQUAL(85, gui_nick_hash_color ("abcdef")); - LONGS_EQUAL(141, gui_nick_hash_color ("abcdefghi")); - LONGS_EQUAL(31, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(62, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" - "abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(220, gui_nick_hash_color ("zzzzzz")); + UNSIGNED_LONGS_EQUAL(97, gui_nick_hash_color ("a", 256)); + UNSIGNED_LONGS_EQUAL(38, gui_nick_hash_color ("abc", 256)); + UNSIGNED_LONGS_EQUAL(85, gui_nick_hash_color ("abcdef", 256)); + UNSIGNED_LONGS_EQUAL(141, gui_nick_hash_color ("abcdefghi", 256)); + UNSIGNED_LONGS_EQUAL(31, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(62, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(220, gui_nick_hash_color ("zzzzzz", 256)); /* test hash: djb2_32 */ config_file_option_set (config_look_nick_color_hash, "djb2_32", 1); - LONGS_EQUAL(0, gui_nick_hash_color (NULL)); - LONGS_EQUAL(0, gui_nick_hash_color ("")); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color (NULL, 256)); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color ("", 256)); + + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color ("abcdef", 0)); - LONGS_EQUAL(71, gui_nick_hash_color ("a")); - LONGS_EQUAL(108, gui_nick_hash_color ("abc")); - LONGS_EQUAL(146, gui_nick_hash_color ("abcdef")); - LONGS_EQUAL(73, gui_nick_hash_color ("abcdefghi")); - LONGS_EQUAL(209, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(116, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" - "abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(94, gui_nick_hash_color ("zzzzzz")); + UNSIGNED_LONGS_EQUAL(1382582162, gui_nick_hash_color ("abcdef", -1)); + + UNSIGNED_LONGS_EQUAL(71, gui_nick_hash_color ("a", 256)); + UNSIGNED_LONGS_EQUAL(108, gui_nick_hash_color ("abc", 256)); + UNSIGNED_LONGS_EQUAL(146, gui_nick_hash_color ("abcdef", 256)); + UNSIGNED_LONGS_EQUAL(73, gui_nick_hash_color ("abcdefghi", 256)); + UNSIGNED_LONGS_EQUAL(209, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(116, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(94, gui_nick_hash_color ("zzzzzz", 256)); /* test hash: sum_32 */ config_file_option_set (config_look_nick_color_hash, "sum_32", 1); - LONGS_EQUAL(0, gui_nick_hash_color (NULL)); - LONGS_EQUAL(0, gui_nick_hash_color ("")); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color (NULL, 256)); + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color ("", 256)); + + UNSIGNED_LONGS_EQUAL(0, gui_nick_hash_color ("abcdef", 0)); + + UNSIGNED_LONGS_EQUAL(597, gui_nick_hash_color ("abcdef", -1)); - LONGS_EQUAL(97, gui_nick_hash_color ("a")); - LONGS_EQUAL(38, gui_nick_hash_color ("abc")); - LONGS_EQUAL(85, gui_nick_hash_color ("abcdef")); - LONGS_EQUAL(141, gui_nick_hash_color ("abcdefghi")); - LONGS_EQUAL(31, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(62, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" - "abcdefghijklmnopqrstuvwxyz")); - LONGS_EQUAL(220, gui_nick_hash_color ("zzzzzz")); + UNSIGNED_LONGS_EQUAL(97, gui_nick_hash_color ("a", 256)); + UNSIGNED_LONGS_EQUAL(38, gui_nick_hash_color ("abc", 256)); + UNSIGNED_LONGS_EQUAL(85, gui_nick_hash_color ("abcdef", 256)); + UNSIGNED_LONGS_EQUAL(141, gui_nick_hash_color ("abcdefghi", 256)); + UNSIGNED_LONGS_EQUAL(31, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(62, gui_nick_hash_color ("abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz", 256)); + UNSIGNED_LONGS_EQUAL(220, gui_nick_hash_color ("zzzzzz", 256)); /* hash with salt */ @@ -126,21 +275,116 @@ TEST(GuiNick, HashColor) /* test hash: djb2 */ config_file_option_set (config_look_nick_color_hash, "djb2", 1); - LONGS_EQUAL(146, gui_nick_hash_color ("def")); + UNSIGNED_LONGS_EQUAL(146, gui_nick_hash_color ("def", 256)); + UNSIGNED_LONGS_EQUAL(199603970247853410, gui_nick_hash_color ("abcdef", -1)); /* test hash: sum */ config_file_option_set (config_look_nick_color_hash, "sum", 1); - LONGS_EQUAL(85, gui_nick_hash_color ("def")); + UNSIGNED_LONGS_EQUAL(85, gui_nick_hash_color ("def", 256)); + UNSIGNED_LONGS_EQUAL(891, gui_nick_hash_color ("abcdef", -1)); /* test hash: djb2_32 */ config_file_option_set (config_look_nick_color_hash, "djb2_32", 1); - LONGS_EQUAL(146, gui_nick_hash_color ("def")); + UNSIGNED_LONGS_EQUAL(146, gui_nick_hash_color ("def", 256)); + UNSIGNED_LONGS_EQUAL(2988541282, gui_nick_hash_color ("abcdef", -1)); /* test hash: sum_32 */ config_file_option_set (config_look_nick_color_hash, "sum_32", 1); - LONGS_EQUAL(85, gui_nick_hash_color ("def")); + UNSIGNED_LONGS_EQUAL(85, gui_nick_hash_color ("def", 256)); + UNSIGNED_LONGS_EQUAL(891, gui_nick_hash_color ("abcdef", -1)); + + config_file_option_reset (config_look_nick_color_hash_salt, 1); +} + +/* + * Tests functions: + * gui_nick_get_forced_color + */ + +TEST(GuiNick, GetForcedColor) +{ + config_file_option_set (config_look_nick_color_force, + "alice:green;bob:cyan", 1); + + POINTERS_EQUAL(NULL, gui_nick_get_forced_color (NULL)); + POINTERS_EQUAL(NULL, gui_nick_get_forced_color ("")); + + POINTERS_EQUAL(NULL, gui_nick_get_forced_color ("unknown")); + + STRCMP_EQUAL("green", gui_nick_get_forced_color ("alice")); + STRCMP_EQUAL("cyan", gui_nick_get_forced_color ("bob")); + + POINTERS_EQUAL(NULL, gui_nick_get_forced_color ("alice2")); + POINTERS_EQUAL(NULL, gui_nick_get_forced_color ("alice_")); + POINTERS_EQUAL(NULL, gui_nick_get_forced_color ("bob2")); + POINTERS_EQUAL(NULL, gui_nick_get_forced_color ("bob_")); + + config_file_option_reset (config_look_nick_color_force, 1); +} + +/* + * Tests functions: + * gui_nick_strdup_for_color + */ + +TEST(GuiNick, StrdupForColor) +{ + char *nick; + + WEE_NICK_STRDUP_FOR_COLOR(NULL, NULL); + WEE_NICK_STRDUP_FOR_COLOR("", ""); + WEE_NICK_STRDUP_FOR_COLOR("abcdef", "abcdef"); + WEE_NICK_STRDUP_FOR_COLOR("abcdef", "abcdef_"); + WEE_NICK_STRDUP_FOR_COLOR("abcdef", "abcdef[]"); +} + +/* + * Tests functions: + * gui_nick_find_color + */ + +TEST(GuiNick, FindColor) +{ + const char *color_default, *color_lightgreen, *color_brown; + const char *color_green, *color_cyan; + + color_default = gui_color_get_custom ("default"); + color_lightgreen = gui_color_get_custom ("lightgreen"); + color_brown = gui_color_get_custom ("brown"); + color_green = gui_color_get_custom ("green"); + color_cyan = gui_color_get_custom ("cyan"); + + STRCMP_EQUAL(color_default, gui_nick_find_color (NULL)); + STRCMP_EQUAL(color_default, gui_nick_find_color ("")); + + STRCMP_EQUAL(color_lightgreen, gui_nick_find_color ("abcdef")); + STRCMP_EQUAL(color_brown, gui_nick_find_color ("abcdefghi")); + + /* with forced color */ + config_file_option_set (config_look_nick_color_force, + "abcdef:green;abcdefghi:cyan", 1); + STRCMP_EQUAL(color_green, gui_nick_find_color ("abcdef")); + STRCMP_EQUAL(color_cyan, gui_nick_find_color ("abcdefghi")); + config_file_option_reset (config_look_nick_color_force, 1); +} + +/* + * Tests functions: + * gui_nick_find_color_name + */ + +TEST(GuiNick, FindColorName) +{ + STRCMP_EQUAL("default", gui_nick_find_color_name (NULL)); + STRCMP_EQUAL("default", gui_nick_find_color_name ("")); - config_file_option_reset (config_look_nick_color_hash_salt, 0); + STRCMP_EQUAL("lightgreen", gui_nick_find_color_name ("abcdef")); + STRCMP_EQUAL("brown", gui_nick_find_color_name ("abcdefghi")); - config_file_option_reset (config_color_chat_nick_colors, 0); + /* with forced color */ + config_file_option_set (config_look_nick_color_force, + "abcdef:green;abcdefghi:cyan", 1); + STRCMP_EQUAL("green", gui_nick_find_color_name ("abcdef")); + STRCMP_EQUAL("cyan", gui_nick_find_color_name ("abcdefghi")); + config_file_option_reset (config_look_nick_color_force, 1); } |