summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorThomas Holder <thomas.holder@schrodinger.com>2019-11-30 12:10:22 +0100
committerKjell Ahlstedt <kjellahlstedt@gmail.com>2019-11-30 12:10:22 +0100
commit729b1ae95d5d05b78741eeef1d635f95b19d790b (patch)
treea9387aeb698a5d09dc31bb6409c175a962bb813b /tests
parente3f164006e785b9327cae3be3d5ea9f6266a1a1c (diff)
downloadglibmm-729b1ae95d5d05b78741eeef1d635f95b19d790b.tar.gz
Use Glib::UStringView with Glib::ustring::compare()
and with the comparison operators, operator==(), etc. Add the glibmm_ustring_compare test case. https://gitlab.gnome.org/GNOME/glibmm/issues/64 https://gitlab.gnome.org/GNOME/glibmm/issues/65
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/glibmm_ustring_compare/main.cc115
2 files changed, 117 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 68effc10..2ad6e858 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -40,7 +40,7 @@ check_PROGRAMS = \
glibmm_objectbase/test \
glibmm_objectbase_move/test \
glibmm_regex/test \
- glibmm_ustring_compose/test \
+ glibmm_ustring_compare/test \
glibmm_ustring_format/test \
glibmm_ustring_sprintf/test \
glibmm_value/test \
@@ -115,6 +115,7 @@ glibmm_objectbase_test_SOURCES = glibmm_objectbase/main.cc \
glibmm_objectbase_move_test_SOURCES = glibmm_objectbase_move/main.cc \
glibmm_objectbase/test_derived_objectbase.h \
glibmm_object/test_derived_object.h
+glibmm_ustring_compare_test_SOURCES = glibmm_ustring_compare/main.cc
glibmm_ustring_compose_test_SOURCES = glibmm_ustring_compose/main.cc
glibmm_ustring_format_test_SOURCES = glibmm_ustring_format/main.cc
glibmm_ustring_sprintf_test_SOURCES = glibmm_ustring_sprintf/main.cc
diff --git a/tests/glibmm_ustring_compare/main.cc b/tests/glibmm_ustring_compare/main.cc
new file mode 100644
index 00000000..349a2741
--- /dev/null
+++ b/tests/glibmm_ustring_compare/main.cc
@@ -0,0 +1,115 @@
+#include <glibmm.h>
+
+#include <iostream>
+
+// Helper class to check for non-existing overload
+template<typename T>
+struct Convertible
+{
+ Convertible(const T&){};
+};
+
+static bool expect_missing_overload = false;
+
+void
+operator==(Convertible<std::string> const&, Glib::ustring const&)
+{
+ g_assert_true(expect_missing_overload);
+ expect_missing_overload = false;
+}
+
+int
+main(int, char**)
+{
+ // allocating
+ static_assert(std::is_convertible<const char*, Glib::ustring>::value, "");
+ // non-allocating
+ static_assert(std::is_convertible<const char*, Glib::UStringView>::value, "");
+ static_assert(std::is_convertible<Glib::ustring, Glib::UStringView>::value, "");
+ // deliberately omitted
+ static_assert(!std::is_convertible<Glib::UStringView, Glib::ustring>::value, "");
+ static_assert(!std::is_convertible<Glib::UStringView, const char *>::value, "");
+
+ const char *cstr1 = "Hello";
+ const char *cstr2 = "World";
+ const char *cstr12 = "HelloWorld";
+ const char *cstr12_25 = "lloWo"; // cstr12[2:2 + 5]
+
+ Glib::ustring ustr1 = cstr1;
+ Glib::ustring ustr2 = cstr2;
+ Glib::ustring ustr12 = cstr12;
+ Glib::ustring ustr12_25 = cstr12_25;
+
+ Glib::UStringView vstr1 = cstr1;
+ Glib::UStringView vstr2 = cstr2;
+ Glib::UStringView vstr12_25 = cstr12_25;
+
+ g_assert_true(ustr1.compare(ustr1) == 0);
+ g_assert_true(ustr1.compare(cstr1) == 0);
+ g_assert_true(ustr1.compare(vstr1) == 0);
+
+ g_assert_true(ustr1.compare(ustr2) < 0);
+ g_assert_true(ustr1.compare(cstr2) < 0);
+ g_assert_true(ustr1.compare(vstr2) < 0);
+
+ g_assert_true(ustr12.compare(2, 5, ustr12_25) == 0);
+ g_assert_true(ustr12.compare(2, 5, cstr12_25) == 0);
+ g_assert_true(ustr12.compare(2, 5, vstr12_25) == 0);
+
+ g_assert_true(ustr1 == ustr1);
+ g_assert_true(ustr1 == cstr1);
+ g_assert_true(ustr1 == vstr1);
+ g_assert_true(cstr1 == ustr1);
+ g_assert_true(vstr1 == ustr1);
+
+ g_assert_true(ustr2 != ustr1);
+ g_assert_true(ustr2 != cstr1);
+ g_assert_true(ustr2 != vstr1);
+ g_assert_true(cstr2 != ustr1);
+ g_assert_true(vstr2 != ustr1);
+
+ g_assert_true(ustr2 > ustr1);
+ g_assert_true(ustr2 > cstr1);
+ g_assert_true(ustr2 > vstr1);
+ g_assert_true(cstr2 > ustr1);
+ g_assert_true(vstr2 > ustr1);
+
+ g_assert_false(ustr2 < ustr1);
+ g_assert_false(ustr2 < cstr1);
+ g_assert_false(ustr2 < vstr1);
+ g_assert_false(cstr2 < ustr1);
+ g_assert_false(vstr2 < ustr1);
+
+ g_assert_true(ustr2 >= ustr1);
+ g_assert_true(ustr2 >= cstr1);
+ g_assert_true(ustr2 >= vstr1);
+ g_assert_true(cstr2 >= ustr1);
+ g_assert_true(vstr2 >= ustr1);
+
+ g_assert_false(ustr2 <= ustr1);
+ g_assert_false(ustr2 <= cstr1);
+ g_assert_false(ustr2 <= vstr1);
+ g_assert_false(cstr2 <= ustr1);
+ g_assert_false(vstr2 <= ustr1);
+
+ // no conversions between std::string and UStringView and no comparison between
+ // std::string and ustring/UStringView
+
+ static_assert(!std::is_convertible<std::string, Glib::UStringView>::value, "");
+ static_assert(!std::is_convertible<Glib::UStringView, std::string>::value, "");
+
+ std::string sstr1 = cstr1;
+
+ // Would not compile without the helper overload
+ expect_missing_overload = true;
+ sstr1 == ustr1;
+ g_assert_false(expect_missing_overload);
+
+ // Doesn't compile because of missing Glib::ustring::compare overload (expected), but
+ // unfortunately not testable like the other way round.
+#if 0
+ ustr1 == sstr1;
+#endif
+
+ return EXIT_SUCCESS;
+}