Debian Codegraph @

mypyc/lib-rt/test_capi.cc

raw
1 // Test cases
2
3 #include <Python.h>
4 #include "CPy.h"
5 #include <gtest/gtest.h>
6
7 static PyObject *moduleDict;
8
9 static PyObject *int_from_str(const char *str) {
10 return PyLong_FromString(str, 0, 10);
11 }
12
13 static std::string str_from_object(PyObject *x) {
14 PyObject *str = PyObject_Str(x);
15 const char *utf8 = PyUnicode_AsUTF8(str);
16 return std::string(utf8);
17 }
18
19 static std::string str_from_int(CPyTagged x) {
20 return str_from_object(CPyTagged_AsObject(x));
21 }
22
23 static bool is_py_equal(PyObject *x, PyObject *y) {
24 int result = PyObject_RichCompareBool(x, y, Py_EQ);
25 if (result < 0) {
26 std::cout << "ERROR: Rich compare failed";
27 }
28 return result == 1;
29 }
30
31 static bool is_int_equal(CPyTagged x, CPyTagged y) {
32 if (CPyTagged_CheckShort(x)) {
33 return x == y;
34 } else if (CPyTagged_CheckShort(y)) {
35 return false;
36 } else {
37 return is_py_equal(CPyTagged_LongAsObject(x), CPyTagged_LongAsObject(y));
38 }
39 }
40
41 static void fail(std::string message) {
42 std::cerr << message << "\n";
43 exit(1);
44 }
45
46 static PyObject *eval(std::string expr) {
47 PyObject *dict = PyDict_New();
48 auto result = PyRun_String(expr.c_str(), Py_eval_input, moduleDict, dict);
49 Py_DECREF(dict);
50 if (result == 0) {
51 fail("Python exception");
52 }
53 return result;
54 }
55
56 static CPyTagged eval_int(std::string expr) {
57 auto o = eval(expr);
58 EXPECT_TRUE(PyLong_Check(o));
59 return CPyTagged_FromObject(o);
60 }
61
62 static PyObject *empty_list() {
63 PyObject *list = PyList_New(0);
64 EXPECT_TRUE(list);
65 return list;
66 }
67
68 static void list_append(PyObject *list, std::string expr) {
69 PyObject *obj = eval(expr);
70 int result = PyList_Append(list, obj);
71 EXPECT_TRUE(result == 0);
72 }
73
74 class CAPITest : public ::testing::Test {
75 protected:
76 PyObject *max_short;
77 PyObject *min_short;
78 PyObject *min_pos_long;
79 PyObject *max_neg_long;
80 Py_ssize_t c_max_short;
81 Py_ssize_t c_min_short;
82 Py_ssize_t c_min_pos_long;
83 Py_ssize_t c_max_neg_long;
84
85 virtual void SetUp() {
86 if (!moduleDict) {
87 fail("Could not find module dictionary");
88 }
89
90 c_max_short = CPY_TAGGED_MAX; // 2**62-1
91 c_min_pos_long = c_max_short + 1; // 2**62
92 c_min_short = CPY_TAGGED_MIN; // -2**62
93 c_max_neg_long = c_min_short - 1; // -(2**62+1)
94 max_short = PyLong_FromSsize_t(c_max_short);
95 min_pos_long = PyLong_FromSsize_t(c_min_pos_long);
96 min_short = PyLong_FromSsize_t(c_min_short);
97 max_neg_long = PyLong_FromSsize_t(c_max_neg_long);
98 }
99
100 virtual void TearDown() {
101 Py_DECREF(max_short);
102 Py_DECREF(min_pos_long);
103 Py_DECREF(min_short);
104 Py_DECREF(max_neg_long);
105 }
106 };
107
108 TEST_F(CAPITest, test_cint_conversions) {
109 EXPECT_EQ(CPyTagged_ShortFromInt(0), 0);
110 EXPECT_EQ(CPyTagged_ShortFromInt(3), 6);
111 EXPECT_EQ(CPyTagged_ShortFromInt(-5), -10);
112 EXPECT_EQ(CPyTagged_ShortAsSsize_t(0), 0);
113 EXPECT_EQ(CPyTagged_ShortAsSsize_t(6), 3);
114 EXPECT_EQ(CPyTagged_ShortAsSsize_t(-10), -5);
115 }
116
117 TEST_F(CAPITest, test_is_long_int) {
118 EXPECT_TRUE(CPyTagged_CheckLong(1));
119 EXPECT_TRUE(CPyTagged_CheckLong(15));
120 EXPECT_FALSE(CPyTagged_CheckLong(0));
121 EXPECT_FALSE(CPyTagged_CheckLong(6));
122 EXPECT_FALSE(CPyTagged_CheckLong(-4));
123 }
124
125 TEST_F(CAPITest, test_is_short_int) {
126 EXPECT_FALSE(CPyTagged_CheckShort(1));
127 EXPECT_FALSE(CPyTagged_CheckShort(15));
128 EXPECT_TRUE(CPyTagged_CheckShort(0));
129 EXPECT_TRUE(CPyTagged_CheckShort(6));
130 EXPECT_TRUE(CPyTagged_CheckShort(-4));
131 }
132
133 TEST_F(CAPITest, test_obj_to_short_int) {
134 EXPECT_EQ(CPyTagged_FromObject(int_from_str("0")), CPyTagged_ShortFromInt(0));
135 EXPECT_EQ(CPyTagged_FromObject(int_from_str("1234")), CPyTagged_ShortFromInt(1234));
136 EXPECT_EQ(CPyTagged_FromObject(int_from_str("-1234")), CPyTagged_ShortFromInt(-1234));
137
138 EXPECT_EQ(CPyTagged_FromObject(max_short), CPyTagged_ShortFromSsize_t(c_max_short));
139 EXPECT_EQ(CPyTagged_FromObject(min_short), CPyTagged_ShortFromSsize_t(c_min_short));
140 }
141
142 TEST_F(CAPITest, test_obj_to_long_int) {
143 // A value larger than 2**64
144 PyObject *large = int_from_str("18464758493694263305");
145 PyObject *small = int_from_str("-18464758493694263305");
146 CPyTagged x;
147
148 x = CPyTagged_FromObject(large);
149 ASSERT_TRUE(CPyTagged_CheckLong(x));
150 EXPECT_TRUE(is_py_equal(large, CPyTagged_LongAsObject(x)));
151
152 x = CPyTagged_FromObject(small);
153 ASSERT_TRUE(CPyTagged_CheckLong(x));
154 EXPECT_TRUE(is_py_equal(small, CPyTagged_LongAsObject(x)));
155
156 x = CPyTagged_FromObject(min_pos_long);
157 ASSERT_TRUE(CPyTagged_CheckLong(x));
158 EXPECT_TRUE(is_py_equal(min_pos_long, CPyTagged_LongAsObject(x)));
159
160 x = CPyTagged_FromObject(max_neg_long);
161 ASSERT_TRUE(CPyTagged_CheckLong(x));
162 EXPECT_TRUE(is_py_equal(max_neg_long, CPyTagged_LongAsObject(x)));
163 }
164
165 TEST_F(CAPITest, test_short_int_to_obj) {
166 EXPECT_TRUE(is_py_equal(CPyTagged_AsObject(CPyTagged_ShortFromInt(0)), int_from_str("0")));
167 EXPECT_TRUE(is_py_equal(CPyTagged_AsObject(CPyTagged_ShortFromInt(1234)),
168 int_from_str("1234")));
169 EXPECT_TRUE(is_py_equal(CPyTagged_AsObject(CPyTagged_ShortFromInt(-1234)),
170 int_from_str("-1234")));
171 EXPECT_TRUE(is_py_equal(CPyTagged_AsObject(CPyTagged_ShortFromSsize_t(c_max_short)),
172 max_short));
173 EXPECT_TRUE(is_py_equal(CPyTagged_AsObject(CPyTagged_ShortFromSsize_t(c_min_short)),
174 min_short));
175 }
176
177 TEST_F(CAPITest, test_long_int_to_obj) {
178 // A value larger than 2**64
179 PyObject *large = int_from_str("18464758493694263305");
180 PyObject *small = int_from_str("-18464758493694263305");
181 PyObject *x;
182
183 x = CPyTagged_AsObject(CPyTagged_FromObject(large));
184 EXPECT_TRUE(is_py_equal(large, x));
185 x = CPyTagged_AsObject(CPyTagged_FromObject(small));
186 EXPECT_TRUE(is_py_equal(small, x));
187 x = CPyTagged_AsObject(CPyTagged_FromObject(min_pos_long));
188 EXPECT_TRUE(is_py_equal(min_pos_long, x));
189 x = CPyTagged_AsObject(CPyTagged_FromObject(max_neg_long));
190 EXPECT_TRUE(is_py_equal(max_neg_long, x));
191 }
192
193 #define EXPECT_INT_EQUAL(x, y) \
194 do { \
195 if (!is_int_equal(x, y)) \
196 std::cout << "Failure: " << str_from_int(x) << " != " << str_from_int(y) << \
197 "\n"; \
198 EXPECT_TRUE(is_int_equal(x, y)); \
199 } while (false)
200
201 #define ASSERT_ADD(x, y, result) \
202 EXPECT_TRUE(is_int_equal(CPyTagged_Add(eval_int(x), eval_int(y)), eval_int(result)))
203
204 TEST_F(CAPITest, test_add_short_int) {
205 ASSERT_ADD("13", "8", "21");
206 ASSERT_ADD("-13", "8", "-5");
207 ASSERT_ADD("13", "-7", "6");
208 ASSERT_ADD("13", "-14", "-1");
209 ASSERT_ADD("-3", "-5", "-8");
210 }
211
212 TEST_F(CAPITest, test_add_long_ints) {
213 ASSERT_ADD("2**64", "2**65", "2**64 + 2**65");
214 ASSERT_ADD("2**64", "-2**65", "2**64 - 2**65");
215 }
216
217 TEST_F(CAPITest, test_add_long_and_short) {
218 ASSERT_ADD("1", "2**65", "1 + 2**65");
219 ASSERT_ADD("2**65", "1", "1 + 2**65");
220 }
221
222 TEST_F(CAPITest, test_add_short_overflow) {
223 // Overfloat
224 ASSERT_ADD("2**62 - 1", "1", "2**62");
225 ASSERT_ADD("-2**62", "-1", "-2**62 - 1");
226 }
227
228 TEST_F(CAPITest, test_add_short_edge_cases) {
229 // Close but not quite overflow
230 ASSERT_ADD("2**62 - 2", "1", "2**62 - 1");
231 ASSERT_ADD("-2**62 + 1", "-1", "-2**62");
232 // Max magnitudes
233 ASSERT_ADD("2**62 - 1", "2**62 - 1", "2**63 - 2");
234 ASSERT_ADD("2**62 - 1", "-2**62", "-1");
235 }
236
237 #define ASSERT_SUBTRACT(x, y, result) \
238 EXPECT_TRUE(is_int_equal(CPyTagged_Subtract(eval_int(x), eval_int(y)), eval_int(result)))
239
240 TEST_F(CAPITest, test_subtract_short_int) {
241 ASSERT_SUBTRACT("13", "8", "5");
242 ASSERT_SUBTRACT("8", "13", "-5");
243 ASSERT_SUBTRACT("-13", "8", "-21");
244 ASSERT_SUBTRACT("13", "-7", "20");
245 ASSERT_SUBTRACT("-3", "-5", "2");
246 }
247
248 TEST_F(CAPITest, test_subtract_long_int) {
249 ASSERT_SUBTRACT("2**65", "2**64", "2**65 - 2**64");
250 ASSERT_SUBTRACT("2**65", "-2**64", "2**65 + 2**64");
251 }
252
253 TEST_F(CAPITest, test_subtract_long_and_short) {
254 ASSERT_SUBTRACT("1", "2**65", "1 - 2**65");
255 ASSERT_SUBTRACT("2**65", "1", "2**65 - 1");
256 }
257
258 TEST_F(CAPITest, test_subtract_short_overflow) {
259 ASSERT_SUBTRACT("2**62-1", "-1", "2**62");
260 ASSERT_SUBTRACT("-2**62", "1", "-2**62 - 1");
261 ASSERT_SUBTRACT("0", "-2**62", "2**62");
262 ASSERT_SUBTRACT("1", "-2**62 + 1", "2**62");
263 ASSERT_SUBTRACT("-2", "2**62 - 1", "-2**62 - 1");
264 }
265
266 TEST_F(CAPITest, test_subtract_short_edge_cases) {
267 // Close but not quite overflow
268 ASSERT_SUBTRACT("2**62 - 2", "-1", "2**62 - 1");
269 ASSERT_SUBTRACT("-2**62 + 1", "1", "-2**62");
270 // Max magnitudes
271 ASSERT_SUBTRACT("2**62 - 1", "-2**62", "2**63 - 1");
272 ASSERT_SUBTRACT("-2**62", "2**62 - 1", "-2**63 + 1");
273 }
274
275 #define ASSERT_MULTIPLY(x, y, result) \
276 EXPECT_TRUE(is_int_equal(CPyTagged_Multiply(eval_int(x), eval_int(y)), eval_int(result)))
277
278 TEST_F(CAPITest, test_multiply_int) {
279 ASSERT_MULTIPLY("0", "0", "0");
280 ASSERT_MULTIPLY("3", "5", "15");
281 ASSERT_MULTIPLY("2**40", "2**40", "2**80");
282 ASSERT_MULTIPLY("2**30-1", "2**30-1", "(2**30-1)**2");
283 ASSERT_MULTIPLY("2**30", "2**30-1", "2**30 * (2**30-1)");
284 ASSERT_MULTIPLY("2**30-1", "2**30", "2**30 * (2**30-1)");
285 ASSERT_MULTIPLY("2**15", "2**15-1", "2**15 * (2**15-1)");
286 ASSERT_MULTIPLY("2**15-1", "2**15", "2**15 * (2**15-1)");
287 ASSERT_MULTIPLY("3", "-5", "-15");
288 ASSERT_MULTIPLY("-3", "5", "-15");
289 ASSERT_MULTIPLY("-3", "-5", "15");
290 }
291
292 #define ASSERT_FLOOR_DIV(x, y, result) \
293 EXPECT_INT_EQUAL(CPyTagged_FloorDivide(eval_int(x), eval_int(y)), eval_int(result))
294
295 TEST_F(CAPITest, test_floor_divide_short_int) {
296 ASSERT_FLOOR_DIV("18", "6", "3");
297 ASSERT_FLOOR_DIV("17", "6", "2");
298 ASSERT_FLOOR_DIV("12", "6", "2");
299 ASSERT_FLOOR_DIV("15", "5", "3");
300 ASSERT_FLOOR_DIV("14", "5", "2");
301 ASSERT_FLOOR_DIV("11", "5", "2");
302 ASSERT_FLOOR_DIV("-18", "6", "-3");
303 ASSERT_FLOOR_DIV("-13", "6", "-3");
304 ASSERT_FLOOR_DIV("-12", "6", "-2");
305 ASSERT_FLOOR_DIV("18", "-6", "-3");
306 ASSERT_FLOOR_DIV("13", "-6", "-3");
307 ASSERT_FLOOR_DIV("12", "-6", "-2");
308 ASSERT_FLOOR_DIV("-3", "-3", "1");
309 ASSERT_FLOOR_DIV("-5", "-3", "1");
310 ASSERT_FLOOR_DIV("-6", "-3", "2");
311
312 ASSERT_FLOOR_DIV("2**60", "3", "2**60 // 3");
313 ASSERT_FLOOR_DIV("-2**62", "-1", "2**62");
314 ASSERT_FLOOR_DIV("-2**62", "1", "-2**62");
315 ASSERT_FLOOR_DIV("2**62 - 1", "1", "2**62 - 1");
316 ASSERT_FLOOR_DIV("2**62 - 1", "-1", "-2**62 + 1");
317 ASSERT_FLOOR_DIV("2**60", "3", "2**60 // 3");
318 ASSERT_FLOOR_DIV("-2**30", "-1", "2**30");
319 ASSERT_FLOOR_DIV("-2**30", "1", "-2**30");
320 ASSERT_FLOOR_DIV("2**30 - 1", "1", "2**30 - 1");
321 ASSERT_FLOOR_DIV("2**30 - 1", "-1", "-2**30 + 1");
322 }
323
324 TEST_F(CAPITest, test_floor_divide_long_int) {
325 ASSERT_FLOOR_DIV("2**100", "3", "2**100 // 3");
326 ASSERT_FLOOR_DIV("3", "2**100", "0");
327 ASSERT_FLOOR_DIV("2**100", "2**70 // 3", "2**100 // (2**70 // 3)");
328 }
329
330 #define ASSERT_REMAINDER(x, y, result) \
331 EXPECT_INT_EQUAL(CPyTagged_Remainder(eval_int(x), eval_int(y)), eval_int(result))
332
333 TEST_F(CAPITest, test_remainder_short_int) {
334 ASSERT_REMAINDER("18", "6", "0");
335 ASSERT_REMAINDER("17", "6", "5");
336 ASSERT_REMAINDER("13", "6", "1");
337 ASSERT_REMAINDER("12", "6", "0");
338 ASSERT_REMAINDER("15", "5", "0");
339 ASSERT_REMAINDER("14", "5", "4");
340 ASSERT_REMAINDER("11", "5", "1");
341 ASSERT_REMAINDER("-18", "6", "0");
342 ASSERT_REMAINDER("-13", "6", "5");
343 ASSERT_REMAINDER("-12", "6", "0");
344 ASSERT_REMAINDER("18", "-6", "0");
345 ASSERT_REMAINDER("13", "-6", "-5");
346 ASSERT_REMAINDER("12", "-6", "0");
347 ASSERT_REMAINDER("-3", "-3", "0");
348 ASSERT_REMAINDER("-5", "-3", "-2");
349 ASSERT_REMAINDER("-6", "-3", "0");
350
351 ASSERT_REMAINDER("-1", "2**62 - 1", "2**62 - 2");
352 ASSERT_REMAINDER("1", "-2**62", "-2**62 + 1");
353 }
354
355 TEST_F(CAPITest, test_remainder_long_int) {
356 ASSERT_REMAINDER("2**100", "3", "2**100 % 3");
357 ASSERT_REMAINDER("3", "2**100", "3");
358 ASSERT_REMAINDER("2**100", "2**70 // 3", "2**100 % (2**70 // 3)");
359 }
360
361 #define INT_EQ(x, y) \
362 CPyTagged_IsEq(eval_int(x), eval_int(y))
363
364 TEST_F(CAPITest, test_int_equality) {
365 EXPECT_TRUE(INT_EQ("0", "0"));
366 EXPECT_TRUE(INT_EQ("5", "5"));
367 EXPECT_TRUE(INT_EQ("-7", "-7"));
368 EXPECT_TRUE(INT_EQ("2**65 + 0x1234", "2**65 + 0x1234"));
369 EXPECT_TRUE(INT_EQ("-2**65 + 0x1234", "-2**65 + 0x1234"));
370 EXPECT_FALSE(INT_EQ("0", "1"));
371 EXPECT_FALSE(INT_EQ("5", "4"));
372 EXPECT_FALSE(INT_EQ("-7", "7"));
373 EXPECT_FALSE(INT_EQ("-7", "-6"));
374 EXPECT_FALSE(INT_EQ("-7", "-5"));
375 EXPECT_FALSE(INT_EQ("2**65 + 0x1234", "2**65 + 0x1233"));
376 EXPECT_FALSE(INT_EQ("2**65 + 0x1234", "2**66 + 0x1234"));
377 EXPECT_FALSE(INT_EQ("2**65 + 0x1234", "-2**65 - 0x1234"));
378 EXPECT_FALSE(INT_EQ("-2**65 + 0x1234", "-2**65 + 0x1233"));
379 }
380
381 #define INT_NE(x, y) \
382 CPyTagged_IsNe(eval_int(x), eval_int(y))
383
384 TEST_F(CAPITest, test_int_non_equality) {
385 EXPECT_FALSE(INT_NE("0", "0"));
386 EXPECT_FALSE(INT_NE("5", "5"));
387 EXPECT_FALSE(INT_NE("-7", "-7"));
388 EXPECT_FALSE(INT_NE("2**65 + 0x1234", "2**65 + 0x1234"));
389 EXPECT_FALSE(INT_NE("-2**65 + 0x1234", "-2**65 + 0x1234"));
390 EXPECT_TRUE(INT_NE("0", "1"));
391 EXPECT_TRUE(INT_NE("5", "4"));
392 EXPECT_TRUE(INT_NE("-7", "7"));
393 EXPECT_TRUE(INT_NE("-7", "-6"));
394 EXPECT_TRUE(INT_NE("-7", "-5"));
395 EXPECT_TRUE(INT_NE("2**65 + 0x1234", "2**65 + 0x1233"));
396 EXPECT_TRUE(INT_NE("2**65 + 0x1234", "2**66 + 0x1234"));
397 EXPECT_TRUE(INT_NE("2**65 + 0x1234", "-2**65 - 0x1234"));
398 EXPECT_TRUE(INT_NE("-2**65 + 0x1234", "-2**65 + 0x1233"));
399 }
400
401 #define INT_LT(x, y) \
402 CPyTagged_IsLt(eval_int(x), eval_int(y))
403
404 TEST_F(CAPITest, test_int_less_than) {
405 EXPECT_TRUE(INT_LT("0", "5"));
406 EXPECT_TRUE(INT_LT("4", "5"));
407 EXPECT_TRUE(INT_LT("-3", "1"));
408 EXPECT_TRUE(INT_LT("-3", "0"));
409 EXPECT_TRUE(INT_LT("-3", "-2"));
410
411 EXPECT_FALSE(INT_LT("5", "0"));
412 EXPECT_FALSE(INT_LT("5", "4"));
413 EXPECT_FALSE(INT_LT("1", "-3"));
414 EXPECT_FALSE(INT_LT("0", "-3"));
415 EXPECT_FALSE(INT_LT("-2", "-3"));
416 EXPECT_FALSE(INT_LT("-3", "-3"));
417 EXPECT_FALSE(INT_LT("3", "3"));
418
419 EXPECT_TRUE(INT_LT("5", "2**65"));
420 EXPECT_TRUE(INT_LT("-2**65", "-5"));
421 EXPECT_TRUE(INT_LT("-2**66", "2**65"));
422 EXPECT_TRUE(INT_LT("2**65", "2**66"));
423 EXPECT_TRUE(INT_LT("-2**66", "-2**65"));
424
425 EXPECT_FALSE(INT_LT("2**65", "5"));
426 EXPECT_FALSE(INT_LT("-5", "-2**65"));
427 EXPECT_FALSE(INT_LT("2**65", "-2**66"));
428 EXPECT_FALSE(INT_LT("2**66", "2**65"));
429 EXPECT_FALSE(INT_LT("-2**65", "-2**66"));
430 EXPECT_FALSE(INT_LT("-2**65", "-2**65"));
431 EXPECT_FALSE(INT_LT("2**65", "2**65"));
432 }
433
434 #define INT_GE(x, y) \
435 CPyTagged_IsGe(eval_int(x), eval_int(y))
436
437 TEST_F(CAPITest, test_int_greater_than_or_equal) {
438 EXPECT_TRUE(INT_GE("3", "2"));
439 EXPECT_TRUE(INT_GE("3", "3"));
440 EXPECT_FALSE(INT_GE("3", "4"));
441 EXPECT_TRUE(INT_GE("3", "-4"));
442 EXPECT_TRUE(INT_GE("-3", "-4"));
443 EXPECT_TRUE(INT_GE("-3", "-3"));
444 EXPECT_FALSE(INT_GE("-3", "-2"));
445 EXPECT_FALSE(INT_GE("-3", "2"));
446
447 EXPECT_TRUE(INT_GE("2**65", "2**65"));
448 EXPECT_TRUE(INT_GE("2**65", "2**65 - 1"));
449 EXPECT_FALSE(INT_GE("2**65", "2**65 + 1"));
450 EXPECT_TRUE(INT_GE("2**65", "2**60"));
451 }
452
453 #define INT_GT(x, y) \
454 CPyTagged_IsGt(eval_int(x), eval_int(y))
455
456 TEST_F(CAPITest, test_int_greater_than) {
457 EXPECT_TRUE(INT_GT("5", "0"));
458 EXPECT_TRUE(INT_GT("5", "4"));
459 EXPECT_FALSE(INT_GT("5", "5"));
460 EXPECT_FALSE(INT_GT("5", "6"));
461
462 EXPECT_TRUE(INT_GT("1", "-3"));
463 EXPECT_FALSE(INT_GT("-3", "1"));
464
465 EXPECT_TRUE(INT_GT("0", "-3"));
466 EXPECT_TRUE(INT_GT("-2", "-3"));
467 EXPECT_FALSE(INT_GT("-2", "-2"));
468 EXPECT_FALSE(INT_GT("-2", "-1"));
469
470 EXPECT_TRUE(INT_GT("2**65", "5"));
471 EXPECT_TRUE(INT_GT("2**65", "2**65 - 1"));
472 EXPECT_FALSE(INT_GT("2**65", "2**65"));
473 EXPECT_FALSE(INT_GT("2**65", "2**65 + 1"));
474
475 EXPECT_FALSE(INT_GT("-2**65", "1"));
476 EXPECT_TRUE(INT_GT("-2**65", "-2**65 - 1"));
477 EXPECT_FALSE(INT_GT("-2**65", "-2**65"));
478 EXPECT_FALSE(INT_GT("-2**65", "-2**65 + 1"));
479 }
480
481 #define INT_LE(x, y) \
482 CPyTagged_IsLe(eval_int(x), eval_int(y))
483
484 TEST_F(CAPITest, test_int_less_than_or_equal) {
485 EXPECT_TRUE(INT_LE("0", "5"));
486 EXPECT_TRUE(INT_LE("5", "6"));
487 EXPECT_TRUE(INT_LE("5", "5"));
488 EXPECT_FALSE(INT_LE("5", "4"));
489
490 EXPECT_FALSE(INT_LE("1", "-3"));
491 EXPECT_TRUE(INT_LE("-3", "1"));
492
493 EXPECT_TRUE(INT_LT("-3", "0"));
494 EXPECT_TRUE(INT_LE("-2", "-1"));
495 EXPECT_TRUE(INT_LE("-2", "-2"));
496 EXPECT_FALSE(INT_LT("-2", "-3"));
497
498 EXPECT_TRUE(INT_LE("5", "2**65"));
499 EXPECT_FALSE(INT_LE("2**65", "5"));
500 EXPECT_TRUE(INT_LE("2**65", "2**65 + 1"));
501 EXPECT_TRUE(INT_LE("2**65", "2**65"));
502 EXPECT_FALSE(INT_LE("2**65", "2**65 - 1"));
503
504 EXPECT_TRUE(INT_LE("-2**65", "1"));
505 EXPECT_FALSE(INT_LE("1", "-2**65"));
506 EXPECT_TRUE(INT_LE("-2**65", "-2**65 + 1"));
507 EXPECT_TRUE(INT_LE("-2**65", "-2**65"));
508 EXPECT_FALSE(INT_LE("-2**65", "-2**65 - 1"));
509 }
510
511 #define list_get_eq(list, index, value) \
512 is_py_equal(CPyList_GetItem(list, eval_int(index)), eval(value))
513
514 TEST_F(CAPITest, test_list_get) {
515 auto l = empty_list();
516 list_append(l, "3");
517 list_append(l, "5");
518 list_append(l, "7");
519 EXPECT_TRUE(list_get_eq(l, "0", "3"));
520 EXPECT_TRUE(list_get_eq(l, "1", "5"));
521 EXPECT_TRUE(list_get_eq(l, "2", "7"));
522 EXPECT_TRUE(list_get_eq(l, "-1", "7"));
523 EXPECT_TRUE(list_get_eq(l, "-2", "5"));
524 EXPECT_TRUE(list_get_eq(l, "-3", "3"));
525 }
526
527 TEST_F(CAPITest, test_tagged_as_long_long) {
528 auto s = eval_int("3");
529 auto neg = eval_int("-1");
530 auto l = eval_int("2**128");
531 EXPECT_TRUE(CPyTagged_AsSsize_t(s) == 3);
532 EXPECT_FALSE(PyErr_Occurred());
533 EXPECT_TRUE(CPyTagged_AsSsize_t(neg) == -1);
534 EXPECT_FALSE(PyErr_Occurred());
535 EXPECT_TRUE(CPyTagged_AsSsize_t(l) == -1);
536 EXPECT_TRUE(PyErr_Occurred());
537 PyErr_Clear();
538 }
539
540
541 ////
542 // Python module glue to drive the C-API tests.
543 //
544 // The reason we have this as an extension module instead of a
545 // standalone binary is because building an extension module is pretty
546 // well behaved (just specify it with distutils/setuptools and it will
547 // get compiled and linked against the running python) while linking a
548 // library against libpython is a huge non-standard
549 // PITA: python-config locations are janky and it behaves in weird
550 // ways that I don't understand, while this works very cleanly.
551
552 static PyObject *run_tests(PyObject *dummy, PyObject *should_be_null) {
553 // Fake command line arguments. We could arrange to actually pass
554 // in command line arguments (either real ones or ones given as
555 // arguments) but have not bothered.
556 int argc = 1;
557 char asdf[] = "test_capi"; // InitGoogleTest wants char** which means it can't be const...
558 char *argv[] = {asdf, NULL};
559 ::testing::InitGoogleTest(&argc, argv);
560 return PyLong_FromLong(RUN_ALL_TESTS());
561 }
562
563
564 static PyMethodDef test_methods[] = {
565 {"run_tests", run_tests, METH_NOARGS, "Run the C API tests"},
566 {NULL, NULL, 0, NULL}
567 };
568
569 static struct PyModuleDef test_module = {
570 PyModuleDef_HEAD_INIT,
571 "test_capi",
572 NULL,
573 -1,
574 test_methods
575 };
576
577 PyMODINIT_FUNC
578 PyInit_test_capi(void)
579 {
580 PyObject *module = PyModule_Create(&test_module);
581 if (module) {
582 moduleDict = PyModule_GetDict(module);
583 }
584 return module;
585 }