File: isValid.c

package info (click to toggle)
swirc 3.5.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,988 kB
  • sloc: ansic: 18,321; cpp: 17,795; sh: 968; python: 254; makefile: 104; javascript: 20
file content (110 lines) | stat: -rw-r--r-- 2,182 bytes parent folder | download | duplicates (2)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "common.h"

#include <setjmp.h>
#ifdef UNIT_TESTING
#undef UNIT_TESTING
#endif
#include <cmocka.h>

#include "dataClassify.h"

#ifndef HAVE_ETEXT_SEGMENT
#pragma message("Etext segment not present")
#endif

#define yesno(_b) ((_b) ? "yes" : "no")

int main(void);

int global;

static bool bval;

static void
isValid_ptrToLocal(void **state)
{
	int local;

	print_message("pointer to local var valid? %s\n",
	    yesno(isValid(&local)));
	UNUSED_PARAM(state);
}

static void
isValid_ptrToGlobal(void **state)
{
	print_message("pointer to global var valid? %s\n",
	    yesno(isValid(&global)));
	UNUSED_PARAM(state);
}

static void
isValid_ptrToFn(void **state)
{
	print_message("pointer to function valid? %s\n",
	    yesno(isValid((void *)main)));
	UNUSED_PARAM(state);
}

static void
isValid_ptrToHeap(void **state)
{
	int *ip = malloc(sizeof *ip);

	print_message("pointer to heap valid? %s\n", yesno(isValid(ip)));
	print_message("pointer to end of allocated heap valid? %s\n",
	    yesno(isValid(++ip)));
	free(--ip);
	print_message("pointer to freed heap valid? %s\n", yesno(isValid(ip)));
	UNUSED_PARAM(state);
}

static void
isValid_nullPtr(void **state)
{
	bval = isValid(NULL);
	print_message("null pointer valid? %s\n", yesno(bval));
	assert_false(bval);
	UNUSED_PARAM(state);
}

static void
isValid_unusualLoc(void **state)
{
	bval = isValid((void *)6);
	print_message("unusual location valid? %s\n", yesno(bval));
	UNUSED_PARAM(state);
}

static void
isValid_emptyStr(void **state)
{
	bval = isValid("");
	print_message("empty string valid? %s\n", yesno(bval));
	UNUSED_PARAM(state);
}

static void
isValid_fixedStr(void **state)
{
	bval = isValid("foo");
	print_message("fixed string valid? %s\n", yesno(bval));
	UNUSED_PARAM(state);
}

int
main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(isValid_ptrToLocal),
		cmocka_unit_test(isValid_ptrToGlobal),
		cmocka_unit_test(isValid_ptrToFn),
		cmocka_unit_test(isValid_ptrToHeap),
		cmocka_unit_test(isValid_nullPtr),
		cmocka_unit_test(isValid_unusualLoc),
		cmocka_unit_test(isValid_emptyStr),
		cmocka_unit_test(isValid_fixedStr),
	};

	return cmocka_run_group_tests(tests, NULL, NULL);
}