summaryrefslogtreecommitdiff
path: root/tests/support/test.h
blob: 5de386418c905eea4993ef15f76fcb29055a488b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef TINY_CONVMEM_TEST_SUPPORT_H
#define TINY_CONVMEM_TEST_SUPPORT_H

#include <stdio.h>
#include <string.h>

typedef struct tiny_test_state
{
  const char *case_name;
  unsigned checks;
  unsigned failures;
} tiny_test_state_t;

#define TINY_TEST_CASE(state, name) ((state)->case_name = (name))
#define TINY_EXPECT(state, expression)                                                             \
  do                                                                                               \
    {                                                                                              \
      (state)->checks++;                                                                           \
      if (!(expression))                                                                           \
        {                                                                                          \
          fprintf(stderr,                                                                          \
                  "%s:%d [%s] expected %s\n",                                                      \
                  __FILE__,                                                                        \
                  __LINE__,                                                                        \
                  (state)->case_name,                                                              \
                  #expression);                                                                    \
          (state)->failures++;                                                                     \
        }                                                                                          \
    }                                                                                              \
  while (0)
#define TINY_EXPECT_EQ_U64(state, expected, actual)                                                \
  TINY_EXPECT((state), (unsigned long long)(expected) == (unsigned long long)(actual))
#define TINY_EXPECT_BYTES(state, expected, actual, length)                                         \
  TINY_EXPECT((state), memcmp((expected), (actual), (length)) == 0)

static inline int
tiny_test_finish(const tiny_test_state_t *state)
{
  if (state->failures == 0)
    {
      printf("%u checks passed\n", state->checks);
      return 0;
    }
  fprintf(stderr, "%u of %u checks failed\n", state->failures, state->checks);
  return 1;
}

#endif