]>
jfr.im git - solanum.git/blob - tests/rb_snprintf_try_append1.c
2 * rb_snprintf_try_append1.c: Test rb_snprintf_try_append under various conditions
3 * Copyright 2017 Simon Arlott
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 #include "tap/basic.h"
27 #include "ircd_defs.h"
31 #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
35 static void from_empty1(void)
37 char output
[2048] = { 0 };
40 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "test");
42 is_string("test", output
, MSG
);
45 static void basic_append1(void)
47 char output
[2048] = { 0 };
50 strcat(output
, "test");
52 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "TESTING");
54 is_string("testTESTING", output
, MSG
);
57 static void append_empty1(void)
59 char output
[2048] = { 0 };
62 strcat(output
, "test");
64 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "");
66 is_string("test", output
, MSG
);
69 static void exact_fit_empty1(void)
71 char output
[5] = { 0 };
74 strcat(output
, "test");
76 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "");
78 is_string("test", output
, MSG
);
81 static void exact_fit_basic_append1(void)
83 char output
[12] = { 0 };
86 strcat(output
, "test");
88 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "TESTING");
90 is_string("testTESTING", output
, MSG
);
93 static void too_long_basic_append1(void)
95 char output
[11] = { 0 };
98 strcat(output
, "test");
100 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "TESTING");
101 is_int(-1, len
, MSG
);
102 is_string("test", output
, MSG
);
105 static void exact_fit_from_empty1(void)
107 char output
[5] = { 0 };
110 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "test");
112 is_string("test", output
, MSG
);
115 static void too_long_from_empty1(void)
117 char output
[4] = { 0 };
120 len
= rb_snprintf_try_append(output
, sizeof(output
), "%s", "test");
121 is_int(-1, len
, MSG
);
122 is_string("", output
, MSG
);
125 static void truncate_existing1(void)
127 char output
[2048] = { 0 };
130 strcat(output
, "testing");
132 len
= rb_snprintf_try_append(output
, 5, "%s", "");
133 is_int(-1, len
, MSG
);
134 is_string("test", output
, MSG
);
137 static void truncate_existing2(void)
139 char output
[2048] = { 0 };
142 strcat(output
, "testing");
144 len
= rb_snprintf_try_append(output
, 5, "%s", "TEST");
145 is_int(-1, len
, MSG
);
146 is_string("test", output
, MSG
);
149 static void no_buffer(void)
153 len
= rb_snprintf_try_append(NULL
, 0, "%s", "test");
154 is_int(-1, len
, MSG
);
157 int main(int argc
, char *argv
[])
159 memset(&me
, 0, sizeof(me
));
160 strcpy(me
.name
, "me.name.");
162 rb_lib_init(NULL
, NULL
, NULL
, 0, 1024, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
163 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
171 exact_fit_from_empty1();
172 too_long_from_empty1();
174 exact_fit_basic_append1();
175 too_long_basic_append1();
179 truncate_existing1();
180 truncate_existing2();