diff options
author | Marcin Kolny <marcin.kolny@gmail.com> | 2015-08-08 14:45:12 +0200 |
---|---|---|
committer | Marcin Kolny <marcin.kolny@gmail.com> | 2015-08-08 14:45:59 +0200 |
commit | 0d5f63b18f5766760cf39e82ee11482984e0a938 (patch) | |
tree | e3f74b9b37e7618c92840024077dff43e6d08f0d /tests | |
parent | dce7a844e48a582e42eb2b60eef5c1f2527540ac (diff) | |
parent | d94115843f38967b5e883f5f7d8057882ae364cb (diff) | |
download | glibmm-gir-gmmproc.tar.gz |
Merge branch 'master' into glibmm-gir-gmmprocglibmm-gir-gmmproc
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/giomm_asyncresult_sourceobject/main.cc | 4 | ||||
-rw-r--r-- | tests/giomm_ioerror/main.cc | 4 | ||||
-rw-r--r-- | tests/giomm_memoryinputstream/main.cc | 2 | ||||
-rw-r--r-- | tests/giomm_simple/main.cc | 4 | ||||
-rw-r--r-- | tests/giomm_tls_client/main.cc | 34 | ||||
-rw-r--r-- | tests/glibmm_btree/main.cc | 5 | ||||
-rw-r--r-- | tests/glibmm_interface_implementation/main.cc | 52 | ||||
-rw-r--r-- | tests/glibmm_mainloop/main.cc | 6 | ||||
-rw-r--r-- | tests/glibmm_refptr/main.cc | 275 | ||||
-rw-r--r-- | tests/glibmm_refptr_sigc_bind/main.cc | 1 | ||||
-rw-r--r-- | tests/glibmm_valuearray/main.cc | 22 | ||||
-rw-r--r-- | tests/glibmm_variant/main.cc | 103 | ||||
-rw-r--r-- | tests/glibmm_vector/main.cc | 4 |
14 files changed, 449 insertions, 69 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index c2b0458b..8f35cbb7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -41,6 +41,7 @@ check_PROGRAMS = \ glibmm_bool_arrayhandle/test \ glibmm_null_vectorutils/test \ glibmm_null_containerhandle/test \ + glibmm_refptr/test \ glibmm_refptr_sigc_bind/test \ glibmm_bytearray/test @@ -101,5 +102,6 @@ glibmm_null_vectorutils_test_SOURCES = glibmm_null_vectorutils/main.cc glibmm_null_vectorutils_test_LDADD = $(giomm_ldadd) glibmm_null_containerhandle_test_SOURCES = glibmm_null_containerhandle/main.cc glibmm_null_containerhandle_test_LDADD = $(giomm_ldadd) +glibmm_refptr_test_SOURCES = glibmm_refptr/main.cc glibmm_refptr_sigc_bind_test_SOURCES = glibmm_refptr_sigc_bind/main.cc glibmm_bytearray_test_SOURCES = glibmm_bytearray/main.cc diff --git a/tests/giomm_asyncresult_sourceobject/main.cc b/tests/giomm_asyncresult_sourceobject/main.cc index c9f79e58..12136fa8 100644 --- a/tests/giomm_asyncresult_sourceobject/main.cc +++ b/tests/giomm_asyncresult_sourceobject/main.cc @@ -29,9 +29,9 @@ int main(int, char**) Glib::init(); Gio::init(); - Glib::RefPtr<Glib::MainLoop> mainloop = Glib::MainLoop::create(); + auto mainloop = Glib::MainLoop::create(); - Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("/etc/passwd"); + auto file = Gio::File::create_for_path("/etc/passwd"); file->read_async(&on_read_async); mainloop->run(); diff --git a/tests/giomm_ioerror/main.cc b/tests/giomm_ioerror/main.cc index ae51ec21..797ed22e 100644 --- a/tests/giomm_ioerror/main.cc +++ b/tests/giomm_ioerror/main.cc @@ -34,14 +34,14 @@ int main(int, char**) try { - Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(TEST_FILE); + auto file = Gio::File::create_for_path(TEST_FILE); if(!file) { std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl; return EXIT_FAILURE; } - Glib::RefPtr<Gio::FileInputStream> stream = file->read(); + auto stream = file->read(); if(!stream) { std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl; diff --git a/tests/giomm_memoryinputstream/main.cc b/tests/giomm_memoryinputstream/main.cc index 813fa4e3..8228bd21 100644 --- a/tests/giomm_memoryinputstream/main.cc +++ b/tests/giomm_memoryinputstream/main.cc @@ -43,7 +43,7 @@ int main(int, char**) std::memset(buffer, 0, sizeof buffer); try { - Glib::RefPtr<Gio::MemoryInputStream> stream = Gio::MemoryInputStream::create(); + auto stream = Gio::MemoryInputStream::create(); if (!stream) { std::cerr << "Could not create a MemoryInputStream." << std::endl; diff --git a/tests/giomm_simple/main.cc b/tests/giomm_simple/main.cc index e88c4604..175cadf3 100644 --- a/tests/giomm_simple/main.cc +++ b/tests/giomm_simple/main.cc @@ -22,14 +22,14 @@ int main(int, char**) try { - Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(TEST_FILE); + auto file = Gio::File::create_for_path(TEST_FILE); if(!file) { std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl; return EXIT_FAILURE; } - Glib::RefPtr<Gio::FileInputStream> stream = file->read(); + auto stream = file->read(); if(!stream) { std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl; diff --git a/tests/giomm_tls_client/main.cc b/tests/giomm_tls_client/main.cc index c76551a1..2eb0eaf0 100644 --- a/tests/giomm_tls_client/main.cc +++ b/tests/giomm_tls_client/main.cc @@ -29,7 +29,7 @@ bool on_accept_certificate(const Glib::RefPtr<const Gio::TlsCertificate>& cert, std::cout << "Outputing certificate data:" << std::endl << cert->property_certificate_pem().get_value(); - Glib::RefPtr<const Gio::TlsCertificate> issuer = cert->get_issuer(); + auto issuer = cert->get_issuer(); std::cout << "Outputing the issuer's certificate data:" << std::endl << issuer->property_certificate_pem().get_value(); @@ -44,7 +44,7 @@ int main(int, char**) { Gio::init(); - const Glib::ustring test_host = "www.google.com"; + const Glib::ustring test_host = "www.gnome.org"; std::vector< Glib::RefPtr<Gio::InetAddress> > inet_addresses; @@ -74,19 +74,29 @@ int main(int, char**) std::cout << "Successfully resolved address of test host '" << test_host << "'." << std::endl; - Glib::RefPtr<Gio::InetAddress> first_inet_address = inet_addresses[0]; + auto first_inet_address = inet_addresses[0]; std::cout << "First address of test host is " << first_inet_address->to_string() << "." << std::endl; - Glib::RefPtr<Gio::Socket> socket = + auto socket = Gio::Socket::create(first_inet_address->get_family(), Gio::SOCKET_TYPE_STREAM, Gio::SOCKET_PROTOCOL_TCP); - Glib::RefPtr<Gio::InetSocketAddress> address = + auto address = Gio::InetSocketAddress::create(first_inet_address, 443); - socket->connect(address); + try + { + socket->connect(address); + } + catch(const Gio::Error& ex) + { + std::cout << "Could not connect socket to " << + address->get_address()->to_string() << ":" << address->get_port() << + ". Exception: " << ex.what() << std::endl; + return EXIT_FAILURE; + } if(!socket->is_connected()) { @@ -95,7 +105,7 @@ int main(int, char**) "." << std::endl; } - Glib::RefPtr<Gio::TcpConnection> conn = Glib::RefPtr<Gio::TcpConnection>::cast_dynamic(Gio::SocketConnection::create(socket)); + auto conn = Glib::RefPtr<Gio::TcpConnection>::cast_dynamic(Gio::SocketConnection::create(socket)); if(!conn || !conn->is_connected()) { @@ -110,11 +120,9 @@ int main(int, char**) address->get_address()->to_string() << ":" << address->get_port() << "." << std::endl; - Glib::RefPtr<Gio::TlsClientConnection> tls_connection; - try { - Glib::RefPtr<Gio::TlsClientConnection> tls_connection = + auto tls_connection = Gio::TlsClientConnection::create(conn, address); tls_connection->signal_accept_certificate().connect( @@ -125,7 +133,7 @@ int main(int, char**) std::cout << "Attempting to get the issuer's certificate from the " "connection." << std::endl; - Glib::RefPtr<Gio::TlsCertificate> issuer_certificate = + auto issuer_certificate = tls_connection->get_peer_certificate()->get_issuer(); if(!issuer_certificate) @@ -138,12 +146,12 @@ int main(int, char**) std::endl; std::cout << "Attempting to use the connection's database." << std::endl; - Glib::RefPtr<Gio::TlsDatabase> database = tls_connection->get_database(); + auto database = tls_connection->get_database(); std::cout << "Looking up the certificate's issuer in the database." << std::endl; - Glib::RefPtr<Gio::TlsCertificate> db_certificate = + auto db_certificate = database->lookup_certificate_issuer(issuer_certificate); if(!db_certificate) diff --git a/tests/glibmm_btree/main.cc b/tests/glibmm_btree/main.cc index c678c37a..837b7ae7 100644 --- a/tests/glibmm_btree/main.cc +++ b/tests/glibmm_btree/main.cc @@ -74,8 +74,7 @@ my_p_key_compare(const type_p_key_value& key_a, const type_p_key_value& key_b) int main() { - type_key_value::const_iterator i; - Glib::RefPtr< Glib::BalancedTree<type_key_value, type_key_value> > tree = Glib::BalancedTree<type_key_value, type_key_value>::create(); + auto tree = Glib::BalancedTree<type_key_value, type_key_value>::create(); for (type_key_value::size_type i = 0; i < str.size(); ++i) tree->insert(str.substr(i, 1), str.substr(i, 1)); @@ -138,7 +137,7 @@ main() value = tree->search(sigc::ptr_fun(my_search), "|"); g_assert(value == NULL); - Glib::RefPtr< Glib::BalancedTree<type_p_key_value, type_p_key_value> > ptree = Glib::BalancedTree<type_p_key_value, type_p_key_value>::create(sigc::ptr_fun(my_p_key_compare)); + auto ptree = Glib::BalancedTree<type_p_key_value, type_p_key_value>::create(sigc::ptr_fun(my_p_key_compare)); for (type_key_value::size_type i = 0; i < str.size(); ++i) pstr.push_back(new type_key_value(str.substr(i, 1))); diff --git a/tests/glibmm_interface_implementation/main.cc b/tests/glibmm_interface_implementation/main.cc index bc8253b1..40da2787 100644 --- a/tests/glibmm_interface_implementation/main.cc +++ b/tests/glibmm_interface_implementation/main.cc @@ -1,6 +1,12 @@ +// This program does not only test the implementation of an interface +// in a custom class. It also tests virtual functions that have leaked memory +// or printed unjustified critical messages in glibmm before version 2.44. +// See https://bugzilla.gnome.org/show_bug.cgi?id=705124. + #include <glibmm.h> #include <giomm.h> //There are no Interfaces in glibmm, but there are in giomm. #include <iostream> +#include <cstring> class CustomAction : public Gio::Action, @@ -13,8 +19,10 @@ public: Glib::Property<Glib::ustring> property; protected: - //Implement a vfunc: - virtual Glib::ustring get_name_vfunc() const; + //Implement vfuncs: + Glib::ustring get_name_vfunc() const override; + Glib::VariantType get_state_type_vfunc() const override; + Glib::VariantBase get_state_hint_vfunc() const override; }; CustomAction::CustomAction() @@ -32,33 +40,65 @@ Glib::ustring CustomAction::get_name_vfunc() const return "custom-name"; } +Glib::VariantType CustomAction::get_state_type_vfunc() const +{ + return Glib::VariantType(G_VARIANT_TYPE_INT16); +} + +Glib::VariantBase CustomAction::get_state_hint_vfunc() const +{ + return Glib::Variant<gint16>::create(42); +} int main(int, char**) { Glib::init(); CustomAction action; + bool success = true; + Glib::ustring name = action.get_name(); std::cout << "The name is '" << name << "'." << std::endl; + success &= name == "custom-name"; + std::cout << "The name property of the implemented interface is '" << action.property_name().get_value() << "'." << std::endl; + success &= action.property_name().get_value() == ""; + std::cout << "The custom string property is '" << action.property.get_value() << "'." << std::endl; + success &= action.property.get_value() == "Initial value."; action.property = "A new value."; std::cout << "The custom string property (after changing it) is '" << action.property.get_value() << "'." << std::endl; + success &= action.property.get_value() == "A new value."; - gchar* prop_value = 0; + gchar* prop_value = nullptr; g_object_set(action.gobj(), "custom_property", "Another value", NULL); g_object_get(action.gobj(), "custom_property", &prop_value, NULL); - std::cout << "The custom property after g_object_get/set() is '" + std::cout << "The custom property after g_object_set/get() is '" << prop_value << "'." << std::endl; + success &= std::strcmp(prop_value, "Another value") == 0; + g_free(prop_value); + prop_value = nullptr; + std::cout << "The custom property through the Glib::Property<> is '" << action.property.get_value() << "'." << std::endl; + success &= action.property.get_value() == "Another value"; + std::cout << "The name property of the implemented interface is '" << action.property_name().get_value() << "'." << std::endl; - g_assert(get_name_called); + success &= action.property_name().get_value() == ""; + success &= get_name_called; + + // Check if other vfuncs leak memory. Use valgrind! + action.get_parameter_type(); + action.get_state_type(); + action.get_state_type(); + action.get_state_hint_variant(); + action.get_state_variant(); + action.get_enabled(); - return EXIT_SUCCESS; + return success ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/glibmm_mainloop/main.cc b/tests/glibmm_mainloop/main.cc index fbf70027..56bd02d9 100644 --- a/tests/glibmm_mainloop/main.cc +++ b/tests/glibmm_mainloop/main.cc @@ -50,8 +50,8 @@ bool mark_and_quit(const Glib::Threads::Thread* expected_thread, void thread_function(const Glib::Threads::Thread* first_thread, const Glib::RefPtr<Glib::MainLoop>& first_mainloop) { - Glib::RefPtr<Glib::MainContext> second_context = Glib::MainContext::create(); - Glib::RefPtr<Glib::MainLoop> second_mainloop = Glib::MainLoop::create(second_context); + auto second_context = Glib::MainContext::create(); + auto second_mainloop = Glib::MainLoop::create(second_context); // Show how Glib::MainContext::invoke() can be used for calling a function, // possibly executed in another thread. @@ -75,7 +75,7 @@ int main(int, char**) { Glib::init(); - Glib::RefPtr<Glib::MainLoop> first_mainloop = Glib::MainLoop::create(); + auto first_mainloop = Glib::MainLoop::create(); // This thread shall be the owner of the default main context, when // thread_function() calls mark_and_quit() via Glib::MainContext::invoke(), diff --git a/tests/glibmm_refptr/main.cc b/tests/glibmm_refptr/main.cc new file mode 100644 index 00000000..d03b2249 --- /dev/null +++ b/tests/glibmm_refptr/main.cc @@ -0,0 +1,275 @@ +// Bug 564005 - Valgrind errors and crash on exit with Gtk::UIManager +// Bug 154498 - Unnecessary warning on console: signalproxy_connectionnode.cc + + +#include <glibmm.h> +#include <sigc++/sigc++.h> +#include <iostream> +#include <stdlib.h> + +#define ACTIVATE_BUG 1 + +// A class with its own reference-count, for use with RefPtr. +class Something +{ +public: + Something() + : ref_count_(1), + max_ref_count_(ref_count_) + {} + + void reference() + { + ++ref_count_; + + //Track the highest-ever max count. + if(max_ref_count_ < ref_count_) + max_ref_count_ = ref_count_; + } + + void unreference() + { + if (--ref_count_ <= 0) + delete this; + } + + //Just so we can check it in our test. + int ref_count() + { + return ref_count_; + } + + //Just so we can check it in our test. + int max_ref_count() + { + return max_ref_count_; + } + + +private: + int ref_count_; + int max_ref_count_; +}; + +class SomethingDerived : public Something +{ +}; + +class Parent +{ +public: + explicit Parent(const Glib::RefPtr<Something>& something) + : something_(something), + was_constructed_via_copy_constructor_(true), + was_constructed_via_move_constructor_(false) + { + } + + explicit Parent(Glib::RefPtr<Something>&& something) + : something_(std::move(something)), + was_constructed_via_copy_constructor_(false), + was_constructed_via_move_constructor_(true) + { + } + + //Non copyable + Parent(const Parent& src) = delete; + Parent& operator=(const Parent& src) = delete; + + bool was_constructed_via_copy_constructor() const + { + return was_constructed_via_copy_constructor_; + } + + bool was_constructed_via_move_constructor() const + { + return was_constructed_via_move_constructor_; + } + + int something_ref_count() const + { + return something_->ref_count(); + } + + int something_max_ref_count() const + { + return something_->max_ref_count(); + } + +private: + Glib::RefPtr<Something> something_; + bool was_constructed_via_copy_constructor_; + bool was_constructed_via_move_constructor_; + +}; + +static +void test_initial_refcount() +{ + Glib::RefPtr<Something> refSomething (new Something()); + g_assert_cmpint(refSomething->ref_count(), ==, 1); + g_assert_cmpint(refSomething->max_ref_count(), ==, 1); +} + +static +void test_refptr_copy_constructor() +{ + Glib::RefPtr<Something> refSomething (new Something()); + g_assert_cmpint(refSomething->ref_count(), ==, 1); + g_assert_cmpint(refSomething->max_ref_count(), ==, 1); + + { + Glib::RefPtr<Something> refSomething2(refSomething); + g_assert_cmpint(refSomething->ref_count(), ==, 2); + g_assert_cmpint(refSomething2->ref_count(), ==, 2); + g_assert_cmpint(refSomething->max_ref_count(), ==, 2); + } + + //Test the refcount after other references should have been released + //when other RefPtrs went out of scope: + g_assert_cmpint(refSomething->ref_count(), ==, 1); + g_assert_cmpint(refSomething->max_ref_count(), ==, 2); +} + +static +void test_refptr_assignment_operator() +{ + Glib::RefPtr<Something> refSomething (new Something()); + g_assert_cmpint(refSomething->ref_count(), ==, 1); + g_assert_cmpint(refSomething->max_ref_count(), ==, 1); + + { + Glib::RefPtr<Something> refSomething2 = refSomething; + g_assert_cmpint(refSomething->ref_count(), ==, 2); + g_assert_cmpint(refSomething2->ref_count(), ==, 2); + g_assert_cmpint(refSomething->max_ref_count(), ==, 2); + } + + //Test the refcount after other references should have been released + //when other RefPtrs went out of scope: + g_assert_cmpint(refSomething->ref_count(), ==, 1); + g_assert_cmpint(refSomething->max_ref_count(), ==, 2); +} + + + +static +Glib::RefPtr<Something> get_something() +{ + static Glib::RefPtr<Something> something_to_get; + + //Reinitialize it each time: + something_to_get = Glib::RefPtr<Something>(new Something()); + + return something_to_get; +} + +static +void test_refptr_with_parent_copy_constructor() +{ + //We use get_something() because test_refptr_with_parent_move_constructor() does. + Glib::RefPtr<Something> refSomething = get_something(); + g_assert_cmpint(refSomething->ref_count(), ==, 2); //1 here and 1 inside get_something() + g_assert_cmpint(refSomething->max_ref_count(), ==, 2); + + { + Parent parent(refSomething); + g_assert(!parent.was_constructed_via_move_constructor()); + g_assert(parent.was_constructed_via_copy_constructor()); + g_assert_cmpint(parent.something_ref_count(), ==, 3); //1 here, 1 in parent, and 1 inside get_something() + g_assert_cmpint(parent.something_max_ref_count(), ==, 3); + } + + //Test the refcount after other references should have been released + //when other RefPtrs went out of scope: + g_assert_cmpint(refSomething->ref_count(), ==, 2); //1 here and 1 inside get_something() + g_assert_cmpint(refSomething->max_ref_count(), ==, 3); +} + +static +void test_refptr_with_parent_move_constructor() +{ + Parent parent(get_something()); + g_assert(parent.was_constructed_via_move_constructor()); + g_assert(!parent.was_constructed_via_copy_constructor()); + g_assert_cmpint(parent.something_ref_count(), ==, 2); //1 in parent and 1 inside get_something() + g_assert_cmpint(parent.something_max_ref_count(), ==, 2); +} + +static +void test_refptr_move_constructor() +{ + Glib::RefPtr<Something> refSomething(new Something()); + Glib::RefPtr<Something> refSomething2(std::move(refSomething)); + g_assert_cmpint(refSomething2->ref_count(), ==, 1); + g_assert(!refSomething); + g_assert_cmpint(refSomething2->max_ref_count(), ==, 1); +} + +static +void test_refptr_move_assignment_operator() +{ + Glib::RefPtr<Something> refSomething(new Something()); + Glib::RefPtr<Something> refSomething2; + refSomething2 = std::move(refSomething); + g_assert_cmpint(refSomething2->ref_count(), ==, 1); + g_assert(!refSomething); + g_assert_cmpint(refSomething2->max_ref_count(), ==, 1); +} + +static +void test_refptr_universal_reference_move_constructor() +{ + Glib::RefPtr<SomethingDerived> refSomethingDerived(new SomethingDerived()); + Glib::RefPtr<Something> refSomething(std::move(refSomethingDerived)); + g_assert_cmpint(refSomething->ref_count(), ==, 1); + g_assert(!refSomethingDerived); + g_assert_cmpint(refSomething->max_ref_count(), ==, 1); +} + +static +void test_refptr_universal_reference_asignment_operator() +{ + Glib::RefPtr<SomethingDerived> refSomethingDerived(new SomethingDerived()); + Glib::RefPtr<Something> refSomething; + refSomething = std::move(refSomethingDerived); + g_assert_cmpint(refSomething->ref_count(), ==, 1); + g_assert(!refSomethingDerived); + g_assert_cmpint(refSomething->max_ref_count(), ==, 1); +} + +int main(int, char**) +{ + //Test initial refcount: + test_initial_refcount(); + + //Test refcount when using the RefPtr copy constructor: + test_refptr_copy_constructor(); + + //Test refcount when using the RefPtr assignment operator (operator=): + test_refptr_assignment_operator(); + + //Test the refcount when using the RefPtr move constuctor: + test_refptr_move_constructor(); + + //Test the refcount when using the RefPtr move asignment operator (operator=): + test_refptr_move_assignment_operator(); + + //Test the refcount when another class makes a copy via its constructor: + test_refptr_with_parent_copy_constructor(); + + //Test the refcount when another class makes a copy via its + //(perfect-forwarding) move constructor, which should not involve a temporary + //instance: + test_refptr_with_parent_move_constructor(); + + //Test the refcount when using the RefPtr move constructor with derived class + //as an argument. + test_refptr_universal_reference_move_constructor(); + + //Test the refcount when using the RefPtr assignment operator (operator=) + //with derived class as an argument. + test_refptr_universal_reference_asignment_operator(); + + return EXIT_SUCCESS; +} diff --git a/tests/glibmm_refptr_sigc_bind/main.cc b/tests/glibmm_refptr_sigc_bind/main.cc index 27b54e9e..38fe93b1 100644 --- a/tests/glibmm_refptr_sigc_bind/main.cc +++ b/tests/glibmm_refptr_sigc_bind/main.cc @@ -1,7 +1,6 @@ // Bug 564005 - Valgrind errors and crash on exit with Gtk::UIManager // Bug 154498 - Unnecessary warning on console: signalproxy_connectionnode.cc -// libsigc++-only test case. (Or almost so. glib_refptr.h is stolen from glibmm.) #include <glibmm/refptr.h> #include <sigc++/sigc++.h> diff --git a/tests/glibmm_valuearray/main.cc b/tests/glibmm_valuearray/main.cc index 2139d436..ca111432 100644 --- a/tests/glibmm_valuearray/main.cc +++ b/tests/glibmm_valuearray/main.cc @@ -41,23 +41,23 @@ int on_compare(const Glib::ValueBase& v1, const Glib::ValueBase& v2) int main(int, char**) { - const int VALUES = 10; + const int VALUES_COUNT = 10; Glib::init(); - Glib::Value<int> value[VALUES]; + Glib::Value<int> values[VALUES_COUNT]; Glib::ValueArray array; - for(int i = 0; i < VALUES; i++) + for(int i = 0; i < VALUES_COUNT; i++) { - value[i].init(Glib::Value<int>::value_type()); - value[i].set(i + 1); // (i + 1) ==> Set to natural counting numbers. - array.prepend(value[i]); + values[i].init(Glib::Value<int>::value_type()); + values[i].set(i + 1); // (i + 1) ==> Set to natural counting numbers. + array.prepend(values[i]); } ostr << "Array members before sorting:" << std::endl; - for(int i = 0; i < VALUES; i++) + for(int i = 0; i < VALUES_COUNT; i++) { Glib::ValueBase value; @@ -69,18 +69,18 @@ int main(int, char**) break; } - Glib::Value<int> int_val = static_cast< Glib::Value<int>& >(value); + auto int_val = static_cast< Glib::Value<int>& >(value); ostr << int_val.get() << " "; } ostr << std::endl; // End of line for list of array elements. // Sort array and remove last element: - array.sort(sigc::ptr_fun(&on_compare)).remove(VALUES - 1); + array.sort(sigc::ptr_fun(&on_compare)).remove(VALUES_COUNT - 1); ostr << "Array members after sorting without last element:" << std::endl; - for(int i = 0; i < VALUES - 1; i++) + for(int i = 0; i < VALUES_COUNT - 1; i++) { Glib::ValueBase value; @@ -92,7 +92,7 @@ int main(int, char**) break; } - Glib::Value<int> int_val = static_cast< Glib::Value<int>& >(value); + auto int_val = static_cast< Glib::Value<int>& >(value); ostr << int_val.get() << " "; } ostr << std::endl; // End of line for list of array elements. diff --git a/tests/glibmm_variant/main.cc b/tests/glibmm_variant/main.cc index 086719f2..47943de3 100644 --- a/tests/glibmm_variant/main.cc +++ b/tests/glibmm_variant/main.cc @@ -26,10 +26,10 @@ int main(int, char**) for(guint i = 0; i < int_vector.size(); i++) ostr << int_vector[i] << std::endl; - Glib::Variant< std::vector<int> > integers_variant = + auto integers_variant = Glib::Variant< std::vector<int> >::create(int_vector); - std::vector<int> int_vector2 = integers_variant.get(); + auto int_vector2 = integers_variant.get(); ostr << "The size of the copied vector is " << int_vector2.size() << '.' << std::endl; @@ -53,7 +53,7 @@ int main(int, char**) //vector<std::string>: std::vector<std::string> vec_strings; vec_strings.push_back("a"); - Glib::Variant<std::vector<std::string> > variant_vec_strings = + auto variant_vec_strings = Glib::Variant<std::vector<std::string> >::create(vec_strings); //Dict: @@ -65,7 +65,7 @@ int main(int, char**) ostr << "The original dictionary entry is (" << dict_entry.first << ", " << dict_entry.second << ")." << std::endl; - Glib::Variant<TypeDictEntry> dict_entry_variant = + auto dict_entry_variant = Glib::Variant<TypeDictEntry>::create(dict_entry); TypeDictEntry copy_entry = dict_entry_variant.get(); @@ -92,7 +92,7 @@ int main(int, char**) ostr << "(" << i << ", " << orig_dict[i] << ")." << std::endl; } - Glib::Variant<TypeDict> orig_dict_variant = + auto orig_dict_variant = Glib::Variant<TypeDict>::create(orig_dict); TypeDict dict_copy = orig_dict_variant.get(); @@ -106,7 +106,7 @@ int main(int, char**) index = 3; - std::pair<unsigned, Glib::ustring> a_pair = orig_dict_variant.get_child(index); + auto a_pair = orig_dict_variant.get_child(index); ostr << "Element number " << index + 1 << " in the variant is: (" << a_pair.first << ", " << a_pair.second << ")." << std::endl; @@ -134,7 +134,7 @@ int main(int, char**) Glib::ustring s = "String " + ss.str(); - Glib::Variant<int> v = Glib::Variant<int>::create(i); + auto v = Glib::Variant<int>::create(i); complex_dict1.insert( std::pair< Glib::ustring, Glib::Variant<int> >("Map 1 " + s, v)); @@ -150,7 +150,7 @@ int main(int, char**) complex_vector.push_back(complex_dict1); complex_vector.push_back(complex_dict2); - Glib::Variant<ComplexVecType> complex_variant = + auto complex_variant = Glib::Variant<ComplexVecType>::create(complex_vector); // This will output the type string aa{sv}. @@ -166,10 +166,8 @@ int main(int, char**) ComplexDictType map = copy_complex_vector[i]; - for(ComplexDictType::const_iterator iter = map.begin(); - iter != map.end(); iter++) + for(const auto& entry : map) { - std::pair< Glib::ustring, Glib::Variant<int> > entry = *iter; ostr << entry.first << " -> " << entry.second.get() << "." << std::endl; } ostr << std::endl; @@ -189,7 +187,7 @@ static void test_dynamic_cast_ustring_types() try { - Glib::Variant<Glib::ustring> derived = + auto derived = Glib::VariantBase::cast_dynamic< Glib::Variant<Glib::ustring> >(vbase_string); ostr << "Casted string Glib::Variant<Glib::ustring>: " << derived.get() << std::endl; } @@ -204,7 +202,7 @@ static void test_dynamic_cast_ustring_types() try { - Glib::Variant<Glib::ustring> derived = + auto derived = Glib::VariantBase::cast_dynamic< Glib::Variant<Glib::ustring> >(vbase_objectpath); ostr << "Casted object path Glib::Variant<Glib::ustring>: " << derived.get() << std::endl; } @@ -218,7 +216,7 @@ static void test_dynamic_cast_ustring_types() try { - Glib::Variant<Glib::ustring> derived = + auto derived = Glib::VariantBase::cast_dynamic< Glib::Variant<Glib::ustring> >(vbase_signature); ostr << "Casted signature Glib::Variant<Glib::ustring>: " << derived.get() << std::endl; } @@ -237,7 +235,7 @@ static void test_dynamic_cast_string_types() try { - Glib::Variant<std::string> derived = + auto derived = Glib::VariantBase::cast_dynamic< Glib::Variant<std::string> >(vbase_string); ostr << "Casted string Glib::Variant<std::string>: " << derived.get() << std::endl; } @@ -252,7 +250,7 @@ static void test_dynamic_cast_string_types() try { - Glib::Variant<std::string> derived = + auto derived = Glib::VariantBase::cast_dynamic< Glib::Variant<std::string> >(vbase_objectpath); ostr << "Casted object path Glib::Variant<std::string>: " << derived.get() << std::endl; } @@ -266,7 +264,7 @@ static void test_dynamic_cast_string_types() try { - Glib::Variant<std::string> derived = + auto derived = Glib::VariantBase::cast_dynamic< Glib::Variant<std::string> >(vbase_signature); ostr << "Casted signature Glib::Variant<std::string>: " << derived.get() << std::endl; } @@ -276,11 +274,69 @@ static void test_dynamic_cast_string_types() } } +// Test casting a complicated type, containing an object path and a DBus type signature. +void test_dynamic_cast_composite_types() +{ + // Build a GVaraint of type a{oag}, and cast it to + // Glib::Variant<std::map<Glib::ustring, std::vector<std::string>>>. + // 'o' is VARIANT_TYPE_OBJECT_PATH and 'g' is VARIANT_TYPE_SIGNATURE. + + GVariantBuilder dict_builder; + GVariantBuilder array_builder; + g_variant_builder_init(&dict_builder, G_VARIANT_TYPE("a{oag}")); + + g_variant_builder_init(&array_builder, G_VARIANT_TYPE("ag")); + g_variant_builder_add(&array_builder, "g","id"); + g_variant_builder_add(&array_builder, "g","isi"); + g_variant_builder_add(&array_builder, "g","ia{si}"); + g_variant_builder_add(&dict_builder, "{oag}", "/remote/object/path1", &array_builder); + + g_variant_builder_init(&array_builder, G_VARIANT_TYPE("ag")); + g_variant_builder_add(&array_builder, "g","i(d)"); + g_variant_builder_add(&array_builder, "g","i(si)"); + g_variant_builder_add(&dict_builder, "{oag}", "/remote/object/path2", &array_builder); + + Glib::VariantBase cppdict(g_variant_builder_end(&dict_builder)); + + try + { + typedef std::map<Glib::ustring, std::vector<std::string> > composite_type; + auto derived = + Glib::VariantBase::cast_dynamic<Glib::Variant<composite_type> >(cppdict); + + ostr << "Cast composite type (get_type_string()=" << derived.get_type_string() + << ", variant_type().get_string()=" << derived.variant_type().get_string() << "): "; + composite_type var = derived.get(); + for (const auto& the_pair : var) + { + ostr << "\n " << the_pair.first << ":"; + const auto& vec = the_pair.second; + for (const auto& str : vec) + ostr << " " << str; + } + ostr << std::endl; + } + catch (const std::bad_cast& e) + { + g_assert_not_reached(); + } + + try + { + auto derived = + Glib::VariantBase::cast_dynamic<Glib::Variant<std::map<Glib::ustring, std::string> > >(cppdict); + g_assert_not_reached(); + } + catch (const std::bad_cast& e) + { + } +} + static void test_dynamic_cast() { - Glib::Variant<int> v1 = Glib::Variant<int>::create(10); + auto v1 = Glib::Variant<int>::create(10); Glib::VariantBase& v2 = v1; - Glib::Variant<int> v3 = Glib::VariantBase::cast_dynamic<Glib::Variant<int> >(v2); + auto v3 = Glib::VariantBase::cast_dynamic<Glib::Variant<int> >(v2); g_assert(v3.get() == 10); Glib::VariantBase v5 = v1; @@ -327,7 +383,7 @@ static void test_dynamic_cast() type_dict_sv var_map; type_map_sv map; - Glib::Variant<Glib::ustring> var_string = + auto var_string = Glib::Variant<Glib::ustring>::create("test variant"); map["test key"] = var_string; var_map = type_dict_sv::create(map); @@ -338,7 +394,7 @@ static void test_dynamic_cast() try { - Glib::Variant<std::map<Glib::ustring, Glib::ustring> > var_wrong_map = + auto var_wrong_map = Glib::VariantBase::cast_dynamic<Glib::Variant<std::map<Glib::ustring, Glib::ustring> > >(ref_var_base); g_assert_not_reached(); } @@ -351,14 +407,15 @@ static void test_dynamic_cast() g_assert(var_string.get() == "test variant"); // A variant of type v - Glib::Variant<Glib::VariantBase> var_v = Glib::Variant<Glib::VariantBase>::create(var_string); + auto var_v = Glib::Variant<Glib::VariantBase>::create(var_string); g_assert(var_v.get_type_string() == "v"); - Glib::Variant<Glib::ustring> var_s2 = + auto var_s2 = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring> >(var_v.get()); g_assert(var_s2.get() == "test variant"); test_dynamic_cast_ustring_types(); test_dynamic_cast_string_types(); + test_dynamic_cast_composite_types(); } static GLogLevelFlags diff --git a/tests/glibmm_vector/main.cc b/tests/glibmm_vector/main.cc index b1ae14bb..d184ecc0 100644 --- a/tests/glibmm_vector/main.cc +++ b/tests/glibmm_vector/main.cc @@ -40,7 +40,7 @@ const unsigned int magic_limit(5); GList* create_list() { - GList* head = 0; + GList* head = nullptr; for(unsigned int iter(0); iter < magic_limit; ++iter) { @@ -142,7 +142,7 @@ copy_array(GCredentials** array) { dup[iter] = array[iter]; } - dup[magic_limit] = 0; + dup[magic_limit] = nullptr; return dup; } |