diff options
author | Christopher White <cwhite@d3engineering.com> | 2020-11-28 12:47:41 -0500 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2020-12-01 14:38:28 +0100 |
commit | 9a9a20a9a747be2e8d63ee2d88f5756b398af3d8 (patch) | |
tree | 3db06b4ab90dada64af222902214db86e8011e7b | |
parent | b05dcfbfac23c22ff96640dd4d1f4a777c5301a9 (diff) | |
download | vala-wip/issue/395.tar.gz |
glib-2.0: Bind assert_cmp* functionswip/issue/395
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
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | tests/basic-types/gassert.vala | 108 | ||||
-rw-r--r-- | vapi/glib-2.0.vapi | 34 |
3 files changed, 143 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 010772c3c..b620a847f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 000000000..05ea9055f --- /dev/null +++ b/tests/basic-types/gassert.vala @@ -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 (); +} diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi index c612cc36e..4a1c95784 100644 --- a/vapi/glib-2.0.vapi +++ b/vapi/glib-2.0.vapi @@ -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")] |