summaryrefslogtreecommitdiffstats
path: root/nsock/tests/ghlists.c
blob: f445022239cb24c7f6999b82825d8a35961bc1a7 (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
/*
 * Nsock regression test suite
 * Same license as nmap -- see https://nmap.org/book/man-legal.html
 */

#include "test-common.h"
/* Additional checks enabled */
#define GH_LIST_PARANOID 1
#include "../src/gh_list.h"
/* For container_of */
#include "../src/gh_heap.h"
#include <stdint.h>
#include <time.h>


#define LIST_COUNT  16
#define ELT_COUNT   2000


struct testlist {
  unsigned int val;
  gh_lnode_t lnode;
};

static unsigned int nodeval(gh_lnode_t *lnode) {
  struct testlist *tl;

  tl = container_of(lnode, struct testlist, lnode);
  return tl->val;
}

static gh_lnode_t *mknode(unsigned int val) {
  struct testlist *tl;

  tl = calloc(1, sizeof(struct testlist));
  tl->val = val;
  return &tl->lnode;
}

static void delnode(gh_lnode_t *lnode) {
  if (lnode)
    free(container_of(lnode, struct testlist, lnode));
}

static int ghlist_stress(void *tdata) {
  gh_list_t lists[LIST_COUNT];
  gh_lnode_t *current, *next;
  int num = 0;
  int ret;
  int i;

  for (i = 0; i < LIST_COUNT; i++)
    gh_list_init(&lists[i]);

  for (num = ELT_COUNT/2; num < ELT_COUNT; num++) {
    for (i = 0; i < LIST_COUNT; i++) {
      gh_list_append(&lists[i], mknode(num));
    }
  }

  for (num = (ELT_COUNT/2 - 1); num >= 0; num--) {
    for (i = 0; i < LIST_COUNT; i++) {
      gh_list_prepend(&lists[i], mknode(num));
    }
  }

  for (num = 0; num < ELT_COUNT; num++) {
    for (i = 0; i < LIST_COUNT; i++) {
      current = gh_list_pop(&lists[i]);
      ret = nodeval(current);
      if (ret != num) {
	fprintf(stderr, "prepend_test: Bogus return value %d when expected %d\n",
	                ret, num);
	return -EINVAL;
      }
      delnode(current);
    }
  }
  for (i = 0; i < LIST_COUNT; i++) {
    current = gh_list_pop(&lists[i]);
    if (current) {
      fprintf(stderr, "Ret is bogus for list %d", i);
      return -EINVAL;
    }
  }

  for (num = (ELT_COUNT/2 - 1); num >= 0; num--) {
    for (i = 0; i < LIST_COUNT; i++) {
      gh_list_prepend(&lists[i], mknode(num));
    }
  }

  for (num = ELT_COUNT/2; num < ELT_COUNT; num++) {
    for (i = 0; i < LIST_COUNT; i++) {
      gh_list_append(&lists[i], mknode(num));
    }
  }

  for (num = 0; num < ELT_COUNT; num++) {
    for (i=0; i < LIST_COUNT; i++) {
      current = gh_list_pop(&lists[i]);
      ret = nodeval(current);
      if (ret != num) {
	fprintf(stderr, "prepend_test: Bogus return value %d when expected %d\n",
	        ret, num);
        return -EINVAL;
      }
      delnode(current);
    }
  }

  for (num = ELT_COUNT/2; num < ELT_COUNT; num++) {
    for (i = 0; i < LIST_COUNT; i++)
      gh_list_append(&lists[i], mknode(num));
  }

  for (num = ELT_COUNT/2 - 1; num >= 0; num--) {
    for (i = 0; i < LIST_COUNT; i++)
      gh_list_prepend(&lists[i], mknode(num));
  }

  for (num = 0; num < ELT_COUNT; num++) {
    for (i = 0; i < LIST_COUNT; i++) {
      current = gh_list_pop(&lists[i]);
      ret = nodeval(current);
      if (ret != num) {
	fprintf(stderr, "prepend_test: Bogus return value %d when expected %d\n",
	        ret, num);
	return -EINVAL;
      }
      delnode(current);
    }
  }

  for (num = ELT_COUNT/2 - 1; num >= 0; num--) {
    for (i = 0; i < LIST_COUNT; i++)
      gh_list_prepend(&lists[i], mknode(num));
  }

  for (num = ELT_COUNT/2; num < ELT_COUNT; num++) {
    for (i=0; i < LIST_COUNT; i++)
      gh_list_append(&lists[i], mknode(num));
  }

  for (i = 0; i < LIST_COUNT; i++) {
    num = 0;

    for (current = gh_list_first_elem(&lists[i]); current; current = next) {
      int k;

      next = gh_lnode_next(current);
      k = nodeval(current);
      if (k != num) {
	fprintf(stderr, "Got %d when I expected %d\n", k, num);
        return -EINVAL;
      }
      gh_list_remove(&lists[i], current);
      delnode(current);
      num++;
    }
    if (num != ELT_COUNT) {
      fprintf(stderr, "Number is %d, even though %d was expected", num, ELT_COUNT);
      return -EINVAL;
    }

    if (gh_list_count(&lists[i]) != 0) {
      fprintf(stderr, "List should be empty, but instead it has %d members!\n",
              gh_list_count(&lists[i]));
      return -EINVAL;
    }
  }

  for (i = 0; i < LIST_COUNT; i++) {
    while ((current = gh_list_pop(&lists[i])) != NULL)
      delnode(current);

    gh_list_free(&lists[i]);
  }

  return 0;
}

const struct test_case TestGHLists = {
  .t_name     = "test nsock internal ghlists",
  .t_setup    = NULL,
  .t_run      = ghlist_stress,
  .t_teardown = NULL
};