File: dynamic_array-t.c

package info (click to toggle)
mariadb 1%3A11.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 772,520 kB
  • sloc: ansic: 2,414,714; cpp: 1,791,394; asm: 381,336; perl: 62,905; sh: 49,647; pascal: 40,897; java: 39,363; python: 20,791; yacc: 20,432; sql: 17,907; xml: 12,344; ruby: 8,544; cs: 6,542; makefile: 6,145; ada: 1,879; lex: 1,193; javascript: 996; objc: 80; tcl: 73; awk: 46; php: 22
file content (246 lines) | stat: -rw-r--r-- 7,306 bytes parent folder | download | duplicates (2)
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
241
242
243
244
245
246
/* Copyright (c) 2024 Eric Herman and MariaDB Foundation.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */

#include <my_global.h>
#include <my_sys.h>
#include <tap.h>

struct thing {
  unsigned id;
  char name[40];
};

#define THING_STACK_BUF_SIZE 3
#define ROUND_TRIP_NO_GROW_SIZE (THING_STACK_BUF_SIZE - 1)
#define ROUND_TRIP_NEED_GROW_SIZE ((2 * THING_STACK_BUF_SIZE) + 1)

int dyn_array_round_trip(size_t grow_to_size)
{
  struct thing things_buf[THING_STACK_BUF_SIZE];

  DYNAMIC_ARRAY things_array;
  PSI_memory_key psi_key= PSI_NOT_INSTRUMENTED;
  size_t element_size= sizeof(struct thing);
  void *init_buffer= (void *)&things_buf;
  size_t init_alloc= THING_STACK_BUF_SIZE;
  size_t alloc_increment= 2;
  myf my_flags= MYF(MY_WME);
  my_bool err= FALSE;

  ok(1, "THING_STACK_BUF_SIZE: %d, grow_to_size: %zu", THING_STACK_BUF_SIZE,
     grow_to_size);

  err= my_init_dynamic_array2(psi_key, &things_array, element_size, init_buffer,
                              init_alloc, alloc_increment, my_flags);

  ok(!err, "my_init_dynamic_array2");
  if (err) {
    return EXIT_FAILURE;
  }

  for (size_t i= 0; i < grow_to_size; ++i) {
    struct thing tmp_thing;

    tmp_thing.id= (unsigned)i;
    snprintf(tmp_thing.name, sizeof(tmp_thing.name), "thing %zu", i);

    err= insert_dynamic(&things_array, &tmp_thing);
    ok(!err, "insert_dynamic for %zu", i);
    if (err) {
      delete_dynamic(&things_array);
      return EXIT_FAILURE;
    }
  }
  ok(grow_to_size == things_array.elements, "size expect: %zu, actual: %zu",
     grow_to_size, things_array.elements);

  for (size_t i= 0; i < things_array.elements; ++i) {
    struct thing retrieved, expected;

    expected.id= (unsigned)i;
    snprintf(expected.name, sizeof(expected.name), "thing %zu", i);

    memset(&retrieved, 0x00, sizeof(struct thing));
    get_dynamic(&things_array, &retrieved, i);

    ok(retrieved.id == expected.id, "%zu: retrieved id: %u, expected id: %u",
       i, retrieved.id, expected.id);
    ok(strncmp(retrieved.name, expected.name, sizeof(expected.name)) == 0,
       "%zu: retrieved name: '%s', expected name: '%s'",
       i, retrieved.name, expected.name);
  }

  delete_dynamic(&things_array);

  return EXIT_SUCCESS;
}
static const int plan_round_trip_no_grow= 3 + (3 * ROUND_TRIP_NO_GROW_SIZE);
static const int plan_round_trip_grow= 3 + (3 * ROUND_TRIP_NEED_GROW_SIZE);

int dyn_array_push_pop(void)
{
  DYNAMIC_ARRAY things_array;
  PSI_memory_key psi_key= PSI_NOT_INSTRUMENTED;
  size_t element_size= sizeof(struct thing);
  size_t init_alloc= THING_STACK_BUF_SIZE;
  size_t alloc_increment= 2;
  myf my_flags= MYF(MY_WME);
  my_bool err= FALSE;
  struct thing tmp_thing, *popped;

  err= my_init_dynamic_array(psi_key, &things_array, element_size, init_alloc,
		             alloc_increment, my_flags);

  ok(!err, "my_init_dynamic_array");
  if (err) {
    return EXIT_FAILURE;
  }

  tmp_thing.id= 0;
  snprintf(tmp_thing.name, sizeof(tmp_thing.name), "thing 0");
  err= push_dynamic(&things_array, &tmp_thing);
  ok(!err, "push_dynamic for 0");
  if (err) {
    delete_dynamic(&things_array);
    return EXIT_FAILURE;
  }

  tmp_thing.id= 1;
  snprintf(tmp_thing.name, sizeof(tmp_thing.name), "thing 1");
  err= push_dynamic(&things_array, &tmp_thing);
  ok(!err, "push_dynamic for 1");
  if (err) {
    delete_dynamic(&things_array);
    return EXIT_FAILURE;
  }

  ok(2 == things_array.elements, "size expect: %d, actual: %zu",
     2, things_array.elements);

  popped= (struct thing *)pop_dynamic(&things_array);
  ok(popped != NULL, "pop_dynamic 1");
  if (!popped) {
    delete_dynamic(&things_array);
    return EXIT_FAILURE;
  }
  ok(popped->id == 1, "pop expect 1, popped->id: %u", popped->id);
  ok(1 == things_array.elements, "size expect: %d, actual: %zu",
     1, things_array.elements);

  popped= (struct thing *)pop_dynamic(&things_array);
  ok(popped != NULL, "pop_dynamic 0");
  if (!popped) {
    delete_dynamic(&things_array);
    return EXIT_FAILURE;
  }
  ok(popped->id == 0, "pop expect 0, popped->id: %u", popped->id);
  ok(0 == things_array.elements, "size expect: %d, actual: %zu",
     0, things_array.elements);


  popped= (struct thing *)pop_dynamic(&things_array);
  ok(!popped, "pop %p from empty array", popped);

  delete_dynamic(&things_array);

  return EXIT_SUCCESS;
}
static const int plan_push_pop= 11;

struct string_thing {
  unsigned id;
  char *str;
  size_t str_size;
};

static void free_string_thing(void *p)
{
  struct string_thing *thing= (struct string_thing *)p;
  my_free(thing->str);
}

#define NUM_DELETE_WITH_CALLBACK 3

int dyn_array_delete_with_callback(void)
{
  DYNAMIC_ARRAY things_array;
  PSI_memory_key psi_key= PSI_NOT_INSTRUMENTED;
  size_t element_size= sizeof(struct string_thing);
  size_t init_alloc= (NUM_DELETE_WITH_CALLBACK - 1);
  size_t alloc_increment= 2;
  myf my_flags= MYF(MY_WME);
  my_bool err= FALSE;

  err= my_init_dynamic_array(psi_key, &things_array, element_size, init_alloc,
                             alloc_increment, my_flags);

  ok(!err, "my_init_dynamic_array");
  if (err) {
    return EXIT_FAILURE;
  }

  for (size_t i= 0; i < NUM_DELETE_WITH_CALLBACK; ++i) {
    struct string_thing thing= { (unsigned)i, NULL, 40 };
    thing.str= my_malloc(psi_key, thing.str_size, my_flags);
    ok(thing.str != NULL, "%zu thing.str= malloc(%zu))", i, thing.str_size);
    if (!thing.str) {
      delete_dynamic_with_callback(&things_array, free_string_thing);
      return EXIT_FAILURE;
    }
    snprintf(thing.str, thing.str_size, "thing %zu", i);

    err= insert_dynamic(&things_array, &thing);
    ok(!err, "insert_dynamic for %zu", i);
    if (err) {
      delete_dynamic_with_callback(&things_array, free_string_thing);
      return EXIT_FAILURE;
    }
  }
  ok(3 == things_array.elements, "size expect: %d, actual: %zu",
     3, things_array.elements);

  delete_dynamic_with_callback(&things_array, free_string_thing);

  ok(0 == things_array.elements, "size expect: %d, actual: %zu",
     0, things_array.elements);

  return EXIT_SUCCESS;
}
static const int plan_delete_with_callback= 3 + (2 * NUM_DELETE_WITH_CALLBACK);

int main(void)
{
  int err= 0;

  plan(plan_round_trip_no_grow + 1
     + plan_round_trip_grow + 1
     + plan_push_pop + 1
     + plan_delete_with_callback + 1
  );

  err= dyn_array_round_trip(ROUND_TRIP_NO_GROW_SIZE);
  ok(!err, "dyn_array_round_trip (no need to grow)");

  err= dyn_array_round_trip(ROUND_TRIP_NEED_GROW_SIZE);
  ok(!err, "dyn_array_round_trip (need to grow)");

  err= dyn_array_push_pop();
  ok(!err, "dyn_array_push_pop");

  err= dyn_array_delete_with_callback();
  ok(!err, "dyn_array_delete_with_callback");

  return exit_status();
}