Skip to content

[English] METRIC_ASSERT

Luiz Felipe edited this page Apr 8, 2020 · 1 revision
METRIC_ASSERT(expression);
METRIC_ASSERT_ARRAY(array1, array2, size);
METRIC_ASSERT_STRING(string1, string2);

Any of these macros will fail the test if the assertion is false. In the METRIC_ASSERT_ARRAY, the size is the length of the array in bytes. You can use it like this:

test_t test_arrays(void)
{
  int arr1[] = {4, 9, 1};
  int arr2[] = {4, 3, 1};

  METRIC_ASSERT_ARRAY(arr1, arr2, sizeof arr1);

  METRIC_TEST_OK("");
}

Because of the design of the macro, you can use it to check multidimensional arrays too. Like in the code below:

In fact it uses memcmp() to compare two memory data. You can use it in any type of content, like in a struct.

#include <stdio.h>
#include "metric.h"

test_t test_2d_array(void)
{
  int arr1[][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
  };

  int arr2[][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
  };

  METRIC_ASSERT_ARRAY(arr1, arr2, sizeof arr1);

  METRIC_TEST_OK("");
}

int main(void)
{
  METRIC_TEST(test_2d_array);
  return 0;
}

Output:

<TEST> tst.c:25: test_2d_array
[ OK ]
Clone this wiki locally