-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_string.c
151 lines (119 loc) · 2.34 KB
/
test_string.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
// Copyright 2022 Mark Seminatore. All rights reserved.
#include "stdio.h"
#include "string.h"
#include "test.h"
//
static void test_strlen()
{
SUITE("strlen");
TEST(strlen("Hello") == 5);
TEST(strlen("") == 0);
}
//
static void test_memcpy()
{
char result[64] = "";
SUITE("memcpy");
TEST(0 == strcmp(memcpy(result, "Hello", strlen("Hello") + 1), "Hello"));
}
//
static void test_memmove()
{
char result[64] = "";
SUITE("memmove");
TEST(0 == strcmp(memmove(result, "Hello", strlen("Hello") + 1), "Hello"));
}
//
static void test_strcmp()
{
SUITE("strcmp");
TEST(0 == strcmp("same", "same"));
TEST(0 > strcmp("ab", "ac"));
TEST(0 < strcmp("abcd", "ABCD"));
}
//
static void test_strcpy()
{
char result[64] = "";
SUITE("strcpy");
TEST(0 == strcmp(strcpy(result, "Hello!"), "Hello!"));
TEST(0 == strcmp(strcpy(result, ""), ""));
}
//
static void test_strncpy()
{
char result[64] = "";
SUITE("strncpy");
TEST(0 == strcmp(strncpy(result, "Hello", 3), "Hel"));
TEST(0 != strcmp(strncpy(result, "Hello", 4), "Hel"));
}
//
static void test_strcat()
{
char result[64] = "Hello ";
SUITE("strcat");
TEST(0 == strcmp("Hello World!", strcat(result, "World!")));
}
//
static void test_strpbrk()
{
SUITE("strpbrk");
TEST(NULL == strpbrk("ABC", "DEF"));
}
//
static void test_strchr()
{
SUITE("strchr");
TEST('H' == *strchr("Hello", 'H'));
TEST('e' == *strchr("Hello", 'e'));
TEST('l' == *strchr("Hello", 'l'));
}
//
static void test_strrchr()
{
SUITE("strrchr");
TEST('H' == *strrchr("Hello", 'H'));
TEST('e' == *strrchr("Hello", 'e'));
TEST('l' == *strrchr("Hello", 'l'));
}
//
static void test_strcspn()
{
SUITE("strcspn");
TEST(2 == strcspn("Hello", "World"));
TEST(1 == strcspn("Hello", "There"));
TEST(4 == strcspn("Stop", "Pill"));
TEST(0 == strcspn("Hello", "Hill"));
}
//
static void test_strspn()
{
SUITE("strspn");
TEST(5 == strspn("Hello", "Hello"));
TEST(0 == strspn("Hello", "There"));
TEST(4 == strspn("Hello", "Help"));
}
//
static void test_strtok()
{
SUITE("strtok");
TEST(0 == strcmp("Hello", strtok("Hello there", " ")));
TEST(0 == strcmp("there", strtok(NULL, " ")));
}
//
void test_string()
{
test_strlen();
test_memcpy();
test_memmove();
test_strcmp();
test_strcpy();
test_strncpy();
test_strcat();
test_strpbrk();
test_strchr();
test_strrchr();
test_strcspn();
test_strspn();
test_strtok();
}