summaryrefslogtreecommitdiff
path: root/source/tests/array_ref.test.c
blob: 921e9fda6e05bf6b4e5a70cceb8728196b279212 (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
49
50
51
52
53
54
55
56
57
#include "../kit/array_ref.h"

#define KIT_TEST_FILE array_ref
#include "../kit/test.h"

TEST("array ref wrap") {
  int foo[] = { 1, 2, 3 };
  AR_WRAP(ref, int, foo);

  REQUIRE(ref.size == 3);
  REQUIRE(ref.values[0] == 1);
  REQUIRE(ref.values[1] == 2);
  REQUIRE(ref.values[2] == 3);

  ref.values[1] = 42;
  REQUIRE(ref.values[1] == 42);
}

TEST("array ref equal") {
  int foo[] = { 1, 2, 3, 4, 5, 6, 7 };
  int bar[] = { 3, 4, 5 };

  AR(int) foo_ref = { .size = 3, .values = foo + 2 };
  AR(int) bar_ref = { .size = 3, .values = bar };

  REQUIRE(AR_EQUAL(foo_ref, bar_ref));
}

static int compare(int *left, int *right) {
  return *left - *right;
}

TEST("array ref compare") {
  int foo[] = { 1, 2, 3, 5 };
  int bar[] = { 1, 2, 4, 5 };

  AR(int) foo_ref = { .size = 3, .values = foo };
  AR(int) bar_ref = { .size = 3, .values = bar };

  REQUIRE(AR_COMPARE(foo_ref, bar_ref, compare) < 0);
  REQUIRE(AR_COMPARE(bar_ref, foo_ref, compare) > 0);
  REQUIRE(AR_COMPARE(foo_ref, foo_ref, compare) == 0);
}

TEST("array ref different element sizes") {
  int  foo[] = { 1, 2, 3 };
  char bar[] = { 1, 2, 3 };

  AR(int) foo_ref  = { .size = 3, .values = foo };
  AR(char) bar_ref = { .size = 3, .values = bar };

  REQUIRE(!AR_EQUAL(foo_ref, bar_ref));
  REQUIRE(AR_COMPARE(foo_ref, bar_ref, compare) > 0);
  REQUIRE(AR_COMPARE(bar_ref, foo_ref, compare) < 0);
}

#undef KIT_TEST_FILE