forked from charybdis-ircd/charybdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb_snprintf_append1.c
184 lines (143 loc) · 3.79 KB
/
rb_snprintf_append1.c
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
/*
* rb_snprintf_append1.c: Test rb_snprintf_append under various conditions
* Copyright 2017 Simon Arlott
*
* 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 of the License, 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "tap/basic.h"
#include "stdinc.h"
#include "ircd_defs.h"
#include "client.h"
#include "rb_lib.h"
#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
struct Client me;
static void from_empty1(void)
{
char output[2048] = { 0 };
int len;
len = rb_snprintf_append(output, sizeof(output), "%s", "test");
is_int(4, len, MSG);
is_string("test", output, MSG);
}
static void basic_append1(void)
{
char output[2048] = { 0 };
int len;
strcat(output, "test");
len = rb_snprintf_append(output, sizeof(output), "%s", "TESTING");
is_int(11, len, MSG);
is_string("testTESTING", output, MSG);
}
static void append_empty1(void)
{
char output[2048] = { 0 };
int len;
strcat(output, "test");
len = rb_snprintf_append(output, sizeof(output), "%s", "");
is_int(4, len, MSG);
is_string("test", output, MSG);
}
static void exact_fit_empty1(void)
{
char output[5] = { 0 };
int len;
strcat(output, "test");
len = rb_snprintf_append(output, sizeof(output), "%s", "");
is_int(4, len, MSG);
is_string("test", output, MSG);
}
static void exact_fit_basic_append1(void)
{
char output[12] = { 0 };
int len;
strcat(output, "test");
len = rb_snprintf_append(output, sizeof(output), "%s", "TESTING");
is_int(11, len, MSG);
is_string("testTESTING", output, MSG);
}
static void too_long_basic_append1(void)
{
char output[11] = { 0 };
int len;
strcat(output, "test");
len = rb_snprintf_append(output, sizeof(output), "%s", "TESTING");
is_int(11, len, MSG);
is_string("testTESTIN", output, MSG);
}
static void exact_fit_from_empty1(void)
{
char output[5] = { 0 };
int len;
len = rb_snprintf_append(output, sizeof(output), "%s", "test");
is_int(4, len, MSG);
is_string("test", output, MSG);
}
static void too_long_from_empty1(void)
{
char output[4] = { 0 };
int len;
len = rb_snprintf_append(output, sizeof(output), "%s", "test");
is_int(4, len, MSG);
is_string("tes", output, MSG);
}
static void truncate_existing1(void)
{
char output[2048] = { 0 };
int len;
strcat(output, "testing");
len = rb_snprintf_append(output, 5, "%s", "");
is_int(4, len, MSG);
is_string("test", output, MSG);
}
static void truncate_existing2(void)
{
char output[2048] = { 0 };
int len;
strcat(output, "testing");
len = rb_snprintf_append(output, 5, "%s", "TEST");
is_int(4, len, MSG);
is_string("test", output, MSG);
}
static void no_buffer(void)
{
int len;
len = rb_snprintf_append(NULL, 0, "%s", "test");
is_int(-1, len, MSG);
}
int main(int argc, char *argv[])
{
memset(&me, 0, sizeof(me));
strcpy(me.name, "me.name.");
rb_lib_init(NULL, NULL, NULL, 0, 1024, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
rb_linebuf_init(LINEBUF_HEAP_SIZE);
plan_lazy();
from_empty1();
basic_append1();
append_empty1();
exact_fit_from_empty1();
too_long_from_empty1();
exact_fit_basic_append1();
too_long_basic_append1();
exact_fit_empty1();
truncate_existing1();
truncate_existing2();
no_buffer();
return 0;
}