FreeRDP
TestPodArrays.c
1 
19 #include <freerdp/utils/pod_arrays.h>
20 
21 // NOLINTNEXTLINE(readability-non-const-parameter)
22 static BOOL cb_compute_sum(UINT32* v, void* target)
23 {
24  UINT32* ret = (UINT32*)target;
25  *ret += *v;
26  return TRUE;
27 }
28 
29 static BOOL cb_stop_at_5(UINT32* v, void* target)
30 {
31  UINT32* ret = (UINT32*)target;
32  *ret += 1;
33  return (*ret != 5);
34 }
35 
36 static BOOL cb_set_to_1(UINT32* v, void* target)
37 {
38  *v = 1;
39  return TRUE;
40 }
41 
42 static BOOL cb_reset_after_1(UINT32* v, void* target)
43 {
44  ArrayUINT32* a = (ArrayUINT32*)target;
45  array_uint32_reset(a);
46  return TRUE;
47 }
48 
49 typedef struct
50 {
51  UINT32 v1;
52  UINT16 v2;
53 } BasicStruct;
54 
55 static BOOL cb_basic_struct(BasicStruct* v, void* target)
56 {
57  return (v->v1 == 1) && (v->v2 == 2);
58 }
59 // NOLINTNEXTLINE(bugprone-suspicious-memory-comparison,cert-exp42-c,cert-flp37-c)
60 POD_ARRAYS_IMPL(BasicStruct, basicstruct)
61 
62 int TestPodArrays(int argc, char* argv[])
63 {
64  int rc = -1;
65  UINT32 sum = 0;
66  UINT32 foreach_index = 0;
67  ArrayUINT32 uint32s = { 0 };
68  UINT32* ptr = NULL;
69  const UINT32* cptr = NULL;
70  ArrayBasicStruct basicStructs = { 0 };
71  BasicStruct basicStruct = { 1, 2 };
72 
73  array_uint32_init(&uint32s);
74  array_basicstruct_init(&basicStructs);
75 
76  for (UINT32 i = 0; i < 10; i++)
77  if (!array_uint32_append(&uint32s, i))
78  goto fail;
79 
80  sum = 0;
81  if (!array_uint32_foreach(&uint32s, cb_compute_sum, &sum))
82  goto fail;
83 
84  if (sum != 45)
85  goto fail;
86 
87  foreach_index = 0;
88  if (array_uint32_foreach(&uint32s, cb_stop_at_5, &foreach_index))
89  goto fail;
90 
91  if (foreach_index != 5)
92  goto fail;
93 
94  if (array_uint32_get(&uint32s, 4) != 4)
95  goto fail;
96 
97  array_uint32_set(&uint32s, 4, 5);
98  if (array_uint32_get(&uint32s, 4) != 5)
99  goto fail;
100 
101  ptr = array_uint32_data(&uint32s);
102  if (*ptr != 0)
103  goto fail;
104 
105  cptr = array_uint32_cdata(&uint32s);
106  if (*cptr != 0)
107  goto fail;
108 
109  /* test modifying values of the array during the foreach */
110  if (!array_uint32_foreach(&uint32s, cb_set_to_1, NULL) || array_uint32_get(&uint32s, 5) != 1)
111  goto fail;
112 
113  /* this one is to test that we can modify the array itself during the foreach and that things
114  * go nicely */
115  if (!array_uint32_foreach(&uint32s, cb_reset_after_1, &uint32s) || array_uint32_size(&uint32s))
116  goto fail;
117 
118  /* give a try with an array of BasicStructs */
119  if (!array_basicstruct_append(&basicStructs, basicStruct))
120  goto fail;
121 
122  if (!array_basicstruct_foreach(&basicStructs, cb_basic_struct, NULL))
123  goto fail;
124 
125  rc = 0;
126 
127 fail:
128  array_uint32_uninit(&uint32s);
129  array_basicstruct_uninit(&basicStructs);
130 
131  return rc;
132 }