forked from fruch/cwrap
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an easy to maintain file based test suite. Just put a `*.h` file into the `test/data/` directory for the input and a corresponding `*.pxd` with the same name with the expected output. Currently not all tests are passing.
- Loading branch information
Showing
14 changed files
with
169 additions
and
0 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
typedef struct _Bar { | ||
double b; | ||
} Bar; | ||
|
||
typedef enum { | ||
ONE, | ||
TWO, | ||
THREE, | ||
FOUR, | ||
FIVE, | ||
} Baz; | ||
|
||
// function which returns pointer to int | ||
int *foo_bar(double t, Bar *barptr); | ||
|
||
// function which returns a pointer to an array of 10 pointers to doubles | ||
double *(*crazy_fn(int*, char*, Baz))[10]; |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
cdef extern from "function.h": | ||
cdef struct _Bar: | ||
double b | ||
ctypedef _Bar Bar | ||
|
||
ctypedef enum Baz: | ||
ONE | ||
TWO | ||
THREE | ||
FOUR | ||
FIVE | ||
|
||
int *foo_bar(double t, Bar *barptr) | ||
|
||
double *((*crazy_fn(int *, char *, Baz))[10]) |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// function that that takes int and returns pointer to function that takes | ||
// two floats and returns float | ||
float (*returns_func_ptr(int foo))(float, float); | ||
|
||
// function which takes pointer to char and returns pointer to function which | ||
// takes int and double and returns pointer to function that takes int and long | ||
// and returns pointer to function that takes pointer to char and returns | ||
// pointer to double. | ||
double *(*(*(*returns_func_ptr_nested(char*))(int, double))(int, long))(char*); | ||
|
||
// function pointer variable declaration | ||
double (*func_ptr_var)(void); | ||
|
||
// function pointer variable which returns function pointer which returns int | ||
int (*(*func_ptr_func_ptr_var)(void))(void); |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cdef extern from "functionpointer.h": | ||
float (*returns_func_ptr(int foo))(float, float) | ||
|
||
double *(*(*(*returns_func_ptr_nested(char *))(int, double))(int, long))(char *) | ||
|
||
|
||
double (*func_ptr_var)() | ||
|
||
int (*(*func_ptr_func_ptr_var)())() |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// struct with function pointer field which takes int and returns pointer | ||
// to function which takes float pointer and returns pointer to char | ||
struct CrazyField { | ||
char *(*(*crazy_ptr)(int))(float*); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
cdef extern from "functionpointer_in_struct.h": | ||
cdef struct CrazyField: | ||
char *(*(*crazy_ptr)(int))(float *) |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// typdef of simple fundamental type | ||
typedef long long LLong; | ||
|
||
// struct containing typedef'd field | ||
struct Foo { | ||
int a; | ||
LLong b; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cdef extern from "struct_with_typedef_field.h": | ||
ctypedef long long LLong | ||
|
||
cdef struct Foo: | ||
int a | ||
LLong b |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
typedef long long LLong; |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cdef extern from "typedef_simple.h": | ||
ctypedef long long LLong |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
union AUnion { | ||
int a; | ||
double b; | ||
long double c; | ||
struct _sNested { | ||
char* data; | ||
} nested_data; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cdef extern from "union_with_struct.h": | ||
cdef struct __AUnion__sNested: | ||
char *data | ||
|
||
ctypedef __AUnion__sNested __AUnion__sNested_t | ||
|
||
cdef union AUnion: | ||
int a | ||
double b | ||
long double c | ||
__AUnion__sNested_t nested_data |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os | ||
import unittest | ||
|
||
from cwrap import frontends | ||
from cwrap.backend import renderer | ||
from cwrap.config import Config, File | ||
|
||
|
||
def create_test(filename): | ||
def do_test_file(self): | ||
result = self.convert(filename + '.h') | ||
expected = self.read_expected(filename + '.pxd') | ||
print result | ||
self.assertEqual(expected, result) | ||
return do_test_file | ||
|
||
|
||
class TestFiles(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.frontend = frontends.get_frontend('clang') | ||
|
||
def convert(self, filename): | ||
files = [File(filename)] | ||
config = Config('clang', files=files) | ||
asts = self.frontend.generate_asts(config) | ||
print '\n\n\nvmx: asts:\n', asts, '\n\n\n\n\n' | ||
ast_renderer = renderer.ASTRenderer() | ||
for ast_container in asts: | ||
mod_node = ast_container.module | ||
code = ast_renderer.render(mod_node) | ||
output = [] | ||
for line in code.splitlines(): | ||
if line.startswith('#') or not line.strip(): | ||
continue | ||
output.append(line.strip()) | ||
return output | ||
|
||
def read_expected(self, filename): | ||
output = [] | ||
with file(filename) as f: | ||
for line in f: | ||
if line.startswith('#') or not line.strip(): | ||
continue | ||
output.append(line.strip()) | ||
return output | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
||
|
||
# Thanks for the hint: http://stackoverflow.com/questions/2798956/python-unittest-generate-multiple-tests-programmatically | ||
curdir = os.path.dirname(__file__) | ||
testfiles = os.listdir(os.path.join(curdir, 'data')) | ||
testfiles.sort() | ||
testfiles = [os.path.join(curdir, 'data', f) for f in testfiles] | ||
testfiles = [os.path.splitext(f)[0] for f in testfiles if f.endswith('.h')] | ||
print 'Files to test:', testfiles | ||
for testfile in testfiles: | ||
test_method = create_test(testfile) | ||
test_method.__name__ = 'test_' + os.path.basename(testfile) | ||
setattr(TestFiles, test_method.__name__, test_method) |