1

I'm writing a unit test with GLib's testing framework where I want to check if two pointers point to the same place in memory. I do NOT want to compare the actual memory which they point to, so g_assert_cmpmem () would not be a solution.

My initial thinking was that pointers are integers, so I would just use g_assert_cmpint (). However, this generates errors as you cannot make integers from pointers without a cast:

In file included from /usr/include/glib-2.0/glib.h:89,
                 from ../src/pmos-tweaks/ms-tweaks-parser.h:11,
                 from ../src/pmos-tweaks/ms-tweaks-parser.c:11,
                 from ../tests/test-tweaks-parser.c:9:
../tests/test-tweaks-parser.c: In function ‘test_copy_setting’:
/usr/include/glib-2.0/glib/gtestutils.h:79:61: error: initialization of ‘guint64’ {aka ‘long unsigned int’} from ‘MsTweaksSetting *’ makes integer from pointer without a cast [-Wint-conversion]
   79 |                                              guint64 __n1 = (n1), __n2 = (n2); \
      |                                                             ^
../tests/test-tweaks-parser.c:53:3: note: in expansion of macro ‘g_assert_cmpuint’
   53 |   g_assert_cmpuint (setting, ==, setting_copied);
      |   ^~~~~~~~~~~~~~~~
/usr/include/glib-2.0/glib/gtestutils.h:79:74: error: initialization of ‘guint64’ {aka ‘long unsigned int’} from ‘MsTweaksSetting *’ makes integer from pointer without a cast [-Wint-conversion]
   79 |                                              guint64 __n1 = (n1), __n2 = (n2); \
      |                                                                          ^
../tests/test-tweaks-parser.c:53:3: note: in expansion of macro ‘g_assert_cmpuint’
   53 |   g_assert_cmpuint (setting, ==, setting_copied);
      |   ^~~~~~~~~~~~~~~~

Is the right solution here to just do a cast, or is there a better option?

1
  • Instead of just closing the question, maybe you could leave a comment explaining what's wrong with it? Commented Jul 15 at 15:51

2 Answers 2

3

There is no pointer equality assertion in GLib, so you should use g_assert_true():

g_assert_true (setting == setting_copied)

Note that you should not use g_assert(), as it will be compiled out if GLib is configured with G_DISABLE_ASSERT, and some distributions configure it this way.

Sign up to request clarification or add additional context in comments.

Comments

1

The documentation for g_assert_cmpmem says it is equivalent to g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)

So you might want to use just g_assert_true(l1 == l2)

3 Comments

Please link the documentation
Thel1 and l2 arguments to g_assert_cmpmem are lengths. Asserting they are equal will not accomplish what OP asks for, asserting two pointers are equal. Did you intend to apply the comparison to pointers, not lengths?
Maybe g_assert_true(m1 == m2)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.