|
| 1 | +/* -*- coding: utf-8; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: t -*- */ |
| 2 | + |
| 3 | +// This code and all comments, written by Daniel Trebbien, are hereby entered into the Public Domain by their author. |
| 4 | + |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +#define BOOST_TEST_DYN_LINK 1 |
| 8 | +#define BOOST_TEST_MODULE "string_utils tests" |
| 9 | +#include <boost/test/unit_test.hpp> |
| 10 | + |
| 11 | +#include "../../string_utils.h" |
| 12 | + |
| 13 | +BOOST_AUTO_TEST_CASE(test_x_strlcpy) |
| 14 | +{ |
| 15 | + char buf[10]; |
| 16 | + |
| 17 | + // No room for the NUL terminator |
| 18 | + buf[0] = 'a'; |
| 19 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "test", 0), 4); |
| 20 | + BOOST_CHECK_EQUAL(buf[0], 'a'); |
| 21 | + |
| 22 | + // Only room for the NUL terminator and `*src` is a NUL character |
| 23 | + buf[0] = buf[1] = 'a'; |
| 24 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "\0", 1), 0); |
| 25 | + BOOST_CHECK_EQUAL(buf[0], '\0'); |
| 26 | + BOOST_CHECK_EQUAL(buf[1], 'a'); |
| 27 | + |
| 28 | + // `*src` is a NUL character and `dest` has space for characters other than the NUL terminator. |
| 29 | + buf[0] = buf[1] = 'a'; |
| 30 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "\0", 10), 0); |
| 31 | + BOOST_CHECK_EQUAL(buf[0], '\0'); |
| 32 | + BOOST_CHECK_EQUAL(buf[1], 'a'); |
| 33 | + |
| 34 | + // Only room for the NUL terminator and `*src` is not a NUL character |
| 35 | + buf[0] = buf[1] = 'a'; |
| 36 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "test", 1), 4); |
| 37 | + BOOST_CHECK_EQUAL(buf[0], '\0'); |
| 38 | + BOOST_CHECK_EQUAL(buf[1], 'a'); |
| 39 | + |
| 40 | + // Only room to copy one character from `src` |
| 41 | + std::fill(buf, buf + (sizeof buf), 'a'); |
| 42 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "test", 2), 4); |
| 43 | + BOOST_CHECK_EQUAL(buf[0], 't'); |
| 44 | + BOOST_CHECK_EQUAL(buf[1], '\0'); |
| 45 | + BOOST_CHECK_EQUAL(buf[2], 'a'); |
| 46 | + |
| 47 | + // Only room to copy three characters from `src` |
| 48 | + std::fill(buf, buf + (sizeof buf), 'a'); |
| 49 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "test", 4), 4); |
| 50 | + BOOST_CHECK_EQUAL(buf[0], 't'); |
| 51 | + BOOST_CHECK_EQUAL(buf[1], 'e'); |
| 52 | + BOOST_CHECK_EQUAL(buf[2], 's'); |
| 53 | + BOOST_CHECK_EQUAL(buf[3], '\0'); |
| 54 | + BOOST_CHECK_EQUAL(buf[4], 'a'); |
| 55 | + |
| 56 | + // `dest` is just big enough to copy `src` and the NUL terminator. |
| 57 | + std::fill(buf, buf + (sizeof buf), 'a'); |
| 58 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "test", 5), 4); |
| 59 | + BOOST_CHECK_EQUAL(buf[0], 't'); |
| 60 | + BOOST_CHECK_EQUAL(buf[1], 'e'); |
| 61 | + BOOST_CHECK_EQUAL(buf[2], 's'); |
| 62 | + BOOST_CHECK_EQUAL(buf[3], 't'); |
| 63 | + BOOST_CHECK_EQUAL(buf[4], '\0'); |
| 64 | + BOOST_CHECK_EQUAL(buf[5], 'a'); |
| 65 | + |
| 66 | + // `dest` has space for more than `strlen(src) + 1` characters. |
| 67 | + std::fill(buf, buf + (sizeof buf), 'a'); |
| 68 | + BOOST_CHECK_EQUAL(x_strlcpy(buf, "test", 10), 4); |
| 69 | + BOOST_CHECK_EQUAL(buf[0], 't'); |
| 70 | + BOOST_CHECK_EQUAL(buf[1], 'e'); |
| 71 | + BOOST_CHECK_EQUAL(buf[2], 's'); |
| 72 | + BOOST_CHECK_EQUAL(buf[3], 't'); |
| 73 | + BOOST_CHECK_EQUAL(buf[4], '\0'); |
| 74 | + BOOST_CHECK_EQUAL(buf[5], 'a'); |
| 75 | +} |
0 commit comments