File: sw_strcpy.c

package info (click to toggle)
swirc 3.5.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,044 kB
  • sloc: cpp: 18,690; ansic: 18,414; sh: 1,011; python: 254; makefile: 108; javascript: 20
file content (78 lines) | stat: -rw-r--r-- 1,697 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
#include "common.h"

#include <setjmp.h>
#include <cmocka.h>

#include "strHand.h"

static char	buf[12] = { '\0' };
static int	ret = 0;

static void
handlesInvalidArguments_test1(void **state)
{
	ret = sw_strcpy(NULL, "some string", ARRAY_SIZE(buf));
	assert_int_equal(ret, EINVAL);
	assert_string_equal(buf, "");
	UNUSED_PARAM(state);
}

static void
handlesInvalidArguments_test2(void **state)
{
	ret = sw_strcpy(buf, NULL, ARRAY_SIZE(buf));
	assert_int_equal(ret, EINVAL);
	assert_string_equal(buf, "");
	UNUSED_PARAM(state);
}

static void
handlesInvalidArguments_test3(void **state)
{
	ret = sw_strcpy(buf, "some string", 0);
	assert_int_equal(ret, EINVAL);
	assert_string_equal(buf, "");
	UNUSED_PARAM(state);
}

static void
doesBoundsChecking_test1(void **state)
{
	ret = sw_strcpy(buf, "some string!", ARRAY_SIZE(buf));
	assert_int_equal(ret, ERANGE);
	assert_string_equal(buf, "");
	UNUSED_PARAM(state);
}

static void
doesBoundsChecking_test2(void **state)
{
	ret = sw_strcpy(buf, "some string...", ARRAY_SIZE(buf));
	assert_int_equal(ret, ERANGE);
	assert_string_equal(buf, "");
	UNUSED_PARAM(state);
}

static void
canCopy_test1(void **state)
{
	ret = sw_strcpy(buf, "some string", ARRAY_SIZE(buf));
	assert_int_equal(ret, 0);
	assert_string_equal(buf, "some string");
	UNUSED_PARAM(state);
}

int
main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(handlesInvalidArguments_test1),
		cmocka_unit_test(handlesInvalidArguments_test2),
		cmocka_unit_test(handlesInvalidArguments_test3),
		cmocka_unit_test(doesBoundsChecking_test1),
		cmocka_unit_test(doesBoundsChecking_test2),
		cmocka_unit_test(canCopy_test1),
	};

	return cmocka_run_group_tests(tests, NULL, NULL);
}