summaryrefslogtreecommitdiff
path: root/test/unit/string_t.c
blob: 82c6448e1cae0a2291beb4fb2aedf5114e96db11 (plain)
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
/*
 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU General Public License v.2.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "units.h"
#include "device_mapper/all.h"

#include <stdio.h>
#include <string.h>

#if 0
static int _mem_init(void)
{
	struct dm_pool *mem = dm_pool_create("string test", 1024);
	if (!mem) {
		fprintf(stderr, "out of memory\n");
		exit(1);
	}

	return mem;
}

static void _mem_exit(void *mem)
{
	dm_pool_destroy(mem);
}

/* TODO: Add more string unit tests here */
#endif

static void test_strncpy(void *fixture)
{
	const char st[] = "1234567890";
	char buf[sizeof(st)];

	T_ASSERT_EQUAL(dm_strncpy(buf, st, sizeof(buf)), 1);
	T_ASSERT_EQUAL(strcmp(buf, st), 0);

	T_ASSERT_EQUAL(dm_strncpy(buf, st, sizeof(buf) - 1), 0);
	T_ASSERT_EQUAL(strlen(buf) + 1, sizeof(buf) - 1);
}

static void test_asprint(void *fixture)
{
	const char st0[] = "";
	const char st1[] = "12345678901";
	const char st2[] = "1234567890123456789012345678901234567890123456789012345678901234567";
	char *buf;
	int a;

	a = dm_asprintf(&buf, "%s", st0);
	T_ASSERT_EQUAL(strcmp(buf, st0), 0);
	T_ASSERT_EQUAL(a, sizeof(st0));
	free(buf);

	a = dm_asprintf(&buf, "%s", st1);
	T_ASSERT_EQUAL(strcmp(buf, st1), 0);
	T_ASSERT_EQUAL(a, sizeof(st1));
	free(buf);

	a = dm_asprintf(&buf, "%s", st2);
	T_ASSERT_EQUAL(a, sizeof(st2));
	T_ASSERT_EQUAL(strcmp(buf, st2), 0);
	free(buf);
}

#define T(path, desc, fn) register_test(ts, "/base/data-struct/string/" path, desc, fn)

void string_tests(struct dm_list *all_tests)
{
	struct test_suite *ts = test_suite_create(NULL, NULL);
	if (!ts) {
		fprintf(stderr, "out of memory\n");
		exit(1);
	}

	T("asprint", "tests asprint", test_asprint);
	T("strncpy", "tests string copying", test_strncpy);

	dm_list_add(all_tests, &ts->list);
}