~ubuntu-branches/ubuntu/vivid/lvm2/vivid

« back to all changes in this revision

Viewing changes to test/unit/string_t.c

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2012-05-01 20:27:50 UTC
  • mto: (3.1.23 sid)
  • mto: This revision was merged to the branch mainline in revision 72.
  • Revision ID: package-import@ubuntu.com-20120501202750-gljjjtblowwq9mw8
Tags: upstream-2.02.95
ImportĀ upstreamĀ versionĀ 2.02.95

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
 
3
 *
 
4
 * This file is part of LVM2.
 
5
 *
 
6
 * This copyrighted material is made available to anyone wishing to use,
 
7
 * modify, copy, or redistribute it subject to the terms and conditions
 
8
 * of the GNU General Public License v.2.
 
9
 *
 
10
 * You should have received a copy of the GNU General Public License
 
11
 * along with this program; if not, write to the Free Software Foundation,
 
12
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
13
 */
 
14
 
 
15
#include "libdevmapper.h"
 
16
 
 
17
#include <stdio.h>
 
18
#include <string.h>
 
19
 
 
20
#include <CUnit/CUnit.h>
 
21
 
 
22
int string_init(void);
 
23
int string_fini(void);
 
24
 
 
25
static struct dm_pool *mem = NULL;
 
26
 
 
27
int string_init(void)
 
28
{
 
29
        mem = dm_pool_create("string test", 1024);
 
30
 
 
31
        return (mem == NULL);
 
32
}
 
33
 
 
34
int string_fini(void)
 
35
{
 
36
        dm_pool_destroy(mem);
 
37
 
 
38
        return 0;
 
39
}
 
40
 
 
41
/* TODO: Add more string unit tests here */
 
42
 
 
43
static void test_strncpy(void)
 
44
{
 
45
        const char st[] = "1234567890";
 
46
        char buf[sizeof(st)];
 
47
 
 
48
        CU_ASSERT_EQUAL(dm_strncpy(buf, st, sizeof(buf)), 1);
 
49
        CU_ASSERT_EQUAL(strcmp(buf, st), 0);
 
50
 
 
51
        CU_ASSERT_EQUAL(dm_strncpy(buf, st, sizeof(buf) - 1), 0);
 
52
        CU_ASSERT_EQUAL(strlen(buf) + 1, sizeof(buf) - 1);
 
53
}
 
54
 
 
55
static void test_asprint(void)
 
56
{
 
57
        const char st0[] = "";
 
58
        const char st1[] = "12345678901";
 
59
        const char st2[] = "1234567890123456789012345678901234567890123456789012345678901234567";
 
60
        char *buf;
 
61
        int a;
 
62
 
 
63
        a = dm_asprintf(&buf, "%s", st0);
 
64
        CU_ASSERT_EQUAL(strcmp(buf, st0), 0);
 
65
        CU_ASSERT_EQUAL(a, sizeof(st0));
 
66
        free(buf);
 
67
 
 
68
        a = dm_asprintf(&buf, "%s", st1);
 
69
        CU_ASSERT_EQUAL(strcmp(buf, st1), 0);
 
70
        CU_ASSERT_EQUAL(a, sizeof(st1));
 
71
        free(buf);
 
72
 
 
73
        a = dm_asprintf(&buf, "%s", st2);
 
74
        CU_ASSERT_EQUAL(a, sizeof(st2));
 
75
        CU_ASSERT_EQUAL(strcmp(buf, st2), 0);
 
76
        free(buf);
 
77
}
 
78
 
 
79
CU_TestInfo string_list[] = {
 
80
        { (char*)"asprint", test_asprint },
 
81
        { (char*)"strncpy", test_strncpy },
 
82
        CU_TEST_INFO_NULL
 
83
};