This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_foreign.cpp
More file actions
430 lines (360 loc) · 15.4 KB
/
test_foreign.cpp
File metadata and controls
430 lines (360 loc) · 15.4 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* Copyright (C) 2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "gtest/gtest.h"
#include "foundation.h"
#include "foundation-auto.h"
#include <limits>
#include <type_traits>
typedef MCAutoValueRefBase<MCForeignValueRef> MCAutoForeignValueRef;
template<typename T>
void check_creation(MCTypeInfoRef p_type_info, T p_value)
{
MCAutoForeignValueRef t_boxed_value;
EXPECT_TRUE(MCForeignValueCreate(p_type_info, &p_value, &t_boxed_value));
EXPECT_EQ(*static_cast<T *>(MCForeignValueGetContentsPtr(*t_boxed_value)), p_value);
}
template<typename T>
void check_equality(MCTypeInfoRef p_type_info, T p_left, T p_right, bool p_expected)
{
MCAutoForeignValueRef t_left_box, t_right_box;
EXPECT_TRUE(MCForeignValueCreate(p_type_info, &p_left, &t_left_box));
EXPECT_TRUE(MCForeignValueCreate(p_type_info, &p_right, &t_right_box));
EXPECT_EQ(MCValueIsEqualTo(*t_left_box, *t_right_box), p_expected);
}
template<typename T, typename U = T>
void check_hash(MCTypeInfoRef p_type_info, T p_value, hash_t (*p_hash_func)(U p_value))
{
MCAutoForeignValueRef t_boxed_value;
EXPECT_TRUE(MCForeignValueCreate(p_type_info, &p_value, &t_boxed_value));
EXPECT_EQ(MCValueHash(*t_boxed_value), p_hash_func(p_value));
}
template<typename T>
void check_describe(MCTypeInfoRef p_type_info, T p_value, const char *p_format_str)
{
char t_format[256];
sprintf(t_format, p_format_str, p_value);
MCAutoForeignValueRef t_boxed_value;
EXPECT_TRUE(MCForeignValueCreate(p_type_info, &p_value, &t_boxed_value));
MCAutoStringRef t_description;
EXPECT_TRUE(MCValueCopyDescription(*t_boxed_value, &t_description));
if (t_description.IsSet())
EXPECT_STREQ(MCStringGetCString(*t_description), t_format);
}
template<typename T, typename U, typename W = T>
void check_export(MCTypeInfoRef p_type_info, bool (*p_bridge)(W, U&), T p_value)
{
MCAutoValueRefBase<U> t_bridge_value;
EXPECT_TRUE(p_bridge(p_value, &t_bridge_value));
MCAutoForeignValueRef t_boxed_value;
EXPECT_TRUE(MCForeignValueExport(p_type_info, *t_bridge_value, &t_boxed_value));
if (t_boxed_value.IsSet())
EXPECT_EQ(*static_cast<T *>(MCForeignValueGetContentsPtr(*t_boxed_value)), p_value);
}
void check_integral_export_overflow(MCTypeInfoRef p_type_info, MCTypeInfoRef p_err_type_info, int64_t p_value)
{
MCAutoNumberRef t_bridge_value;
EXPECT_TRUE(MCNumberCreateWithReal(p_value, &t_bridge_value));
MCAutoForeignValueRef t_boxed_value;
EXPECT_FALSE(MCForeignValueExport(p_type_info, *t_bridge_value, &t_boxed_value));
if (!t_boxed_value.IsSet())
{
MCAutoErrorRef t_error;
EXPECT_TRUE(MCErrorCatch(&t_error));
EXPECT_EQ(MCValueGetTypeInfo(*t_error), p_err_type_info);
}
}
template<typename T, typename U, typename W = T>
void check_export_raw(MCTypeInfoRef p_type_info, bool (*p_bridge)(W, U&), T p_value)
{
MCAutoValueRefBase<U> t_bridge_value;
EXPECT_TRUE(p_bridge(p_value, &t_bridge_value));
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_type_info);
EXPECT_TRUE(t_desc->doexport != nullptr);
T t_exported_value;
EXPECT_TRUE(t_desc->doexport(t_desc, *t_bridge_value, false, &t_exported_value));
EXPECT_EQ(t_exported_value, p_value);
}
template<typename T>
void check_integral_raw_export_overflow(MCTypeInfoRef p_type_info, MCTypeInfoRef p_err_type_info, int64_t p_value)
{
MCAutoNumberRef t_bridge_value;
EXPECT_TRUE(MCNumberCreateWithReal(p_value, &t_bridge_value));
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_type_info);
EXPECT_TRUE(t_desc->doexport != nullptr);
MCAutoForeignValueRef t_boxed_value;
T t_exported_value = 0;
EXPECT_FALSE(t_desc->doexport(t_desc, *t_bridge_value, false, &t_exported_value));
if (t_exported_value != 0)
{
MCAutoErrorRef t_error;
EXPECT_TRUE(MCErrorCatch(&t_error));
EXPECT_EQ(MCValueGetTypeInfo(*t_error), p_err_type_info);
}
}
template<typename T, typename U, typename W = T>
void check_import_raw(MCTypeInfoRef p_type_info, bool (*p_bridge)(W, U&), T p_value)
{
MCAutoValueRefBase<U> t_bridge_value;
EXPECT_TRUE(p_bridge(p_value, &t_bridge_value));
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_type_info);
EXPECT_TRUE(t_desc->doimport != nullptr);
MCAutoValueRef t_imported_value;
EXPECT_TRUE(t_desc->doimport(t_desc, &p_value, false, &t_imported_value));
if (t_imported_value.IsSet())
EXPECT_TRUE(MCValueIsEqualTo(*t_imported_value, *t_bridge_value));
}
template<typename T>
void check_integral_raw_import_overflow(MCTypeInfoRef p_type_info, MCTypeInfoRef p_err_type_info, T p_value)
{
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(p_type_info);
EXPECT_TRUE(t_desc->doimport != nullptr);
MCAutoValueRef t_imported_value;
EXPECT_FALSE(t_desc->doimport(t_desc, &p_value, false, &t_imported_value));
if (!t_imported_value.IsSet())
{
MCAutoErrorRef t_error;
EXPECT_TRUE(MCErrorCatch(&t_error));
EXPECT_EQ(MCValueGetTypeInfo(*t_error), p_err_type_info);
}
}
/* Bool Type Tests */
static void
test_bool_type(MCTypeInfoRef p_type, const char *p_false, const char *p_true)
{
/* Check Copy */
check_creation<bool>(p_type, false);
check_creation<bool>(p_type, true);
/* Check Equality */
check_equality<bool>(p_type, false, false, true);
check_equality<bool>(p_type, true, true, true);
check_equality<bool>(p_type, false, true, false);
check_equality<bool>(p_type, true, false, false);
/* Check Hash */
check_hash<bool>(p_type, false, MCHashBool);
check_hash<bool>(p_type, true, MCHashBool);
/* Check describe */
check_describe<bool>(p_type, false,p_false);
check_describe<bool>(p_type, true, p_true);
/* Check export (via api) - from Boolean */
check_export<bool>(p_type, MCBooleanCreateWithBool, false);
check_export<bool>(p_type, MCBooleanCreateWithBool, true);
/* Check export (direct) - from Boolean */
check_export_raw<bool>(p_type, MCBooleanCreateWithBool, false);
check_export_raw<bool>(p_type, MCBooleanCreateWithBool, true);
/* Check import (direct) - to Boolean */
check_import_raw<bool>(p_type, MCBooleanCreateWithBool, false);
check_import_raw<bool>(p_type, MCBooleanCreateWithBool, true);
}
TEST(foreign, Bool)
{
test_bool_type(kMCBoolTypeInfo, "<foreign bool false>", "<foreign bool true>");
}
TEST(foreign, CBool)
{
test_bool_type(kMCCBoolTypeInfo, "<foreign c bool false>", "<foreign c bool true>");
}
/* Pointer Type Tests */
TEST(foreign, Pointer)
{
const MCForeignTypeDescriptor *t_desc = MCForeignTypeInfoGetDescriptor(kMCPointerTypeInfo);
/* Check Copy */
check_creation<void*>(kMCPointerTypeInfo, nullptr);
check_creation<void*>(kMCPointerTypeInfo, &kMCPointerTypeInfo);
/* Check Equality */
check_equality<void*>(kMCPointerTypeInfo, nullptr, nullptr, true);
check_equality<void*>(kMCPointerTypeInfo, &kMCPointerTypeInfo, &kMCPointerTypeInfo, true);
check_equality<void*>(kMCPointerTypeInfo, nullptr, &kMCPointerTypeInfo, false);
check_equality<void*>(kMCPointerTypeInfo, &kMCPointerTypeInfo, nullptr, false);
/* Check Hash */
check_hash<void*>(kMCPointerTypeInfo, nullptr, MCHashPointer);
check_hash<void*>(kMCPointerTypeInfo, &kMCPointerTypeInfo, MCHashPointer);
/* Check describe */
check_describe<void*>(kMCPointerTypeInfo, nullptr, "<foreign pointer %p>");
check_describe<void*>(kMCPointerTypeInfo, &kMCPointerTypeInfo, "<foreign pointer %p>");
/* Check initialize */
ASSERT_NE(t_desc->initialize, nullptr);
if (t_desc->initialize != nullptr)
{
void *t_test = &kMCPointerTypeInfo;
ASSERT_TRUE(t_desc->initialize(&t_test));
ASSERT_EQ(t_test, nullptr);
}
/* Check defined */
ASSERT_NE(t_desc->defined, nullptr);
if (t_desc->defined != nullptr)
{
void *t_test_null = nullptr;
ASSERT_FALSE(t_desc->defined(&t_test_null));
ASSERT_TRUE(t_desc->defined(&kMCPointerTypeInfo));
}
}
/* Real Type Tests */
template<typename T, typename U, typename W = T>
void test_numeric(MCTypeInfoRef p_type_info, T x, T y, hash_t (*p_hash)(W value), const char *p_format_str, bool (*p_bridge)(W value, U& r_bridged_value))
{
check_creation<T>(p_type_info, x);
check_creation<T>(p_type_info, y);
/* Check Equality */
check_equality<T>(p_type_info, x, x, true);
check_equality<T>(p_type_info, y, y, true);
check_equality<T>(p_type_info, x, y, false);
check_equality<T>(p_type_info, y, x, false);
/* Check Hash */
check_hash<T>(p_type_info, x, p_hash);
check_hash<T>(p_type_info, y, p_hash);
/* Check describe */
check_describe<T>(p_type_info, x, p_format_str);
check_describe<T>(p_type_info, y, p_format_str);
/* Check export (via api) */
check_export<T>(p_type_info, p_bridge, x);
check_export<T>(p_type_info, p_bridge, y);
/* Check export (direct) */
check_export_raw<T>(p_type_info, p_bridge, x);
check_export_raw<T>(p_type_info, p_bridge, y);
/* Check import (direct) */
check_import_raw<T>(p_type_info, p_bridge, x);
check_import_raw<T>(p_type_info, p_bridge, y);
}
TEST(foreign, Float)
{
test_numeric<float, MCNumberRef, double>(kMCFloatTypeInfo, FLT_MIN, FLT_MAX, MCHashDouble, "<foreign float %g>", MCNumberCreateWithReal);
}
TEST(foreign, Double)
{
test_numeric<double>(kMCDoubleTypeInfo, DBL_MIN, DBL_MAX, MCHashDouble, "<foreign double %lg>", MCNumberCreateWithReal);
}
TEST(foreign, NaturalFloat)
{
#ifdef __32_BIT__
test_numeric<float>(kMCNaturalFloatTypeInfo, FLT_MIN, FLT_MAX, MCHashDouble, "<foreign natural float %lg>", MCNumberCreateWithReal);
#else
test_numeric<double>(kMCNaturalFloatTypeInfo, DBL_MIN, DBL_MAX, MCHashDouble, "<foreign natural float %lg>", MCNumberCreateWithReal);
#endif
}
/* Integral Type Tests */
template<typename T>
hash_t __hash_int(T p_value)
{
if (std::is_signed<T>::value)
return MCHashInt64(p_value);
return MCHashUInt64(p_value);
}
template<typename T>
bool __bridge_int(T p_value, MCNumberRef& r_number)
{
if (sizeof(T) > sizeof(integer_t))
{
return MCNumberCreateWithReal(p_value, r_number);
}
if (std::is_signed<T>::value)
{
return MCNumberCreateWithInteger(p_value, r_number);
}
return MCNumberCreateWithUnsignedInteger(p_value, r_number);
}
#define TEST_INTEGRAL(name, type, format) \
TEST(foreign, name) \
{ \
test_integral<type>(kMC##name##TypeInfo, "<foreign " format ">"); \
}
#define INT_NUMBER_MIN -(1LL << std::numeric_limits<double>::digits)
#define INT_NUMBER_MAX 1LL << std::numeric_limits<double>::digits
template<typename T>
void test_integral(MCTypeInfoRef p_type_info, const char *p_format)
{
bool t_check_export_overflow = false;
bool t_check_import_min_overflow = false;
bool t_check_import_max_overflow = false;
int64_t t_min, t_max;
// For C integer types of less than 64-bits in size, we can represent the
// whole range in MCNumberRefs. For 64-bit size integral types, we can only
// represent the exact integer range of double. Thus we compute the minimum
// and maximum values based on the type size and arity.
if (sizeof(T) <= 4)
{
t_min = std::numeric_limits<T>::min();
t_max = std::numeric_limits<T>::max();
t_check_export_overflow = true;
t_check_import_min_overflow = false;
t_check_import_max_overflow = false;
}
else
{
if (std::is_signed<T>::value)
{
t_min = INT_NUMBER_MIN;
t_check_import_min_overflow = true;
}
else
{
t_min = 0;
t_check_import_min_overflow = false;
}
t_max = INT_NUMBER_MAX;
t_check_import_max_overflow = true;
}
// Check all the base characteristics.
test_numeric<T, MCNumberRef>(p_type_info, t_min, t_max, __hash_int, p_format, __bridge_int);
// If the minimum of the type is greater than the min integer number can
// hold, then check exporting a smaller value throws an error.
if (t_check_export_overflow)
{
check_integral_export_overflow(p_type_info, kMCForeignExportErrorTypeInfo, int64_t(t_min) - 1);
check_integral_raw_export_overflow<T>(p_type_info, kMCForeignExportErrorTypeInfo, int64_t(t_min) - 1);
}
// If the maximum of the type is less than the max integer number can hold,
// then check exporting a larger value throws an error.
if (t_check_export_overflow)
{
check_integral_export_overflow(p_type_info, kMCForeignExportErrorTypeInfo, int64_t(t_max) + 1);
check_integral_raw_export_overflow<T>(p_type_info, kMCForeignExportErrorTypeInfo, int64_t(t_max) + 1);
}
// If the minimum value of the type is less than the minimum integer number
// can hold, check importing a smaller value throws an error.
if (t_check_import_min_overflow)
{
check_integral_raw_import_overflow<T>(p_type_info, kMCForeignImportErrorTypeInfo, t_min - 1);
}
// If the maximum value of the type is greater than the maximum integer
// number can hold, check importing a larger value throws an error.
if (t_check_import_max_overflow)
{
check_integral_raw_import_overflow<T>(p_type_info, kMCForeignImportErrorTypeInfo, t_max + 1);
}
}
TEST_INTEGRAL(UInt8, uint8_t, "8-bit unsigned integer %u")
TEST_INTEGRAL(SInt8, int8_t, "8-bit signed integer %d")
TEST_INTEGRAL(UInt16, uint16_t, "16-bit unsigned integer %u")
TEST_INTEGRAL(SInt16, int16_t, "16-bit signed integer %d")
TEST_INTEGRAL(UInt32, uint32_t, "32-bit unsigned integer %u")
TEST_INTEGRAL(SInt32, int32_t, "32-bit signed integer %d")
TEST_INTEGRAL(UInt64, uint64_t, "64-bit unsigned integer %llu")
TEST_INTEGRAL(SInt64, int64_t, "64-bit signed integer %lld")
TEST_INTEGRAL(UIntSize, size_t, "unsigned size %zu")
TEST_INTEGRAL(SIntSize, ssize_t, "signed size %zd")
TEST_INTEGRAL(UIntPtr, uintptr_t, "unsigned intptr %zu")
TEST_INTEGRAL(SIntPtr, intptr_t, "signed intptr %zd")
TEST_INTEGRAL(NaturalUInt, uintptr_t, "natural unsigned integer %zu")
TEST_INTEGRAL(NaturalSInt, intptr_t, "natural signed integer %zd")
TEST_INTEGRAL(CChar, char, "c char '%c'");
TEST_INTEGRAL(CUChar, unsigned char, "c unsigned char %u")
TEST_INTEGRAL(CSChar, signed char, "c signed char %d")
TEST_INTEGRAL(CUShort, unsigned short, "c unsigned short %u")
TEST_INTEGRAL(CSShort, signed short, "c signed short %d")
TEST_INTEGRAL(CUInt, unsigned int, "c unsigned int %u")
TEST_INTEGRAL(CSInt, signed int, "c signed int %d")
TEST_INTEGRAL(CULong, unsigned long, "c unsigned long %lu")
TEST_INTEGRAL(CSLong, signed long, "c signed long %ld")
TEST_INTEGRAL(CULongLong, unsigned long long, "c unsigned long long %llu")
TEST_INTEGRAL(CSLongLong, signed long long, "c signed long long %lld")
TEST_INTEGRAL(UInt, uinteger_t, "unsigned integer %u")
TEST_INTEGRAL(SInt, integer_t, "signed integer %d")