File: CompileTest.cpp

package info (click to toggle)
safeint 3.0.28a%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,668 kB
  • sloc: cpp: 24,880; ansic: 3,183; makefile: 53
file content (292 lines) | stat: -rw-r--r-- 4,632 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#if !defined __GNUC__
#pragma warning( disable: 4571 4820 4514 4987 4710 4309 4986 4548 4189 4866)
// relative include path contains '..'
// (function) selected for automatic inline expansion
#pragma warning( disable: 4464 4711 )
#else
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif

#include "../SafeInt.hpp"


void ConstExpr();

enum class Foo
{
	Foo1 = 0,
	Foo2
};

template <typename T, typename U>
void ModulusFriendTest()
{
	U u = 3;
	SafeInt<T> st(1);
	T t = u % st;
}

template <typename T, typename U>
void CompiledMixedType()
{
	// Mixed constructors
	U u = 0;
	T t = 0;
	bool b = false;
	SafeInt<T> st(u);
	SafeInt<U> su(u);
	SafeInt<T> st2(su);

	// Shut up the compiler
	b = !b;

	// Assignment
	st = t;
	st = u;
	st = su;
	st = st2;

	// Casting
	U u2 = (U)st;

	// Multiplication
	t = st * u;
	t = st * st2;
	t = u * st;
	st *= u;
	st *= su;

	// Modulus, modulus assignment
	u = 1;
	st = 1;
	su = 1;
	// For some reason, this is annoying the VS17 link time code generation
//	t = u % st;

	t = st % u;
	st2 = 1;
	t = st % st2;
	st %= u;
	st %= su;

	// Division
	u = 1;
	st2 = 1;
	st = 1;
	t = st / u;
	t = st / st2;
	t = u / st;
	st /= u;
	st /= su;

	// Addition
	t = st + u;
	t = st + st2;
	t = u + st;
	st += u;
	st += su;

	// Subtraction
	st = 0;
	u = 0;
	st2 = 0;
	t = st - u;
	t = st - st2;
	t = u - st;
	st -= u;

	st = 1;
	su = 1;
	st -= su;

	// Shift operators
	u = 1;
	su = 1;
	// Left
	t = st << u;
	t = st << su;
	t = t << su;

	st <<= u;
	st <<= su;

	// Right
	t = st >> u;
	t = st >> su;
	t = t >> su;

	st >>= u;
	st >>= su;

	// Binary operations
	// And
	t = st & st2;
	t = st & u;
	t = t & su;

	st &= st2;
	st &= u;
	st &= su;

	// Or
	t = st | st2;
	t = st | u;
	t = t | su;

	st |= st2;
	st |= u;
	st |= su;

	// Xor
	t = st ^ st2;
	t = st ^ u;
	t = t ^ su;

	st ^= st2;
	st ^= u;
	st ^= su;

	// Comparisons
	// Less than
	b = st < su;
	b = st < u;
	b = u < st;
	b = st < su;

	// Less than or equal
	b = st <= su;
	b = st <= u;
	b = u <= st;
	b = st <= su;

	// Greater than
	b = st > su;
	b = st > u;
	b = u > st;
	b = st > su;

	// Greater than or equal
	b = st >= su;
	b = st >= u;
	b = u >= st;
	b = st >= su;

	// Equals
	b = st == su;
	b = st == u;
	b = u == st;
	b = st == su;
}

template <typename T>
void CompileSigned()
{
	SafeInt<T> st;
	T t = -st;
}

template <typename T>
void CompileType()
{
	// Test constructors
	SafeInt<T> s;
	SafeInt<T> i(Foo::Foo1);
	T t = 0;
	SafeInt<T> st(t);
	SafeInt<T> sb(false);

	// Mixed operations
	CompiledMixedType<T, char>();
	CompiledMixedType<T, signed char>();
	CompiledMixedType<T, unsigned char>();
	CompiledMixedType<T, short>();
	CompiledMixedType<T, unsigned short>();
	CompiledMixedType<T, int>();
	CompiledMixedType<T, unsigned int>();
	CompiledMixedType<T, long>();
	CompiledMixedType<T, unsigned long>();
	CompiledMixedType<T, long long>();
	CompiledMixedType<T, unsigned long long>();

	// This is making trouble for the compiler
	ModulusFriendTest<T, char>();
	ModulusFriendTest<T, signed char>();
	ModulusFriendTest<T, unsigned char>();
	ModulusFriendTest<T, short>();
	ModulusFriendTest<T, unsigned short>();
	ModulusFriendTest<T, int>();
	ModulusFriendTest<T, unsigned int>();
	ModulusFriendTest<T, long>();
	ModulusFriendTest<T, unsigned long>();
	ModulusFriendTest<T, long long>();
	ModulusFriendTest<T, unsigned long long>();
	
	// Special case casts
	bool b = (bool)s;
	wchar_t w = (wchar_t)s;
	size_t size = (size_t)s;
	float f = (float)s;
	double d = (double)s;
	long double ld = (long double)s;

	// Pointer operations
	T* p1 = s.Ptr();
	const T* p2 = s.Ptr();
	const T& r = s.Ref();
	T* p3 = &s;
	const T* p4 = &s;

	// Unary operators
	bool b2 = !st;
	t = +st;

	st++;
	++st;
	st--;
	--st;
	t = ~st;

	// Get compile time coverage of negation
	T x = 1;
	T result;

	(void)SafeNegation(x, result);

}

void CompileMe()
{
	CompileType<char>();
	CompileType<unsigned char>();
	CompileType<signed char>();

	CompileType<unsigned short>();
	CompileType<signed short>();
	CompileType<unsigned int>();
	CompileType<signed int>();
	CompileType<unsigned long>();
	CompileType<signed long>();
	CompileType<unsigned long long>();
	CompileType<signed long long>();

	CompileSigned<signed char>();
	CompileSigned<signed short>();
	CompileSigned<signed int>();
	CompileSigned<signed long>();
	CompileSigned<signed long long>();
}

/*
	Need to add tests for the miscellaneous helper functions
	Min, Max, Align, Swap
	Also need the pointer functions
*/
int main(int, char**)
{
	CompileMe();
	void ConstExpr();
	return 0;
}