aboutsummaryrefslogtreecommitdiff
path: root/contrib/sendmail/libsm/t-exc.c
blob: e0b45a86d5253d360a77864b6ef0e4f3d3fb9bd5 (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_IDSTR(id, "@(#)$Id: t-exc.c,v 1.21 2013-11-22 20:51:43 ca Exp $")

#include <string.h>
#include <sm/heap.h>
#include <sm/io.h>
#include <sm/test.h>

const SM_EXC_TYPE_T EtypeTest1 =
{
	SmExcTypeMagic,
	"E:test1",
	"i",
	sm_etype_printf,
	"test1 exception argv[0]=%0",
};

const SM_EXC_TYPE_T EtypeTest2 =
{
	SmExcTypeMagic,
	"E:test2",
	"i",
	sm_etype_printf,
	"test2 exception argv[0]=%0",
};

int
main(argc, argv)
	int argc;
	char **argv;
{
	void *p;
	int volatile x;
	char *unknown, *cant;

	sm_test_begin(argc, argv, "test exception handling");

	/*
	**  SM_TRY
	*/

	cant = "can't happen";
	x = 0;
	SM_TRY
		x = 1;
	SM_END_TRY
	SM_TEST(x == 1);

	/*
	**  SM_FINALLY-0
	*/

	x = 0;
	SM_TRY
		x = 1;
	SM_FINALLY
		x = 2;
	SM_END_TRY
	SM_TEST(x == 2);

	/*
	**  SM_FINALLY-1
	*/

	x = 0;
	SM_TRY
		SM_TRY
			x = 1;
			sm_exc_raisenew_x(&EtypeTest1, 17);
		SM_FINALLY
			x = 2;
			sm_exc_raisenew_x(&EtypeTest2, 42);
		SM_END_TRY
	SM_EXCEPT(exc, "E:test2")
		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
			 "got exception test2: can't happen\n");
	SM_EXCEPT(exc, "E:test1")
		SM_TEST(x == 2 && exc->exc_argv[0].v_int == 17);
		if (!(x == 2 && exc->exc_argv[0].v_int == 17))
		{
			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
				"can't happen: x=%d argv[0]=%d\n",
				x, exc->exc_argv[0].v_int);
		}
	SM_EXCEPT(exc, "*")
	{
		unknown = "unknown exception: ";
		SM_TEST(strcmp(unknown, cant) == 0);
	}
	SM_END_TRY

	x = 3;
	SM_TRY
		x = 4;
		sm_exc_raisenew_x(&EtypeTest1, 94);
	SM_FINALLY
		x = 5;
		sm_exc_raisenew_x(&EtypeTest2, 95);
	SM_EXCEPT(exc, "E:test2")
	{
		unknown = "got exception test2: ";
		SM_TEST(strcmp(unknown, cant) == 0);
	}
	SM_EXCEPT(exc, "E:test1")
		SM_TEST(x == 5 && exc->exc_argv[0].v_int == 94);
		if (!(x == 5 && exc->exc_argv[0].v_int == 94))
		{
			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
				"can't happen: x=%d argv[0]=%d\n",
				x, exc->exc_argv[0].v_int);
		}
	SM_EXCEPT(exc, "*")
	{
		unknown = "unknown exception: ";
		SM_TEST(strcmp(unknown, cant) == 0);
	}
	SM_END_TRY

	SM_TRY
		sm_exc_raisenew_x(&SmEtypeErr, "test %d", 0);
	SM_EXCEPT(exc, "*")
#if DEBUG
		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
			"test 0 got an exception, as expected:\n");
		sm_exc_print(exc, smioout);
#endif /* DEBUG */
		return sm_test_end();
	SM_END_TRY

	p = sm_malloc_x((size_t)(-1));
	(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
		"sm_malloc_x unexpectedly succeeded, returning %p\n", p);
	unknown = "sm_malloc_x unexpectedly succeeded";
	SM_TEST(strcmp(unknown, cant) == 0);
	return sm_test_end();
}