summaryrefslogtreecommitdiff
path: root/3rdparty/plibsys/tests/pstdarg_test.cpp
blob: 7df041fb0397065d21e679cf740e2883d762f3c1 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * The MIT License
 *
 * Copyright (C) 2017 Jean-Damien Durand <jeandamiendurand@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "plibsys.h"
#include "ptestmacros.h"

#if defined(P_CC_WATCOM)
#  pragma disable_message (7)
#endif

P_TEST_MODULE_INIT ();

static void variadic_function (pint unused, ...);
static void variadic_function_copy_all (pint unused, p_va_list ap);
static void variadic_function_copy_trail (pint unused, p_va_list ap);

/* The "runtime" thingies here are just to avoid compiler warnings */
/*                                                 In head,          In trail */
static const pint8    pint8_var[2]    = {        P_MININT8,         P_MAXINT8 };
static const puint8   puint8_var[2]   = {                0,        P_MAXUINT8 };
static const pint16   pint16_var[2]   = {       P_MININT16,        P_MAXINT16 };
static const puint16  puint16_var[2]  = {                0,       P_MAXUINT16 };
static       pint32   pint32_var[2]   = {  0 /* runtime */,   0 /* runtime */ };
static       puint32  puint32_var[2]  = {                0,   0 /* runtime */ };
static       pint64   pint64_var[2]   = {  0 /* runtime */,   0 /* runtime */ };
static       puint64  puint64_var[2]  = {                0,   0 /* runtime */ };
static const pint     pint_var[2]     = {         P_MININT,          P_MAXINT };
static const puint    puint_var[2]    = {                0,         P_MAXUINT };
static const pshort   pshort_var[2]   = {       P_MINSHORT,        P_MAXSHORT };
static const plong    plong_var[2]    = {        P_MINLONG,         P_MAXLONG };
static const pchar    pchar_var[2]    = {             '\0',               'z' };
static const ppointer ppointer_var[2] = {             NULL, (ppointer) p_libsys_init };
static const pfloat   pfloat_var[2]   = {          -1.234f,            1.234f };
static const pdouble  pdouble_var[2]  = {           -1.567,             1.567 };

/* Macros are used for testing because these tests MUST play with the CURRENT stack. */

#define P_TEST_VA_ARG(ap, type, wantedvalue) do {		\
	P_DEBUG ("Unstacking a " #type);			\
	P_TEST_CHECK (type##_va_arg(ap) == wantedvalue);	\
  } while (0)

#define P_TEST_VA_ARG_HEAD(ap) do {				\
	P_TEST_VA_ARG(ap, pint8, pint8_var[0]);			\
	P_TEST_VA_ARG(ap, puint8, puint8_var[0]);		\
	P_TEST_VA_ARG(ap, pint16, pint16_var[0]);		\
	P_TEST_VA_ARG(ap, puint16, puint16_var[0]);		\
	P_TEST_VA_ARG(ap, pint32, pint32_var[0]);		\
	P_TEST_VA_ARG(ap, puint32, puint32_var[0]);		\
	P_TEST_VA_ARG(ap, pint64, pint64_var[0]);		\
	P_TEST_VA_ARG(ap, puint64, puint64_var[0]);		\
	P_TEST_VA_ARG(ap, pint, pint_var[0]);			\
	P_TEST_VA_ARG(ap, puint, puint_var[0]);			\
	P_TEST_VA_ARG(ap, pshort, pshort_var[0]);		\
	P_TEST_VA_ARG(ap, plong, plong_var[0]);			\
	P_TEST_VA_ARG(ap, pchar, pchar_var[0]);			\
	P_TEST_VA_ARG(ap, ppointer, ppointer_var[0]);		\
	P_TEST_VA_ARG(ap, pfloat, pfloat_var[0]);		\
	P_TEST_VA_ARG(ap, pdouble, pdouble_var[0]);		\
  } while (0)

#define P_TEST_VA_ARG_TRAIL(ap) do {				\
	P_TEST_VA_ARG(ap, pdouble, pdouble_var[1]);		\
	P_TEST_VA_ARG(ap, pfloat, pfloat_var[1]);		\
	P_TEST_VA_ARG(ap, ppointer, ppointer_var[1]);		\
	P_TEST_VA_ARG(ap, pchar, pchar_var[1]);			\
	P_TEST_VA_ARG(ap, plong, plong_var[1]);			\
	P_TEST_VA_ARG(ap, pshort, pshort_var[1]);		\
	P_TEST_VA_ARG(ap, puint, puint_var[1]);			\
	P_TEST_VA_ARG(ap, pint, pint_var[1]);			\
	P_TEST_VA_ARG(ap, puint64, puint64_var[1]);		\
	P_TEST_VA_ARG(ap, pint64, pint64_var[1]);		\
	P_TEST_VA_ARG(ap, puint32, puint32_var[1]);		\
	P_TEST_VA_ARG(ap, pint32, pint32_var[1]);		\
	P_TEST_VA_ARG(ap, puint16, puint16_var[1]);		\
	P_TEST_VA_ARG(ap, pint16, pint16_var[1]);		\
	P_TEST_VA_ARG(ap, puint8, puint8_var[1]);		\
	P_TEST_VA_ARG(ap, pint8, pint8_var[1]);			\
  } while (0)

static void variadic_function_copy_all (pint unused, p_va_list ap)
{
	P_UNUSED (unused);
	P_DEBUG ("Unstacking a copy of all the arguments");
	P_TEST_VA_ARG_HEAD (ap);
	P_TEST_VA_ARG_TRAIL (ap);
}

static void variadic_function_copy_trail (pint unused, p_va_list ap)
{
	P_UNUSED (unused);
	P_DEBUG ("Unstacking second part of the arguments");
	P_TEST_VA_ARG_TRAIL (ap);
}

static void variadic_function (pint unused, ...)
{
	p_va_list ap;
	p_va_list ap2;

	p_va_start (ap, unused);

	P_DEBUG ("Copy of arguments");
	p_va_copy (ap2, ap);
	variadic_function_copy_all (unused, ap2);
	p_va_end (ap2);

	P_DEBUG ("Unstacking first part of arguments");
	P_TEST_VA_ARG_HEAD (ap);

	P_DEBUG ("Copy of arguments at current unstack state, i.e. in the middle");
	p_va_copy (ap2, ap);
	variadic_function_copy_trail (unused, ap2);
	p_va_end (ap2);

	P_DEBUG ("Unstacking second part of arguments");
	P_TEST_VA_ARG_TRAIL (ap);

	p_va_end (ap);
}

P_TEST_CASE_BEGIN (pstdarg_general_test)
{
	p_libsys_init ();

	pint32_var[0] = P_MININT16;
	pint32_var[0] <<= 16;
	pint32_var[1] = P_MAXINT16;
	pint32_var[1] <<= 16;
	puint32_var[1] = P_MAXUINT16;
	puint32_var[1] <<= 16;

	pint64_var[0] = pint32_var[0];
	pint64_var[0] <<= 32;
	pint64_var[1] = pint32_var[1];
	pint64_var[1] <<= 32;
	puint64_var[1] = puint32_var[1];
	puint64_var[1] <<= 32;

	variadic_function (0,
			   pint8_var[0],
			   puint8_var[0],
			   pint16_var[0],
			   puint16_var[0],
			   pint32_var[0],
			   puint32_var[0],
			   pint64_var[0],
			   puint64_var[0],
			   pint_var[0],
			   puint_var[0],
			   pshort_var[0],
			   plong_var[0],
			   pchar_var[0],
			   ppointer_var[0],
			   pfloat_var[0],
			   pdouble_var[0],
			   /* Cut is here when testing p_va_copy, we stack in reverse order */
			   pdouble_var[1],
			   pfloat_var[1],
			   ppointer_var[1],
			   pchar_var[1],
			   plong_var[1],
			   pshort_var[1],
			   puint_var[1],
			   pint_var[1],
			   puint64_var[1],
			   pint64_var[1],
			   puint32_var[1],
			   pint32_var[1],
			   puint16_var[1],
			   pint16_var[1],
			   puint8_var[1],
			   pint8_var[1]);

	p_libsys_shutdown ();
}
P_TEST_CASE_END ()

P_TEST_SUITE_BEGIN()
{
	P_TEST_SUITE_RUN_CASE (pstdarg_general_test);
}
P_TEST_SUITE_END()