forked from siriobalmelli/nonlibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nstring_test.c
81 lines (67 loc) · 1.79 KB
/
nstring_test.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
/* nstring_test.c
*
* (c) 2018 Sirio Balmelli
*/
#include <nstring.h>
#include <ndebug.h>
static const char source[] = "a_string";
/* main()
*/
int main()
{
int err_cnt = 0;
size_t len;
char *alloc = NULL;
/* identical copy */
char identical[sizeof(source)];
len = nstrcpy(identical, source, sizeof(identical), false);
NB_die_if(len != strlen(source) || len != strlen(identical), "");
NB_die_if(strcmp(source, identical), "");
/* truncated copy */
char trunc[4];
len = nstrcpy(trunc, source, sizeof(trunc), false);
NB_die_if(errno != E2BIG, ""); errno=0;
NB_die_if(len != strlen(trunc), "");
NB_die_if(strncmp(source, trunc, len), "");
/* padded copy */
char padded[64];
len = nstrcpy(padded, source, sizeof(padded), true);
NB_die_if(len != strlen(padded), "");
NB_die_if(strncmp(source, padded, len), "");
for (int i=len; i < sizeof(padded); i++) {
NB_die_if(padded[i] != '\0', "");
}
/* zero-size copy */
char zero[1];
len = nstrcpy(zero, source, 0, false);
NB_die_if(len != 0, "");
NB_die_if(zero[0] != '\0', "");
/* invalid copy */
len = nstrcpy(NULL, source, -1, false);
NB_die_if(errno != EINVAL, ""); errno = 0;
NB_die_if(len != 0, "");
/* alloc */
NB_die_if(!(
alloc = nstralloc(source, sizeof(source), &len)
), "");
NB_die_if(len != strlen(source) || len != strlen(alloc), "");
NB_die_if(strcmp(source, alloc), "");
free(alloc); alloc = NULL;
/* truncated alloc */
NB_die_if(!(
alloc = nstralloc(source, 6, &len)
), "");
NB_die_if(errno != E2BIG, ""); errno = 0;
NB_die_if(len != strlen(alloc), "");
NB_die_if(strncmp(source, alloc, len), "");
free(alloc); alloc = NULL;
/* zero alloc */
NB_die_if(!(
alloc = nstralloc(source, 0, &len)
), "");
NB_die_if(len, "");
free(alloc); alloc = NULL;
die:
free(alloc);
return err_cnt;
}