xref: /llvm-project/polly/lib/External/isl/imath/tests/gmp-compat-test/genpytest.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
1#!/usr/bin/env python
2
3import gmpapi
4import sys
5
6
7def print_header(outf):
8    outf.write(
9        """
10#AUTOGENERATED FILE
11import ctypes
12import os
13import sys
14import binascii
15
16verbose = False
17fork    = False
18  """
19    )
20
21
22def print_cmp(outf):
23    outf.write(
24        """
25def passt(line, name, a_s, b_s):
26    if verbose:
27      print("PASS: {}@{} {} == {}".format(line, name, a_s, b_s))
28    return True
29
30def fail(line, name, a_s, b_s):
31    print("FAIL: {}@{} {} != {}".format(line, name, a_s,b_s))
32    return False
33
34def cstr_eq(line, name, a, b):
35  a_s = a.value.decode("utf-8")
36  b_s = b.value.decode("utf-8")
37
38  if a_s == b_s:
39    return passt(line, name, a_s, b_s)
40  else:
41    return fail(line, name, a_s, b_s)
42
43def bytes_eq(line, name, a, b):
44  if a == b:
45    return passt(line, name, a, b)
46  else:
47    return fail(line, name, a, b)
48
49def run_test(test, line, name, gmp_test_so, imath_test_so, *args):
50  if fork:
51    childpid = os.fork()
52  else:
53    childpid = 0
54  if childpid == 0:
55    eq = test(line, name, gmp_test_so, imath_test_so, *args)
56    if fork:
57      sys.exit(eq != True)
58    else:
59      return eq
60  else:
61    (pid, status) = os.waitpid(childpid, 0)
62    return status == 0
63
64# custom tests
65def test_mpz_export(line, name, gmp_test_so, imath_test_so, *args):
66  # do not use first two args from the test. need to create our own pointers
67  used_args = args[2:]
68  gbuf  = ctypes.create_string_buffer(b'0xdeadbeef'*1024);
69  gsize = ctypes.c_size_t()
70  gout  = ctypes.c_void_p()
71  ibuf  = ctypes.create_string_buffer(b'0xdeadbeef'*1024);
72  isize = ctypes.c_size_t()
73  iout  = ctypes.c_void_p()
74  word_size = args[3].value
75
76  #Test with a NULL pointer
77  gmp_test_so.test_mpz_export(ctypes.byref(gout), None, ctypes.byref(gsize), *used_args)
78  imath_test_so.test_mpz_export(ctypes.byref(iout), None, ctypes.byref(isize), *used_args)
79  gb = ctypes.string_at(gout.value, gsize.value * word_size)
80  ib = ctypes.string_at(iout.value, isize.value * word_size)
81  if not bytes_eq(line, name, gb, ib):
82    return False
83
84  #Test with a provided buffer
85  gmp_test_so.test_mpz_export(ctypes.byref(gout), gbuf, ctypes.byref(gsize), *used_args)
86  imath_test_so.test_mpz_export(ctypes.byref(iout), ibuf, ctypes.byref(isize), *used_args)
87  #print("G:gbuf", gbuf.raw[:gsize.value * word_size])
88  #print("I:ibuf", ibuf.raw[:isize.value * word_size])
89  gb = ctypes.string_at(gout.value, gsize.value * word_size)
90  ib = ctypes.string_at(iout.value, isize.value * word_size)
91  if not bytes_eq(line, name, gb, ib):
92    return False
93
94  return True
95
96def test_mpz_import(line, name, gmp_test_so, imath_test_so, *args):
97  # do not use first two args from the test. need to create our own pointers
98  gout  = ctypes.create_string_buffer(b'0xdeadbeef'*1024);
99  iout  = ctypes.create_string_buffer(b'0xdeadbeef'*1024);
100
101  gmp_test_so.test_mpz_import(gout, *args)
102  imath_test_so.test_mpz_import(iout, *args)
103  #print(gout.raw[:70])
104  #print(iout.raw[:70])
105  return cstr_eq(line, name, gout, iout)
106
107"""
108    )
109
110
111def print_api(name, outf):
112    outf.write(
113        """
114def test_{0}(line, name, gmp_test_so, imath_test_so, *args):
115  gout = ctypes.create_string_buffer(1024*4);
116  iout = ctypes.create_string_buffer(1024*4);
117  gmp_test_so.test_{0}(gout, *args)
118  imath_test_so.test_{0}(iout, *args)
119  eq = cstr_eq(line, name, gout, iout)
120  return eq
121""".format(
122            name
123        )
124    )
125
126
127def print_api_map(outf):
128    outf.write(
129        """
130def get_wrapper(name):
131  test_wrappers = {
132"""
133    )
134    for api in gmpapi.apis:
135        outf.write('    "{}" : {},\n'.format(api.name, "test_" + api.name))
136    outf.write("  }\n")
137    outf.write("  return test_wrappers[name]\n")
138
139
140if __name__ == "__main__":
141    outf = sys.stdout
142    print_header(outf)
143    print_cmp(outf)
144    for api in gmpapi.apis:
145        if not api.custom_test:
146            print_api(api.name, outf)
147    print_api_map(outf)
148