summaryrefslogtreecommitdiffstats
path: root/regressions/ck_array/validate/serial.c
blob: b6d7b562a8c3cf086ea712b21eb2f0d5a16c3520 (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
#include <ck_array.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

#include "../../common.h"

#ifndef ITERATION
#define ITERATION 128
#endif

static void
my_free(void *p, size_t m, bool d)
{

	(void)m;
	(void)d;

	free(p);
	return;
}

static void *
my_malloc(size_t b)
{

	return malloc(b);
}

static void *
my_realloc(void *r, size_t a, size_t b, bool d)
{

	(void)a;
	(void)d;

	return realloc(r, b);
}

int
main(void)
{
	void *r;
	uintptr_t i;
	ck_array_t array;
	ck_array_iterator_t iterator;
	struct ck_malloc m = {
		.malloc = my_malloc,
		.free = NULL,
		.realloc = my_realloc
	};

	if (ck_array_init(&array, CK_ARRAY_MODE_SPMC, &m, 4) == true)
		ck_error("ck_array_init with NULL free succeeded\n");

	m.free = my_free;
	if (ck_array_init(&array, CK_ARRAY_MODE_SPMC, &m, 4) == false)
		ck_error("ck_array_init\n");

	for (i = 0; i < ITERATION; i++) {
		if (ck_array_put(&array, (void *)i) == false)
			ck_error("ck_error_put\n");

		if (ck_array_remove(&array, (void *)i) == false)
			ck_error("ck_error_remove after put\n");
	}

	i = 0; CK_ARRAY_FOREACH(&array, &iterator, &r) i++;
	if (i != 0)
		ck_error("Non-empty array after put -> remove workload.\n");

	ck_array_commit(&array);

	i = 0; CK_ARRAY_FOREACH(&array, &iterator, &r) i++;
	if (i != 0)
		ck_error("Non-empty array after put -> remove -> commit workload.\n");

	for (i = 0; i < ITERATION; i++) {
		if (ck_array_put(&array, (void *)i) == false)
			ck_error("ck_error_put\n");
	}

	i = 0; CK_ARRAY_FOREACH(&array, &iterator, &r) i++;
	if (i != 0)
		ck_error("Non-empty array after put workload.\n");

	for (i = 0; i < ITERATION; i++) {
		if (ck_array_remove(&array, (void *)i) == false)
			ck_error("ck_error_remove after put\n");
	}

	i = 0; CK_ARRAY_FOREACH(&array, &iterator, &r) i++;
	if (i != 0)
		ck_error("Non-empty array after put -> remove workload.\n");

	ck_array_commit(&array);

	i = 0; CK_ARRAY_FOREACH(&array, &iterator, &r) i++;
	if (i != 0)
		ck_error("Non-empty array after put -> remove -> commit workload.\n");

	for (i = 0; i < ITERATION; i++) {
		if (ck_array_put(&array, (void *)i) == false)
			ck_error("ck_error_put\n");
	}

	ck_array_commit(&array);

	i = 0;
	CK_ARRAY_FOREACH(&array, &iterator, &r) {
		i++;
	}

	if (i != ITERATION)
		ck_error("Incorrect item count in iteration\n");

	ck_array_remove(&array, (void *)(uintptr_t)0);
	ck_array_remove(&array, (void *)(uintptr_t)1);
	ck_array_commit(&array);
	i = 0; CK_ARRAY_FOREACH(&array, &iterator, &r) i++;
	if (i != ITERATION - 2 || ck_array_length(&array) != ITERATION - 2)
		ck_error("Incorrect item count in iteration after remove\n");

	if (ck_array_put_unique(&array, (void *)UINTPTR_MAX) != 0)
		ck_error("Unique value put failed.\n");

	if (ck_array_put_unique(&array, (void *)(uintptr_t)4) != 1)
		ck_error("put of 4 not detected as non-unique.\n");

	if (ck_array_put_unique(&array, (void *)UINTPTR_MAX) != 1)
		ck_error("put of UINTPTR_MAX not detected as non-unique.\n");

	ck_array_commit(&array);
	i = 0;
	CK_ARRAY_FOREACH(&array, &iterator, &r) {
		i++;
	}
	if (i != ITERATION - 1 || ck_array_length(&array) != ITERATION - 1)
		ck_error("Incorrect item count in iteration after unique put\n");

	if (ck_array_initialized(&array) == false)
		ck_error("Error, expected array to be initialized.\n");

	for (i = 0; i < ITERATION * 4; i++) {
		ck_array_remove(&array, (void *)i);
	}

	for (i = 0; i < ITERATION * 16; i++) {
		ck_array_put(&array, (void *)i);
	}

	ck_array_commit(&array);

	for (i = 0; i < ITERATION * 128; i++) {
		ck_array_put(&array, (void *)i);
		if (ck_array_put_unique(&array, (void *)i) != 1)
			ck_error("put_unique for non-unique value should fail.\n");
	}

	for (i = 0; i < ITERATION * 64; i++) {
		bool f = ck_array_remove(&array, (void *)i);

		if (f == false && i < ITERATION * 144)
			ck_error("Remove failed for existing entry.\n");

		if (f == true && i > ITERATION * 144)
			ck_error("Remove succeeded for non-existing entry.\n");
	}

	ck_array_commit(&array);
	ck_array_deinit(&array, false);

	if (ck_array_initialized(&array) == true)
		ck_error("Error, expected array to be uninitialized.\n");

	return 0;
}