File: string_t.c

package info (click to toggle)
lvm2 2.02.111-2.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,856 kB
  • sloc: ansic: 101,481; sh: 15,369; makefile: 1,843; python: 777; ruby: 332; awk: 20
file content (83 lines) | stat: -rw-r--r-- 1,871 bytes parent folder | download | duplicates (3)
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
/*
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "libdevmapper.h"

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

#include <CUnit/CUnit.h>

int string_init(void);
int string_fini(void);

static struct dm_pool *mem = NULL;

int string_init(void)
{
	mem = dm_pool_create("string test", 1024);

	return (mem == NULL);
}

int string_fini(void)
{
	dm_pool_destroy(mem);

	return 0;
}

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

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

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

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

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

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

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

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

CU_TestInfo string_list[] = {
	{ (char*)"asprint", test_asprint },
	{ (char*)"strncpy", test_strncpy },
	CU_TEST_INFO_NULL
};