2012-05-31 04:17:08 +02:00
|
|
|
#include <CuTest.h>
|
2012-05-29 19:45:46 +02:00
|
|
|
#include "bsdstring.h"
|
2015-08-18 17:08:02 +02:00
|
|
|
#include <errno.h>
|
2012-05-29 19:45:46 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static void test_strlcat(CuTest * tc)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
char buffer[32];
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
memset(buffer, 0x7f, sizeof(buffer));
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
buffer[0] = '\0';
|
2015-05-15 20:35:36 +02:00
|
|
|
CuAssertIntEquals(tc, 4, (int)strlcat(buffer, "herp", 4));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "her", buffer);
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
buffer[0] = '\0';
|
2015-05-15 20:35:36 +02:00
|
|
|
CuAssertIntEquals(tc, 4, (int)strlcat(buffer, "herp", 8));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "herp", buffer);
|
|
|
|
CuAssertIntEquals(tc, 0x7f, buffer[5]);
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-05-15 20:35:36 +02:00
|
|
|
CuAssertIntEquals(tc, 8, (int)strlcat(buffer, "derp", 8));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "herpder", buffer);
|
|
|
|
CuAssertIntEquals(tc, 0x7f, buffer[8]);
|
2012-05-29 19:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_strlcpy(CuTest * tc)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
char buffer[32];
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
memset(buffer, 0x7f, sizeof(buffer));
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-05-15 20:35:36 +02:00
|
|
|
CuAssertIntEquals(tc, 4, (int)strlcpy(buffer, "herp", 4));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "her", buffer);
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2016-11-23 19:19:04 +01:00
|
|
|
CuAssertIntEquals(tc, 4, (int)strlcpy(buffer, "herp", 8)); //-V666
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "herp", buffer);
|
|
|
|
CuAssertIntEquals(tc, 0x7f, buffer[5]);
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-05-15 20:35:36 +02:00
|
|
|
CuAssertIntEquals(tc, 8, (int)strlcpy(buffer, "herpderp", 8));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "herpder", buffer);
|
|
|
|
CuAssertIntEquals(tc, 0x7f, buffer[8]);
|
2015-08-18 17:08:02 +02:00
|
|
|
errno = 0;
|
2012-05-29 19:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_slprintf(CuTest * tc)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
char buffer[32];
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
memset(buffer, 0x7f, sizeof(buffer));
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertTrue(tc, slprintf(buffer, 4, "%s", "herpderp") > 3);
|
|
|
|
CuAssertStrEquals(tc, "her", buffer);
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-05-15 20:35:36 +02:00
|
|
|
CuAssertIntEquals(tc, 4, (int)slprintf(buffer, 8, "%s", "herp"));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "herp", buffer);
|
|
|
|
CuAssertIntEquals(tc, 0x7f, buffer[5]);
|
2012-05-29 19:45:46 +02:00
|
|
|
|
2015-05-15 20:35:36 +02:00
|
|
|
CuAssertIntEquals(tc, 8, (int)slprintf(buffer, 8, "%s", "herpderp"));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "herpder", buffer);
|
|
|
|
CuAssertIntEquals(tc, 0x7f, buffer[8]);
|
2012-05-29 19:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CuSuite *get_bsdstring_suite(void)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_strlcat);
|
|
|
|
SUITE_ADD_TEST(suite, test_strlcpy);
|
|
|
|
SUITE_ADD_TEST(suite, test_slprintf);
|
|
|
|
return suite;
|
2012-05-29 19:45:46 +02:00
|
|
|
}
|