summaryrefslogtreecommitdiff
path: root/src/test/check_list.c
blob: fe23b2b95d951d343dcd195d9ad449ba9b5d54dd (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>
#include <stdlib.h>
#include "../list.h"

static void
print_int(void *p)
{
  printf("%d\n", *((int *)p));
}

int
main(U_ATTR int argc, U_ATTR char **argv)
{
  List *ls = new_list();

  if (!ls) {
    eprintf("%s:%d, failed to create a new list\n",
            __FILE__, __LINE__);
    exit(1);
  }

  unsigned char result = 0;

  int x[] = { 1, 2, 3, 4, 1 };

  for (size_t i = 0; i < sizeof(x) / sizeof(*x); i++) {
    if (add_to_list(ls, x+i)) {
      eprintf("failed to add %ld-th element to LS\n",
              i);
      exit(1);
    }
  }

  print_list(ls, print_int);

  NUM expected_ls_len = sizeof(x) / sizeof(*x);

  if (list_length(ls) != expected_ls_len) {
    eprintf("The length of the list is wrong!\n");
    exit(1);
  }

  eprintf("The length is correct!\n");

  NUM array_len = 0;
  int *array = list_to_array(ls, sizeof(*array), &array_len);

  if (!array) {
    eprintf("%s:%d, failed to convert the list to an array\n",
            __FILE__, __LINE__);
    exit(1);
  }

  if (array_len != expected_ls_len) {
    eprintf("The length of the array is wrong!\n");
    exit(1);
  }

  eprintf("The length of the array is correct!\n");

  for (NUM i = 0; i < expected_ls_len; i++) {
    if (*(array+i) != *(x+i)) {
      eprintf("The %lu-th element of array = %d, x = %d\n",
              i, *(array+i), *(x+i));
      exit(1);
    }
  }

  eprintf("Every element of the array is as expected!\n");

  for (NUM i = 0; i < expected_ls_len; i++) {
    int *pointer = pop_from_list(ls);

    if (!pointer) {
      eprintf("%s:%d, i = %ld, Popping returns NULL\n",
              __FILE__, __LINE__, i);
      exit(1);
    }

    if (*pointer != *(x+expected_ls_len-i-1)) {
      eprintf("i = %ld, *pointer = %d, and x = %d\n",
              i, *pointer, *(x+expected_ls_len-i-1));
      exit(1);
    }
  }

  eprintf("pop_from_list works as expected.\n");

  print_list(ls, print_int);

  expected_ls_len = 5;

  for (NUM i = 0; i < expected_ls_len; i++) {
    int *pointer = malloc(sizeof *pointer * 1);

    if (!pointer) {
      eprintf("%s:%d, i = %ld, failed to malloc\n",
              __FILE__, __LINE__, i);
      exit(1);
    }

    *pointer = expected_ls_len-1-i;
    if (add_to_list(ls, pointer)) {
      eprintf("%s:%d, i = %ld, failed to add element\n",
              __FILE__, __LINE__, i);
      exit(1);
    }
  }

  print_list(ls, print_int);

  destroy_list(ls, 1);

  eprintf("Successfully destroyed the list and freed every element\n");

  free(array);

  ls = new_list();

  if (!ls) {
    eprintf("%s:%d, failed to create a new list\n",
            __FILE__, __LINE__);
    exit(1);
  }

  if (list_assure_size(ls, 513)) {
    eprintf("%s:%d, failed to assure size\n",
            __FILE__, __LINE__);
    exit(1);
  }

  if (list_set_length(ls, 513)) {
    eprintf("%s:%d, failed to set length\n",
            __FILE__, __LINE__);
    exit(1);
  }

  print_list(ls, print_int);

  destroy_list(ls, 2);

  eprintf("Successfully destroyed the list\n");

  if (!(ls = new_list())) {
    eprintf("%s:%d, failed to create a new list\n",
            __FILE__, __LINE__);
    exit(1);
  }

  List *ls2 = new_list();

  if (!ls2) {
    eprintf("%s:%d, failed to create a new list\n",
            __FILE__, __LINE__);
    exit(1);
  }

  eprintf("Successfully created LS2!\n");

  for (NUM i = 0; i < 5; i++) {
    int *pointer = malloc(sizeof *pointer * 1);

    if (!pointer) {
      eprintf("%s:%d, failed to malloc\n",
              __FILE__, __LINE__);
      exit(1);
    }

    *pointer = 4-i;

    if (add_to_list(ls2, pointer)) {
      eprintf("%s:%d, i = %ld, failed to add to list\n",
              __FILE__, __LINE__, i);
      exit(1);
    }
  }

  print_list(ls2, print_int);

  result = copy_list(ls, ls2, copy_num);

  if (result) {
    eprintf("%s:%d, failed to copy list\n",
            __FILE__, __LINE__);
    exit(1);
  }

  destroy_list(ls2, 1);
  destroy_list(ls, 1);

  eprintf("Successfully destroyed lists!\n");

  /* test a list with pointers in an array */

  if (!(ls = new_list())) {
    eprintf("%s:%d, failed to create a new list\n",
            __FILE__, __LINE__);
    exit(1);
  }

  NUM arr[10];

  for (NUM i = 0; i < 10; i++) {
    arr[i] = i*i-i+1;
    if (add_to_list(ls, arr+i)) {
      eprintf("%s:%d, i = %ld, failed to add element\n",
              __FILE__, __LINE__, i);
      exit(1);
    }
  }

  print_list(ls, print_int);

  destroy_list(ls, 0);

  /* test array_to_list */
  int *arr3 = MYALLOC(int, 20);

  void **arr2 = NULL;

  for (NUM i = 0; i < 20; i++) {
    *(arr3+i) = i*i+i+1;
  }

  arr2 = MYALLOC(void *, 20);

  for (NUM i = 0; i < 20; i++) *(arr2+i) = arr3+i;

  ls = array_to_list(arr2, 20);

  printf("Printing a list from an array\n");

  map_list(ls, print_int);

  destroy_list(ls, 2);

  printf("Every test is successful!\n");

  return 0;
}