@@ -78,6 +78,92 @@ void test_strnlen_max_len_5(void) {
78
78
CU_ASSERT_EQUAL (len , 3 );
79
79
}
80
80
81
+ void test_strncpy_null (void ) {
82
+ char dest [] = {'a' , 'b' , 'c' };
83
+ size_t len = utils_strncpy (dest , sizeof (dest ), NULL , 1 );
84
+ CU_ASSERT_EQUAL (len , 0 );
85
+ CU_ASSERT_NSTRING_EQUAL (dest , "abc" , sizeof (dest ));
86
+
87
+ len = utils_strncpy (NULL , 99 , "xyz" , 1 );
88
+ CU_ASSERT_EQUAL (len , 0 );
89
+ len = utils_strncpy (NULL , 5 , NULL , 3 );
90
+ CU_ASSERT_EQUAL (len , 0 );
91
+ }
92
+
93
+ void test_strncpy_dest_0_src_0 (void ) {
94
+ char dst [] = {'a' , 'b' , 'c' };
95
+ const char * src = "xyz" ;
96
+ const size_t len = utils_strncpy (dst , 0 , src , 0 );
97
+ CU_ASSERT_EQUAL (len , 0 );
98
+ CU_ASSERT_NSTRING_EQUAL (dst , "abc" , sizeof (dst ));
99
+ }
100
+
101
+ void test_strncpy_dest_1_src_0 (void ) {
102
+ char dst [] = {'a' , 'b' , 'c' };
103
+ const char * src = "xyz" ;
104
+ const size_t len = utils_strncpy (dst , 1 , src , 0 );
105
+ CU_ASSERT_EQUAL (dst [0 ], '\0' );
106
+ CU_ASSERT_EQUAL (len , 0 );
107
+ CU_ASSERT_EQUAL (utils_strnlen (dst , sizeof (dst )), 0 );
108
+ }
109
+
110
+ void test_strncpy_dest_sizeof_src_0 (void ) {
111
+ char dst [] = {'a' , 'b' , 'c' };
112
+ const char * src = "xyz" ;
113
+ const size_t len = utils_strncpy (dst , sizeof (dst ), src , 0 );
114
+ CU_ASSERT_EQUAL (dst [0 ], '\0' );
115
+ CU_ASSERT_EQUAL (len , 0 );
116
+ CU_ASSERT_EQUAL (utils_strnlen (dst , sizeof (dst )), 0 );
117
+ }
118
+
119
+ void test_strncpy_dest_sizeof_src_1 (void ) {
120
+ char dst [] = {'a' , 'b' , 'c' };
121
+ const char * src = "xyz" ;
122
+ const size_t len = utils_strncpy (dst , sizeof (dst ), src , 1 );
123
+ CU_ASSERT_NSTRING_EQUAL (dst , "x" , 1 );
124
+ CU_ASSERT_EQUAL (dst [1 ], '\0' );
125
+ CU_ASSERT_EQUAL (len , 1 );
126
+ CU_ASSERT_EQUAL (utils_strnlen (dst , sizeof (dst )), 1 );
127
+ }
128
+
129
+ void test_strncpy_dest_sizeof_src_2 (void ) {
130
+ char dst [] = {'a' , 'b' , 'c' };
131
+ const char * src = "xyz" ;
132
+ const size_t len = utils_strncpy (dst , sizeof (dst ), src , 2 );
133
+ CU_ASSERT_NSTRING_EQUAL (dst , "xy" , 2 );
134
+ CU_ASSERT_EQUAL (dst [2 ], '\0' );
135
+ CU_ASSERT_EQUAL (len , 2 );
136
+ CU_ASSERT_EQUAL (utils_strnlen (dst , sizeof (dst )), 2 );
137
+ }
138
+
139
+ void test_strncpy_dest_sizeof_src_3 (void ) {
140
+ char dst [] = {'a' , 'b' , 'c' };
141
+ const char * src = "xyz" ;
142
+ const size_t len = utils_strncpy (dst , sizeof (dst ), src , 3 );
143
+ // only 2 characters and NULL has space in dst
144
+ CU_ASSERT_NSTRING_EQUAL (dst , "xy" , 2 );
145
+ CU_ASSERT_EQUAL (dst [2 ], '\0' );
146
+ CU_ASSERT_EQUAL (len , 2 );
147
+ CU_ASSERT_EQUAL (utils_strnlen (dst , sizeof (dst )), 2 );
148
+ }
149
+
150
+ void test_strncpy_dest_sizeof_src_4 (void ) {
151
+ char dst [] = {'a' , 'b' , 'c' };
152
+ char src [10 ];
153
+ memset (src , '\0' , sizeof (src ));
154
+ src [0 ] = 'u' ;
155
+ src [1 ] = 'v' ;
156
+ src [2 ] = 'w' ;
157
+ const size_t src_len = sizeof (src );
158
+ CU_ASSERT_EQUAL (src_len , 10 );
159
+ const size_t len = utils_strncpy (dst , sizeof (dst ), src , src_len );
160
+ // only 2 characters and NULL has space in dst
161
+ CU_ASSERT_NSTRING_EQUAL (dst , "uv" , 2 );
162
+ CU_ASSERT_EQUAL (dst [2 ], '\0' );
163
+ CU_ASSERT_EQUAL (len , 2 );
164
+ CU_ASSERT_EQUAL (utils_strnlen (dst , sizeof (dst )), 2 );
165
+ }
166
+
81
167
static struct TestTable table [] = {
82
168
{"test_strnlen_null()" , test_strnlen_null },
83
169
{"test_strnlen_0()" , test_strnlen_0 },
@@ -89,6 +175,13 @@ static struct TestTable table[] = {
89
175
{"test_strnlen_max_len_3()" , test_strnlen_max_len_3 },
90
176
{"test_strnlen_max_len_4()" , test_strnlen_max_len_4 },
91
177
{"test_strnlen_max_len_5()" , test_strnlen_max_len_5 },
178
+ {"test_strncpy_null()" , test_strncpy_null },
179
+ {"test_strncpy_dest_0_src_0()" , test_strncpy_dest_0_src_0 },
180
+ {"test_strncpy_dest_1_src_0()" , test_strncpy_dest_1_src_0 },
181
+ {"test_strncpy_dest_sizeof_src_0()" , test_strncpy_dest_sizeof_src_0 },
182
+ {"test_strncpy_dest_sizeof_src_1()" , test_strncpy_dest_sizeof_src_1 },
183
+ {"test_strncpy_dest_sizeof_src_3()" , test_strncpy_dest_sizeof_src_3 },
184
+ {"test_strncpy_dest_sizeof_src_4()" , test_strncpy_dest_sizeof_src_4 },
92
185
{NULL , NULL },
93
186
};
94
187
0 commit comments