aboutsummaryrefslogtreecommitdiff
path: root/malloc/tst-dynarray.c
blob: 0c07fc86d3c902e6a5356f25a4b4c2c828d7d505 (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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* Test for dynamic arrays.
   Copyright (C) 2017-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include "tst-dynarray-shared.h"

#include <errno.h>
#include <stdint.h>

#define DYNARRAY_STRUCT dynarray_long
#define DYNARRAY_ELEMENT long
#define DYNARRAY_PREFIX dynarray_long_
#define DYNARRAY_ELEMENT_INIT(e) (*(e) = 17)
#include <malloc/dynarray-skeleton.c>

struct long_array
{
  long *array;
  size_t length;
};

#define DYNARRAY_STRUCT dynarray_long_noscratch
#define DYNARRAY_ELEMENT long
#define DYNARRAY_PREFIX dynarray_long_noscratch_
#define DYNARRAY_ELEMENT_INIT(e) (*(e) = 23)
#define DYNARRAY_FINAL_TYPE struct long_array
#define DYNARRAY_INITIAL_SIZE 0
#include <malloc/dynarray-skeleton.c>

#define DYNARRAY_STRUCT zstr
#define DYNARRAY_ELEMENT char
#define DYNARRAY_PREFIX zstr_
#define DYNARRAY_INITIAL_SIZE 128
#include <malloc/dynarray-skeleton.c>

#include <malloc.h>
#include <mcheck.h>
#include <stdint.h>
#include <support/check.h>
#include <support/support.h>

enum { max_count = 20 };

/* Test dynamic arrays with int elements (no automatic deallocation
   for elements).  */
static void
test_int (void)
{
  /* Empty array.  */
  {
    struct dynarray_int dyn;
    dynarray_int_init (&dyn);
    CHECK_EMPTY (int, &dyn);
  }

  /* Empty array with finalization.  */
  {
    struct dynarray_int dyn;
    dynarray_int_init (&dyn);
    CHECK_INIT_STATE (int, &dyn);
    struct int_array result = { (int *) (uintptr_t) -1, -1 };
    TEST_VERIFY_EXIT (dynarray_int_finalize (&dyn, &result));
    CHECK_INIT_STATE (int, &dyn);
    TEST_VERIFY_EXIT (result.array == NULL);
    TEST_VERIFY_EXIT (result.length == 0);
  }

  /* Non-empty array tests.

     do_add: Switch between emplace (false) and add (true).
     do_finalize: Perform finalize call at the end.
     do_clear: Perform clear call at the end.
     do_remove_last: Perform remove_last call after adding elements.
     count: Number of elements added to the array.  */
  for (int do_add = 0; do_add < 2; ++do_add)
    for (int do_finalize = 0; do_finalize < 2; ++do_finalize)
      for (int do_clear = 0; do_clear < 2; ++do_clear)
        for (int do_remove_last = 0; do_remove_last < 2; ++do_remove_last)
          for (unsigned int count = 0; count < max_count; ++count)
            {
              if (do_remove_last && count == 0)
                continue;
              unsigned int base = count * count;
              struct dynarray_int dyn;
              dynarray_int_init (&dyn);
              for (unsigned int i = 0; i < count; ++i)
                {
                  if (do_add)
                    dynarray_int_add (&dyn, base + i);
                  else
                    {
                      int *place = dynarray_int_emplace (&dyn);
                      TEST_VERIFY_EXIT (place != NULL);
                      *place = base + i;
                    }
                  TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn));
                  TEST_VERIFY_EXIT (dynarray_int_size (&dyn) == i + 1);
                  TEST_VERIFY_EXIT (dynarray_int_size (&dyn)
                                    <= dyn.dynarray_header.allocated);
                }
              TEST_VERIFY_EXIT (dynarray_int_size (&dyn) == count);
              TEST_VERIFY_EXIT (count <= dyn.dynarray_header.allocated);
              if (count > 0)
                {
                  TEST_VERIFY (dynarray_int_begin (&dyn)
                               == dynarray_int_at (&dyn, 0));
                  TEST_VERIFY (dynarray_int_end (&dyn)
                               == dynarray_int_at (&dyn, count - 1) + 1);
                }
              unsigned final_count;
              bool heap_array = dyn.dynarray_header.array != dyn.scratch;
              if (do_remove_last)
                {
                  dynarray_int_remove_last (&dyn);
                  if (count == 0)
                    final_count = 0;
                  else
                    final_count = count - 1;
                }
              else
                final_count = count;
              if (final_count > 0)
                {
                  TEST_VERIFY (dynarray_int_begin (&dyn)
                               == dynarray_int_at (&dyn, 0));
                  TEST_VERIFY (dynarray_int_end (&dyn)
                               == dynarray_int_at (&dyn, final_count - 1) + 1);
                }
              if (do_clear)
                {
                  dynarray_int_clear (&dyn);
                  final_count = 0;
                }
              TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn));
              TEST_VERIFY_EXIT ((dyn.dynarray_header.array != dyn.scratch)
                                == heap_array);
              TEST_VERIFY_EXIT (dynarray_int_size (&dyn) == final_count);
              TEST_VERIFY_EXIT (dyn.dynarray_header.allocated >= final_count);
              if (!do_clear)
                for (unsigned int i = 0; i < final_count; ++i)
                  TEST_VERIFY_EXIT (*dynarray_int_at (&dyn, i) == base + i);
              if (do_finalize)
                {
                  struct int_array result = { (int *) (uintptr_t) -1, -1 };
                  TEST_VERIFY_EXIT (dynarray_int_finalize (&dyn, &result));
                  CHECK_INIT_STATE (int, &dyn);
                  TEST_VERIFY_EXIT (result.length == final_count);
                  if (final_count == 0)
                    TEST_VERIFY_EXIT (result.array == NULL);
                  else
                    {
                      TEST_VERIFY_EXIT (result.array != NULL);
                      TEST_VERIFY_EXIT (result.array != (int *) (uintptr_t) -1);
                      TEST_VERIFY_EXIT
                        (malloc_usable_size (result.array)
                         >= final_count * sizeof (result.array[0]));
                      for (unsigned int i = 0; i < final_count; ++i)
                        TEST_VERIFY_EXIT (result.array[i] == base + i);
                      free (result.array);
                    }
                }
              else /* !do_finalize */
                {
                  dynarray_int_free (&dyn);
                  CHECK_INIT_STATE (int, &dyn);
                }
            }
}

/* Test dynamic arrays with char * elements (with automatic
   deallocation of the pointed-to strings).  */
static void
test_str (void)
{
  /* Empty array.  */
  {
    struct dynarray_str dyn;
    dynarray_str_init (&dyn);
    CHECK_EMPTY (str, &dyn);
  }

  /* Empty array with finalization.  */
  {
    struct dynarray_str dyn;
    dynarray_str_init (&dyn);
    TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
    struct str_array result = { (char **) (uintptr_t) -1, -1 };
    TEST_VERIFY_EXIT (dynarray_str_finalize (&dyn, &result));
    CHECK_INIT_STATE (str, &dyn);
    TEST_VERIFY_EXIT (result.array == NULL);
    TEST_VERIFY_EXIT (result.length == 0);
  }

  /* Non-empty array tests.

     do_add: Switch between emplace (false) and add (true).
     do_finalize: Perform finalize call at the end.
     do_clear: Perform clear call at the end.
     do_remove_last: Perform remove_last call after adding elements.
     count: Number of elements added to the array.  */
  for (int do_add = 0; do_add < 2; ++do_add)
    for (int do_finalize = 0; do_finalize < 2; ++do_finalize)
      for (int do_clear = 0; do_clear < 2; ++do_clear)
        for (int do_remove_last = 0; do_remove_last < 2; ++do_remove_last)
          for (unsigned int count = 0; count < max_count; ++count)
            {
              if (do_remove_last && count == 0)
                continue;
              unsigned int base = count * count;
              struct dynarray_str dyn;
              dynarray_str_init (&dyn);
              for (unsigned int i = 0; i < count; ++i)
                {
                  char *item = xasprintf ("%d", base + i);
                  if (do_add)
                    dynarray_str_add (&dyn, item);
                  else
                    {
                      char **place = dynarray_str_emplace (&dyn);
                      TEST_VERIFY_EXIT (place != NULL);
                      TEST_VERIFY_EXIT (*place == NULL);
                      *place = item;
                    }
                  TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
                  TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == i + 1);
                  TEST_VERIFY_EXIT (dynarray_str_size (&dyn)
                                    <= dyn.dynarray_header.allocated);
                }
              TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == count);
              TEST_VERIFY_EXIT (count <= dyn.dynarray_header.allocated);
              if (count > 0)
                {
                  TEST_VERIFY (dynarray_str_begin (&dyn)
                               == dynarray_str_at (&dyn, 0));
                  TEST_VERIFY (dynarray_str_end (&dyn)
                               == dynarray_str_at (&dyn, count - 1) + 1);
                }
              unsigned final_count;
              bool heap_array = dyn.dynarray_header.array != dyn.scratch;
              if (do_remove_last)
                {
                  dynarray_str_remove_last (&dyn);
                  if (count == 0)
                    final_count = 0;
                  else
                    final_count = count - 1;
                }
              else
                final_count = count;
              if (final_count > 0)
                {
                  TEST_VERIFY (dynarray_str_begin (&dyn)
                               == dynarray_str_at (&dyn, 0));
                  TEST_VERIFY (dynarray_str_end (&dyn)
                               == dynarray_str_at (&dyn, final_count - 1) + 1);
                }
              if (do_clear)
                {
                  dynarray_str_clear (&dyn);
                  final_count = 0;
                }
              TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
              TEST_VERIFY_EXIT ((dyn.dynarray_header.array != dyn.scratch)
                                == heap_array);
              TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == final_count);
              TEST_VERIFY_EXIT (dyn.dynarray_header.allocated >= final_count);
              if (!do_clear)
                for (unsigned int i = 0; i < count - do_remove_last; ++i)
                  {
                    char *expected = xasprintf ("%d", base + i);
                    const char *actual = *dynarray_str_at (&dyn, i);
                    TEST_VERIFY_EXIT (strcmp (actual, expected) == 0);
                    free (expected);
                  }
              if (do_finalize)
                {
                  struct str_array result = { (char **) (uintptr_t) -1, -1 };
                  TEST_VERIFY_EXIT (dynarray_str_finalize (&dyn, &result));
                  CHECK_INIT_STATE (str, &dyn);
                  TEST_VERIFY_EXIT (result.length == final_count);
                  if (final_count == 0)
                    TEST_VERIFY_EXIT (result.array == NULL);
                  else
                    {
                      TEST_VERIFY_EXIT (result.array != NULL);
                      TEST_VERIFY_EXIT (result.array
                                        != (char **) (uintptr_t) -1);
                      TEST_VERIFY_EXIT (result.length
                                        == count - do_remove_last);
                      TEST_VERIFY_EXIT
                        (malloc_usable_size (result.array)
                         >= final_count * sizeof (result.array[0]));
                      for (unsigned int i = 0; i < count - do_remove_last; ++i)
                        {
                          char *expected = xasprintf ("%d", base + i);
                          char *actual = result.array[i];
                          TEST_VERIFY_EXIT (strcmp (actual, expected) == 0);
                          free (expected);
                          free (actual);
                        }
                      free (result.array);
                    }
                }
              else /* !do_finalize */
                {
                  dynarray_str_free (&dyn);
                  CHECK_INIT_STATE (str, &dyn);
                }
            }

  /* Test resizing.  */
  {
    enum { count = 2131 };
    struct dynarray_str dyn;
    dynarray_str_init (&dyn);

    /* From length 0 to length 1.  */
    TEST_VERIFY (dynarray_str_resize (&dyn, 1));
    TEST_VERIFY (dynarray_str_size (&dyn) == 1);
    TEST_VERIFY (*dynarray_str_at (&dyn, 0) == NULL);
    *dynarray_str_at (&dyn, 0) = xstrdup ("allocated");
    dynarray_str_free (&dyn);

    /* From length 0 to length 1 and 2.  */
    TEST_VERIFY (dynarray_str_resize (&dyn, 1));
    TEST_VERIFY (dynarray_str_size (&dyn) == 1);
    TEST_VERIFY (*dynarray_str_at (&dyn, 0) == NULL);
    *dynarray_str_at (&dyn, 0) = xstrdup ("allocated0");
    TEST_VERIFY (dynarray_str_resize (&dyn, 2));
    TEST_VERIFY (dynarray_str_size (&dyn) == 2);
    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 0), "allocated0") == 0);
    TEST_VERIFY (*dynarray_str_at (&dyn, 1) == NULL);
    *dynarray_str_at (&dyn, 1) = xstrdup ("allocated1");
    TEST_VERIFY (dynarray_str_resize (&dyn, count));
    TEST_VERIFY (dynarray_str_size (&dyn) == count);
    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 0), "allocated0") == 0);
    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 1), "allocated1") == 0);
    for (int i = 2; i < count; ++i)
      TEST_VERIFY (*dynarray_str_at (&dyn, i) == NULL);
    *dynarray_str_at (&dyn, count - 1) = xstrdup ("allocated2");
    TEST_VERIFY (dynarray_str_resize (&dyn, 3));
    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 0), "allocated0") == 0);
    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 1), "allocated1") == 0);
    TEST_VERIFY (*dynarray_str_at (&dyn, 2) == NULL);
    dynarray_str_free (&dyn);
  }
}

/* Verify that DYNARRAY_ELEMENT_INIT has an effect.  */
static void
test_long_init (void)
{
  enum { count = 2131 };
  {
    struct dynarray_long dyn;
    dynarray_long_init (&dyn);
    for (int i = 0; i < count; ++i)
      {
        long *place = dynarray_long_emplace (&dyn);
        TEST_VERIFY_EXIT (place != NULL);
        TEST_VERIFY (*place == 17);
      }
    TEST_VERIFY (dynarray_long_size (&dyn) == count);
    for (int i = 0; i < count; ++i)
      TEST_VERIFY (*dynarray_long_at (&dyn, i) == 17);
    dynarray_long_free (&dyn);

    TEST_VERIFY (dynarray_long_resize (&dyn, 1));
    TEST_VERIFY (dynarray_long_size (&dyn) == 1);
    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 17);
    *dynarray_long_at (&dyn, 0) = 18;
    dynarray_long_free (&dyn);
    TEST_VERIFY (dynarray_long_resize (&dyn, 1));
    TEST_VERIFY (dynarray_long_size (&dyn) == 1);
    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 17);
    TEST_VERIFY (dynarray_long_resize (&dyn, 2));
    TEST_VERIFY (dynarray_long_size (&dyn) == 2);
    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 17);
    TEST_VERIFY (*dynarray_long_at (&dyn, 1) == 17);
    *dynarray_long_at (&dyn, 0) = 18;
    TEST_VERIFY (dynarray_long_resize (&dyn, count));
    TEST_VERIFY (dynarray_long_size (&dyn) == count);
    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 18);
    for (int i = 1; i < count; ++i)
      TEST_VERIFY (*dynarray_long_at (&dyn, i) == 17);
    dynarray_long_free (&dyn);
  }

  /* Similar, but without an on-stack scratch region
     (DYNARRAY_INITIAL_SIZE is 0).  */
  {
    struct dynarray_long_noscratch dyn;
    dynarray_long_noscratch_init (&dyn);
    struct long_array result;
    TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn, &result));
    TEST_VERIFY (result.array == NULL);
    TEST_VERIFY (result.length == 0);

    /* Test with one element.  */
    {
      long *place = dynarray_long_noscratch_emplace (&dyn);
      TEST_VERIFY_EXIT (place != NULL);
      TEST_VERIFY (*place == 23);
    }
    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 1);
    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
    TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn, &result));
    TEST_VERIFY_EXIT (result.array != NULL);
    TEST_VERIFY (result.length == 1);
    TEST_VERIFY (result.array[0] == 23);
    free (result.array);

    for (int i = 0; i < count; ++i)
      {
        long *place = dynarray_long_noscratch_emplace (&dyn);
        TEST_VERIFY_EXIT (place != NULL);
        TEST_VERIFY (*place == 23);
        if (i == 0)
          *place = 29;
      }
    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == count);
    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 29);
    for (int i = 1; i < count; ++i)
      TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, i) == 23);
    TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn, &result));
    TEST_VERIFY_EXIT (result.array != NULL);
    TEST_VERIFY (result.length == count);
    TEST_VERIFY (result.array[0] == 29);
    for (int i = 1; i < count; ++i)
      TEST_VERIFY (result.array[i] == 23);
    free (result.array);

    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, 1));
    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 1);
    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
    *dynarray_long_noscratch_at (&dyn, 0) = 24;
    dynarray_long_noscratch_free (&dyn);
    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, 1));
    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 1);
    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, 2));
    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 2);
    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 1) == 23);
    *dynarray_long_noscratch_at (&dyn, 0) = 24;
    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, count));
    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == count);
    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 24);
    for (int i = 1; i < count; ++i)
      TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, i) == 23);
    dynarray_long_noscratch_free (&dyn);
  }
}

/* Test overflow in resize.  */
static void
test_long_overflow (void)
{
  {
    struct dynarray_long dyn;
    dynarray_long_init (&dyn);
    errno = EINVAL;
    TEST_VERIFY (!dynarray_long_resize
                 (&dyn, (SIZE_MAX / sizeof (long)) + 1));
    TEST_VERIFY (errno == ENOMEM);
    TEST_VERIFY (dynarray_long_has_failed (&dyn));
  }

  {
    struct dynarray_long_noscratch dyn;
    dynarray_long_noscratch_init (&dyn);
    errno = EINVAL;
    TEST_VERIFY (!dynarray_long_noscratch_resize
                 (&dyn, (SIZE_MAX / sizeof (long)) + 1));
    TEST_VERIFY (errno == ENOMEM);
    TEST_VERIFY (dynarray_long_noscratch_has_failed (&dyn));
  }
}

/* Test NUL-terminated string construction with the add function and
   the simple finalize function.  */
static void
test_zstr (void)
{
  /* Totally empty string (no NUL termination).  */
  {
    struct zstr s;
    zstr_init (&s);
    char *result = zstr_finalize (&s, NULL);
    TEST_VERIFY (result == NULL);
    TEST_VERIFY (zstr_size (&s) == 0);
    size_t length = 1;
    result = zstr_finalize (&s, &length);
    TEST_VERIFY (result == NULL);
    TEST_VERIFY (length == 0);
    TEST_VERIFY (zstr_size (&s) == 0);
  }

  /* Empty string.  */
  {
    struct zstr s;
    zstr_init (&s);
    zstr_add (&s, '\0');
    char *result = zstr_finalize (&s, NULL);
    TEST_VERIFY_EXIT (result != NULL);
    TEST_VERIFY (*result == '\0');
    TEST_VERIFY (zstr_size (&s) == 0);
    free (result);

    zstr_add (&s, '\0');
    size_t length = 1;
    result = zstr_finalize (&s, &length);
    TEST_VERIFY_EXIT (result != NULL);
    TEST_VERIFY (*result == '\0');
    TEST_VERIFY (length == 1);
    TEST_VERIFY (zstr_size (&s) == 0);
    free (result);
  }

  /* A few characters.  */
  {
    struct zstr s;
    zstr_init (&s);
    zstr_add (&s, 'A');
    zstr_add (&s, 'b');
    zstr_add (&s, 'c');
    zstr_add (&s, '\0');
    char *result = zstr_finalize (&s, NULL);
    TEST_VERIFY_EXIT (result != NULL);
    TEST_VERIFY (strcmp (result, "Abc") == 0);
    TEST_VERIFY (zstr_size (&s) == 0);
    free (result);

    zstr_add (&s, 'X');
    zstr_add (&s, 'y');
    zstr_add (&s, 'z');
    zstr_add (&s, '\0');
    size_t length = 1;
    result = zstr_finalize (&s, &length);
    TEST_VERIFY_EXIT (result != NULL);
    TEST_VERIFY (strcmp (result, "Xyz") == 0);
    TEST_VERIFY (length == 4);
    TEST_VERIFY (zstr_size (&s) == 0);
    free (result);
  }
}

static int
do_test (void)
{
  mtrace ();
  test_int ();
  test_str ();
  test_long_init ();
  test_long_overflow ();
  test_zstr ();
  return 0;
}

#include <support/test-driver.c>