Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for hash-based vtable creation
- Loading branch information
1 parent
6f90a2f
commit a94aa2d
Showing
1 changed file
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,78 @@ | ||
__author__ = 'mark' | ||
# -*- coding: utf-8 -*- | ||
from __future__ import print_function, division, absolute_import | ||
|
||
import numba as nb | ||
from numba import * | ||
from numba.minivect.minitypes import FunctionType | ||
from numba.exttypes import virtual | ||
from numba.exttypes import ordering | ||
from numba.exttypes import methodtable | ||
from numba.exttypes.signatures import Method | ||
from numba.testing.test_support import parametrize, main | ||
|
||
class py_class(object): | ||
pass | ||
|
||
def myfunc1(a): | ||
pass | ||
|
||
def myfunc2(a, b): | ||
pass | ||
|
||
def myfunc3(a, b, c): | ||
pass | ||
|
||
types = list(nb.numeric) + [object_] | ||
|
||
array_types = [t[:] for t in types] | ||
array_types += [t[:, :] for t in types] | ||
array_types += [t[:, :, :] for t in types] | ||
|
||
all_types = types + array_types | ||
|
||
def method(func, name, sig): | ||
return Method(func, name, sig, False, False) | ||
|
||
make_methods1 = lambda: [ | ||
method(myfunc1, 'method', FunctionType(argtype, [argtype])) | ||
for argtype in all_types] | ||
|
||
make_methods2 = lambda: [ | ||
method(myfunc2, 'method', FunctionType(argtype1, [argtype1, argtype2])) | ||
for argtype1 in all_types | ||
for argtype2 in all_types] | ||
|
||
|
||
def make_table(methods): | ||
table = methodtable.VTabType(py_class, []) | ||
table.create_method_ordering() | ||
|
||
for i, method in enumerate(make_methods1()): | ||
key = method.name, method.signature.args | ||
method.lfunc_pointer = i | ||
table.specialized_methods[key] = method | ||
|
||
assert len(methods) == len(table.specialized_methods) | ||
|
||
return table | ||
|
||
def make_hashtable(methods): | ||
table = make_table(methods) | ||
hashtable = virtual.build_hashing_vtab(table) | ||
return hashtable | ||
|
||
#------------------------------------------------------------------------ | ||
# Tests | ||
#------------------------------------------------------------------------ | ||
|
||
@parametrize(make_methods1()) | ||
def test_specializations(methods): | ||
hashtable = make_hashtable(methods) | ||
print(hashtable) | ||
|
||
for i, method in enumerate(methods): | ||
key = virtual.sep201_signature_string(method.signature, method.name) | ||
assert hashtable.find_method(key), (i, method, key) | ||
|
||
if __name__ == '__main__': | ||
main() |