]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
glib-2.0: Bind assert_cmp* functions
authorChristopher White <cwhite@d3engineering.com>
Sat, 28 Nov 2020 17:47:41 +0000 (12:47 -0500)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 2 Dec 2020 21:37:28 +0000 (22:37 +0100)
Add assert_cmpstr, assert_cmpint, assert_cmpuint, assert_cmphex,
assert_cmpfloat, assert_cmpfloat_with_epsilon and assert_cmpvariant.

Add enum GLib.CompareOperator which defines the supported operators.

  "g_assert_cmpint(foo,==,1)" translates to "assert_cmpint(foo, EQ, 1)"

Based on patch by Luca Bruno

Fixes https://gitlab.gnome.org/GNOME/vala/issues/395

tests/Makefile.am
tests/basic-types/gassert.vala [new file with mode: 0644]
vapi/glib-2.0.vapi

index 010772c3ca61998597b852c7b42713dd1cf0bc4f..b620a847f6f83b1324af61e9be090940c749ba0f 100644 (file)
@@ -38,6 +38,7 @@ AM_TESTS_ENVIRONMENT = \
        export CC='$(CC)';
 
 TESTS = \
+       basic-types/gassert.vala \
        basic-types/integers.vala \
        basic-types/integers-boxed-cast.vala \
        basic-types/escape-chars.vala \
diff --git a/tests/basic-types/gassert.vala b/tests/basic-types/gassert.vala
new file mode 100644 (file)
index 0000000..05ea905
--- /dev/null
@@ -0,0 +1,108 @@
+void test_assert_cmpstr () {
+       // assume g_strcmp0() behaviour for nulls
+       assert_cmpstr (null, EQ, null);
+       assert_cmpstr ("", GT, null);
+       assert_cmpstr (null, LT, "");
+       assert_cmpstr (null, LT, "");
+       assert_cmpstr (null, NE, "some non-null, non-empty string");
+       assert_cmpstr (null, LT, "some non-null, non-empty string");
+       assert_cmpstr (null, LE, "some non-null, non-empty string");
+       assert_cmpstr ("some non-null, non-empty string", NE, null);
+       assert_cmpstr ("some non-null, non-empty string", GT, null);
+       assert_cmpstr ("some non-null, non-empty string", GE, null);
+
+       assert_cmpstr ("0", LT, "1");
+       assert_cmpstr ("0", LE, "1");
+       assert_cmpstr ("1", LE, "1");
+       assert_cmpstr ("2", EQ, "2");
+       assert_cmpstr ("3", GE, "3");
+       assert_cmpstr ("4", GE, "3");
+       assert_cmpstr ("4", GT, "3");
+       assert_cmpstr ("4", NE, "3");
+}
+
+void test_assert_cmpint () {
+       assert_cmpint (0, LT, 1);
+       assert_cmpint (0, NE, 1);
+       assert_cmpint (0, LE, 1);
+       assert_cmpint (1, LE, 1);
+       assert_cmpint (1, EQ, 1);
+       assert_cmpint (1, GE, 1);
+       assert_cmpint (2, GE, 1);
+       assert_cmpint (2, GT, 1);
+
+       assert_cmpint (-1, GT, -2);
+       assert_cmpint (-1, NE, -2);
+       assert_cmpint (-1, GE, -2);
+       assert_cmpint (-2, GE, -2);
+       assert_cmpint (-2, EQ, -2);
+       assert_cmpint (-2, LE, -2);
+       assert_cmpint (-3, LE, -2);
+       assert_cmpint (-3, LT, -2);
+
+       assert_cmpint (-100, LT, 101);
+       assert_cmpint (-100, NE, 101);
+       assert_cmpint (-100, LE, 101);
+       assert_cmpint (-101, LE, 101);
+       assert_cmpint (101, GE, -101);
+       assert_cmpint (102, GE, -101);
+       assert_cmpint (102, GT, -101);
+}
+
+void test_assert_cmpuint () {
+       assert_cmpuint (0U, LT, 1U);
+       assert_cmpuint (0U, NE, 1U);
+       assert_cmpuint (0U, LE, 1U);
+       assert_cmpuint (1U, LE, 1U);
+       assert_cmpuint (1U, EQ, 1U);
+       assert_cmpuint (1U, GE, 1U);
+       assert_cmpuint (2U, GE, 1U);
+       assert_cmpuint (2U, GT, 1U);
+}
+
+void test_assert_cmphex () {
+       assert_cmphex (0x0, LT, 0x1);
+       assert_cmphex (0x0, NE, 0x1);
+       assert_cmphex (0x0, LE, 0x1);
+       assert_cmphex (0x1, LE, 0x1);
+       assert_cmphex (0x1, EQ, 0x1);
+       assert_cmphex (0x1, GE, 0x1);
+       assert_cmphex (0x2, GE, 0x1);
+       assert_cmphex (0x2, GT, 0x1);
+}
+
+void test_assert_cmpfloat () {
+       assert_cmpfloat (0.0f, LT, 1.0f);
+       assert_cmpfloat (0.0f, NE, 1.0f);
+       assert_cmpfloat (0.0f, LE, 1.0f);
+       assert_cmpfloat (1.0f, LE, 1.0f);
+       assert_cmpfloat (1.0f, EQ, 1.0f);
+       assert_cmpfloat (1.0f, GE, 1.0f);
+       assert_cmpfloat (2.0f, GE, 1.0f);
+       assert_cmpfloat (2.0f, GT, 1.0f);
+
+       assert_cmpfloat (-1.0f, GT, -2.0f);
+       assert_cmpfloat (-1.0f, NE, -2.0f);
+       assert_cmpfloat (-1.0f, GE, -2.0f);
+       assert_cmpfloat (-2.0f, GE, -2.0f);
+       assert_cmpfloat (-2.0f, EQ, -2.0f);
+       assert_cmpfloat (-2.0f, LE, -2.0f);
+       assert_cmpfloat (-3.0f, LE, -2.0f);
+       assert_cmpfloat (-3.0f, LT, -2.0f);
+
+       assert_cmpfloat (-100.0f, LT, 101.0f);
+       assert_cmpfloat (-100.0f, NE, 101.0f);
+       assert_cmpfloat (-100.0f, LE, 101.0f);
+       assert_cmpfloat (-101.0f, LE, 101.0f);
+       assert_cmpfloat (101.0f, GE, -101.0f);
+       assert_cmpfloat (102.0f, GE, -101.0f);
+       assert_cmpfloat (102.0f, GT, -101.0f);
+}
+
+void main () {
+       test_assert_cmpstr ();
+       test_assert_cmpint ();
+       test_assert_cmpuint ();
+       test_assert_cmphex ();
+       test_assert_cmpfloat ();
+}
index c612cc36ed34b43be715d75e31a447245263155b..4a1c957847370e297ae00e19a7e9b499ca57e692 100644 (file)
@@ -2656,6 +2656,40 @@ namespace GLib {
        [NoReturn]
        public static void assert_not_reached ();
 
+       /**
+        * Comparison operators for use with GLib.assert_cmp*() functions
+        */
+       [CCode (has_type_id = false)]
+       public enum CompareOperator {
+               [CCode (cname = "==")]
+               EQ,
+               [CCode (cname = "!=")]
+               NE,
+               [CCode (cname = ">=")]
+               GE,
+               [CCode (cname = "<=")]
+               LE,
+               [CCode (cname = ">")]
+               GT,
+               [CCode (cname = "<")]
+               LT,
+       }
+
+       [Version (since = "2.16")]
+       public static void assert_cmpstr (string? s1, CompareOperator cmp, string? s2);
+       [Version (since = "2.16")]
+       public static void assert_cmpint (int n1, CompareOperator cmp, int n2);
+       [Version (since = "2.16")]
+       public static void assert_cmpuint (uint n1, CompareOperator cmp, uint n2);
+       [Version (since = "2.16")]
+       public static void assert_cmphex (uint n1, CompareOperator cmp, uint n2);
+       [Version (since = "2.16")]
+       public static void assert_cmpfloat (double n1, CompareOperator cmp, double n2);
+       [Version (since = "2.58")]
+       public static void assert_cmpfloat_with_epsilon (double n1, double n2, double epsilon);
+       [Version (since = "2.60")]
+       public static void assert_cmpvariant (Variant v1, Variant v2);
+
        public static void on_error_query (string? prg_name = null);
        public static void on_error_stack_trace (string? prg_name = null);
        [CCode (cname = "G_BREAKPOINT")]