You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
4.8 KiB
175 lines
4.8 KiB
5 years ago
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <assert.h>
|
||
|
#include <string.h>
|
||
|
#include "strlcat.h"
|
||
|
|
||
|
int main () {
|
||
|
// Test argument checking
|
||
|
assert(0 == strlcpy(NULL, "", 10));
|
||
|
assert(0 == strlcpy("", NULL, 10));
|
||
|
assert(0 == strlcpy(NULL, NULL, 10));
|
||
|
|
||
|
assert(0 == strlncpy(NULL, "", 10, 10));
|
||
|
assert(0 == strlncpy("", NULL, 10, 10));
|
||
|
assert(0 == strlncpy(NULL, NULL, 10, 10));
|
||
|
|
||
|
assert(0 == strlcat(NULL, "", 10));
|
||
|
assert(0 == strlcat("", NULL, 10));
|
||
|
assert(0 == strlcat(NULL, NULL, 10));
|
||
|
|
||
|
assert(0 == strlncat(NULL, "", 10, 10));
|
||
|
assert(0 == strlncat("", NULL, 10, 10));
|
||
|
assert(0 == strlncat(NULL, NULL, 10, 10));
|
||
|
|
||
|
|
||
|
// strlcpy
|
||
|
{
|
||
|
char buf[5] = {0,1,2,3,4};
|
||
|
assert(9 == strlcpy(buf, "abcdefgh", 4));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 'b');
|
||
|
assert(buf[2] == 'c');
|
||
|
assert(buf[3] == 0);
|
||
|
assert(buf[4] == 4);
|
||
|
}
|
||
|
{
|
||
|
char buf[5] = {0,1,2,3,4};
|
||
|
assert(3 == strlcpy(buf, "ab", 5));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 'b');
|
||
|
assert(buf[2] == 0);
|
||
|
assert(buf[3] == 3);
|
||
|
}
|
||
|
{
|
||
|
char buf[5] = {5,4,3,2,1};
|
||
|
assert(7 == strlcpy(buf, "abcdef", 1));
|
||
|
assert(buf[0] == 0); // it puts the zero there
|
||
|
assert(buf[1] == 4);
|
||
|
}
|
||
|
|
||
|
// strlncpy
|
||
|
{
|
||
|
char buf[5] = {0,1,2,3,4};
|
||
|
assert(3 == strlncpy(buf, "abcdefgh", 4, 2));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 'b');
|
||
|
assert(buf[2] == 0);
|
||
|
assert(buf[3] == 3);
|
||
|
}
|
||
|
{
|
||
|
char buf[5] = {0,1,2,3,4};
|
||
|
assert(5 == strlncpy(buf, "abcdefgh", 5, 4));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 'b');
|
||
|
assert(buf[2] == 'c');
|
||
|
assert(buf[3] == 'd');
|
||
|
assert(buf[4] == 0);
|
||
|
}
|
||
|
{
|
||
|
char buf[5] = {5,4,3,2,1};
|
||
|
assert(2 == strlncpy(buf, "abcdef", 1, 1));
|
||
|
assert(buf[0] == 0); // it puts the zero there
|
||
|
assert(buf[1] == 4);
|
||
|
}
|
||
|
|
||
|
// strlcat
|
||
|
{
|
||
|
char buf[8] = {0,1,2,3,4,5,6,7};
|
||
|
assert(2 == strlcat(buf, "a", 8));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 0);
|
||
|
assert(buf[2] == 2);
|
||
|
}
|
||
|
{
|
||
|
char buf[8] = {0,1,2,3,4,5,6,7};
|
||
|
assert(3 == strlcat(buf, "ab", 8));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 'b');
|
||
|
assert(buf[2] == 0);
|
||
|
assert(buf[3] == 3);
|
||
|
|
||
|
assert(5 == strlcat(buf, "xx", 8));
|
||
|
assert(0==strcmp(buf,"abxx"));
|
||
|
|
||
|
assert(5 == strlcat(buf, "", 8)); // this just sets the terminator
|
||
|
assert(0==strcmp(buf,"abxx"));
|
||
|
|
||
|
assert(8 == strlcat(buf, "dog", 8));
|
||
|
assert(0==strcmp(buf,"abxxdog"));
|
||
|
|
||
|
assert(10 == strlcat(buf, "ff", 8));
|
||
|
assert(0==strcmp(buf,"abxxdog"));
|
||
|
}
|
||
|
{
|
||
|
char buf[5] = {5,4,3,2,1};
|
||
|
assert(7 == strlcat(buf, "abcdef", 1));
|
||
|
assert(buf[0] == 0); // it puts the zero there
|
||
|
assert(buf[1] == 4);
|
||
|
}
|
||
|
|
||
|
|
||
|
// strlncat
|
||
|
{
|
||
|
char buf[8] = {0,1,2,3,4,5,6,7};
|
||
|
assert(2 == strlncat(buf, "a", 8, 1));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 0);
|
||
|
assert(buf[2] == 2);
|
||
|
}
|
||
|
{
|
||
|
char buf[8] = {0,1,2,3,4,5,6,7};
|
||
|
assert(2 == strlncat(buf, "a", 8, 50)); // larger n than string size
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 0);
|
||
|
assert(buf[2] == 2);
|
||
|
}
|
||
|
{
|
||
|
char buf[8] = {0,1,2,3,4,5,6,7};
|
||
|
assert(26 == strlncat(buf, "abcdefghijklmnopqrstuvwyx", 8, 50)); // larger n than string size & larger than capacity
|
||
|
assert(0==strcmp(buf, "abcdefg"));
|
||
|
}
|
||
|
{
|
||
|
char buf[8] = {0,1,2,3,4,5,6,7};
|
||
|
assert(5 == strlncat(buf, "abcd", 8, 50)); // larger n than string size & larger than capacity
|
||
|
assert(0==strcmp(buf, "abcd"));
|
||
|
assert(buf[4] == 0);
|
||
|
assert(buf[5] == 5);
|
||
|
}
|
||
|
{
|
||
|
char buf[8] = {0,1,2,3,4,5,6,7};
|
||
|
assert(3 == strlncat(buf, "abcdef", 8, 2));
|
||
|
assert(buf[0] == 'a');
|
||
|
assert(buf[1] == 'b');
|
||
|
assert(buf[2] == 0);
|
||
|
assert(buf[3] == 3);
|
||
|
|
||
|
assert(5 == strlncat(buf, "xxyyyyy", 8, 2));
|
||
|
assert(0==strcmp(buf,"abxx"));
|
||
|
|
||
|
assert(5 == strlncat(buf, "", 8, 100)); // this just sets the terminator
|
||
|
assert(0==strcmp(buf,"abxx"));
|
||
|
|
||
|
assert(5 == strlncat(buf, "", 8, 0));
|
||
|
assert(0==strcmp(buf,"abxx"));
|
||
|
|
||
|
assert(8 == strlncat(buf, "doggg", 8, 3));
|
||
|
assert(0==strcmp(buf,"abxxdog"));
|
||
|
|
||
|
assert(10 == strlncat(buf, "ffxxx", 8, 2));
|
||
|
assert(0==strcmp(buf,"abxxdog"));
|
||
|
}
|
||
|
{
|
||
|
char buf[5] = {5,4,3,2,1};
|
||
|
assert(2 == strlncat(buf, "abcdef", 1, 1));
|
||
|
assert(buf[0] == 0); // it puts the zero there
|
||
|
assert(buf[1] == 4);
|
||
|
}
|
||
|
|
||
|
// this is a test that asserts are evaluated
|
||
|
assert((printf("all ok\n"), 1));
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|