summaryrefslogtreecommitdiffstats
path: root/libfreerdp/utils/test/TestPodArrays.c
blob: 6c88b538c0bec009533d5b22153c4a5936aa16bc (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
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * TestPodArrays
 *
 * Copyright 2022 David Fort <contact@hardening-consulting.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <freerdp/utils/pod_arrays.h>

static BOOL cb_compute_sum(UINT32* v, void* target)
{
	UINT32* ret = (UINT32*)target;
	*ret += *v;
	return TRUE;
}

static BOOL cb_stop_at_5(UINT32* v, void* target)
{
	UINT32* ret = (UINT32*)target;
	*ret += 1;
	return (*ret != 5);
}

static BOOL cb_set_to_1(UINT32* v, void* target)
{
	*v = 1;
	return TRUE;
}

static BOOL cb_reset_after_1(UINT32* v, void* target)
{
	ArrayUINT32* a = (ArrayUINT32*)target;
	array_uint32_reset(a);
	return TRUE;
}

typedef struct
{
	UINT32 v1;
	UINT16 v2;
} BasicStruct;

static BOOL cb_basic_struct(BasicStruct* v, void* target)
{
	return (v->v1 == 1) && (v->v2 == 2);
}

POD_ARRAYS_IMPL(BasicStruct, basicstruct)

int TestPodArrays(int argc, char* argv[])
{
	int rc = -1;
	UINT32 sum = 0;
	UINT32 foreach_index = 0;
	ArrayUINT32 uint32s = { 0 };
	UINT32* ptr = NULL;
	const UINT32* cptr = NULL;
	ArrayBasicStruct basicStructs = { 0 };
	BasicStruct basicStruct = { 1, 2 };

	array_uint32_init(&uint32s);
	array_basicstruct_init(&basicStructs);

	for (UINT32 i = 0; i < 10; i++)
		if (!array_uint32_append(&uint32s, i))
			goto fail;

	sum = 0;
	if (!array_uint32_foreach(&uint32s, cb_compute_sum, &sum))
		goto fail;

	if (sum != 45)
		goto fail;

	foreach_index = 0;
	if (array_uint32_foreach(&uint32s, cb_stop_at_5, &foreach_index))
		goto fail;

	if (foreach_index != 5)
		goto fail;

	if (array_uint32_get(&uint32s, 4) != 4)
		goto fail;

	array_uint32_set(&uint32s, 4, 5);
	if (array_uint32_get(&uint32s, 4) != 5)
		goto fail;

	ptr = array_uint32_data(&uint32s);
	if (*ptr != 0)
		goto fail;

	cptr = array_uint32_cdata(&uint32s);
	if (*cptr != 0)
		goto fail;

	/* test modifying values of the array during the foreach */
	if (!array_uint32_foreach(&uint32s, cb_set_to_1, NULL) || array_uint32_get(&uint32s, 5) != 1)
		goto fail;

	/* this one is to test that we can modify the array itself during the foreach and that things
	 * go nicely */
	if (!array_uint32_foreach(&uint32s, cb_reset_after_1, &uint32s) || array_uint32_size(&uint32s))
		goto fail;

	/* give a try with an array of BasicStructs */
	if (!array_basicstruct_append(&basicStructs, basicStruct))
		goto fail;

	if (!array_basicstruct_foreach(&basicStructs, cb_basic_struct, NULL))
		goto fail;

	rc = 0;

fail:
	array_uint32_uninit(&uint32s);
	array_basicstruct_uninit(&basicStructs);

	return rc;
}