From b8d216a0575fbdc5a8eeeed07a1aeda8bd83ffea Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 14 Nov 2020 17:00:09 +0100 Subject: Add a WeakMap implementation + tests --- libdino/CMakeLists.txt | 24 ++++++++- libdino/src/util.vala | 69 ------------------------- libdino/src/util/util.vala | 69 +++++++++++++++++++++++++ libdino/src/util/weak_map.vala | 115 +++++++++++++++++++++++++++++++++++++++++ libdino/tests/common.vala | 67 ++++++++++++++++++++++++ libdino/tests/jid.vala | 39 ++++++++++++++ libdino/tests/testcase.vala | 80 ++++++++++++++++++++++++++++ libdino/tests/weak_map.vala | 97 ++++++++++++++++++++++++++++++++++ 8 files changed, 490 insertions(+), 70 deletions(-) delete mode 100644 libdino/src/util.vala create mode 100644 libdino/src/util/util.vala create mode 100644 libdino/src/util/weak_map.vala create mode 100644 libdino/tests/common.vala create mode 100644 libdino/tests/jid.vala create mode 100644 libdino/tests/testcase.vala create mode 100644 libdino/tests/weak_map.vala diff --git a/libdino/CMakeLists.txt b/libdino/CMakeLists.txt index 95b95ae2..9c2145e3 100644 --- a/libdino/CMakeLists.txt +++ b/libdino/CMakeLists.txt @@ -49,7 +49,8 @@ SOURCES src/service/stream_interactor.vala src/service/util.vala - src/util.vala + src/util/util.vala + src/util/weak_map.vala CUSTOM_VAPIS "${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi" "${CMAKE_BINARY_DIR}/exports/qlite.vapi" @@ -89,3 +90,24 @@ set_target_properties(libdino PROPERTIES PREFIX "" VERSION 0.0 SOVERSION 0) install(TARGETS libdino ${TARGET_INSTALL}) install(FILES ${CMAKE_BINARY_DIR}/exports/dino.vapi ${CMAKE_BINARY_DIR}/exports/dino.deps DESTINATION ${VAPI_INSTALL_DIR}) install(FILES ${CMAKE_BINARY_DIR}/exports/dino.h ${CMAKE_BINARY_DIR}/exports/dino_i18n.h DESTINATION ${INCLUDE_INSTALL_DIR}) + +if(BUILD_TESTS) + vala_precompile(LIBDINO_TEST_VALA_C + SOURCES + "tests/weak_map.vala" + "tests/testcase.vala" + "tests/common.vala" + CUSTOM_VAPIS + ${CMAKE_BINARY_DIR}/exports/dino_internal.vapi + ${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi + ${CMAKE_BINARY_DIR}/exports/qlite.vapi + PACKAGES + ${LIBDINO_PACKAGES} + OPTIONS + ${LIBDINO_EXTRA_OPTIONS} + ) + + add_definitions(${VALA_CFLAGS}) + add_executable(libdino-test ${LIBDINO_TEST_VALA_C}) + target_link_libraries(libdino-test libdino) +endif(BUILD_TESTS) diff --git a/libdino/src/util.vala b/libdino/src/util.vala deleted file mode 100644 index c691a279..00000000 --- a/libdino/src/util.vala +++ /dev/null @@ -1,69 +0,0 @@ -namespace Dino { - -private extern const string SYSTEM_LIBDIR_NAME; -private extern const string SYSTEM_PLUGIN_DIR; - -public class SearchPathGenerator { - - public string? exec_path { get; private set; } - - public SearchPathGenerator(string? exec_path) { - this.exec_path = exec_path; - } - - public string get_locale_path(string gettext_package, string locale_install_dir) { - string? locale_dir = null; - if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { - string exec_locale = Path.build_filename(Path.get_dirname(exec_path), "locale"); - if (FileUtils.test(Path.build_filename(exec_locale, "en", "LC_MESSAGES", gettext_package + ".mo"), FileTest.IS_REGULAR)) { - locale_dir = exec_locale; - } - } - return locale_dir ?? locale_install_dir; - } - - public string[] get_plugin_paths() { - string[] search_paths = new string[0]; - if (Environment.get_variable("DINO_PLUGIN_DIR") != null) { - search_paths += Environment.get_variable("DINO_PLUGIN_DIR"); - } - search_paths += Path.build_filename(Environment.get_home_dir(), ".local", "lib", "dino", "plugins"); - string? exec_path = this.exec_path; - if (exec_path != null) { - if (!exec_path.contains(Path.DIR_SEPARATOR_S)) { - exec_path = Environment.find_program_in_path(this.exec_path); - } - if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { - search_paths += Path.build_filename(Path.get_dirname(exec_path), "plugins"); - } - if (Path.get_basename(Path.get_dirname(exec_path)) == "bin") { - search_paths += Path.build_filename(Path.get_dirname(Path.get_dirname(exec_path)), SYSTEM_LIBDIR_NAME, "dino", "plugins"); - } - } - search_paths += SYSTEM_PLUGIN_DIR; - return search_paths; - } -} - -public static string get_storage_dir() { - return Path.build_filename(Environment.get_user_data_dir(), "dino"); -} - -[CCode (cname = "dino_gettext", cheader_filename = "dino_i18n.h")] -public static extern unowned string _(string s); - -[CCode (cname = "dino_ngettext", cheader_filename = "dino_i18n.h")] -public static extern unowned string n(string msgid, string plural, ulong number); - -[CCode (cname = "bindtextdomain", cheader_filename = "libintl.h")] -private static extern unowned string? bindtextdomain(string domainname, string? dirname); - -[CCode (cname = "bind_textdomain_codeset", cheader_filename = "libintl.h")] -private static extern unowned string? bind_textdomain_codeset(string domainname, string? codeset); - -public static void internationalize(string gettext_package, string locales_dir) { - Intl.bind_textdomain_codeset(gettext_package, "UTF-8"); - Intl.bindtextdomain(gettext_package, locales_dir); -} - -} diff --git a/libdino/src/util/util.vala b/libdino/src/util/util.vala new file mode 100644 index 00000000..c691a279 --- /dev/null +++ b/libdino/src/util/util.vala @@ -0,0 +1,69 @@ +namespace Dino { + +private extern const string SYSTEM_LIBDIR_NAME; +private extern const string SYSTEM_PLUGIN_DIR; + +public class SearchPathGenerator { + + public string? exec_path { get; private set; } + + public SearchPathGenerator(string? exec_path) { + this.exec_path = exec_path; + } + + public string get_locale_path(string gettext_package, string locale_install_dir) { + string? locale_dir = null; + if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { + string exec_locale = Path.build_filename(Path.get_dirname(exec_path), "locale"); + if (FileUtils.test(Path.build_filename(exec_locale, "en", "LC_MESSAGES", gettext_package + ".mo"), FileTest.IS_REGULAR)) { + locale_dir = exec_locale; + } + } + return locale_dir ?? locale_install_dir; + } + + public string[] get_plugin_paths() { + string[] search_paths = new string[0]; + if (Environment.get_variable("DINO_PLUGIN_DIR") != null) { + search_paths += Environment.get_variable("DINO_PLUGIN_DIR"); + } + search_paths += Path.build_filename(Environment.get_home_dir(), ".local", "lib", "dino", "plugins"); + string? exec_path = this.exec_path; + if (exec_path != null) { + if (!exec_path.contains(Path.DIR_SEPARATOR_S)) { + exec_path = Environment.find_program_in_path(this.exec_path); + } + if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { + search_paths += Path.build_filename(Path.get_dirname(exec_path), "plugins"); + } + if (Path.get_basename(Path.get_dirname(exec_path)) == "bin") { + search_paths += Path.build_filename(Path.get_dirname(Path.get_dirname(exec_path)), SYSTEM_LIBDIR_NAME, "dino", "plugins"); + } + } + search_paths += SYSTEM_PLUGIN_DIR; + return search_paths; + } +} + +public static string get_storage_dir() { + return Path.build_filename(Environment.get_user_data_dir(), "dino"); +} + +[CCode (cname = "dino_gettext", cheader_filename = "dino_i18n.h")] +public static extern unowned string _(string s); + +[CCode (cname = "dino_ngettext", cheader_filename = "dino_i18n.h")] +public static extern unowned string n(string msgid, string plural, ulong number); + +[CCode (cname = "bindtextdomain", cheader_filename = "libintl.h")] +private static extern unowned string? bindtextdomain(string domainname, string? dirname); + +[CCode (cname = "bind_textdomain_codeset", cheader_filename = "libintl.h")] +private static extern unowned string? bind_textdomain_codeset(string domainname, string? codeset); + +public static void internationalize(string gettext_package, string locales_dir) { + Intl.bind_textdomain_codeset(gettext_package, "UTF-8"); + Intl.bindtextdomain(gettext_package, locales_dir); +} + +} diff --git a/libdino/src/util/weak_map.vala b/libdino/src/util/weak_map.vala new file mode 100644 index 00000000..5a89be11 --- /dev/null +++ b/libdino/src/util/weak_map.vala @@ -0,0 +1,115 @@ +using Gee; + +public class WeakMap : Gee.AbstractMap { + + private HashMap hash_map; + private HashMap notify_map; + + public WeakMap(owned HashDataFunc? key_hash_func = null, owned EqualDataFunc? key_equal_func = null, owned EqualDataFunc? value_equal_func = null) { + if (!typeof(V).is_object()) { + error("WeakMap only takes values that are Objects"); + } + + hash_map = new HashMap(key_hash_func, key_equal_func, value_equal_func); + notify_map = new HashMap(key_hash_func, key_equal_func, value_equal_func); + } + + public override void clear() { + foreach (K key in notify_map.keys) { + Object o = (Object) hash_map[key]; + o.weak_unref(notify_map[key].func); + } + hash_map.clear(); + notify_map.clear(); + } + + public override V @get(K key) { + if (!hash_map.has_key(key)) return null; + + var v = hash_map[key]; + + return (owned) v; + } + + public override bool has(K key, V value) { + assert_not_reached(); + } + + public override bool has_key(K key) { + return hash_map.has_key(key); + } + + public override Gee.MapIterator map_iterator() { + assert_not_reached(); + } + + public override void @set(K key, V value) { + assert(value != null); + + unset(key); + + Object v_obj = (Object) value; + var notify_wrap = new WeakNotifyWrapper((obj) => { + hash_map.unset(key); + notify_map.unset(key); + }); + notify_map[key] = notify_wrap; + v_obj.weak_ref(notify_wrap.func); + + hash_map[key] = value; + } + + public override bool unset(K key, out V value = null) { + if (!hash_map.has_key(key)) return false; + + Object v_obj = (Object) hash_map[key]; + v_obj.weak_unref(notify_map[key].func); + notify_map.unset(key); + return hash_map.unset(key); + } + public override Gee.Set> entries { owned get; } + + [CCode (notify = false)] + public Gee.EqualDataFunc key_equal_func { + get { return hash_map.key_equal_func; } + } + + [CCode (notify = false)] + public Gee.HashDataFunc key_hash_func { + get { return hash_map.key_hash_func; } + } + + public override Gee.Set keys { + owned get { return hash_map.keys; } + } + + public override bool read_only { get { assert_not_reached(); } } + + public override int size { get { return hash_map.size; } } + + [CCode (notify = false)] + public Gee.EqualDataFunc value_equal_func { + get { return hash_map.value_equal_func; } + } + + public override Gee.Collection values { + owned get { + assert_not_reached(); + } + } + + public override void dispose() { + foreach (K key in notify_map.keys) { + Object o = (Object) hash_map[key]; + o.weak_unref(notify_map[key].func); + } + } +} + +internal class WeakNotifyWrapper { + public WeakNotify func; + + public WeakNotifyWrapper(owned WeakNotify func) { + this.func = (owned) func; + } +} \ No newline at end of file diff --git a/libdino/tests/common.vala b/libdino/tests/common.vala new file mode 100644 index 00000000..2a0b189b --- /dev/null +++ b/libdino/tests/common.vala @@ -0,0 +1,67 @@ +namespace Dino.Test { + +int main(string[] args) { + GLib.Test.init(ref args); + GLib.Test.set_nonfatal_assertions(); + TestSuite.get_root().add_suite(new WeakMapTest().get_suite()); + return GLib.Test.run(); +} + +bool fail_if(bool exp, string? reason = null) { + if (exp) { + if (reason != null) GLib.Test.message(reason); + GLib.Test.fail(); + return true; + } + return false; +} + +void fail_if_reached(string? reason = null) { + fail_if(true, reason); +} + +delegate void ErrorFunc() throws Error; + +void fail_if_not_error_code(ErrorFunc func, int expectedCode, string? reason = null) { + try { + func(); + fail_if_reached(@"$(reason + ": " ?? "")no error thrown"); + } catch (Error e) { + fail_if_not_eq_int(e.code, expectedCode, @"$(reason + ": " ?? "")catched unexpected error"); + } +} + +bool fail_if_not(bool exp, string? reason = null) { + return fail_if(!exp, reason); +} + +bool fail_if_eq_int(int left, int right, string? reason = null) { + return fail_if(left == right, @"$(reason + ": " ?? "")$left == $right"); +} + +bool fail_if_not_eq_int(int left, int right, string? reason = null) { + return fail_if_not(left == right, @"$(reason + ": " ?? "")$left != $right"); +} + +bool fail_if_not_eq_str(string left, string right, string? reason = null) { + return fail_if_not(left == right, @"$(reason + ": " ?? "")$left != $right"); +} + +bool fail_if_not_eq_uint8_arr(uint8[] left, uint8[] right, string? reason = null) { + if (fail_if_not_eq_int(left.length, right.length, @"$(reason + ": " ?? "")array length not equal")) return true; + return fail_if_not_eq_str(Base64.encode(left), Base64.encode(right), reason); +} + +bool fail_if_not_zero_int(int zero, string? reason = null) { + return fail_if_not_eq_int(zero, 0, reason); +} + +bool fail_if_zero_int(int zero, string? reason = null) { + return fail_if_eq_int(zero, 0, reason); +} + +bool fail_if_null(void* what, string? reason = null) { + return fail_if(what == null || (size_t)what == 0, reason); +} + +} diff --git a/libdino/tests/jid.vala b/libdino/tests/jid.vala new file mode 100644 index 00000000..08ec5c3a --- /dev/null +++ b/libdino/tests/jid.vala @@ -0,0 +1,39 @@ +using Dino.Entities; + +namespace Dino.Test { + +class JidTest : Gee.TestCase { + + public JidTest() { + base("Jid"); + add_test("parse", test_parse); + add_test("components", test_components); + add_test("with_res", test_with_res); + } + + private void test_parse() { + Jid jid = new Jid("user@example.com/res"); + fail_if(jid.localpart != "user"); + fail_if(jid.domainpart != "example.com"); + fail_if(jid.resourcepart != "res"); + fail_if(jid.to_string() != "user@example.com/res"); + } + + private void test_components() { + Jid jid = new Jid.components("user", "example.com", "res"); + fail_if(jid.localpart != "user"); + fail_if(jid.domainpart != "example.com"); + fail_if(jid.resourcepart != "res"); + fail_if(jid.to_string() != "user@example.com/res"); + } + + private void test_with_res() { + Jid jid = new Jid.with_resource("user@example.com", "res"); + fail_if(jid.localpart != "user"); + fail_if(jid.domainpart != "example.com"); + fail_if(jid.resourcepart != "res"); + fail_if(jid.to_string() != "user@example.com/res"); + } +} + +} diff --git a/libdino/tests/testcase.vala b/libdino/tests/testcase.vala new file mode 100644 index 00000000..87147604 --- /dev/null +++ b/libdino/tests/testcase.vala @@ -0,0 +1,80 @@ +/* testcase.vala + * + * Copyright (C) 2009 Julien Peeters + * + * This library 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 library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Julien Peeters + */ + +public abstract class Gee.TestCase : Object { + + private GLib.TestSuite suite; + private Adaptor[] adaptors = new Adaptor[0]; + + public delegate void TestMethod (); + + protected TestCase (string name) { + this.suite = new GLib.TestSuite (name); + } + + public void add_test (string name, owned TestMethod test) { + var adaptor = new Adaptor (name, (owned)test, this); + this.adaptors += adaptor; + + this.suite.add (new GLib.TestCase (adaptor.name, + adaptor.set_up, + adaptor.run, + adaptor.tear_down )); + } + + public virtual void set_up () { + } + + public virtual void tear_down () { + } + + public GLib.TestSuite get_suite () { + return this.suite; + } + + private class Adaptor { + [CCode (notify = false)] + public string name { get; private set; } + private TestMethod test; + private TestCase test_case; + + public Adaptor (string name, + owned TestMethod test, + TestCase test_case) { + this.name = name; + this.test = (owned)test; + this.test_case = test_case; + } + + public void set_up (void* fixture) { + this.test_case.set_up (); + } + + public void run (void* fixture) { + this.test (); + } + + public void tear_down (void* fixture) { + this.test_case.tear_down (); + } + } +} diff --git a/libdino/tests/weak_map.vala b/libdino/tests/weak_map.vala new file mode 100644 index 00000000..3f552661 --- /dev/null +++ b/libdino/tests/weak_map.vala @@ -0,0 +1,97 @@ +using Dino.Entities; + +namespace Dino.Test { + + class WeakMapTest : Gee.TestCase { + + public WeakMapTest() { + base("WeakMapTest"); + add_test("set", test_set); + add_test("set2", test_set2); + add_test("set3", test_set3); + add_test("set4", test_unset); + add_test("remove_when_out_of_scope", test_remove_when_out_of_scope); +// add_test("non_object_construction", test_non_object_construction); + } + + private void test_set() { + WeakMap map = new WeakMap(); + var o = new Object(); + map[1] = o; + + assert(map.size == 1); + assert(map.has_key(1)); + assert(map[1] == o); + } + + private void test_set2() { + WeakMap map = new WeakMap(); + var o = new Object(); + var o2 = new Object(); + map[1] = o; + map[1] = o2; + + assert(map.size == 1); + assert(map.has_key(1)); + assert(map[1] == o2); + } + + private void test_set3() { + WeakMap map = new WeakMap(); + + var o1 = new Object(); + var o2 = new Object(); + + map[0] = o1; + map[3] = o2; + + { + var o3 = new Object(); + var o4 = new Object(); + map[7] = o3; + map[50] = o4; + } + + var o5 = new Object(); + map[5] = o5; + + assert(map.size == 3); + + assert(map.has_key(0)); + assert(map.has_key(3)); + assert(map.has_key(5)); + + assert(map[0] == o1); + assert(map[3] == o2); + assert(map[5] == o5); + } + + private void test_unset() { + WeakMap map = new WeakMap(); + var o1 = new Object(); + map[7] = o1; + map.unset(7); + + assert_true(map.size == 0); + assert_true(map.is_empty); + assert_false(map.has_key(7)); + + } + + private void test_remove_when_out_of_scope() { + WeakMap map = new WeakMap(); + + { + map[0] = new Object(); + } + + assert_false(map.has_key(0)); + } + + private void test_non_object_construction() { + WeakMap map = new WeakMap(); + assert_not_reached(); + } + } + +} -- cgit v1.2.3-70-g09d2