Skip to content

[Portuguese] 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);

Qualquer um destes macros irá falhar o teste se a afirmação (assertion) for falsa. No METRIC_ASSERT_ARRAY, o size é o comprimento da array em bytes. Você pode usá-lo assim:

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("");
}

Por causa da maneira como o macro funciona, você pode usá-lo para verificar arrays multidimensionais também. Como no código abaixo:

Na verdade o macro usa memcmp() para comparar dois dados na memória. Você pode usá-lo em qualquer tipo de dado, como em uma 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;
}

Saída:

<TEST> tst.c:25: test_2d_array
[ OK ]