summaryrefslogtreecommitdiff
path: root/tests/test-string-buffer.c
blob: ecf516012cf55bb8f273f3f1f18cd5d511f63f31 (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
/* Test of buffer that accumulates a string by piecewise concatenation.
   Copyright (C) 2021 Free Software Foundation, Inc.

   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; either version 2, or (at your option)
   any later version.

   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, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2021.  */

#include <config.h>

#include "string-buffer.h"

#include <string.h>

#include "macros.h"

static int
my_appendf (struct string_buffer *buffer, const char *formatstring, ...)
{
  va_list args;

  va_start (args, formatstring);
  int ret = sb_appendvf (buffer, formatstring, args);
  va_end (args);

  return ret;
}

char invalid_format_string_1[] = "%&";
char invalid_format_string_2[] = "%^";

int
main ()
{
  /* Test simple string concatenation.  */
  {
    struct string_buffer buffer;

    sb_init (&buffer);
    char *s = sb_dupfree (&buffer);
    ASSERT (s != NULL && strcmp (s, "") == 0);
    free (s);
  }

  {
    struct string_buffer buffer;

    sb_init (&buffer);
    sb_append (&buffer, "abc");
    sb_append (&buffer, "");
    sb_append (&buffer, "defg");
    char *s = sb_dupfree (&buffer);
    ASSERT (s != NULL && strcmp (s, "abcdefg") == 0);
    free (s);
  }

  /* Test printf-like formatting.  */
  {
    struct string_buffer buffer;

    sb_init (&buffer);
    sb_append (&buffer, "<");
    sb_appendf (&buffer, "%x", 3735928559U);
    sb_append (&buffer, ">");
    char *s = sb_dupfree (&buffer);
    ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
    free (s);
  }

  /* Test vprintf-like formatting.  */
  {
    struct string_buffer buffer;

    sb_init (&buffer);
    sb_append (&buffer, "<");
    my_appendf (&buffer, "%x", 3735928559U);
    sb_append (&buffer, ">");
    char *s = sb_dupfree (&buffer);
    ASSERT (s != NULL && strcmp (s, "<deadbeef>") == 0);
    free (s);
  }

  /* Test printf-like formatting failure.
     On all systems except AIX, trying to convert the wide-character 0x76543210
     to a multibyte string (in the "C" locale) fails.
     On all systems where REPLACE_VSNPRINTF=1 (this includes AIX), i.e. where
     the Gnulib implementation of vsnprintf() is used), invalid format
     directives make the *printf call fail.  */
  {
    struct string_buffer buffer;

    sb_init (&buffer);
    sb_append (&buffer, "<");
    sb_appendf (&buffer, "%lc", 0x76543210);
    sb_append (&buffer, "|");
    sb_appendf (&buffer, invalid_format_string_1, 1);
    sb_append (&buffer, "|");
    sb_appendf (&buffer, invalid_format_string_2, 2);
    sb_append (&buffer, ">");
    char *s = sb_dupfree (&buffer);
    ASSERT (s == NULL);
  }

  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.