summaryrefslogtreecommitdiff
path: root/source/tests/move_back.test.c
blob: f3f6dc4ba9a92326ec4e8ba4736ffa48eca20e8c (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "../kit/move_back.h"

#define KIT_TEST_FILE move_back
#include "../kit_test/test.h"

static int is_equal(int x, int y) {
  return x == y;
}

static int is_equal_ref(int *x, int y) {
  return *x == y;
}

static int is_even(int x, int _) {
  return (x % 2) == 0;
}

static int is_even_ref(int *x, int _) {
  return (*x % 2) == 0;
}

TEST("move back val") {
  int v[] = { 1, 2, 2, 2, 1, 1 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK(ref.size, ref, 2, is_equal);

  REQUIRE_EQ(ref.size, 3);
  REQUIRE_EQ(v[0], 1);
  REQUIRE_EQ(v[1], 1);
  REQUIRE_EQ(v[2], 1);
}

TEST("move back ref val") {
  int v[] = { 1, 2, 2, 2, 1, 1 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK_REF(ref.size, ref, 2, is_equal_ref);

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

TEST("move back 1") {
  int v[] = { 1, 2, 3, 4, 5, 6 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK(ref.size, ref, 0, is_even);

  REQUIRE(ref.size == 3);
  REQUIRE(v[0] == 1);
  REQUIRE(v[1] == 5);
  REQUIRE(v[2] == 3);
}

TEST("move back 2") {
  int v[] = { 2, 4, 6, 1, 3, 5 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK(ref.size, ref, 0, is_even);

  REQUIRE(ref.size == 3);
  REQUIRE(v[0] == 5);
  REQUIRE(v[1] == 3);
  REQUIRE(v[2] == 1);
}

TEST("move back 3") {
  int v[] = { 1, 3, 5, 2, 4, 6 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK(ref.size, ref, 0, is_even);

  REQUIRE(ref.size == 3);
  REQUIRE(v[0] == 1);
  REQUIRE(v[1] == 3);
  REQUIRE(v[2] == 5);
}

TEST("move back ref 1") {
  int v[] = { 1, 2, 3, 4, 5, 6 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK_REF(ref.size, ref, 0, is_even_ref);

  REQUIRE(ref.size == 3);
  REQUIRE(v[0] == 1);
  REQUIRE(v[1] == 5);
  REQUIRE(v[2] == 3);
}

TEST("move back ref 2") {
  int v[] = { 2, 4, 6, 1, 3, 5 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK_REF(ref.size, ref, 0, is_even_ref);

  REQUIRE(ref.size == 3);
  REQUIRE(v[0] == 5);
  REQUIRE(v[1] == 3);
  REQUIRE(v[2] == 1);
}

TEST("move back ref 3") {
  int v[] = { 1, 3, 5, 2, 4, 6 };

  struct {
    int  size;
    int *values;
  } ref = { .size = sizeof v / sizeof *v, .values = v };

  MOVE_BACK_REF(ref.size, ref, 0, is_even_ref);

  REQUIRE(ref.size == 3);
  REQUIRE(v[0] == 1);
  REQUIRE(v[1] == 3);
  REQUIRE(v[2] == 5);
}