├── .flake8 ├── .gitignore ├── .suite_ignores ├── .travis.yml ├── LICENSE ├── README.rst ├── bin └── polyphony ├── error.py ├── polyphony ├── __init__.py ├── _internal │ ├── __init__.py │ ├── _builtins.py │ ├── _io.py │ ├── _modules.py │ ├── _timing.py │ ├── _typing.py │ └── _verilog.py ├── base.py ├── compiler │ ├── __init__.py │ ├── __main__.py │ ├── ahdl │ │ ├── __init__.py │ │ ├── ahdl.py │ │ ├── ahdltransformer.py │ │ ├── ahdlvisitor.py │ │ ├── analysis │ │ │ ├── __init__.py │ │ │ ├── ahdlusedef.py │ │ │ └── bitwidthdetector.py │ │ ├── hdlgen.py │ │ ├── hdlmodule.py │ │ ├── hdlscope.py │ │ ├── signal.py │ │ ├── stg.py │ │ ├── stg_pipeline.py │ │ ├── stgbuilder.py │ │ └── transformers │ │ │ ├── __init__.py │ │ │ ├── ahdlcloner.py │ │ │ ├── ahdlopt.py │ │ │ ├── bitwidthreducer.py │ │ │ ├── canonical.py │ │ │ ├── iotransformer.py │ │ │ ├── netreducer.py │ │ │ ├── netrenamer.py │ │ │ ├── statereducer.py │ │ │ ├── varcollector.py │ │ │ └── varreplacer.py │ ├── api.py │ ├── common │ │ ├── __init__.py │ │ ├── common.py │ │ ├── env.py │ │ ├── errors.py │ │ ├── graph.py │ │ └── utils.py │ ├── driver.py │ ├── frontend │ │ ├── __init__.py │ │ └── python │ │ │ ├── __init__.py │ │ │ ├── irtranslator.py │ │ │ └── pure.py │ ├── ir │ │ ├── __init__.py │ │ ├── analysis │ │ │ ├── __init__.py │ │ │ ├── diagnostic.py │ │ │ ├── dominator.py │ │ │ ├── fieldusedef.py │ │ │ ├── liveness.py │ │ │ ├── loopdetector.py │ │ │ ├── regreducer.py │ │ │ ├── scopegraph.py │ │ │ ├── typecheck.py │ │ │ └── usedef.py │ │ ├── block.py │ │ ├── builtin.py │ │ ├── ir.py │ │ ├── irhelper.py │ │ ├── irparser.py │ │ ├── irvisitor.py │ │ ├── loop.py │ │ ├── scheduling │ │ │ ├── __init__.py │ │ │ ├── dataflow.py │ │ │ ├── latency.py │ │ │ └── scheduler.py │ │ ├── scope.py │ │ ├── setlineno.py │ │ ├── symbol.py │ │ ├── synth.py │ │ ├── transformers │ │ │ ├── __init__.py │ │ │ ├── bitwidth.py │ │ │ ├── cfgopt.py │ │ │ ├── constopt.py │ │ │ ├── copyopt.py │ │ │ ├── deadcode.py │ │ │ ├── iftransform.py │ │ │ ├── inlineopt.py │ │ │ ├── instantiator.py │ │ │ ├── looptransformer.py │ │ │ ├── objtransform.py │ │ │ ├── phiopt.py │ │ │ ├── portconverter.py │ │ │ ├── quadruplet.py │ │ │ ├── ssa.py │ │ │ ├── treebalancer.py │ │ │ ├── tuple.py │ │ │ ├── typeeval.py │ │ │ ├── typeprop.py │ │ │ ├── unroll.py │ │ │ └── varreplacer.py │ │ └── types │ │ │ ├── __init__.py │ │ │ ├── booltype.py │ │ │ ├── classtype.py │ │ │ ├── exprtype.py │ │ │ ├── functiontype.py │ │ │ ├── inttype.py │ │ │ ├── listtype.py │ │ │ ├── namespacetype.py │ │ │ ├── nonetype.py │ │ │ ├── objecttype.py │ │ │ ├── porttype.py │ │ │ ├── scopetype.py │ │ │ ├── simpletype.py │ │ │ ├── strtype.py │ │ │ ├── tupletype.py │ │ │ ├── type.py │ │ │ ├── typehelper.py │ │ │ └── undefined.py │ └── target │ │ ├── __init__.py │ │ └── verilog │ │ ├── __init__.py │ │ ├── flatten.py │ │ ├── vericodegen.py │ │ ├── verilog_common.py │ │ └── veritestgen.py ├── io.py ├── modules.py ├── simulator.py ├── timing.py ├── typing.py ├── verilog.py └── version.py ├── requirements.txt ├── setup.py ├── simu.py ├── suite.py └── tests ├── apps ├── ad7091r.py ├── bitonic_sort.py ├── fib.py ├── fifo.py ├── filter_tester.py ├── fir.py ├── minivm.py ├── minivm2.py ├── odd_even_sort.py ├── pipelined_minivm.py ├── shellsort.py ├── sobel_filter.py └── stack.py ├── channel ├── channel01.py ├── channel02.py ├── channel03.py ├── channel04.py └── channel05.py ├── chstone ├── adpcm │ ├── adpcm.c │ └── adpcm.py ├── aes │ ├── aes.c │ ├── aes.h │ ├── aes_dec.c │ ├── aes_enc.c │ ├── aes_func.c │ └── aes_key.c ├── blowfish │ ├── bf.c │ ├── bf.py │ ├── bf_cfb64.c │ ├── bf_enc.c │ ├── bf_locl.h │ ├── bf_pi.h │ ├── bf_skey.c │ └── blowfish.h ├── dfadd │ ├── SPARC-GCC.h │ ├── dfadd.c │ ├── milieu.h │ ├── softfloat-macros │ ├── softfloat-specialize │ ├── softfloat.c │ └── softfloat.h ├── dfdiv │ ├── SPARC-GCC.h │ ├── dfdiv.c │ ├── milieu.h │ ├── softfloat-macros │ ├── softfloat-specialize │ ├── softfloat.c │ └── softfloat.h ├── dfmul │ ├── SPARC-GCC.h │ ├── dfmul.c │ ├── milieu.h │ ├── softfloat-macros │ ├── softfloat-specialize │ ├── softfloat.c │ └── softfloat.h ├── dfsin │ ├── SPARC-GCC.h │ ├── dfsin.c │ ├── milieu.h │ ├── softfloat-macros │ ├── softfloat-specialize │ ├── softfloat.c │ └── softfloat.h ├── gsm │ ├── add.c │ ├── gsm.c │ ├── lpc.c │ └── private.h ├── jpeg │ ├── chenidct.c │ ├── chenidct.py │ ├── chenidct_pipe.py │ ├── decode.c │ ├── decode.h │ ├── global.h │ ├── huffman.c │ ├── huffman.h │ ├── init.h │ ├── jfif_read.c │ ├── jpeg2bmp.c │ ├── main.c │ └── marker.c ├── mips │ ├── imem.h │ ├── mips.c │ ├── mips.py │ └── pipelined_mips.py ├── motion │ ├── config.h │ ├── getbits.c │ ├── getvlc.c │ ├── getvlc.h │ ├── global.h │ ├── motion.c │ ├── mpeg2.c │ └── mpeg2dec.h └── sha │ ├── sha.c │ ├── sha.h │ └── sha_driver.c ├── class ├── alias01.py ├── alias02.py ├── alias03.py ├── alias04.py ├── alias05.py ├── alias06.py ├── alias07.py ├── alias08.py ├── alias09.py ├── alias10.py ├── callable01.py ├── callable02.py ├── callable03.py ├── callable04.py ├── callable05.py ├── classfield01.py ├── classfield02.py ├── classfield03.py ├── classfield04.py ├── classfield05.py ├── classfield06.py ├── classfield07.py ├── classfield08.py ├── composition01.py ├── composition02.py ├── composition03.py ├── composition04.py ├── field01.py ├── field02.py ├── field03.py ├── field04.py ├── field05.py ├── field06.py ├── method01.py ├── method02.py ├── method03.py ├── method04.py ├── method05.py ├── method06.py ├── objfield01.py ├── param01.py ├── param02.py ├── param03.py ├── param04.py └── param05.py ├── error ├── callable.py ├── global01.py ├── global02.py ├── global03.py ├── global_module.py ├── io01.py ├── io02.py ├── io03.py ├── io_conflict01.py ├── io_conflict02.py ├── io_pipeline_read_conflict01.py ├── io_pipeline_write_conflict01.py ├── io_write_conflict01.py ├── io_write_conflict02.py ├── io_write_conflict03.py ├── is_not_subscriptable01.py ├── is_not_subscriptable02.py ├── is_not_subscriptable03.py ├── list_multiplier.py ├── localclass01.py ├── loop_var01.py ├── missing_required_arg01.py ├── missing_required_arg02.py ├── missing_required_arg03.py ├── module_args01.py ├── module_method01.py ├── module_method02.py ├── must_be_x_type01.py ├── must_be_x_type02.py ├── must_be_x_type03.py ├── must_be_x_type04.py ├── must_be_x_type05.py ├── must_be_x_type06.py ├── pipeline01.py ├── pipeline02.py ├── pipeline03.py ├── pipeline04.py ├── pipeline05.py ├── print01.py ├── print02.py ├── pure01.py ├── pure02.py ├── range01.py ├── redefined_name01.py ├── redefined_name02.py ├── reserved_port_name.py ├── return_type01.py ├── return_type02.py ├── seq_capacity01.py ├── seq_capacity02.py ├── sub.py ├── timed_loop01.py ├── timed_loop02.py ├── toomany_args01.py ├── toomany_args02.py ├── typing01.py ├── typing02.py ├── typing03.py ├── unroll01.py ├── unroll02.py ├── unroll03.py ├── unroll04.py ├── unroll05.py ├── unsupported_expr01.py ├── unsupported_expr02.py └── unsupported_expr03.py ├── expr ├── expr01.py ├── expr02.py ├── expr03.py ├── expr04.py ├── expr05.py ├── expr06.py ├── expr07.py ├── expr08.py ├── expr09.py ├── expr10.py ├── expr11.py └── expr12.py ├── func ├── func01.py ├── func02.py ├── func03.py ├── func04.py ├── func05.py ├── func06.py ├── func07.py ├── func08.py ├── func09.py ├── func10.py ├── func11.py ├── kwargs01.py ├── kwargs02.py ├── param01.py ├── special01.py ├── special02.py ├── special03.py ├── sysfunc01.py └── sysfunc02.py ├── if ├── if01.py ├── if02.py ├── if03.py ├── if04.py ├── if05.py ├── if06.py ├── if07.py ├── if08.py ├── if09.py ├── if10.py ├── if11.py ├── if12.py ├── if13.py ├── if14.py ├── if15.py ├── if16.py ├── if17.py ├── if18.py ├── if19.py ├── if20.py ├── if21.py ├── if22.py ├── if23.py ├── if24.py ├── if25.py ├── if26.py ├── if27.py ├── if28.py ├── if29.py ├── if30.py ├── ifexp01.py └── ifexp02.py ├── import ├── import01.py ├── import02.py ├── import03.py ├── import04.py ├── import05.py ├── import06.py ├── import07.py ├── import08.py ├── import09.py ├── import10.py ├── import11.py ├── import12.py ├── import13.py ├── import14.py ├── import_dir01.py ├── import_dir02.py ├── import_pkg01.py ├── import_pkg02.py ├── import_pkg03.py ├── import_pkg04.py ├── import_pkg05.py ├── sub1.py ├── sub2.py ├── sub3.py ├── sub4.py ├── subdir │ ├── sub1.py │ └── sub2.py └── subpkg │ ├── __init__.py │ ├── sub1.py │ ├── subsubpkg │ ├── __init__.py │ └── subsub1.py │ └── subsubpkg2 │ ├── __init__.py │ └── subsub2.py ├── io ├── assign01.py ├── assign02.py ├── assign03.py ├── assign04.py ├── assign05.py ├── assign06.py ├── assign07.py ├── assign08.py ├── connect01.py ├── connect02.py ├── connect03.py ├── connect04.py ├── flipped01.py ├── flipped02.py ├── flipped03.py ├── flipped04.py ├── handshake01.py ├── handshake02.py ├── init01.py ├── interface01.py ├── port01.py ├── port02.py ├── port_edge01.py ├── port_edge02.py ├── thru01.py ├── thru02.py ├── wait_until01.py └── wait_until02.py ├── issues ├── busy_loop.py ├── cfg01.py ├── cfg02.py ├── cfg03.py ├── cfg04.py ├── cfg05.py ├── cfg06.py ├── cfg07.py ├── inline.py ├── object_if01.py ├── worker_with_tuple.py └── xor_nn.py ├── list ├── init01.py ├── list01.py ├── list02.py ├── list03.py ├── list04.py ├── list05.py ├── list06.py ├── list07.py ├── list08.py ├── list09.py ├── list10.py ├── list11.py ├── list12.py ├── list13.py ├── list14.py ├── list15.py ├── list16.py ├── list17.py ├── list18.py ├── list19.py ├── list20.py ├── list21.py ├── list22.py ├── list23.py ├── list24.py ├── list25.py ├── list26.py ├── list27.py ├── list28.py ├── list29.py ├── list30.py ├── list31.py ├── list32.py ├── list33.py ├── list34.py ├── rom01.py ├── rom02.py ├── rom03.py ├── rom04.py ├── rom05.py ├── rom06.py ├── rom07.py ├── rom08.py ├── rom09.py └── rom10.py ├── loop ├── for01.py ├── for02.py ├── for03.py ├── for04.py ├── for05.py ├── for06.py ├── for07.py ├── for08.py ├── for09.py ├── for10.py ├── for11.py ├── for12.py ├── for13.py ├── for14.py ├── for15.py ├── for16.py ├── for17.py ├── for18.py ├── while01.py ├── while02.py ├── while03.py ├── while04.py ├── while05.py ├── while06.py ├── while07.py ├── while08.py └── while09.py ├── module ├── field01.py ├── field02.py ├── module01.py ├── module02.py ├── module03.py ├── module04.py ├── module05.py ├── module06.py ├── module07.py ├── module08.py ├── module09.py ├── module10.py ├── module11.py ├── module12.py ├── nesting01.py ├── nesting02.py ├── nesting03.py ├── nesting04.py └── parameter01.py ├── pipeline ├── for01.py ├── for02.py ├── for03.py ├── for04.py ├── for05.py ├── for06.py ├── for07.py ├── for08.py ├── for09.py ├── for10.py ├── for11.py ├── for12.py ├── for13.py ├── for14.py ├── for15.py ├── for16.py ├── for17.py ├── nested01.py ├── nested02.py ├── nested03.py ├── nested04.py ├── nested05.py └── nested06.py ├── pure ├── module01.py ├── module02.py ├── module03.py ├── module04.py ├── module05.py ├── module06.py ├── module07.py ├── module08.py ├── module09.py ├── module_ctor01.py ├── module_ctor02.py ├── nesting01.py ├── nesting02.py ├── nesting03.py ├── pure01.py ├── pure02.py ├── pure03.py ├── pure04.py ├── pure05.py ├── pure06.py ├── pure07.py ├── pure08.py ├── pure09.py ├── pure10.py ├── pure11.py └── pure12.py ├── return ├── noreturn01.py ├── return01.py ├── return02.py ├── return03.py ├── return04.py └── return05.py ├── scope ├── call01.py ├── call02.py ├── global01.py ├── global02.py ├── global03.py ├── global04.py ├── global05.py ├── global06.py ├── lookup01.py └── lookup02.py ├── testbench ├── tb01.py ├── tb02.py ├── tb03.py ├── tb04.py ├── tb05.py ├── tb06.py ├── tb07.py ├── tb08.py ├── tb09.py ├── tb10.py └── tb11.py ├── timed ├── clkrange01.py ├── clkrange02.py ├── clkrange03.py ├── clkrange04.py ├── clkrange05.py ├── clktime01.py ├── clktime02.py ├── timed01.py ├── timed02.py ├── timed03.py ├── wait01.py ├── wait02.py ├── wait_until01.py └── wait_until02.py ├── tuple ├── tuple01.py ├── tuple02.py ├── tuple03.py ├── tuple04.py ├── tuple05.py ├── tuple06.py ├── tuple07.py ├── tuple08.py ├── tuple09.py ├── tuple10.py ├── tuple11.py ├── tuple12.py ├── tuple13.py └── tuple_field01.py ├── typing ├── bitwidth01.py ├── bitwidth02.py ├── bitwidth03.py ├── module_param01.py ├── signed01.py ├── signed02.py ├── typing01.py ├── typing02.py ├── typing03.py ├── typing04.py ├── typing05.py ├── typing06.py ├── typing07.py ├── typing08.py └── typing09.py ├── unroll ├── pipelined_unroll01.py ├── unroll01.py ├── unroll02.py ├── unroll03.py ├── unroll04.py ├── unroll05.py ├── unroll06.py ├── unroll07.py ├── unroll08.py ├── unroll09.py ├── unroll10.py ├── unroll11.py ├── unroll12.py ├── unroll13.py ├── unroll14.py ├── unroll15.py ├── unroll16.py └── unroll17.py └── warning ├── pipeline_hazard01.py ├── pipeline_resource01.py ├── pipeline_resource02.py ├── port_is_not_used01.py ├── port_is_not_used02.py └── static_assert.py /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = H101,H301,H304,H306,E221,E231,E241,E265,F403,F405,W503,W504 3 | max-line-length = 120 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | .*/ 3 | .* 4 | __pycache__ 5 | debug* 6 | out/ 7 | .suite_ignores 8 | -------------------------------------------------------------------------------- /.suite_ignores: -------------------------------------------------------------------------------- 1 | io/connect01.py 2 | io/connect02.py 3 | io/connect03.py 4 | io/connect04.py 5 | io/flipped01.py 6 | io/flipped02.py 7 | io/flipped03.py 8 | io/flipped04.py 9 | io/thru01.py 10 | io/thru02.py 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | sudo: false 3 | python: 4 | - "3.6" 5 | - "nightly" 6 | addons: 7 | apt: 8 | packages: 9 | - iverilog 10 | install: 11 | - pip install -r requirements.txt 12 | script: 13 | - python suite.py -j -f 14 | -------------------------------------------------------------------------------- /bin/polyphony: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | from polyphony.compiler.__main__ import main 3 | 4 | main() -------------------------------------------------------------------------------- /polyphony/_internal/_builtins.py: -------------------------------------------------------------------------------- 1 | @typeclass 2 | class none: pass 3 | 4 | @typeclass 5 | class int: pass 6 | 7 | 8 | @typeclass 9 | class bool: pass 10 | 11 | 12 | @typeclass 13 | class str: pass 14 | 15 | 16 | @typeclass 17 | class list: pass 18 | 19 | 20 | @typeclass 21 | class tuple: pass 22 | 23 | 24 | @typeclass 25 | class object: pass 26 | 27 | 28 | @typeclass 29 | class type: pass 30 | 31 | 32 | @typeclass 33 | class function: pass 34 | 35 | 36 | def print(*args) -> None: 37 | pass 38 | 39 | 40 | def range(stop:int) -> None: 41 | pass 42 | 43 | 44 | def len() -> int: 45 | pass 46 | 47 | 48 | def _assert(expr:bool) -> None: 49 | pass 50 | 51 | 52 | def _new(typ): 53 | pass 54 | -------------------------------------------------------------------------------- /polyphony/_internal/_io.py: -------------------------------------------------------------------------------- 1 | @builtin 2 | class Port: 3 | def __init__(self, dtype:type, direction:str, init=None, 4 | rewritable:bool=False) -> object: 5 | pass 6 | 7 | def rd(self) -> object: 8 | pass 9 | 10 | @mutable 11 | def wr(self, v) -> None: 12 | pass 13 | 14 | def assign(self, fn:function) -> None: 15 | pass 16 | 17 | def edge(self, old, new) -> bool: 18 | pass 19 | 20 | 21 | from . import timing 22 | 23 | 24 | @builtin 25 | def flipped(obj:object) -> object: 26 | pass 27 | 28 | 29 | @builtin 30 | def connect(p0:object, p1:object) -> None: 31 | pass 32 | 33 | 34 | @builtin 35 | def thru(parent:object, child:object) -> None: 36 | pass 37 | -------------------------------------------------------------------------------- /polyphony/_internal/_verilog.py: -------------------------------------------------------------------------------- 1 | @builtin 2 | def write(fmt:str, *args) -> None: 3 | pass 4 | 5 | 6 | @builtin 7 | def display(fmt:str, *args) -> None: 8 | pass 9 | -------------------------------------------------------------------------------- /polyphony/compiler/__init__.py: -------------------------------------------------------------------------------- 1 | from .api import from_python, from_module, from_object 2 | -------------------------------------------------------------------------------- /polyphony/compiler/ahdl/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ahdl/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ahdl/analysis/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ahdl/analysis/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ahdl/transformers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ahdl/transformers/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ahdl/transformers/varreplacer.py: -------------------------------------------------------------------------------- 1 | from ..ahdl import AHDL_VAR 2 | from ..ahdltransformer import AHDLTransformer 3 | 4 | class AHDLSignalReplacer(AHDLTransformer): 5 | def __init__(self, replace_table:dict[tuple, tuple]): 6 | self._replace_table = replace_table 7 | 8 | def visit_AHDL_VAR(self, ahdl): 9 | if ahdl.vars in self._replace_table: 10 | vars = self._replace_table[ahdl.vars] 11 | return AHDL_VAR(vars, ahdl.ctx) 12 | else: 13 | return ahdl 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /polyphony/compiler/common/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/common/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/frontend/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/frontend/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/frontend/python/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/frontend/python/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ir/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ir/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ir/analysis/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ir/analysis/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ir/scheduling/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ir/scheduling/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ir/transformers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ir/transformers/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ir/types/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/ir/types/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/ir/types/booltype.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass, field 2 | from .simpletype import SimpleType 3 | 4 | @dataclass(frozen=True) 5 | class BoolType(SimpleType): 6 | name: str = field(init=False, default='bool') 7 | 8 | def can_assign(self, rhs_t): 9 | return (self.name == rhs_t.name 10 | or rhs_t.is_int()) 11 | 12 | @property 13 | def width(self): 14 | return 1 15 | 16 | @property 17 | def signed(self): 18 | return False 19 | -------------------------------------------------------------------------------- /polyphony/compiler/ir/types/classtype.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass, field 2 | from .scopetype import ScopeType 3 | 4 | 5 | @dataclass(frozen=True) 6 | class ClassType(ScopeType): 7 | name: str = field(init=False, default='class') 8 | 9 | def __post_init__(self): 10 | assert self.scope.is_class() or self.scope.is_typeclass() 11 | 12 | def can_assign(self, rhs_t): 13 | return self.name == rhs_t.name and (self.scope is rhs_t.scope or self.scope.is_object()) 14 | 15 | def propagate(self, rhs_t): 16 | if self.name == rhs_t.name and self.scope.is_object(): 17 | return rhs_t.clone(explicit=self.explicit) 18 | else: 19 | return self 20 | -------------------------------------------------------------------------------- /polyphony/compiler/ir/types/namespacetype.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass, field 2 | from .scopetype import ScopeType 3 | 4 | 5 | @dataclass(frozen=True) 6 | class NamespaceType(ScopeType): 7 | name: str = field(init=False, default='namespace') 8 | 9 | def can_assign(self, rhs_t): 10 | return False 11 | 12 | def propagate(self, rhs_t): 13 | return self 14 | -------------------------------------------------------------------------------- /polyphony/compiler/ir/types/nonetype.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass, field 2 | from .simpletype import SimpleType 3 | 4 | 5 | @dataclass(frozen=True) 6 | class NoneType(SimpleType): 7 | name: str = field(init=False, default='none') 8 | -------------------------------------------------------------------------------- /polyphony/compiler/ir/types/strtype.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass, field 2 | from .simpletype import SimpleType 3 | 4 | 5 | @dataclass(frozen=True) 6 | class StrType(SimpleType): 7 | name: str = field(init=False, default='str') 8 | -------------------------------------------------------------------------------- /polyphony/compiler/ir/types/undefined.py: -------------------------------------------------------------------------------- 1 | 2 | from dataclasses import dataclass, field 3 | from .type import Type 4 | from ...common.env import env 5 | 6 | 7 | @dataclass(frozen=True) 8 | class UndefinedType(Type): 9 | name: str = field(init=False, default='undef') 10 | 11 | def clone(self, **args): 12 | return self 13 | 14 | def can_assign(self, rhs_t): 15 | return True 16 | 17 | def propagate(self, rhs_t): 18 | return rhs_t.clone() 19 | -------------------------------------------------------------------------------- /polyphony/compiler/target/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/target/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/target/verilog/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/polyphony/compiler/target/verilog/__init__.py -------------------------------------------------------------------------------- /polyphony/compiler/target/verilog/flatten.py: -------------------------------------------------------------------------------- 1 | from ...ahdl.ahdl import AHDL_VAR, AHDL_MEMVAR 2 | from ...ahdl.ahdltransformer import AHDLTransformer 3 | 4 | class FlattenSignals(AHDLTransformer): 5 | def visit_AHDL_VAR(self, ahdl): 6 | if ahdl.is_local_var(): 7 | return ahdl 8 | new_sig = self.hdlmodule.gen_sig(ahdl.hdl_name, ahdl.sig.width, ahdl.sig.tags, ahdl.sig.sym) 9 | return AHDL_VAR(new_sig, ahdl.ctx) 10 | 11 | def visit_AHDL_MEMVAR(self, ahdl): 12 | if ahdl.is_local_var(): 13 | return ahdl 14 | new_sig = self.hdlmodule.gen_sig(ahdl.hdl_name, ahdl.sig.width, ahdl.sig.tags, ahdl.sig.sym) 15 | return AHDL_MEMVAR(new_sig, ahdl.ctx) 16 | -------------------------------------------------------------------------------- /polyphony/verilog.py: -------------------------------------------------------------------------------- 1 | def _verilog_format_to_python3_format(str): 2 | return str 3 | 4 | 5 | def display(*args, end='\n'): 6 | if len(args) == 1: 7 | print(args[0], end=end) 8 | else: 9 | print(_verilog_format_to_python3_format(args[0]) % tuple(args[1:]), end=end) 10 | 11 | 12 | def write(*args): 13 | display(*args, end='') 14 | -------------------------------------------------------------------------------- /polyphony/version.py: -------------------------------------------------------------------------------- 1 | __version__ = '0.4.0' 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/requirements.txt -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | from polyphony import __version__ 3 | 4 | setuptools.setup( 5 | name='polyphony', 6 | version=__version__, 7 | packages=setuptools.find_packages(), 8 | author="Hiroaki Kataoka", 9 | author_email="kataoka@sinby.com", 10 | description="Python based High Level Synthesis compiler", 11 | long_description=open('README.rst').read(), 12 | keywords="HLS High Level Synthesis FPGA HDL Verilog VHDL EDA", 13 | license='MIT', 14 | entry_points={'console_scripts':['polyphony=polyphony.compiler.__main__:main'],}, 15 | url='https://github.com/ktok07b6/polyphony', 16 | ) 17 | # scripts=['bin/polyphony'], -------------------------------------------------------------------------------- /tests/apps/fib.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def fib(n): 4 | a, b = 0, 1 5 | while a < n: 6 | print(a) 7 | a, b = b, a+b 8 | return 9 | 10 | @testbench 11 | def test(): 12 | fib(1000) 13 | -------------------------------------------------------------------------------- /tests/apps/odd_even_sort.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def sort(d:list, start): 4 | flag = 0 5 | for i in range(start, len(d)-1, 2): 6 | if d[i] > d[i+1]: 7 | tmp = d[i] 8 | d[i] = d[i+1] 9 | d[i+1] = tmp 10 | flag = 1 11 | return flag 12 | 13 | def odd_even_sort(): 14 | data = [12, 8, 4, 16, 3, 10, 9, 13, 7, 6, 14, 15, 5, 11, 2, 1] 15 | 16 | flag = 1 17 | while flag: 18 | flag = 0 19 | flag |= sort(data, 0) 20 | flag |= sort(data, 1) 21 | for d in data: 22 | print(d) 23 | 24 | for i in range(1, 16): 25 | assert data[i-1] < data[i] 26 | 27 | @testbench 28 | def test(): 29 | odd_even_sort() 30 | -------------------------------------------------------------------------------- /tests/apps/stack.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | SIZE=256 4 | class Stack: 5 | def __init__(self, size): 6 | self.mem = [None] * size 7 | self.idx = 0 8 | 9 | def push(self, x): 10 | self.mem[self.idx] = x 11 | self.idx += 1 12 | 13 | def pop(self): 14 | self.idx -= 1 15 | return self.mem[self.idx] 16 | 17 | def stack(): 18 | stack = Stack(SIZE) 19 | stack.push(0) 20 | stack.push(1) 21 | stack.push(2) 22 | stack.push(3) 23 | assert 3 == stack.pop() 24 | assert 2 == stack.pop() 25 | assert 1 == stack.pop() 26 | assert 0 == stack.pop() 27 | 28 | @testbench 29 | def test(): 30 | stack() 31 | -------------------------------------------------------------------------------- /tests/class/alias01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def alias01(x): 8 | c0 = C(x) 9 | c1 = c0 10 | c2 = c1 11 | result0 = c2.x == x and c1.x == x and c0.x == x 12 | c2.x = 10 13 | result1 = c2.x == 10 and c1.x == 10 and c0.x == 10 14 | return result0 and result1 15 | 16 | @testbench 17 | def test(): 18 | assert alias01(1) 19 | assert alias01(2) 20 | assert alias01(3) 21 | -------------------------------------------------------------------------------- /tests/class/alias02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | class D: 8 | def __init__(self, c): 9 | self.c = c 10 | 11 | def alias02(x): 12 | c0 = C(x) 13 | c1 = C(x*x) 14 | d = D(c0) 15 | result0 = d.c.x == x 16 | d.c = c1 17 | result1 = d.c.x == x*x 18 | c1.x = 0 19 | result2 = d.c.x == 0 20 | d.c = c0 21 | result3 = d.c.x == x 22 | 23 | return result0 and result1 and result2 and result3 24 | 25 | @testbench 26 | def test(): 27 | assert alias02(1) 28 | assert alias02(2) 29 | assert alias02(3) 30 | -------------------------------------------------------------------------------- /tests/class/alias03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, v): 5 | self.v = v 6 | 7 | class D: 8 | def __init__(self, c): 9 | self.c = c 10 | 11 | def alias03(p, x, y): 12 | c0 = C(x) 13 | c1 = C(y) 14 | d = D(c0) 15 | if p: 16 | d = D(c1) 17 | c0.v += 10 18 | return d.c.v 19 | 20 | @testbench 21 | def test(): 22 | assert 2 == alias03(True, 1, 2) 23 | assert 3 == alias03(True, 2, 3) 24 | assert 11 == alias03(False, 1, 2) 25 | assert 12 == alias03(False, 2, 3) 26 | -------------------------------------------------------------------------------- /tests/class/alias04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, v): 5 | self.v = v 6 | 7 | class D: 8 | def __init__(self, c): 9 | self.c = c 10 | 11 | class E: 12 | def __init__(self, d): 13 | self.d = d 14 | 15 | def alias04(p, x, y): 16 | c1 = C(x) 17 | c2 = C(y) 18 | d1 = D(c1) 19 | d2 = D(c2) 20 | if p: 21 | e = E(d1) 22 | else: 23 | e = E(d2) 24 | return e.d.c.v 25 | 26 | @testbench 27 | def test(): 28 | assert 1 == alias04(True, 1, 2) 29 | assert 2 == alias04(True, 2, 3) 30 | assert 2 == alias04(False, 1, 2) 31 | assert 3 == alias04(False, 2, 3) 32 | -------------------------------------------------------------------------------- /tests/class/alias05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, v): 5 | self.v = v 6 | 7 | def get_v(self): 8 | return self.v 9 | 10 | def alias05(p, x, y): 11 | c0 = C(x) 12 | c1 = C(y) 13 | if p: 14 | c2 = c0 15 | else: 16 | c2 = c1 17 | c0.v += 10 18 | return c2.v + c2.get_v() 19 | 20 | @testbench 21 | def test(): 22 | assert 22 == alias05(True, 1, 2) 23 | assert 24 == alias05(True, 2, 3) 24 | assert 4 == alias05(False, 1, 2) 25 | assert 6 == alias05(False, 2, 3) 26 | -------------------------------------------------------------------------------- /tests/class/alias06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, v): 5 | self.v = v 6 | 7 | def get_v(self): 8 | return self.v 9 | 10 | class D: 11 | def __init__(self, c): 12 | self.c = c 13 | 14 | def alias06(p, x, y): 15 | c0 = C(x) 16 | c1 = C(y) 17 | if p: 18 | d = D(c0) 19 | else: 20 | d = D(c1) 21 | c0.v += 10 22 | return d.c.get_v() 23 | 24 | @testbench 25 | def test(): 26 | assert 11 == alias06(True, 1, 2) 27 | assert 12 == alias06(True, 2, 3) 28 | assert 2 == alias06(False, 1, 2) 29 | assert 3 == alias06(False, 2, 3) 30 | -------------------------------------------------------------------------------- /tests/class/alias07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, v): 5 | self.v = v 6 | 7 | def get_v(self): 8 | return self.v 9 | 10 | class D: 11 | def get_c(self, c): 12 | return c 13 | 14 | def alias07(p, x, y): 15 | c0 = C(x) 16 | c1 = C(y) 17 | if p: 18 | c = D().get_c(c0) 19 | else: 20 | c = D().get_c(c1) 21 | c0.v += 10 22 | return c.get_v() 23 | 24 | @testbench 25 | def test(): 26 | assert 11 == alias07(True, 1, 2) 27 | assert 12 == alias07(True, 2, 3) 28 | assert 2 == alias07(False, 1, 2) 29 | assert 3 == alias07(False, 2, 3) 30 | -------------------------------------------------------------------------------- /tests/class/alias08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class D: 4 | def get_v(self, v): 5 | return v 6 | 7 | class C: 8 | def __init__(self, v): 9 | self.v = v 10 | 11 | def alias08(x): 12 | s = 0 13 | for i in range(x): 14 | c = C(i) 15 | s += c.v 16 | return s 17 | 18 | @testbench 19 | def test(): 20 | assert 1+2+3 == alias08(4) 21 | assert 0 == alias08(0) 22 | -------------------------------------------------------------------------------- /tests/class/alias09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, v): 5 | self.v = v 6 | 7 | def get_v(self): 8 | return self.v 9 | 10 | def alias09(p, x, y): 11 | c0 = C(x) 12 | c1 = C(y) 13 | if p: 14 | c2 = c0 15 | else: 16 | c2 = c1 17 | c2.v += 10 18 | return c2.get_v() 19 | 20 | @testbench 21 | def test(): 22 | assert 11 == alias09(True, 1, 2) 23 | assert 12 == alias09(True, 2, 3) 24 | assert 12 == alias09(False, 1, 2) 25 | assert 13 == alias09(False, 2, 3) 26 | -------------------------------------------------------------------------------- /tests/class/alias10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x, y, z): 5 | self.x = x 6 | self.y = y 7 | self.z = z 8 | 9 | 10 | def alias10(p, a, b, c): 11 | c0 = C(a, b, c) 12 | c1 = C(b, c, a) 13 | c2 = C(c, a, b) 14 | d = c0 15 | if p == 0: 16 | d = c0 17 | elif p == 1: 18 | d = c1 19 | elif p == 2: 20 | d = c2 21 | d.x += d.y * 2 + d.z * 3 22 | return d.x + d.y 23 | 24 | 25 | @testbench 26 | def test(): 27 | assert 16 == alias10(0, 1, 2, 3) 28 | assert 21 == alias10(1, 2, 3, 4) 29 | assert 26 == alias10(2, 3, 4, 5) 30 | assert 37 == alias10(3, 4, 5, 6) 31 | -------------------------------------------------------------------------------- /tests/class/callable01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __call__(self, x): 5 | return x 6 | 7 | def call01(x): 8 | c = C() 9 | return c(x) 10 | 11 | @testbench 12 | def test(): 13 | assert 1 == call01(1) 14 | assert 2 == call01(2) 15 | assert 3 == call01(3) 16 | -------------------------------------------------------------------------------- /tests/class/callable02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self): 5 | self.count = 0 6 | 7 | def __call__(self): 8 | self.count += 1 9 | 10 | def call02(cnt): 11 | c = C() 12 | for i in range(cnt): 13 | c() 14 | return c.count 15 | 16 | @testbench 17 | def test(): 18 | assert 10 == call02(10) 19 | assert 20 == call02(20) 20 | assert 30 == call02(30) 21 | -------------------------------------------------------------------------------- /tests/class/callable03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self): 5 | self.count = 0 6 | 7 | def __call__(self): 8 | self.count += 1 9 | 10 | def fun(c, cnt): 11 | for i in range(cnt): 12 | c() 13 | 14 | def call03(cnt): 15 | c = C() 16 | fun(c, cnt) 17 | return c.count 18 | 19 | @testbench 20 | def test(): 21 | assert 10 == call03(10) 22 | assert 20 == call03(20) 23 | assert 30 == call03(30) 24 | -------------------------------------------------------------------------------- /tests/class/callable04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self): 5 | self.count = 0 6 | 7 | def __call__(self, step): 8 | self.count += step 9 | 10 | def fun(c, cnt, step): 11 | for i in range(cnt): 12 | c(step) 13 | 14 | def call04(cnt, step): 15 | c = C() 16 | fun(c, cnt, step) 17 | return c.count 18 | 19 | @testbench 20 | def test(): 21 | assert 10 == call04(10, 1) 22 | assert 40 == call04(20, 2) 23 | assert 90 == call04(30, 3) 24 | -------------------------------------------------------------------------------- /tests/class/callable05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def __call__(self): 8 | return self.x 9 | 10 | class D: 11 | def __init__(self, c): 12 | self.c = c 13 | 14 | def __call__(self): 15 | return self.c 16 | 17 | 18 | def call05(x): 19 | d = D(C(x)) 20 | c = d() 21 | return c() 22 | 23 | @testbench 24 | def test(): 25 | assert 1 == call05(1) 26 | assert 2 == call05(2) 27 | assert 3 == call05(3) 28 | -------------------------------------------------------------------------------- /tests/class/classfield01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | j = 1 5 | i = 1233 + j 6 | 7 | def classfield01(): 8 | return C.i 9 | 10 | @testbench 11 | def test(): 12 | assert 1234 == classfield01() 13 | -------------------------------------------------------------------------------- /tests/class/classfield02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | i = (1, 2, 3) 5 | 6 | def classfield02(i): 7 | return C.i[i] 8 | 9 | @testbench 10 | def test(): 11 | assert 1 == classfield02(0) 12 | assert 2 == classfield02(1) 13 | assert 3 == classfield02(2) 14 | -------------------------------------------------------------------------------- /tests/class/classfield03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | i = (1, 2, 3) 5 | j = (1, 2, 3) 6 | k = i[j[0]] + i[j[1]] 7 | 8 | def classfield03(): 9 | return C.k 10 | 11 | @testbench 12 | def test(): 13 | assert 5 == classfield03() 14 | -------------------------------------------------------------------------------- /tests/class/classfield04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | class C: 5 | data = (1, 2, 3) 6 | 7 | def func(self, x): 8 | return func_a(x) 9 | 10 | 11 | def func_a(x): 12 | return C.data[x] 13 | 14 | 15 | def classfield04(x): 16 | c = C() 17 | return c.func(x) 18 | 19 | 20 | @testbench 21 | def test(): 22 | assert 1 == classfield04(0) 23 | assert 2 == classfield04(1) 24 | assert 3 == classfield04(2) 25 | -------------------------------------------------------------------------------- /tests/class/classfield05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | class D: 5 | i = (1, 2, 3) 6 | 7 | 8 | class C: 9 | i = (D.i[2], D.i[2], D.i[0]) 10 | 11 | 12 | def classfield05(x): 13 | return C.i[x] 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert 3 == classfield05(0) 19 | assert 3 == classfield05(1) 20 | assert 1 == classfield05(2) 21 | -------------------------------------------------------------------------------- /tests/class/classfield06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | V1 = 1 5 | V2 = 1 + V1 + 0 6 | 7 | class C: 8 | class D: 9 | class E: 10 | V3 = V1 + V2 11 | i = (V1, V2, E.V3) 12 | i = (D.i[2], D.i[2], D.i[0]) 13 | 14 | def classfield06(x): 15 | return C.i[x] 16 | 17 | 18 | @testbench 19 | def test(): 20 | assert 3 == classfield06(0) 21 | assert 3 == classfield06(1) 22 | assert 1 == classfield06(2) 23 | -------------------------------------------------------------------------------- /tests/class/classfield07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | class C: 5 | i = 1234 6 | 7 | def __init__(self): 8 | self.v1 = self.i 9 | self.v2 = C.i 10 | 11 | 12 | def classfield07(): 13 | return C.i == C().v1 == C().v2 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert classfield07() 19 | -------------------------------------------------------------------------------- /tests/class/classfield08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | class C: 5 | class D: 6 | i = (10, 20, 30) 7 | 8 | class E: 9 | i = (100, 200, 300) 10 | 11 | 12 | def classfield08(x): 13 | return C.D.i[x] + C.E.i[x] 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert 110 == classfield08(0) 19 | assert 220 == classfield08(1) 20 | assert 330 == classfield08(2) 21 | -------------------------------------------------------------------------------- /tests/class/composition01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class D: 4 | def __init__(self, x): 5 | self.x = x 6 | #self.y = y 7 | 8 | class C: 9 | def __init__(self, x): 10 | self.d = D(x) 11 | 12 | def composition01(x): 13 | c = C(x) 14 | a = c.d.x + c.d.x 15 | c.d.x = 10 16 | return a + c.d.x 17 | 18 | @testbench 19 | def test(): 20 | assert 12 == composition01(1) 21 | assert 14 == composition01(2) 22 | assert 4 == composition01(-3) 23 | -------------------------------------------------------------------------------- /tests/class/composition02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class D: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | class C: 8 | def __init__(self, x, y): 9 | self.d1 = D(x) 10 | self.d2 = D(y) 11 | 12 | def composition02(x, y): 13 | c = C(x, y) 14 | a = c.d1.x + c.d2.x 15 | c.d1.x = 10 16 | return a + c.d1.x 17 | 18 | @testbench 19 | def test(): 20 | assert 13 == composition02(1, 2) 21 | assert 16 == composition02(2, 4) 22 | assert 10 == composition02(-3, 3) 23 | -------------------------------------------------------------------------------- /tests/class/composition03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class E: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | class D: 8 | def __init__(self, x): 9 | self.e = E(x) 10 | 11 | class C: 12 | def __init__(self, x): 13 | self.d = D(x) 14 | 15 | def composition03(x): 16 | c = C(x) 17 | a = c.d.e.x + c.d.e.x 18 | c.d.e.x = 10 19 | return a + c.d.e.x 20 | 21 | @testbench 22 | def test(): 23 | assert 12 == composition03(1) 24 | assert 14 == composition03(2) 25 | assert 4 == composition03(-3) 26 | -------------------------------------------------------------------------------- /tests/class/composition04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class D: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def get_x(self): 8 | return self.x 9 | 10 | class C: 11 | def __init__(self, x): 12 | self.d = D(x) 13 | 14 | def get_x(self): 15 | return self.d.get_x() 16 | 17 | def composition04(x): 18 | c = C(x) 19 | a = c.get_x() + c.get_x() 20 | return a 21 | 22 | @testbench 23 | def test(): 24 | assert 2 == composition04(1) 25 | assert 4 == composition04(2) 26 | assert -6 == composition04(-3) 27 | -------------------------------------------------------------------------------- /tests/class/field01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x, y): 5 | self.x = x 6 | self.y = y 7 | 8 | def field01(x, y): 9 | c = C(x, y) 10 | return c.x + c.y 11 | 12 | @testbench 13 | def test(): 14 | assert 3 == field01(1, 2) 15 | assert 5 == field01(2, 3) 16 | assert 7 == field01(3, 4) 17 | -------------------------------------------------------------------------------- /tests/class/field02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x, y): 5 | self.x = x 6 | self.y = y 7 | 8 | def field02(x, y): 9 | c = C(x, y) 10 | a = c.x + c.y 11 | c.x = 10 12 | return a + c.x 13 | 14 | @testbench 15 | def test(): 16 | assert 13 == field02(1, 2) 17 | assert 15 == field02(2, 3) 18 | assert 17 == field02(3, 4) 19 | -------------------------------------------------------------------------------- /tests/class/field03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x, y): 5 | self.x = x 6 | self.y = y 7 | 8 | def field03(x, y): 9 | c = C(x, y) 10 | d = C(x+1, y+1) 11 | a = c.x + c.y + d.x + d.y 12 | c.x = 10 13 | return a + c.x 14 | 15 | @testbench 16 | def test(): 17 | assert 18 == field03(1, 2) 18 | assert 22 == field03(2, 3) 19 | assert 26 == field03(3, 4) 20 | -------------------------------------------------------------------------------- /tests/class/field04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.lst = [x] 6 | self.x = x 7 | 8 | def field04(x): 9 | c = C(x) 10 | c.lst[0] = c.lst[0] 11 | return c.lst[0] 12 | 13 | @testbench 14 | def test(): 15 | assert 1 == field04(1) 16 | assert 2 == field04(2) 17 | assert 3 == field04(3) 18 | -------------------------------------------------------------------------------- /tests/class/field05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def field05(x): 8 | c = C(x) 9 | c = C(x+1) 10 | return c.x 11 | 12 | @testbench 13 | def test(): 14 | assert 2 == field05(1) 15 | assert 3 == field05(2) 16 | assert 4 == field05(3) 17 | -------------------------------------------------------------------------------- /tests/class/field06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.t = (x,) 6 | 7 | def field06(x): 8 | c = C(x) 9 | return c.t[0] 10 | 11 | @testbench 12 | def test(): 13 | assert 1 == field06(1) 14 | assert 2 == field06(2) 15 | assert 3 == field06(3) 16 | -------------------------------------------------------------------------------- /tests/class/method01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def get_x(self): 8 | return self.x 9 | 10 | def method01(x): 11 | c = C(x) 12 | return c.get_x() + c.get_x() 13 | 14 | @testbench 15 | def test(): 16 | assert 2 == method01(1) 17 | assert 4 == method01(2) 18 | assert 6 == method01(3) 19 | -------------------------------------------------------------------------------- /tests/class/method02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def get_x(self): 8 | return self.x 9 | 10 | def set_x(self, x): 11 | self.x = x 12 | 13 | def method02(x): 14 | c = C(x) 15 | a = c.get_x() + c.get_x() 16 | c.set_x(x*2) 17 | b = c.x + c.x 18 | return a + b 19 | 20 | @testbench 21 | def test(): 22 | assert 6 == method02(1) 23 | assert 12 == method02(2) 24 | assert 18 == method02(3) 25 | -------------------------------------------------------------------------------- /tests/class/method03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x, y): 5 | self.x = x 6 | self.y = y 7 | 8 | def get_x(self): 9 | return self.x 10 | 11 | def get_y(self): 12 | return self.y 13 | 14 | def set_x(self, x): 15 | self.x = x 16 | 17 | def set_y(self, y): 18 | self.y = y 19 | 20 | def method03(x, y): 21 | c = C(x, y) 22 | a = c.get_x() + c.get_y() 23 | c.set_x(x*2) 24 | c.set_y(y*2) 25 | b = c.x + c.y 26 | return a + b 27 | 28 | @testbench 29 | def test(): 30 | assert 9 == method03(1, 2) 31 | assert 15 == method03(2, 3) 32 | assert 21 == method03(3, 4) 33 | -------------------------------------------------------------------------------- /tests/class/method04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def x(self, x): 5 | return x 6 | 7 | def method04(x): 8 | c = C() 9 | return c.x(x) 10 | 11 | @testbench 12 | def test(): 13 | assert 1 == method04(1) 14 | assert 2 == method04(2) 15 | assert 3 == method04(3) 16 | -------------------------------------------------------------------------------- /tests/class/method05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x * x 6 | 7 | def calc(self, x): 8 | return self.x * x 9 | 10 | def method05(x): 11 | return C(x).calc(x) 12 | 13 | @testbench 14 | def test(): 15 | assert 1 == method05(1) 16 | assert 8 == method05(2) 17 | assert 27 == method05(3) 18 | -------------------------------------------------------------------------------- /tests/class/method06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x * x 6 | 7 | def calc(self, x): 8 | for i in range(x): 9 | self.x += 1 10 | return self.x 11 | 12 | def method06(x): 13 | return C(x).calc(x) 14 | 15 | @testbench 16 | def test(): 17 | assert 2 == method06(1) 18 | assert 6 == method06(2) 19 | assert 12 == method06(3) 20 | -------------------------------------------------------------------------------- /tests/class/param01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def c_x_mul(cc:object): 8 | return cc.x * cc.x 9 | 10 | def param01(x): 11 | c = C(x) 12 | return c_x_mul(c) 13 | 14 | @testbench 15 | def test(): 16 | assert 1 == param01(1) 17 | assert 4 == param01(2) 18 | assert 9 == param01(3) 19 | -------------------------------------------------------------------------------- /tests/class/param02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def get_x(self): 8 | return self.x 9 | 10 | def c_get_x_mul(cc:object): 11 | return cc.get_x() * cc.get_x() 12 | 13 | def param02(x): 14 | c = C(x) 15 | return c_get_x_mul(c) 16 | 17 | @testbench 18 | def test(): 19 | assert 1 == param02(1) 20 | assert 4 == param02(2) 21 | assert 9 == param02(3) 22 | -------------------------------------------------------------------------------- /tests/class/param03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def get_x(self): 8 | return self.x 9 | 10 | def c_get_x_mul(c1, c2): 11 | return c1.get_x() * c2.get_x() 12 | 13 | def param03(x): 14 | c1 = C(x) 15 | c2 = C(x+1) 16 | return c_get_x_mul(c1, c2) 17 | 18 | @testbench 19 | def test(): 20 | assert 2 == param03(1) 21 | assert 6 == param03(2) 22 | assert 12 == param03(3) 23 | -------------------------------------------------------------------------------- /tests/class/param04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, x): 5 | self.x = x 6 | 7 | def get_x(self): 8 | return self.x 9 | 10 | class D: 11 | def __init__(self): 12 | pass 13 | 14 | def get_x(self, c): 15 | return c.x + c.get_x() 16 | 17 | def param04(x): 18 | c = C(x) 19 | d = D() 20 | return d.get_x(c) 21 | 22 | @testbench 23 | def test(): 24 | assert 2 == param04(1) 25 | assert 4 == param04(2) 26 | assert 6 == param04(3) 27 | -------------------------------------------------------------------------------- /tests/class/param05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | class C: 4 | def __init__(self, v): 5 | self.v = v 6 | 7 | class D: 8 | def __init__(self, c): 9 | self.c = c 10 | 11 | class E: 12 | def __init__(self, d): 13 | self.d = d 14 | 15 | def param05(p, x, y): 16 | c1 = C(x) 17 | c2 = C(y) 18 | d1 = D(c1) 19 | d2 = D(c2) 20 | if p: 21 | e = E(d1) 22 | else: 23 | e = E(d2) 24 | return e.d.c.v 25 | 26 | @testbench 27 | def test(): 28 | assert 1 == param05(True, 1, 2) 29 | assert 2 == param05(True, 2, 3) 30 | assert 2 == param05(False, 1, 2) 31 | assert 3 == param05(False, 2, 3) 32 | -------------------------------------------------------------------------------- /tests/error/callable.py: -------------------------------------------------------------------------------- 1 | #'f' is not callable 2 | from polyphony import testbench 3 | 4 | #def f(): 5 | # return 0 6 | 7 | def callable(): 8 | # This must be error in python 9 | f = 0 10 | return f() # f() is not callable 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == callable() 16 | 17 | 18 | test() 19 | -------------------------------------------------------------------------------- /tests/error/global01.py: -------------------------------------------------------------------------------- 1 | #Writing to a global object is not allowed 2 | from polyphony import testbench 3 | 4 | ram = [0] * 100 5 | 6 | 7 | def global01(): 8 | ram[0] = 123 9 | 10 | 11 | @testbench 12 | def test(): 13 | global01() 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/global02.py: -------------------------------------------------------------------------------- 1 | #A global instance is not supported 2 | from polyphony import testbench 3 | 4 | 5 | class C: 6 | def __init__(self, v): 7 | self.v = v 8 | 9 | c = C(100) 10 | 11 | 12 | def global02(): 13 | return c.v 14 | 15 | 16 | @testbench 17 | def test(): 18 | global02() 19 | 20 | 21 | test() 22 | -------------------------------------------------------------------------------- /tests/error/global03.py: -------------------------------------------------------------------------------- 1 | #Writing to a global object is not allowed 2 | from polyphony import testbench 3 | 4 | ram = [0] * 100 5 | 6 | 7 | class C: 8 | def __init__(self): 9 | self.mem = ram 10 | 11 | 12 | def global03(): 13 | c = C() 14 | c.mem[0] = 123 15 | 16 | 17 | @testbench 18 | def test(): 19 | global03() 20 | 21 | 22 | test() 23 | -------------------------------------------------------------------------------- /tests/error/global_module.py: -------------------------------------------------------------------------------- 1 | #Invalid access to a module class object 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class M: 8 | def __init__(self): 9 | self.i = Port(int, 'in') 10 | self.append_worker(self.w) 11 | 12 | def w(self): 13 | m.i.rd() 14 | 15 | 16 | m = M() 17 | -------------------------------------------------------------------------------- /tests/error/io01.py: -------------------------------------------------------------------------------- 1 | #Port object must created in the constructor of the module class 2 | from polyphony import testbench 3 | from polyphony.io import Port 4 | 5 | 6 | def io01(): 7 | p = Port(bool, 'in') 8 | p.wr(0) 9 | 10 | 11 | @testbench 12 | def test(): 13 | io01() 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/io02.py: -------------------------------------------------------------------------------- 1 | #Port object must created in the constructor of the module class 2 | from polyphony import testbench 3 | from polyphony.io import Port 4 | 5 | 6 | class C: 7 | def __init__(self): 8 | self.p = Port(bool, 'in') 9 | 10 | 11 | def io02(): 12 | c = C() 13 | c.p.wr(0) 14 | 15 | 16 | @testbench 17 | def test(): 18 | io02() 19 | 20 | 21 | test() 22 | -------------------------------------------------------------------------------- /tests/error/io03.py: -------------------------------------------------------------------------------- 1 | #The port class constructor accepts only constants 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class io03: 8 | def __init__(self, x): 9 | d = [x] * 10 10 | self.p = Port(bool, 'in', init=d[0]) 11 | self.append_worker(self.w) 12 | 13 | def w(self): 14 | d = self.p.rd() 15 | 16 | 17 | m = io03(1) 18 | -------------------------------------------------------------------------------- /tests/error/io_conflict01.py: -------------------------------------------------------------------------------- 1 | #Port direction of 'p' is conflicted 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class io_conflict01: 8 | def __init__(self): 9 | self.p = Port(bool, 'in') 10 | self.append_worker(self.w0, self.p) 11 | self.append_worker(self.w1, self.p) 12 | 13 | def w0(self, p): 14 | data = p.rd() 15 | 16 | def w1(self, p): 17 | p.wr(1) 18 | 19 | 20 | m = io_conflict01() 21 | -------------------------------------------------------------------------------- /tests/error/io_conflict02.py: -------------------------------------------------------------------------------- 1 | #Port direction of 'p' is conflicted 2 | from polyphony import testbench 3 | from polyphony import module 4 | from polyphony.io import Port 5 | 6 | 7 | @module 8 | class io_conflict02: 9 | def __init__(self): 10 | self.p = Port(int, 'in') 11 | self.append_worker(self.w) 12 | 13 | def w(self): 14 | data = self.p.rd() 15 | print(data) 16 | 17 | 18 | m = io_conflict02() 19 | 20 | 21 | @testbench 22 | def test(m): 23 | v = m.p.rd() 24 | 25 | 26 | test(m) 27 | -------------------------------------------------------------------------------- /tests/error/io_pipeline_read_conflict01.py: -------------------------------------------------------------------------------- 1 | #Reading from 'i' is conflicted in a pipeline 2 | from polyphony import rule 3 | from polyphony import module 4 | from polyphony import Channel 5 | from polyphony import is_worker_running 6 | 7 | 8 | @module 9 | class io_pipeline_read_conflict01: 10 | def __init__(self): 11 | self.i = Channel(int) 12 | self.o = Channel(int) 13 | self.append_worker(self.w, self.i, self.o) 14 | 15 | def w(self, i, o): 16 | with rule(scheduling='pipeline'): 17 | while is_worker_running(): 18 | v = i.get() 19 | v += i.get() 20 | o.put(v) 21 | 22 | 23 | m = io_pipeline_read_conflict01() 24 | -------------------------------------------------------------------------------- /tests/error/io_pipeline_write_conflict01.py: -------------------------------------------------------------------------------- 1 | #Writing to 'o' is conflicted in a pipeline 2 | from polyphony import rule 3 | from polyphony import module 4 | from polyphony import Channel 5 | from polyphony import is_worker_running 6 | 7 | 8 | @module 9 | class io_pipeline_write_conflict01: 10 | def __init__(self): 11 | self.i = Channel(int) 12 | self.o = Channel(int) 13 | self.append_worker(self.w, self.i, self.o) 14 | 15 | def w(self, i, o): 16 | with rule(scheduling='pipeline'): 17 | while is_worker_running(): 18 | v = i.get() 19 | o.put(v) 20 | o.put(v) 21 | 22 | 23 | m = io_pipeline_write_conflict01() 24 | -------------------------------------------------------------------------------- /tests/error/io_write_conflict01.py: -------------------------------------------------------------------------------- 1 | #Writing to 'p' is conflicted 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class io_write_conflict01: 8 | def __init__(self): 9 | self.p = Port(bool, 'out') 10 | self.append_worker(self.w, self.p) 11 | self.append_worker(self.w, self.p) 12 | 13 | def w(self, p): 14 | p.wr(1) 15 | 16 | 17 | m = io_write_conflict01() 18 | -------------------------------------------------------------------------------- /tests/error/io_write_conflict02.py: -------------------------------------------------------------------------------- 1 | #Writing to 'p' is conflicted 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class io_write_conflict02: 8 | def __init__(self): 9 | self.p = Port(bool, 'out') 10 | self.append_worker(self.w) 11 | self.append_worker(self.w) 12 | 13 | def w(self): 14 | self.p.wr(1) 15 | 16 | 17 | m = io_write_conflict02() 18 | -------------------------------------------------------------------------------- /tests/error/io_write_conflict03.py: -------------------------------------------------------------------------------- 1 | #Writing to 'p' is conflicted 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class io_write_conflict03: 8 | def __init__(self): 9 | self.p = Port(int, 'out') 10 | self.append_worker(w, self.p) 11 | self.append_worker(w, self.p) 12 | 13 | 14 | def w(p): 15 | p.wr(1) 16 | 17 | 18 | m = io_write_conflict03() 19 | -------------------------------------------------------------------------------- /tests/error/is_not_subscriptable01.py: -------------------------------------------------------------------------------- 1 | #'x' is not subscriptable 2 | from polyphony import testbench 3 | 4 | 5 | def is_not_subscriptable01(): 6 | x = 0 7 | return x[0] 8 | 9 | 10 | @testbench 11 | def test(): 12 | is_not_subscriptable01() 13 | 14 | 15 | test() 16 | -------------------------------------------------------------------------------- /tests/error/is_not_subscriptable02.py: -------------------------------------------------------------------------------- 1 | #'x' is not subscriptable 2 | from polyphony import testbench 3 | 4 | 5 | def is_not_subscriptable02(): 6 | x = 0 7 | x[0] = 0 8 | 9 | 10 | @testbench 11 | def test(): 12 | is_not_subscriptable02() 13 | 14 | 15 | test() 16 | -------------------------------------------------------------------------------- /tests/error/is_not_subscriptable03.py: -------------------------------------------------------------------------------- 1 | #'x' is not subscriptable 2 | from polyphony import testbench 3 | 4 | 5 | def is_not_subscriptable03(x): 6 | x[1] = 0 7 | 8 | 9 | @testbench 10 | def test(): 11 | is_not_subscriptable03(0) 12 | 13 | 14 | test() 15 | -------------------------------------------------------------------------------- /tests/error/list_multiplier.py: -------------------------------------------------------------------------------- 1 | #Type of sequence multiplier must be constant 2 | from polyphony import testbench 3 | 4 | 5 | def list_multiplier(x): 6 | l = [1, 2, 3] * x 7 | return l[0] 8 | 9 | 10 | @testbench 11 | def test(): 12 | list_multiplier(5) 13 | 14 | 15 | test() -------------------------------------------------------------------------------- /tests/error/localclass01.py: -------------------------------------------------------------------------------- 1 | #Local class definition in the function is not allowed 2 | def func(): 3 | class C: 4 | pass 5 | pass 6 | -------------------------------------------------------------------------------- /tests/error/loop_var01.py: -------------------------------------------------------------------------------- 1 | #Using the variable 'i' is restricted by polyphony's name scope rule 2 | from polyphony import testbench 3 | 4 | 5 | def loop_var01(): 6 | for i in range(10): 7 | pass 8 | return i 9 | 10 | 11 | @testbench 12 | def test(): 13 | loop_var01() 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/missing_required_arg01.py: -------------------------------------------------------------------------------- 1 | #missing_required_arg01() missing required argument 'x' 2 | from polyphony import testbench 3 | 4 | 5 | def missing_required_arg01(x): 6 | return x 7 | 8 | 9 | @testbench 10 | def test(): 11 | missing_required_arg01() 12 | 13 | 14 | test() 15 | -------------------------------------------------------------------------------- /tests/error/missing_required_arg02.py: -------------------------------------------------------------------------------- 1 | #polyphony.timing.clksleep() missing required argument 2 | from polyphony import testbench 3 | from polyphony.timing import clksleep 4 | 5 | 6 | def missing_required_arg02(): 7 | clksleep() 8 | 9 | 10 | @testbench 11 | def test(): 12 | missing_required_arg02() 13 | 14 | 15 | test() 16 | -------------------------------------------------------------------------------- /tests/error/missing_required_arg03.py: -------------------------------------------------------------------------------- 1 | #wait_rising() missing required argument 'port' 2 | from polyphony import module 3 | from polyphony.io import Port 4 | from polyphony.timing import wait_rising 5 | 6 | 7 | @module 8 | class missing_required_arg03: 9 | def __init__(self): 10 | p = Port(bool, 'in') 11 | self.append_worker(self.w, p) 12 | 13 | def w(self, p): 14 | wait_rising() 15 | 16 | 17 | m = missing_required_arg03() 18 | -------------------------------------------------------------------------------- /tests/error/module_args01.py: -------------------------------------------------------------------------------- 1 | #The type of @module class argument must be constant, not list 2 | from polyphony import module 3 | 4 | 5 | @module 6 | class module_args01: 7 | def __init__(self, arg): 8 | pass 9 | 10 | 11 | m = module_args01([1, 2, 3]) 12 | -------------------------------------------------------------------------------- /tests/error/module_method01.py: -------------------------------------------------------------------------------- 1 | #Calling a method of the module class can only in the module itself 2 | from polyphony import module 3 | 4 | 5 | @module 6 | class module_method01: 7 | def __init__(self): 8 | pass 9 | 10 | def func(self): 11 | pass 12 | 13 | 14 | m = module_method01() 15 | m.func() 16 | -------------------------------------------------------------------------------- /tests/error/module_method02.py: -------------------------------------------------------------------------------- 1 | #Calling append_worker method can only at the constructor 2 | from polyphony import module 3 | 4 | 5 | def w(): 6 | pass 7 | 8 | 9 | @module 10 | class module_method02: 11 | def __init__(self): 12 | pass 13 | 14 | 15 | m = module_method02() 16 | m.append_worker(w) 17 | -------------------------------------------------------------------------------- /tests/error/must_be_x_type01.py: -------------------------------------------------------------------------------- 1 | #Type of 'i' must be int, not str 2 | from polyphony import testbench 3 | 4 | 5 | def must_be_x_type01(): 6 | x = [0] 7 | i = '0' 8 | return x[i] 9 | 10 | 11 | @testbench 12 | def test(): 13 | must_be_x_type01() 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/must_be_x_type02.py: -------------------------------------------------------------------------------- 1 | #Type of 'i' must be int, not str 2 | from polyphony import testbench 3 | 4 | 5 | def must_be_x_type02(): 6 | x = [0] 7 | i = '0' 8 | x[i] = 0 9 | 10 | 11 | @testbench 12 | def test(): 13 | must_be_x_type02() 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/must_be_x_type03.py: -------------------------------------------------------------------------------- 1 | #Type of 'y' must be int, not list 2 | from polyphony import testbench 3 | 4 | 5 | def must_be_x_type03(): 6 | x = [0] 7 | y = [0] 8 | return x[y] 9 | 10 | 11 | @testbench 12 | def test(): 13 | must_be_x_type03() 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/must_be_x_type04.py: -------------------------------------------------------------------------------- 1 | #Type of 'y' must be int, not list 2 | from polyphony import testbench 3 | 4 | 5 | def must_be_x_type04(): 6 | x = [0] 7 | y = [0] 8 | x[y] = 0 9 | 10 | 11 | @testbench 12 | def test(): 13 | must_be_x_type04() 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/must_be_x_type05.py: -------------------------------------------------------------------------------- 1 | #Type of 'i' must be int, not str 2 | from polyphony import testbench 3 | 4 | 5 | def must_be_x_type05(i): 6 | x = [0] 7 | return x[i] 8 | 9 | 10 | @testbench 11 | def test(): 12 | must_be_x_type05('0') 13 | 14 | 15 | test() 16 | -------------------------------------------------------------------------------- /tests/error/must_be_x_type06.py: -------------------------------------------------------------------------------- 1 | #Type of 'i' must be int, not str 2 | from polyphony import testbench 3 | 4 | 5 | def must_be_x_type06(i): 6 | x = [0] 7 | x[i] = 0 8 | 9 | 10 | @testbench 11 | def test(): 12 | must_be_x_type06('0') 13 | 14 | 15 | test() 16 | -------------------------------------------------------------------------------- /tests/error/pipeline01.py: -------------------------------------------------------------------------------- 1 | #Cannot use 'break' statement in the pipeline loop 2 | from polyphony import testbench 3 | from polyphony import pipelined 4 | 5 | 6 | def pipeline01(): 7 | s = 0 8 | for i in pipelined(range(10)): 9 | if s > 10: 10 | break 11 | s += i 12 | return s 13 | 14 | 15 | @testbench 16 | def test(): 17 | pipeline01() 18 | 19 | 20 | test() 21 | -------------------------------------------------------------------------------- /tests/error/pipeline02.py: -------------------------------------------------------------------------------- 1 | #Cannot use 'continue' statement in the pipeline loop 2 | from polyphony import testbench 3 | from polyphony import pipelined 4 | 5 | 6 | def pipeline02(): 7 | s = 0 8 | for i in pipelined(range(10)): 9 | if s > 10: 10 | continue 11 | s += i 12 | return s 13 | 14 | 15 | @testbench 16 | def test(): 17 | pipeline02() 18 | 19 | 20 | test() 21 | -------------------------------------------------------------------------------- /tests/error/pipeline03.py: -------------------------------------------------------------------------------- 1 | #Normal function cannot be pipelined 2 | from polyphony import testbench 3 | from polyphony import rule 4 | 5 | 6 | @rule(scheduling='pipeline') 7 | def pipeline03(): 8 | s = 0 9 | for i in range(10): 10 | s += i 11 | return s 12 | 13 | 14 | @testbench 15 | def test(): 16 | pipeline03() 17 | 18 | 19 | test() 20 | -------------------------------------------------------------------------------- /tests/error/pipeline04.py: -------------------------------------------------------------------------------- 1 | #Flattening of multiple inner loops in a pipeline loop is not supported 2 | from polyphony import testbench 3 | from polyphony import pipelined 4 | 5 | 6 | def pipeline04(): 7 | s = 0 8 | for i in pipelined(range(10)): 9 | for j in range(10): 10 | s += i + j 11 | for j in range(12): 12 | s += i + j 13 | return s 14 | 15 | 16 | @testbench 17 | def test(): 18 | pipeline04() 19 | 20 | 21 | test() 22 | -------------------------------------------------------------------------------- /tests/error/pipeline05.py: -------------------------------------------------------------------------------- 1 | #'polyphony.pipelined' is incompatible type as a parameter of polyphony.pipelined() 2 | from polyphony import testbench 3 | from polyphony import pipelined 4 | 5 | 6 | def pipeline05(): 7 | s = 0 8 | for i in pipelined(pipelined(range(10))): 9 | if s > 10: 10 | break 11 | s += i 12 | return s 13 | 14 | 15 | @testbench 16 | def test(): 17 | pipeline05() 18 | 19 | 20 | test() 21 | -------------------------------------------------------------------------------- /tests/error/print01.py: -------------------------------------------------------------------------------- 1 | #print() takes only scalar type (e.g. int, str, ...) argument 2 | from polyphony import testbench 3 | 4 | 5 | @testbench 6 | def test(): 7 | print([1, 2, 3]) 8 | 9 | 10 | test() 11 | -------------------------------------------------------------------------------- /tests/error/print02.py: -------------------------------------------------------------------------------- 1 | #print() takes only scalar type (e.g. int, str, ...) argument 2 | from polyphony import testbench 3 | from polyphony import module 4 | from polyphony.io import Port 5 | 6 | 7 | @module 8 | class M: 9 | def __init__(self): 10 | self.in_q = Port(int, 'in') 11 | self.append_worker(self.w) 12 | 13 | def w(self): 14 | self.func(self.in_q) 15 | 16 | def func(self, q): 17 | print(q) 18 | 19 | 20 | @testbench 21 | def test(m): 22 | print([1, 2, 3]) 23 | 24 | 25 | if __name__ == '__main__': 26 | m = M() 27 | test(m) 28 | -------------------------------------------------------------------------------- /tests/error/pure01.py: -------------------------------------------------------------------------------- 1 | #@pure function must be in the global scope 2 | from polyphony import pure 3 | from polyphony import testbench 4 | 5 | 6 | def f(x): 7 | @pure 8 | def ff(x): 9 | return sum([i for i in range(x)]) 10 | return ff(x) 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 4950 == f(100) 16 | 17 | 18 | test() 19 | -------------------------------------------------------------------------------- /tests/error/pure02.py: -------------------------------------------------------------------------------- 1 | #An argument of @pure function must be constant 2 | from polyphony import pure 3 | from polyphony import testbench 4 | 5 | 6 | @pure 7 | def ff(x): 8 | return sum([i for i in range(x)]) 9 | 10 | 11 | def f(x): 12 | return ff(x) 13 | 14 | 15 | @testbench 16 | def test(): 17 | assert 4950 == f(100) 18 | 19 | 20 | test() 21 | -------------------------------------------------------------------------------- /tests/error/range01.py: -------------------------------------------------------------------------------- 1 | #Cannot use range() function outside of for statememt 2 | from polyphony import testbench 3 | 4 | 5 | def range01(): 6 | return range(10) 7 | 8 | 9 | @testbench 10 | def test(): 11 | range01() 12 | 13 | 14 | test() -------------------------------------------------------------------------------- /tests/error/redefined_name01.py: -------------------------------------------------------------------------------- 1 | #'func0' has been redefined 2 | from polyphony import testbench 3 | 4 | 5 | def func0(x): 6 | pass 7 | 8 | 9 | def func0(x): 10 | pass 11 | 12 | 13 | @testbench 14 | def test(): 15 | pass 16 | 17 | 18 | test() 19 | -------------------------------------------------------------------------------- /tests/error/redefined_name02.py: -------------------------------------------------------------------------------- 1 | #'C' has been redefined 2 | from polyphony import testbench 3 | 4 | 5 | class C: 6 | pass 7 | 8 | 9 | class C: 10 | pass 11 | 12 | 13 | @testbench 14 | def test(): 15 | pass 16 | 17 | 18 | test() 19 | -------------------------------------------------------------------------------- /tests/error/reserved_port_name.py: -------------------------------------------------------------------------------- 1 | #The name of Port 'clk' is reserved 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class reserved_port_name: 8 | def __init__(self): 9 | self.clk = Port(bool, 'out') 10 | 11 | 12 | m = reserved_port_name() 13 | -------------------------------------------------------------------------------- /tests/error/return_type01.py: -------------------------------------------------------------------------------- 1 | #Type of return value must be int, not bool 2 | from polyphony import testbench 3 | 4 | 5 | def return_type01(p): 6 | if p: 7 | return True 8 | else: 9 | return 1 10 | 11 | 12 | @testbench 13 | def test(): 14 | return_type01(True) 15 | 16 | 17 | test() 18 | -------------------------------------------------------------------------------- /tests/error/return_type02.py: -------------------------------------------------------------------------------- 1 | #Type of return value must be bool, not int 2 | from polyphony import testbench 3 | 4 | 5 | def return_type02(p): 6 | if p: 7 | return 1 8 | else: 9 | return True 10 | 11 | 12 | @testbench 13 | def test(): 14 | return_type02(True) 15 | 16 | 17 | test() 18 | -------------------------------------------------------------------------------- /tests/error/seq_capacity01.py: -------------------------------------------------------------------------------- 1 | #Sequence capacity is overflowing 2 | from polyphony import testbench 3 | from polyphony.typing import List, bit 4 | 5 | 6 | def seq_capacity01(xs:List[bit][4]): 7 | return xs[7] 8 | 9 | 10 | @testbench 11 | def test(): 12 | data = [0, 1, 1, 0, 1] # type: List[bit][4] 13 | seq_capacity01(data) 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/seq_capacity02.py: -------------------------------------------------------------------------------- 1 | #'data' is incompatible type as a parameter of seq_capacity02() 2 | from polyphony import testbench 3 | from polyphony.typing import List, bit 4 | 5 | 6 | def seq_capacity02(xs:List[bit][8]): 7 | return xs[7] 8 | 9 | 10 | @testbench 11 | def test(): 12 | data = [0, 1, 1, 0] # type: List[bit][4] 13 | seq_capacity02(data) 14 | 15 | 16 | test() 17 | -------------------------------------------------------------------------------- /tests/error/sub.py: -------------------------------------------------------------------------------- 1 | #Nothing is generated because any module or function didn't called in global scope. 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class Sub: 8 | def __init__(self): 9 | self.p = Port(int, 'in') 10 | self.append_worker(self.w) 11 | 12 | def w(self): 13 | x = self.p.rd() -------------------------------------------------------------------------------- /tests/error/timed_loop01.py: -------------------------------------------------------------------------------- 1 | #While statement is not allowed in 'timed' scheduling 2 | from polyphony import testbench 3 | from polyphony.timing import timed 4 | 5 | 6 | @timed 7 | @testbench 8 | def test(): 9 | while True: 10 | pass 11 | 12 | 13 | test() 14 | -------------------------------------------------------------------------------- /tests/error/timed_loop02.py: -------------------------------------------------------------------------------- 1 | #Only polyphony.timing.clkrange() can be used at for-in statement in 'timed' scheduling 2 | from polyphony import testbench 3 | from polyphony.timing import timed 4 | 5 | 6 | @timed 7 | @testbench 8 | def test(): 9 | for i in range(10): 10 | pass 11 | 12 | 13 | test() 14 | -------------------------------------------------------------------------------- /tests/error/toomany_args01.py: -------------------------------------------------------------------------------- 1 | #toomany_args01() takes 1 positional arguments but 2 were given 2 | from polyphony import testbench 3 | 4 | 5 | def toomany_args01(x): 6 | return x 7 | 8 | 9 | @testbench 10 | def test(): 11 | toomany_args01(1, 1) 12 | 13 | 14 | test() 15 | -------------------------------------------------------------------------------- /tests/error/toomany_args02.py: -------------------------------------------------------------------------------- 1 | #toomany_args02() takes 2 positional arguments but 3 were given 2 | from polyphony import testbench 3 | 4 | 5 | def toomany_args02(x=0, y=0): 6 | return x + y 7 | 8 | 9 | @testbench 10 | def test(): 11 | toomany_args02(1, 2, 3) 12 | 13 | 14 | test() 15 | -------------------------------------------------------------------------------- /tests/error/typing01.py: -------------------------------------------------------------------------------- 1 | #A type hint is conflicted 2 | a: int = 0 3 | a: str = '0' 4 | -------------------------------------------------------------------------------- /tests/error/typing02.py: -------------------------------------------------------------------------------- 1 | #A type hint is conflicted 2 | class C: 3 | def __init__(self): 4 | self.a: int = 0 5 | 6 | def func(self): 7 | self.a: str = '0' 8 | -------------------------------------------------------------------------------- /tests/error/typing03.py: -------------------------------------------------------------------------------- 1 | #A type hint for other than 'self.*' is not supported 2 | class C: 3 | def __init__(self): 4 | self.a: int = 0 5 | 6 | def func(): 7 | c = C() 8 | c.a:str = '0' 9 | -------------------------------------------------------------------------------- /tests/error/unroll01.py: -------------------------------------------------------------------------------- 1 | #Cannot unroll nested loop 2 | from polyphony import testbench 3 | from polyphony import unroll 4 | 5 | 6 | def unroll01(x): 7 | sum = 0 8 | for i in unroll(range(4), 2): 9 | for j in range(4): 10 | sum += (i * j * x) 11 | return sum 12 | 13 | 14 | @testbench 15 | def test(): 16 | unroll01(1) 17 | 18 | 19 | test() -------------------------------------------------------------------------------- /tests/error/unroll02.py: -------------------------------------------------------------------------------- 1 | #The step value must be a constant 2 | from polyphony import testbench 3 | from polyphony import unroll 4 | 5 | 6 | def unroll02(start, stop, step): 7 | sum = 0 8 | for i in unroll(range(start, stop, step), 5): 9 | sum += i 10 | return sum 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 35 == unroll02(5, 10, 1) 16 | 17 | 18 | test() 19 | -------------------------------------------------------------------------------- /tests/error/unroll03.py: -------------------------------------------------------------------------------- 1 | #'polyphony.unroll' is incompatible type as a parameter of polyphony.unroll() 2 | from polyphony import testbench 3 | from polyphony import unroll 4 | 5 | 6 | def unroll03(x): 7 | sum = 0 8 | for i in unroll(unroll(range(4))): 9 | sum += i 10 | return sum 11 | 12 | 13 | @testbench 14 | def test(): 15 | unroll03(1) 16 | 17 | 18 | test() -------------------------------------------------------------------------------- /tests/error/unroll04.py: -------------------------------------------------------------------------------- 1 | #Cannot use polyphony.unroll() function outside of for statememt 2 | from polyphony import testbench 3 | from polyphony import unroll 4 | 5 | 6 | def unroll04(): 7 | u = unroll([1, 2, 3], 4) 8 | 9 | 10 | @testbench 11 | def test(): 12 | unroll04() 13 | 14 | 15 | test() -------------------------------------------------------------------------------- /tests/error/unroll05.py: -------------------------------------------------------------------------------- 1 | #The unroll factor value must be a constant 2 | from polyphony import testbench 3 | from polyphony import unroll 4 | 5 | 6 | def unroll05(x, factor): 7 | sum = 0 8 | for i in unroll(range(4), factor): 9 | sum += i 10 | return sum 11 | 12 | 13 | @testbench 14 | def test(): 15 | unroll05(1, 1) 16 | 17 | 18 | test() -------------------------------------------------------------------------------- /tests/error/unsupported_expr01.py: -------------------------------------------------------------------------------- 1 | #Unsupported expression 2 | from polyphony import testbench 3 | 4 | 5 | def unsupported_expr01(a): 6 | return not not a 7 | 8 | 9 | @testbench 10 | def test(): 11 | unsupported_expr01(True) 12 | 13 | 14 | test() 15 | -------------------------------------------------------------------------------- /tests/error/unsupported_expr02.py: -------------------------------------------------------------------------------- 1 | #Unsupported expression 2 | from polyphony import testbench 3 | 4 | 5 | def unsupported_expr02(a): 6 | return [0] + a 7 | 8 | 9 | @testbench 10 | def test(): 11 | unsupported_expr02(1) 12 | 13 | 14 | test() 15 | -------------------------------------------------------------------------------- /tests/error/unsupported_expr03.py: -------------------------------------------------------------------------------- 1 | #Unsupported expression 2 | from polyphony import testbench 3 | 4 | 5 | def unsupported_expr03(a): 6 | l = [0] 7 | return l[[a]] 8 | 9 | 10 | @testbench 11 | def test(): 12 | unsupported_expr03(1) 13 | 14 | 15 | test() 16 | -------------------------------------------------------------------------------- /tests/expr/expr01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr01(a): 4 | return a+1+1 5 | 6 | @testbench 7 | def test(): 8 | assert 2 == expr01(0) 9 | assert 3 == expr01(1) 10 | assert 4 == expr01(2) 11 | -------------------------------------------------------------------------------- /tests/expr/expr02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr02(a): 4 | return -a+1-1 5 | 6 | @testbench 7 | def test(): 8 | assert 0 == expr02(0) 9 | assert -1 == expr02(1) 10 | assert -2 == expr02(2) 11 | 12 | -------------------------------------------------------------------------------- /tests/expr/expr03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr03(a): 4 | return -a*1 + a*-1 5 | 6 | @testbench 7 | def test(): 8 | assert 0 == expr03(0) 9 | assert -2 == expr03(1) 10 | assert -4 == expr03(2) 11 | -------------------------------------------------------------------------------- /tests/expr/expr04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr04(a): 4 | return +a & ~0xff00 5 | 6 | @testbench 7 | def test(): 8 | assert 0 == expr04(0) 9 | assert 0 == expr04(0x1100) 10 | assert 0x20 == expr04(0x2020) 11 | assert 0x300030 == expr04(0x303030) 12 | -------------------------------------------------------------------------------- /tests/expr/expr05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def expr05(a): 5 | return not a 6 | 7 | 8 | @testbench 9 | def test(): 10 | assert False == expr05(True) 11 | assert True == expr05(False) 12 | assert True is not expr05(True) 13 | assert False is not expr05(False) 14 | 15 | assert 0 == expr05(1) 16 | assert 1 == expr05(0) 17 | assert 0 == expr05(not 0) 18 | assert 1 == expr05(not 1) 19 | -------------------------------------------------------------------------------- /tests/expr/expr06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr06(a, b): 4 | return a < b 5 | 6 | @testbench 7 | def test(): 8 | assert False == expr06(0, 0) 9 | assert True == expr06(0, 1) 10 | assert False == expr06(1, 0) 11 | assert True == expr06(-1, 0) 12 | assert True == expr06(-2, -1) 13 | assert False == expr06(1, -1) 14 | -------------------------------------------------------------------------------- /tests/expr/expr07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr07(a, b): 4 | return a > b 5 | 6 | @testbench 7 | def test(): 8 | assert False == expr07(0, 0) 9 | assert False == expr07(0, 1) 10 | assert True == expr07(1, 0) 11 | assert False == expr07(-1, 0) 12 | assert False == expr07(-2, -1) 13 | assert True == expr07(1, -1) 14 | -------------------------------------------------------------------------------- /tests/expr/expr08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr08(a, b, c): 4 | return a < b < c 5 | 6 | @testbench 7 | def test(): 8 | assert False == expr08(0, 0, 0) 9 | assert True == expr08(0, 1, 2) 10 | assert False == expr08(1, 0, 2) 11 | assert False == expr08(2, 1, 0) 12 | assert True == expr08(-1, 0, 1) 13 | assert True == expr08(-2, -1, 0) 14 | -------------------------------------------------------------------------------- /tests/expr/expr09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr09(a, b): 4 | return a ^ b 5 | 6 | @testbench 7 | def test(): 8 | assert 1 == expr09(0b1000, 0b1001) 9 | assert 3 == expr09(0b1000, 0b1011) 10 | assert 1 == expr09(0b1010, 0b1011) 11 | -------------------------------------------------------------------------------- /tests/expr/expr10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | #'and' evaluation precedes 'or' 4 | def expr10(a, b, c): 5 | x = a | b & c 6 | y = (a|b) & c 7 | z = a | (b&c) 8 | return (x != y) and (x==z) 9 | 10 | @testbench 11 | def test(): 12 | assert True == expr10(0b1000, 0b1001, 0b0011) 13 | assert True == expr10(0b1000, 0b1111, 0b0111) 14 | assert True == expr10(0b1001, 0b1001, 0b1000) 15 | -------------------------------------------------------------------------------- /tests/expr/expr11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr11(a, b): 4 | return a << b 5 | 6 | @testbench 7 | def test(): 8 | assert 1 == expr11(0b0001, 0) 9 | assert 2 == expr11(0b0001, 1) 10 | assert 24 == expr11(0b0011, 3) 11 | -------------------------------------------------------------------------------- /tests/expr/expr12.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def expr12(a, b): 4 | return a >> b 5 | 6 | @testbench 7 | def test(): 8 | assert 1 == expr12(0b0001, 0) 9 | assert 0 == expr12(0b0001, 1) 10 | assert 1 == expr12(0b1111, 3) 11 | assert -128 == expr12(-255, 1) 12 | -------------------------------------------------------------------------------- /tests/func/func01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func01(x): 4 | def inner(y): 5 | return y + 1 6 | return inner(x) 7 | 8 | @testbench 9 | def test(): 10 | assert 1 == func01(0) 11 | assert 2 == func01(1) 12 | assert 3 == func01(2) 13 | -------------------------------------------------------------------------------- /tests/func/func02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func02(x): 4 | def f(a): 5 | return a 6 | return f(x) + f(x+1) + f(-x) 7 | 8 | @testbench 9 | def test(): 10 | assert 1 == func02(0) 11 | assert 2 == func02(1) 12 | assert 3 == func02(2) 13 | -------------------------------------------------------------------------------- /tests/func/func03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func03(x): 4 | def f(a): 5 | return a 6 | def g(a): 7 | return -a 8 | 9 | return f(x) + f(x+1) + g(x+2) + g(x+3) 10 | 11 | @testbench 12 | def test(): 13 | assert -4 == func03(0) 14 | assert -4 == func03(1) 15 | assert -4 == func03(2) 16 | -------------------------------------------------------------------------------- /tests/func/func04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func04(x): 4 | def f(a): 5 | return g(a) + g(a + 1) 6 | 7 | def g(a): 8 | return a * a 9 | 10 | return f(x) 11 | 12 | @testbench 13 | def test(): 14 | assert 1 == func04(0) 15 | assert 5 == func04(1) 16 | assert 13 == func04(2) 17 | -------------------------------------------------------------------------------- /tests/func/func05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func05(x): 4 | def f(a): 5 | return a * 2 6 | 7 | a = [x+1, x+2] 8 | print(a[0]) 9 | ret = f(a[0]) 10 | return ret 11 | 12 | @testbench 13 | def test(): 14 | assert 2 == func05(0) 15 | assert 4 == func05(1) 16 | assert 6 == func05(2) 17 | -------------------------------------------------------------------------------- /tests/func/func06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func06(x): 4 | def f(a:list, idx:int): 5 | a[idx] *= 2 6 | return a[idx] 7 | 8 | y = [1,2,3] 9 | z = [2,3,4] 10 | f(y, 0) 11 | f(y, 1) 12 | f(y, 2) 13 | return y[x] 14 | 15 | @testbench 16 | def test(): 17 | assert 2 == func06(0) 18 | assert 4 == func06(1) 19 | assert 6 == func06(2) 20 | -------------------------------------------------------------------------------- /tests/func/func07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func07(x): 4 | def f(a:list, idx:int): 5 | a[idx] *= 2 6 | b = a[idx] 7 | return b 8 | 9 | y = [1,2,3] 10 | z = [2,3,4] 11 | f(y, 0) 12 | f(z, 1) 13 | f(y, 2) 14 | return y[x] 15 | 16 | @testbench 17 | def test(): 18 | assert 2 == func07(0) 19 | assert 2 == func07(1) 20 | assert 6 == func07(2) 21 | -------------------------------------------------------------------------------- /tests/func/func08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def f(a:list, idx): 4 | a[idx] *= 2 5 | 6 | def func08(x): 7 | y = [1,2,3] 8 | f(y, x) 9 | return y[x] 10 | 11 | def func08_b(x): 12 | y = [4,5,6] 13 | f(y, x) 14 | return y[x] 15 | 16 | @testbench 17 | def test(): 18 | assert 2 == func08(0) 19 | assert 4 == func08(1) 20 | assert 6 == func08(2) 21 | 22 | assert 8 == func08_b(0) 23 | assert 10 == func08_b(1) 24 | assert 12 == func08_b(2) 25 | -------------------------------------------------------------------------------- /tests/func/func09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | def f(l1:list, l2:list): 3 | return l1[0] + l2[0] 4 | 5 | def func09(x): 6 | a = [x+0] 7 | b = [x+1] 8 | c = [x+2] 9 | t1 = f(a, b) 10 | t2 = f(b, a) 11 | return t1 + t2 12 | 13 | def func09_b(x): 14 | a = [x+0] 15 | b = [x+1] 16 | c = [x+2] 17 | t1 = f(a, b) 18 | t2 = f(a, c) 19 | return t1 + t2 20 | 21 | @testbench 22 | def test(): 23 | assert 2 == func09(0) 24 | assert 6 == func09(1) 25 | assert 10 == func09(2) 26 | 27 | assert 3 == func09_b(0) 28 | assert 7 == func09_b(1) 29 | assert 11 == func09_b(2) 30 | -------------------------------------------------------------------------------- /tests/func/func10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func10(x, y, z): 4 | def inner(a): 5 | return a 6 | 7 | data0 = inner(y) 8 | data1 = inner(z) 9 | if x: 10 | d = data0 11 | else: 12 | d = data1 13 | return d 14 | 15 | @testbench 16 | def test(): 17 | assert 3 == func10(0, 2, 3) 18 | assert 2 == func10(1, 2, 3) 19 | -------------------------------------------------------------------------------- /tests/func/func11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def f(l1:list, l2:list): 4 | return l1[0] + l2[0] 5 | 6 | 7 | def func11(): 8 | a = [1] 9 | b = [2] 10 | t1 = f(a, b) 11 | t2 = f(b, a) 12 | return t1 + t2 13 | 14 | 15 | @testbench 16 | def test(): 17 | assert 6 == func11() 18 | -------------------------------------------------------------------------------- /tests/func/kwargs01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def func(spam, ham, eggs): 5 | return spam, ham, eggs 6 | 7 | 8 | def kwargs01(idx, spam, ham, eggs): 9 | t = func(spam=spam, ham=ham, eggs=eggs) 10 | return t[idx] 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == kwargs01(0, eggs=2, spam=0, ham=1) 16 | assert 1 == kwargs01(1, ham=1, eggs=2, spam=0) 17 | assert 2 == kwargs01(2, spam=0, ham=1, eggs=2) 18 | assert 2 == kwargs01(0, 2, ham=3, eggs=4) 19 | assert 3 == kwargs01(1, 2, 3, eggs=4) 20 | assert 4 == kwargs01(idx=2, spam=2, ham=3, eggs=4) 21 | -------------------------------------------------------------------------------- /tests/func/kwargs02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def func(spam, ham, eggs): 5 | return spam, ham, eggs 6 | 7 | 8 | def kwargs02(idx, spam, ham, eggs): 9 | t = func(spam, eggs=eggs, ham=ham) 10 | return t[idx] 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == kwargs02(0, eggs=2, spam=0, ham=1) 16 | assert 1 == kwargs02(1, ham=1, eggs=2, spam=0) 17 | assert 2 == kwargs02(2, spam=0, ham=1, eggs=2) 18 | assert 2 == kwargs02(0, 2, ham=3, eggs=4) 19 | assert 3 == kwargs02(1, 2, 3, eggs=4) 20 | assert 4 == kwargs02(idx=2, spam=2, ham=3, eggs=4) 21 | -------------------------------------------------------------------------------- /tests/func/param01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def param01(x, y = 10, z = 20): 4 | return x + y + z 5 | 6 | @testbench 7 | def test(): 8 | assert 3 == param01(0, 1, 2) 9 | assert 21 == param01(0, 1) 10 | assert 30 == param01(0) 11 | -------------------------------------------------------------------------------- /tests/func/special01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def special01(x): 4 | def inner(a, b, c): 5 | return a + b + c 6 | 7 | return inner(0, x, 0) + inner(1, 0, 0) 8 | 9 | @testbench 10 | def test(): 11 | assert 1 == special01(0) 12 | assert 2 == special01(1) 13 | assert 3 == special01(2) 14 | -------------------------------------------------------------------------------- /tests/func/special02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def special02(x): 4 | def inner(a): 5 | def inner2(b): 6 | return b 7 | def inner3(b): 8 | return b 9 | return inner2(a) + inner3(0) 10 | 11 | return inner(x) + inner(1) 12 | 13 | @testbench 14 | def test(): 15 | assert 2 == special02(1) 16 | -------------------------------------------------------------------------------- /tests/func/special03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def f(d:list): 4 | return d[0] 5 | 6 | def g(d:list): 7 | return f(d) 8 | 9 | def special03(x): 10 | data1 = [1, 2, 3] 11 | data2 = [4, 5, 6] 12 | 13 | x = g(data1) 14 | y = g(data2) 15 | return x + y 16 | 17 | @testbench 18 | def test(): 19 | assert 5 == special03(1) 20 | -------------------------------------------------------------------------------- /tests/func/sysfunc01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | @testbench 4 | def test(): 5 | assert 1 6 | assert not 0 7 | -------------------------------------------------------------------------------- /tests/func/sysfunc02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def sysfunc02(x): 4 | s = 'string' + ' ' + 'is' + ' printable' 5 | print(s, x*2) 6 | return x 7 | 8 | @testbench 9 | def test(): 10 | assert 0 == sysfunc02(0) 11 | assert 1 == sysfunc02(1) 12 | assert 2 == sysfunc02(2) 13 | -------------------------------------------------------------------------------- /tests/if/if01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if01(x, y): 4 | if x == 0: 5 | z = y 6 | y *= 2 7 | y *= 3 8 | y *= 4 9 | y *= 5 10 | y *= 6 11 | else: 12 | z = y 13 | 14 | return y + z 15 | 16 | @testbench 17 | def test(): 18 | assert 721 == if01(0, 1) 19 | assert 2 == if01(1, 1) 20 | assert 4 == if01(1, 2) 21 | assert 6 == if01(1, 3) 22 | assert 8 == if01(1, 4) 23 | assert 1442 == if01(0, 2) 24 | -------------------------------------------------------------------------------- /tests/if/if02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if02(x, y, z): 4 | if x == 0: 5 | y *= 2 6 | elif y == 0: 7 | z *= 3 8 | elif z == 0: 9 | x *= 3 10 | else: 11 | z = y 12 | 13 | return y + z 14 | 15 | @testbench 16 | def test(): 17 | assert 4 == if02(0, 1, 2) 18 | assert 3 == if02(2, 0, 1) 19 | assert 2 == if02(1, 2, 0) 20 | -------------------------------------------------------------------------------- /tests/if/if03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if03(x, y): 4 | if x == 0: 5 | z = y 6 | y *= 2 7 | y *= 3 8 | if y > 10: 9 | y *= 4 10 | else: 11 | y *= 5 12 | y *= 6 13 | else: 14 | z = y 15 | 16 | return y + z 17 | 18 | @testbench 19 | def test(): 20 | assert 181 == if03(0, 1) 21 | assert 2 == if03(1, 1) 22 | assert 290 == if03(0, 2) 23 | -------------------------------------------------------------------------------- /tests/if/if04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if04(x, y): 4 | i = 0 5 | z = x + y 6 | while True: 7 | i += 1 8 | if i > 10: break 9 | 10 | if x == 0: 11 | if y == 0: 12 | z = i 13 | elif x == 1: 14 | z = 1 15 | else: 16 | z = y 17 | 18 | return y + z 19 | 20 | @testbench 21 | def test(): 22 | assert 10 == if04(0, 0) 23 | assert 2 == if04(1, 1) 24 | assert 4 == if04(2, 2) 25 | -------------------------------------------------------------------------------- /tests/if/if05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if05(x, y): 4 | z = 0 5 | if x > 0: 6 | if y > 0: 7 | z = 3 8 | z += 1 9 | 10 | if x > 5: 11 | if y > 5: 12 | z = 5 13 | z += 1 14 | 15 | return y + z 16 | 17 | @testbench 18 | def test(): 19 | assert 2 == if05(0, 0) 20 | assert 6 == if05(1, 1) 21 | assert 16 == if05(10, 10) 22 | -------------------------------------------------------------------------------- /tests/if/if06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if06(x, y): 4 | z = 0 5 | if True: 6 | z = x + y 7 | return z 8 | 9 | @testbench 10 | def test(): 11 | assert 0 == if06(0, 0) 12 | assert 2 == if06(1, 1) 13 | assert 20 == if06(10, 10) 14 | -------------------------------------------------------------------------------- /tests/if/if07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if07(x, y): 4 | z = 0 5 | if False: 6 | z = x + y 7 | return z 8 | 9 | @testbench 10 | def test(): 11 | assert 0 == if07(0, 0) 12 | assert 0 == if07(1, 1) 13 | assert 0 == if07(10, 10) 14 | -------------------------------------------------------------------------------- /tests/if/if08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if08(x, y): 4 | z = 0 5 | c = [1] 6 | if c[0]: 7 | z = x + y 8 | return z 9 | 10 | @testbench 11 | def test(): 12 | assert 0 == if08(0, 0) 13 | assert 2 == if08(1, 1) 14 | assert 20 == if08(10, 10) 15 | -------------------------------------------------------------------------------- /tests/if/if09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if09(x, y): 4 | z = 0 5 | c = [0] 6 | if c[0]: 7 | z = x + y 8 | return z 9 | 10 | @testbench 11 | def test(): 12 | assert 0 == if09(0, 0) 13 | assert 0 == if09(1, 1) 14 | assert 0 == if09(10, 10) 15 | -------------------------------------------------------------------------------- /tests/if/if10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if10(x, y, z): 4 | if x == 0: 5 | if y == 0: 6 | z = 0 7 | elif y == 1: 8 | z = 1 9 | else: 10 | z = 2 11 | elif x == 1: 12 | z = 1 13 | else: 14 | z = 2 15 | 16 | return z 17 | 18 | @testbench 19 | def test(): 20 | assert 0 == if10(0, 0, 1) 21 | assert 1 == if10(0, 1, 0) 22 | assert 2 == if10(0, 2, 0) 23 | assert 1 == if10(1, 0, 0) 24 | assert 2 == if10(2, 0, 0) 25 | -------------------------------------------------------------------------------- /tests/if/if11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if11(x): 4 | if True: 5 | y = 3 6 | if x == 1: 7 | y = 1 8 | elif x == 2: 9 | y = 2 10 | return y 11 | 12 | @testbench 13 | def test(): 14 | assert 3 == if11(0) 15 | assert 1 == if11(1) 16 | assert 2 == if11(2) 17 | assert 3 == if11(3) 18 | assert 3 == if11(4) 19 | -------------------------------------------------------------------------------- /tests/if/if12.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if12(x, y, z): 4 | if x == 0: 5 | z = 0 6 | elif x == 1: 7 | if y == 0: 8 | z = 0 9 | elif y == 1: 10 | z = 1 11 | else: 12 | z = 2 13 | z = 1 14 | else: 15 | z = 2 16 | 17 | return z 18 | 19 | @testbench 20 | def test(): 21 | assert 0 == if12(0, 1, 2) 22 | assert 2 == if12(2, 0, 1) 23 | assert 1 == if12(1, 2, 0) 24 | -------------------------------------------------------------------------------- /tests/if/if13.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if13(x): 4 | i = 1 5 | if x < 10: 6 | if x == 0: 7 | pass 8 | elif x == 1: 9 | i = 2 10 | else: 11 | i = 3 12 | return i 13 | 14 | 15 | @testbench 16 | def test(): 17 | assert 1 == if13(0) 18 | assert 2 == if13(1) 19 | assert 1 == if13(2) 20 | assert 3 == if13(10) 21 | -------------------------------------------------------------------------------- /tests/if/if14.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def if14(x): 4 | i = 1 5 | if x < 10: 6 | if x == 0: 7 | pass 8 | elif x == 1: 9 | pass 10 | i = 2 11 | else: 12 | pass 13 | return i 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert 2 == if14(0) 19 | assert 2 == if14(1) 20 | assert 2 == if14(2) 21 | assert 1 == if14(10) 22 | -------------------------------------------------------------------------------- /tests/if/if15.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f1(b): 5 | if b: 6 | return 1 7 | else: 8 | return 2 9 | 10 | 11 | def f2(b): 12 | return f1(b) 13 | 14 | 15 | def if15(): 16 | return f2(False) + f1(True) 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 3 == if15() 22 | -------------------------------------------------------------------------------- /tests/if/if16.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f1(b): 5 | if b == 0: 6 | pass 7 | 8 | if b == 1: 9 | return 1 10 | 11 | return 2 12 | 13 | 14 | def if16(): 15 | return f1(0) 16 | 17 | 18 | @testbench 19 | def test(): 20 | assert 2 == if16() 21 | -------------------------------------------------------------------------------- /tests/if/if17.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def if17(x, y): 5 | while True: 6 | if x == 0: 7 | y = 10 8 | elif x == 1: 9 | pass 10 | elif x == 2: 11 | pass 12 | 13 | if y == 0: 14 | y += 1 15 | else: 16 | if y > 1: 17 | y += 1 18 | elif y > 2: 19 | y += 1 20 | else: 21 | pass 22 | pass 23 | if x != 0: 24 | break 25 | x += 1 26 | print(x, y) 27 | return x + y 28 | 29 | 30 | @testbench 31 | def test(): 32 | assert 13 == if17(0, 0) 33 | assert 2 == if17(1, 1) 34 | assert 5 == if17(2, 2) 35 | -------------------------------------------------------------------------------- /tests/if/if18.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def if18(x): 5 | if x < 10: 6 | pass 7 | elif x < 20: 8 | x = -x 9 | elif x < 30: 10 | pass 11 | else: 12 | x = x * x 13 | 14 | return x 15 | 16 | 17 | @testbench 18 | def test(): 19 | assert 0 == if18(0) 20 | assert -10 == if18(10) 21 | assert 20 == if18(20) 22 | assert 900 == if18(30) 23 | -------------------------------------------------------------------------------- /tests/if/if19.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def if19(x): 5 | if x < 10: 6 | pass 7 | elif x < 20: 8 | return x + 1 9 | elif x < 30: 10 | assert False 11 | else: 12 | pass 13 | return x 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert 3 == if19(3) 19 | assert 14 == if19(13) 20 | assert 30 == if19(30) 21 | -------------------------------------------------------------------------------- /tests/if/if20.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def if20(x): 5 | y = 0 6 | if x < 100: 7 | if x < 10: 8 | y += 1 9 | elif x < 20: 10 | y += 2 11 | elif x < 30: 12 | y += 3 13 | else: 14 | pass 15 | y += 20 16 | else: 17 | y = 100 18 | return y 19 | 20 | 21 | @testbench 22 | def test(): 23 | assert 21 == if20(0) 24 | assert 22 == if20(10) 25 | assert 23 == if20(20) 26 | assert 20 == if20(30) 27 | assert 100 == if20(100) 28 | -------------------------------------------------------------------------------- /tests/if/if21.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def if21(x): 5 | y = 0 6 | if x < 100: 7 | if x < 10: 8 | y = 10 9 | else: 10 | y = 20 11 | #y += 1 12 | else: 13 | if x < 200: 14 | y = 100 15 | else: 16 | y = 200 17 | y += 2 18 | return y 19 | 20 | 21 | @testbench 22 | def test(): 23 | assert 10 == if21(0) 24 | assert 20 == if21(10) 25 | assert 20 == if21(20) 26 | assert 20 == if21(30) 27 | assert 102 == if21(100) 28 | assert 202 == if21(200) 29 | -------------------------------------------------------------------------------- /tests/if/if22.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def func(data, p): 5 | if p == 0: 6 | data[0] = data[0] * data[1] 7 | return data[0] 8 | elif p == 1: 9 | data[1] = data[1] * data[2] 10 | return data[1] 11 | elif p == 2: 12 | data[2] = data[2] * data[3] 13 | return data[2] 14 | return 0 15 | 16 | def if22(p): 17 | data = [1, 2, 3, 4] 18 | return func(data, p) 19 | 20 | 21 | @testbench 22 | def test(): 23 | assert 2 == if22(0) 24 | assert 6 == if22(1) 25 | assert 12 == if22(2) 26 | assert 0 == if22(3) 27 | -------------------------------------------------------------------------------- /tests/if/if23.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | class C: 5 | def __init__(self): 6 | self.v1 = 0 7 | self.v2 = 0 8 | 9 | def set_v(self, i, v): 10 | if i == 0: 11 | pass 12 | elif i == 1: 13 | self.v1 = v 14 | elif i == 2: 15 | self.v2 = v 16 | else: 17 | return 18 | 19 | 20 | def if23(): 21 | c = C() 22 | c.set_v(1, 10) 23 | c.set_v(2, 20) 24 | return c.v1 + c.v2 25 | 26 | 27 | @testbench 28 | def test(): 29 | assert 30 == if23() 30 | -------------------------------------------------------------------------------- /tests/if/if24.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(p): 5 | if p == 0: 6 | return 1 7 | else: 8 | if p == 1: 9 | return 2 10 | elif p == 2: 11 | return 3 12 | return 0 13 | 14 | 15 | def if24(x): 16 | return f(x) 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 1 == if24(0) 22 | assert 2 == if24(1) 23 | assert 3 == if24(2) 24 | -------------------------------------------------------------------------------- /tests/if/if25.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(v, i): 5 | if i == 0: 6 | return v 7 | else: 8 | v += 1 9 | if i == 1: 10 | return v + v 11 | else: 12 | for k in range(8): 13 | v += 1 14 | return v 15 | 16 | 17 | def if25(): 18 | v = f(1, 0) 19 | return v 20 | 21 | 22 | @testbench 23 | def test(): 24 | assert 1 == if25() 25 | -------------------------------------------------------------------------------- /tests/if/if26.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(v, i): 5 | if i == 0: 6 | return v 7 | elif i == 1: 8 | return v + 1 9 | else: 10 | for k in range(i): 11 | v += 1 12 | return v 13 | 14 | 15 | def if26(code, r1, r2): 16 | if code == 0: 17 | return f(r1, r2) 18 | return 0 19 | 20 | 21 | @testbench 22 | def test(): 23 | assert 2 == if26(0, 1, 1) 24 | assert 4 == if26(0, 2, 2) 25 | assert 6 == if26(0, 3, 3) 26 | assert 8 == if26(0, 4, 4) 27 | assert 0 == if26(1, 1, 1) 28 | -------------------------------------------------------------------------------- /tests/if/if27.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(v, i): 5 | if i == 0: 6 | return v 7 | elif i == 1: 8 | for k in range(i): 9 | v += 2 10 | return v 11 | else: 12 | for k in range(i): 13 | v += 1 14 | return v 15 | 16 | 17 | def if27(code, r1, r2): 18 | if code == 0: 19 | return f(r1, r2) 20 | return 0 21 | 22 | 23 | @testbench 24 | def test(): 25 | assert 3 == if27(0, 1, 1) 26 | assert 4 == if27(0, 2, 2) 27 | assert 6 == if27(0, 3, 3) 28 | assert 0 == if27(1, 4, 4) 29 | -------------------------------------------------------------------------------- /tests/if/if29.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def if29(p0, p1, p2): 5 | x = 0 6 | if p0 == 0: 7 | pass 8 | elif p0 == 1: 9 | if p1 == 0: 10 | if p2 == 0: 11 | x = 10 12 | elif p1 == 1: 13 | pass 14 | elif p1 == 2: 15 | pass 16 | else: 17 | return -1 18 | #x = -1 19 | return x 20 | 21 | 22 | @testbench 23 | def test(): 24 | assert 0 == if29(0, 0, 0) 25 | assert 10 == if29(1, 0, 0) 26 | assert 0 == if29(1, 0, 1) 27 | assert 0 == if29(1, 1, 0) 28 | assert 0 == if29(1, 2, 0) 29 | assert -1 == if29(1, 3, 0) 30 | assert 0 == if29(2, 3, 0) 31 | -------------------------------------------------------------------------------- /tests/if/ifexp01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def ifexp01(x, y): 4 | return True if x == y else False 5 | 6 | @testbench 7 | def test(): 8 | assert False == ifexp01(0, 1) 9 | assert True == ifexp01(1, 1) 10 | assert False == ifexp01(True, False) 11 | -------------------------------------------------------------------------------- /tests/if/ifexp02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def ifexp02(x, y): 4 | return 1 if x > y else -1 if y > x else 0 5 | 6 | @testbench 7 | def test(): 8 | assert -1 == ifexp02(0, 1) 9 | assert 1 == ifexp02(1, 0) 10 | assert 0 == ifexp02(1, 1) 11 | -------------------------------------------------------------------------------- /tests/import/import01.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import polyphony.io 3 | import polyphony.timing 4 | import polyphony.typing 5 | import sub1 6 | 7 | 8 | def import01(): 9 | return (polyphony.__name__ == 'polyphony' and 10 | polyphony.io.__name__ == 'polyphony.io' and 11 | polyphony.timing.__name__ == 'polyphony.timing' and 12 | polyphony.typing.__name__ == 'polyphony.typing' and 13 | sub1.__name__ == 'sub1') 14 | 15 | 16 | @polyphony.testbench 17 | def test(): 18 | assert True == import01() 19 | -------------------------------------------------------------------------------- /tests/import/import02.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from polyphony import io as io_ 3 | from polyphony import timing as ti_ 4 | from polyphony import typing as ty_ 5 | 6 | 7 | def import02(): 8 | return (io_.__name__ == 'polyphony.io' and 9 | ti_.__name__ == 'polyphony.timing' and 10 | ty_.__name__ == 'polyphony.typing') 11 | 12 | 13 | @polyphony.testbench 14 | def test(): 15 | assert True == import02() 16 | -------------------------------------------------------------------------------- /tests/import/import03.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import sub1 as sub 3 | 4 | 5 | def import03(): 6 | return sub.__name__ == 'sub1' 7 | 8 | 9 | @polyphony.testbench 10 | def test(): 11 | assert True == import03() 12 | -------------------------------------------------------------------------------- /tests/import/import04.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from sub1 import func1 3 | import sub1 4 | 5 | 6 | def func2(x): 7 | return 0 8 | 9 | 10 | def import04(x): 11 | a = func1(x) 12 | b = sub1.func2(x) 13 | c = func2(x) 14 | return a + b + c 15 | 16 | 17 | @polyphony.testbench 18 | def test(): 19 | assert 11 == func1(10) 20 | assert 3 == sub1.func2(1) 21 | assert 0 == func2(1) 22 | assert 5 == import04(1) 23 | -------------------------------------------------------------------------------- /tests/import/import06.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import sub1 3 | import sub2 4 | from sub1 import func2 as func2_1 5 | from sub2 import func2 as func2_2 6 | 7 | 8 | def import06_a1(x): 9 | return sub1.func1(x) + sub2.func1(x) 10 | 11 | 12 | def import06_a2(x): 13 | return func2_1(x) + func2_2(x) 14 | 15 | 16 | @polyphony.testbench 17 | def test(): 18 | assert 31 == import06_a1(10) 19 | assert 42 == import06_a2(10) 20 | -------------------------------------------------------------------------------- /tests/import/import07.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import sub1 3 | import sub2 4 | import sub3 5 | 6 | 7 | def import07_a1(x): 8 | return sub1.func3(x) + sub2.func3(x) 9 | 10 | 11 | def import07_a2(x): 12 | return sub3.func1(x) + sub3.func1(x) + 30 13 | 14 | 15 | @polyphony.testbench 16 | def test(): 17 | print(import07_a1(10)) 18 | assert 250 == import07_a1(10) 19 | assert 250 == import07_a2(10) 20 | -------------------------------------------------------------------------------- /tests/import/import08.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from sub1 import SubC as Sub1 3 | from sub2 import SubC as Sub2 4 | 5 | 6 | def import08_a1(): 7 | return Sub1.VALUE1 8 | 9 | 10 | def import08_a2(): 11 | return Sub2.VALUE1 12 | 13 | 14 | def import08_b1(x): 15 | return Sub1.VALUE2[x] 16 | 17 | 18 | def import08_b2(x): 19 | return Sub2.VALUE2[x] 20 | 21 | 22 | @polyphony.testbench 23 | def test(): 24 | assert 1234 == import08_a1() 25 | assert 5678 == import08_a2() 26 | assert 1 == import08_b1(0) 27 | assert 5 == import08_b2(0) 28 | -------------------------------------------------------------------------------- /tests/import/import09.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from sub1 import SubC as Sub1 3 | from sub2 import SubC as Sub2 4 | from sub1 import get_v 5 | 6 | 7 | def import09_a1(x): 8 | return get_v(Sub1(x)) 9 | 10 | 11 | def import09_a2(x): 12 | return get_v(Sub2(x)) 13 | 14 | 15 | @polyphony.testbench 16 | def test(): 17 | assert 100 == import09_a1(10) 18 | assert 1000 == import09_a2(10) 19 | -------------------------------------------------------------------------------- /tests/import/import10.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import sub1 3 | from sub3 import SUB3_GLOBAL 4 | 5 | 6 | def import10_a(): 7 | return sub1.SUB1_GLOBAL 8 | 9 | 10 | def import10_b(): 11 | return SUB3_GLOBAL 12 | 13 | 14 | @polyphony.testbench 15 | def test(): 16 | assert 111 == import10_a() 17 | assert 333 == import10_b() 18 | -------------------------------------------------------------------------------- /tests/import/import12.py: -------------------------------------------------------------------------------- 1 | from polyphony import module 2 | from polyphony import testbench 3 | #import sub4 4 | from sub4 import sub4 5 | 6 | 7 | @module 8 | class import12: 9 | def __init__(self): 10 | self.sub4 = sub4() 11 | self.append_worker(self.w) 12 | 13 | def w(self): 14 | d = self.sub4.i.rd() 15 | self.sub4.o.wr(d) 16 | 17 | 18 | @testbench 19 | def test(): 20 | m = import12() 21 | m.sub4.i.wr(0) 22 | -------------------------------------------------------------------------------- /tests/import/import14.py: -------------------------------------------------------------------------------- 1 | from polyphony import module 2 | from polyphony import testbench 3 | from polyphony.modules import Handshake 4 | from sub4 import sub_worker as worker 5 | 6 | 7 | @module 8 | class import14: 9 | def __init__(self): 10 | self.i = Handshake(int, 'in') 11 | self.o = Handshake(int, 'out') 12 | self.append_worker(worker, self.i, self.o, loop=True) 13 | 14 | 15 | @testbench 16 | def test(): 17 | m = import14() 18 | m.i.wr(1) 19 | assert 2 == m.o.rd() 20 | m.i.wr(2) 21 | assert 3 == m.o.rd() 22 | m.i.wr(10) 23 | assert 11 == m.o.rd() 24 | -------------------------------------------------------------------------------- /tests/import/import_dir01.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import subdir.sub1 3 | from subdir import sub1 4 | 5 | 6 | def import_dir01(): 7 | return (polyphony.__name__ == 'polyphony' and 8 | subdir.sub1.__name__ == 'subdir.sub1' and 9 | sub1.__name__ == 'subdir.sub1') 10 | 11 | 12 | @polyphony.testbench 13 | def test(): 14 | assert True == import_dir01() 15 | -------------------------------------------------------------------------------- /tests/import/import_dir02.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from subdir import sub1 as sub1_ 3 | from subdir import sub2 as sub2_ 4 | 5 | 6 | def import_dir02(): 7 | return (sub1_.__name__ == 'subdir.sub1' and 8 | sub2_.__name__ == 'subdir.sub2') 9 | 10 | 11 | @polyphony.testbench 12 | def test(): 13 | assert True == import_dir02() 14 | -------------------------------------------------------------------------------- /tests/import/import_pkg01.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import subpkg as sp 3 | 4 | 5 | def import_pkg01(): 6 | return (sp.__name__ == 'subpkg' and 7 | sp.sub1.__name__ == 'subpkg.sub1') 8 | 9 | 10 | @polyphony.testbench 11 | def test(): 12 | assert True == import_pkg01() 13 | -------------------------------------------------------------------------------- /tests/import/import_pkg02.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import subpkg.sub1 3 | 4 | 5 | def import_pkg02(): 6 | return (subpkg.__name__ == 'subpkg' and 7 | subpkg.sub1.__name__ == 'subpkg.sub1') 8 | 9 | 10 | @polyphony.testbench 11 | def test(): 12 | assert True == import_pkg02() 13 | -------------------------------------------------------------------------------- /tests/import/import_pkg04.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from subpkg import SUBPKG_CONSTANT_100 3 | from subpkg import SUBPKG_CONSTANT_111 as const111 4 | 5 | 6 | def import_pkg04_1(): 7 | return SUBPKG_CONSTANT_100 8 | 9 | 10 | def import_pkg04_2(): 11 | return const111 12 | 13 | 14 | @polyphony.testbench 15 | def test(): 16 | assert 100 == import_pkg04_1() 17 | assert 111 == import_pkg04_2() 18 | -------------------------------------------------------------------------------- /tests/import/import_pkg05.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | import subpkg 3 | from subpkg.sub1 import func1, func2 4 | import subpkg.subsubpkg2.subsub2 5 | 6 | 7 | def import_pkg05_1(x): 8 | return subpkg.sub1.func1(x) 9 | 10 | 11 | def import_pkg05_2(x): 12 | return func1(x) 13 | 14 | 15 | def import_pkg05_3(x): 16 | return subpkg.subsubpkg2.subsub2.func2(x) 17 | 18 | 19 | def import_pkg05_4(x): 20 | return func2(x) 21 | 22 | 23 | @polyphony.testbench 24 | def test(): 25 | assert import_pkg05_1(10) == import_pkg05_2(10) 26 | assert import_pkg05_3(10) == import_pkg05_4(10) 27 | -------------------------------------------------------------------------------- /tests/import/sub1.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | import sub3 3 | 4 | 5 | SUB1_GLOBAL = 111 6 | SUB1_GLOBAL_ARRAY1 = (1, 2, 3, 4) 7 | SUB1_GLOBAL_ARRAY2 = (5, 6, 7, 8) 8 | 9 | def func1(x): 10 | return x + 1 11 | 12 | 13 | def func2(x): 14 | return x + 2 15 | 16 | 17 | def func3(x): 18 | return sub3.func1(x) 19 | 20 | 21 | def get_v(c): 22 | return c.v() 23 | 24 | 25 | class SubC: 26 | VALUE1 = 1234 27 | VALUE2 = (1, 2, 3, 4) 28 | 29 | def __init__(self, x): 30 | self.x = x * x 31 | 32 | def v(self): 33 | return self.x 34 | 35 | 36 | @testbench 37 | def sub_test(): 38 | assert 1 == func1(0) 39 | -------------------------------------------------------------------------------- /tests/import/sub2.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | import sub3 3 | 4 | 5 | def func1(x): 6 | return x + 10 7 | 8 | 9 | def func2(x): 10 | return x + 20 11 | 12 | 13 | def func3(x): 14 | return sub3.func1(x) + 30 15 | 16 | 17 | class SubC: 18 | VALUE1 = 5678 19 | VALUE2 = (5, 6, 7, 8) 20 | 21 | def __init__(self, x): 22 | self.x = x * x * x 23 | 24 | def v(self): 25 | return self.x 26 | 27 | 28 | @testbench 29 | def sub_test(): 30 | assert 10 == func1(0) 31 | -------------------------------------------------------------------------------- /tests/import/sub3.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | SUB3_GLOBAL = 333 5 | SUB3_GLOBAL_ARRAY1 = (31, 32, 33, 34) 6 | SUB3_GLOBAL_ARRAY2 = (35, 36, 37, 38) 7 | 8 | 9 | def func1(x): 10 | return x + 100 11 | 12 | 13 | @testbench 14 | def sub_test(): 15 | assert 100 == func1(0) 16 | -------------------------------------------------------------------------------- /tests/import/sub4.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | VALUE = 100 6 | 7 | @module 8 | class sub4: 9 | def __init__(self, param=10): 10 | self.i = Port(int, 'in', init=VALUE) 11 | self.o = Port(int, 'out') 12 | self.param = param 13 | 14 | 15 | def sub_worker(p0, p1): 16 | v = p0.rd() 17 | p1.wr(v + 1) 18 | 19 | 20 | @testbench 21 | def sub_test(): 22 | pass 23 | -------------------------------------------------------------------------------- /tests/import/subdir/sub1.py: -------------------------------------------------------------------------------- 1 | SUB1_GLOBAL = 111 2 | SUB1_GLOBAL_ARRAY1 = (1, 2, 3, 4) 3 | SUB1_GLOBAL_ARRAY2 = (5, 6, 7, 8) 4 | 5 | def func1(x): 6 | return x + 1 7 | 8 | 9 | def func2(x): 10 | return x + 2 11 | 12 | 13 | def get_v(c): 14 | return c.v() 15 | 16 | 17 | class SubC: 18 | VALUE1 = 1234 19 | VALUE2 = (1, 2, 3, 4) 20 | 21 | def __init__(self, x): 22 | self.x = x * x 23 | 24 | def v(self): 25 | return self.x 26 | -------------------------------------------------------------------------------- /tests/import/subdir/sub2.py: -------------------------------------------------------------------------------- 1 | def func1(x): 2 | return x + 10 3 | 4 | 5 | def func2(x): 6 | return x + 20 7 | 8 | 9 | class SubC: 10 | VALUE1 = 5678 11 | VALUE2 = (5, 6, 7, 8) 12 | 13 | def __init__(self, x): 14 | self.x = x * x * x 15 | 16 | def v(self): 17 | return self.x 18 | -------------------------------------------------------------------------------- /tests/import/subpkg/__init__.py: -------------------------------------------------------------------------------- 1 | from . import sub1 2 | 3 | SUBPKG_CONSTANT_100 = 100 4 | SUBPKG_CONSTANT_111 = sub1.SUB1_GLOBAL 5 | -------------------------------------------------------------------------------- /tests/import/subpkg/sub1.py: -------------------------------------------------------------------------------- 1 | from .subsubpkg.subsub1 import func1 as sub_f1 2 | 3 | SUB1_GLOBAL = 111 4 | SUB1_GLOBAL_ARRAY1 = (1, 2, 3, 4) 5 | SUB1_GLOBAL_ARRAY2 = (5, 6, 7, 8) 6 | 7 | 8 | def func1(x): 9 | return x + 1 10 | 11 | 12 | def func2(x): 13 | return sub_f1(x) 14 | 15 | 16 | def get_v(c): 17 | return c.v() 18 | 19 | 20 | class SubC: 21 | VALUE1 = 1234 22 | VALUE2 = (1, 2, 3, 4) 23 | 24 | def __init__(self, x): 25 | self.x = x * x 26 | 27 | def v(self): 28 | return self.x 29 | -------------------------------------------------------------------------------- /tests/import/subpkg/subsubpkg/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/tests/import/subpkg/subsubpkg/__init__.py -------------------------------------------------------------------------------- /tests/import/subpkg/subsubpkg/subsub1.py: -------------------------------------------------------------------------------- 1 | from ..subsubpkg2.subsub2 import func2 as f2 2 | from .. import subsubpkg2 3 | 4 | 5 | def func1(x): 6 | return f2(x) 7 | 8 | 9 | def func2(x): 10 | return subsubpkg2.subsub2.func2(x) 11 | -------------------------------------------------------------------------------- /tests/import/subpkg/subsubpkg2/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/polyphony-dev/polyphony/8774180a4875bf2e270324799fe0b65c972fd550/tests/import/subpkg/subsubpkg2/__init__.py -------------------------------------------------------------------------------- /tests/import/subpkg/subsubpkg2/subsub2.py: -------------------------------------------------------------------------------- 1 | def func2(x): 2 | return x + 20 3 | -------------------------------------------------------------------------------- /tests/io/assign01.py: -------------------------------------------------------------------------------- 1 | from polyphony import module, testbench 2 | from polyphony.io import Port 3 | from polyphony.timing import clkfence 4 | 5 | 6 | @module 7 | class assign01: 8 | def __init__(self, param): 9 | self.i = Port(int, 'in') 10 | self.o = Port(int, 'out') 11 | self.o.assign(lambda:self.i.rd() + param + 1) 12 | 13 | 14 | @testbench 15 | def test(): 16 | m = assign01(5) 17 | m.i.wr(10) 18 | clkfence() 19 | assert 16 == m.o.rd() 20 | -------------------------------------------------------------------------------- /tests/io/assign02.py: -------------------------------------------------------------------------------- 1 | from polyphony import module, testbench 2 | from polyphony.io import Port 3 | from polyphony.timing import clkfence 4 | 5 | 6 | @module 7 | class assign02: 8 | def __init__(self, param): 9 | self.i = Port(int, 'in') 10 | self.o = Port(int, 'out') 11 | self.param = param 12 | self.o.assign(self.func) 13 | 14 | def func(self): 15 | tmp = self.i.rd() + self.param 16 | tmp += 1 17 | return tmp 18 | 19 | 20 | @testbench 21 | def test(): 22 | m = assign02(5) 23 | m.i.wr(10) 24 | clkfence() 25 | assert 16 == m.o.rd() 26 | -------------------------------------------------------------------------------- /tests/io/handshake01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench, module 2 | from polyphony.modules import Handshake 3 | from polyphony.typing import int8 4 | from polyphony.timing import timed 5 | 6 | 7 | @module 8 | class handshake01: 9 | def __init__(self): 10 | self.i = Handshake(int8, 'in') 11 | self.o = Handshake(int8, 'out', init=0) 12 | self.append_worker(self.main) 13 | 14 | @timed 15 | def main(self): 16 | t = self.i.rd() 17 | self.o.wr(t * t) 18 | 19 | 20 | @timed 21 | @testbench 22 | def test(): 23 | p01 = handshake01() 24 | p01.i.wr(2) 25 | assert p01.o.rd() == 4 26 | -------------------------------------------------------------------------------- /tests/io/init01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench, module 2 | from polyphony.io import Port 3 | 4 | 5 | @module 6 | class init01: 7 | def __init__(self): 8 | self.p0 = Port(int, 'out', 123) 9 | self.p1 = Port(int, 'out', 456) 10 | 11 | 12 | @testbench 13 | def test(): 14 | m = init01() 15 | assert 123 == m.p0.rd() 16 | assert 456 == m.p1.rd() 17 | -------------------------------------------------------------------------------- /tests/issues/cfg01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | class C: 5 | def __init__(self): 6 | self.v1 = 0 7 | self.v2 = 0 8 | 9 | def set_v(self, i, v): 10 | if i == 0: 11 | pass 12 | elif i == 1: 13 | self.v1 = v 14 | elif i == 2: 15 | self.v2 = v 16 | else: 17 | return 18 | 19 | 20 | def cfg01(): 21 | c = C() 22 | c.set_v(1, 10) 23 | c.set_v(2, 20) 24 | return c.v1 + c.v2 25 | 26 | 27 | @testbench 28 | def test(): 29 | assert 30 == cfg01() 30 | -------------------------------------------------------------------------------- /tests/issues/cfg02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(p): 5 | if p == 0: 6 | return 1 7 | else: 8 | if p == 1: 9 | return 2 10 | elif p == 2: 11 | return 3 12 | return 0 13 | 14 | 15 | def cfg02(x): 16 | return f(x) 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 1 == cfg02(0) 22 | assert 2 == cfg02(1) 23 | assert 3 == cfg02(2) 24 | -------------------------------------------------------------------------------- /tests/issues/cfg03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(v, i): 5 | if i == 0: 6 | return v 7 | else: 8 | v += 1 9 | if i == 1: 10 | return v + v 11 | else: 12 | for k in range(8): 13 | v += 1 14 | return v 15 | 16 | 17 | def cfg03(a, b): 18 | v = f(1, 0) 19 | return v + f(a, b) 20 | 21 | 22 | @testbench 23 | def test(): 24 | assert 12 == cfg03(2, 2) 25 | -------------------------------------------------------------------------------- /tests/issues/cfg04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(v, i): 5 | if i == 0: 6 | print('!', v) 7 | return v 8 | elif i == 1: 9 | v += 1 10 | print('!!', v) 11 | return v 12 | else: 13 | for k in range(i): 14 | v += 1 15 | print('!!!', v) 16 | return v 17 | 18 | 19 | def cfg04(r0, r1): 20 | return f(r0, r1) 21 | 22 | 23 | @testbench 24 | def test(): 25 | assert 1 == cfg04(1, 0) 26 | assert 2 == cfg04(1, 1) 27 | assert 3 == cfg04(1, 2) 28 | -------------------------------------------------------------------------------- /tests/issues/cfg05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(v, i): 5 | if i == 0: 6 | print('!', v) 7 | return v 8 | elif i == 1: 9 | for k in range(i): 10 | v += 2 11 | print('!!', v) 12 | return v 13 | else: 14 | for k in range(i): 15 | v += 1 16 | print('!!!', v) 17 | return v 18 | 19 | 20 | def cfg05(r0, r1, r2): 21 | if r0 == 0: 22 | return f(r1, r2) 23 | return 0 24 | 25 | 26 | @testbench 27 | def test(): 28 | assert 0 == cfg05(1, 1, 1) 29 | assert 2 == cfg05(0, 2, 0) 30 | assert 3 == cfg05(0, 1, 1) 31 | assert 4 == cfg05(0, 0, 4) 32 | -------------------------------------------------------------------------------- /tests/issues/cfg07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def cfg07(p, vv): 5 | v = [vv] 6 | if p == 0: 7 | for i in range(4): 8 | v[0] += 1 9 | elif p == 1: 10 | for i in range(4): 11 | v[0] += 2 12 | elif p == 2: 13 | v[0] += 3 14 | else: 15 | assert p == 3 16 | v[0] += 1 17 | return v[0] 18 | 19 | @testbench 20 | def test(): 21 | assert 5 == cfg07(0, 1) 22 | assert 9 == cfg07(1, 1) 23 | assert 4 == cfg07(2, 1) 24 | assert 3 == cfg07(3, 2) 25 | -------------------------------------------------------------------------------- /tests/issues/inline.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def f(a): 5 | a += 1 6 | 7 | 8 | def inline(): 9 | a = 0 10 | f(a) 11 | return a 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == inline() 16 | -------------------------------------------------------------------------------- /tests/list/init01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | SIZE1 = 2 5 | SIZE2 = 10 6 | SIZE3 = 20 7 | 8 | 9 | def init01(): 10 | mem = [0] * SIZE1 * SIZE2 * SIZE3 11 | for i in range(SIZE1 * SIZE2 * SIZE3): 12 | mem[i] = i 13 | s = 0 14 | for m in mem: 15 | s += m 16 | return s 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 79800 == init01() 22 | -------------------------------------------------------------------------------- /tests/list/list01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list01(idx1, idx2): 4 | l = [0, 10, 20, 30] 5 | return l[idx1] + l[idx2] 6 | 7 | @testbench 8 | def test(): 9 | assert 10 == list01(0, 1) 10 | assert 50 == list01(2, 3) 11 | assert 30 == list01(3, 0) 12 | -------------------------------------------------------------------------------- /tests/list/list02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list02(x, y): 4 | a = [x, 2, 3]*3 5 | b = a 6 | for i in range(3): 7 | b[i] = a[i] * 2 8 | return a[x] + b[y] 9 | 10 | @testbench 11 | def test(): 12 | assert 6 == list02(1, 0) 13 | assert 12 == list02(2, 2) 14 | assert 5 == list02(3, 4) 15 | -------------------------------------------------------------------------------- /tests/list/list03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list03(x, y, z): 4 | a = [1, 2, 3] 5 | 6 | r0 = x 7 | r1 = y 8 | a[r0] = a[r1] + z 9 | return a[r0] 10 | 11 | @testbench 12 | def test(): 13 | assert 4 == list03(0, 1 ,2) 14 | assert 5 == list03(2, 1 ,3) 15 | -------------------------------------------------------------------------------- /tests/list/list04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list04(x): 4 | a = [1,2,3,4,5,6,7,8] 5 | b = [None]*12 6 | 7 | while True: 8 | for i in range(0, 8): 9 | b[i] = a[i] 10 | break 11 | return b[x] 12 | 13 | @testbench 14 | def test(): 15 | assert 1 == list04(0) 16 | assert 5 == list04(4) 17 | -------------------------------------------------------------------------------- /tests/list/list05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list05(x): 4 | a = [1,2,3,4]*100 5 | 6 | def f(x:list): 7 | for i in range(0, 4): 8 | x[i] *= 2 9 | 10 | def g(x:list): 11 | for i in range(0, 4): 12 | x[i] += 1 13 | 14 | f(a) 15 | g(a) 16 | return a[x] 17 | 18 | @testbench 19 | def test(): 20 | assert 3 == list05(0) 21 | assert 5 == list05(1) 22 | assert 7 == list05(2) 23 | assert 9 == list05(3) 24 | -------------------------------------------------------------------------------- /tests/list/list06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list06(x): 4 | data = [1, 3, 5, x] 5 | data[0] = 1 6 | a = data[0] 7 | b = data[1] 8 | c = data[2] 9 | return a + b + c + x 10 | 11 | @testbench 12 | def test(): 13 | assert 9 == list06(0) 14 | assert 10 == list06(1) 15 | assert 11 == list06(2) 16 | -------------------------------------------------------------------------------- /tests/list/list07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list07(x): 4 | a = [1,2,3,4,5,6,7,8] 5 | b = [0]*12 6 | 7 | def memcpy(a:list, b:list): 8 | for i in range(0, 8): 9 | b[i] = a[i] 10 | 11 | def memcheck(a:list, b:list): 12 | for i in range(0, 8): 13 | if b[i] != a[i]: 14 | return False 15 | return True 16 | 17 | memcpy(a, b) 18 | return memcheck(a, b) 19 | 20 | @testbench 21 | def test(): 22 | assert 1 == list07(0) 23 | -------------------------------------------------------------------------------- /tests/list/list08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list08(x, y, z): 4 | index = [x, y, z] 5 | data = [2, 3, 4] 6 | 7 | a = data[index[0]] 8 | b = data[index[1]] 9 | c = data[index[2]] 10 | return a + b * c 11 | 12 | @testbench 13 | def test(): 14 | assert 14 == list08(0, 1, 2) 15 | assert 11 == list08(1, 2, 0) 16 | assert 10 == list08(2, 0, 1) 17 | -------------------------------------------------------------------------------- /tests/list/list09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list09(x): 4 | data = [0, 1, 2] 5 | def f(d:list): 6 | def swap(dd:list, i, j): 7 | t = dd[i] 8 | dd[i] = dd[j] 9 | dd[j] = t 10 | swap(d, 0, 1) 11 | swap(d, 1, 2) 12 | 13 | f(data) 14 | return data[x] 15 | 16 | @testbench 17 | def test(): 18 | assert 1 == list09(0) 19 | assert 2 == list09(1) 20 | assert 0 == list09(2) 21 | -------------------------------------------------------------------------------- /tests/list/list10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list10(x, y): 4 | data1 = [0, 1, 2] 5 | data2 = [3, 4, 5] 6 | if x: 7 | d1 = data1 8 | return d1[y] 9 | else: 10 | d2 = data2 11 | return d2[y] 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == list10(1, 0) 16 | -------------------------------------------------------------------------------- /tests/list/list11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def sum(l1:list): 5 | def sum_x(l2:list): 6 | def sum_y(l2:list): 7 | s = 0 8 | for l in l2: 9 | s += l 10 | return s 11 | return sum_y(l2) 12 | return sum_x(l1) 13 | 14 | def list11(x): 15 | data1 = [x, 1, 2] 16 | data2 = [x, 1, 2, 3, 4, 5] 17 | s1 = sum(data1) 18 | s2 = sum(data2) 19 | return s1 + s2 + x 20 | 21 | @testbench 22 | def test(): 23 | assert 18 == list11(0) 24 | assert 21 == list11(1) 25 | assert 24 == list11(2) 26 | -------------------------------------------------------------------------------- /tests/list/list12.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list12(x): 4 | reg1 = [0] * 32 5 | reg2 = [10, 12, 14] 6 | reg1[0] = reg2[0] 7 | reg1[1] = reg2[1] 8 | reg1[2] = reg2[2] 9 | return reg1[0] + reg1[1] + reg1[2] 10 | 11 | @testbench 12 | def test(): 13 | assert 36 == list12(3) 14 | -------------------------------------------------------------------------------- /tests/list/list13.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list13(x): 4 | reg = [1,2,3,4] 5 | mem = [None] * 64 6 | for i in range(64): 7 | mem[i] = 0 8 | for i in range(4): 9 | mem[i] = reg[i] 10 | 11 | return mem[x] + mem[1] + mem[2] + mem[3] 12 | 13 | @testbench 14 | def test(): 15 | assert 10 == list13(0) 16 | -------------------------------------------------------------------------------- /tests/list/list14.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list14(x): 4 | rom = [1, None, 3, None, 5] 5 | ram = [2, None, 4, None, 6] 6 | ram[1] = 3 7 | return rom[0] + ram[1] + x 8 | 9 | @testbench 10 | def test(): 11 | assert 4 == list14(0) 12 | -------------------------------------------------------------------------------- /tests/list/list15.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list15(x): 4 | #rom = [1, 2, 3, 4, 5] 5 | ram = [0] * 5 6 | ram[0] = 1 7 | ram[1] = 2 8 | ram[2] = 3 9 | ram[3] = 1 10 | ram[4] = 5 11 | if x != 0: 12 | y = len(ram) - x 13 | else: 14 | y = 0 15 | return ram[x] + ram[y] 16 | 17 | @testbench 18 | def test(): 19 | assert 2 == list15(0) 20 | assert 7 == list15(1) 21 | assert 4 == list15(2) 22 | assert 4 == list15(3) 23 | assert 7 == list15(4) 24 | -------------------------------------------------------------------------------- /tests/list/list16.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list16(x, y): 4 | data = [0, 1, 2] 5 | while True: 6 | if x == 0: 7 | return 0 8 | #pass 9 | else: 10 | x = x + 1 11 | if y == 0: 12 | d = data[0] 13 | elif y == 2: 14 | data[0] = y 15 | d = data[0] 16 | else: 17 | d = 0 18 | break 19 | return d + x 20 | 21 | @testbench 22 | def test(): 23 | assert 0 == list16(0, 1) 24 | assert 4 == list16(1, 2) 25 | assert 2 == list16(1, 0) 26 | -------------------------------------------------------------------------------- /tests/list/list17.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list17(x): 5 | data0 = [1, 2, 3] 6 | data1 = [4, 5, 6] 7 | d = data0 8 | d = data1 9 | if x == 0: 10 | d = data0 11 | elif x == 1: 12 | d = data1 13 | elif x == 2: 14 | d = data0 15 | else: 16 | d = data1 17 | return d[0] 18 | 19 | 20 | @testbench 21 | def test(): 22 | assert 1 == list17(0) 23 | assert 4 == list17(1) 24 | assert 1 == list17(2) 25 | assert 4 == list17(3) 26 | -------------------------------------------------------------------------------- /tests/list/list18.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list18(x, y): 4 | insts = [0, 1, 2, 3] 5 | r = [0]*4 6 | r[0] = x 7 | i = 0 8 | while True: 9 | inst = insts[i] 10 | if inst == 0: 11 | r[0] = x 12 | elif inst == 1: 13 | r[1] = y 14 | elif inst == 2: 15 | r[2] = r[0] + r[1] 16 | elif inst == 3: 17 | break 18 | i += 1 19 | return r[2] 20 | 21 | @testbench 22 | def test(): 23 | assert 1 == list18(0, 1) 24 | assert 2 == list18(1, 1) 25 | assert 3 == list18(2, 1) 26 | -------------------------------------------------------------------------------- /tests/list/list19.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def sum(l1:list): 5 | s = 0 6 | for l in l1: 7 | s += l 8 | return s 9 | 10 | def list19(x): 11 | data1 = [x, 1, 2] 12 | data2 = [x, 1, 2, 3, 4, 5] 13 | s1 = sum(data1) 14 | s2 = sum(data2) 15 | return s1 + s2 + x 16 | 17 | @testbench 18 | def test(): 19 | assert 18 == list19(0) 20 | -------------------------------------------------------------------------------- /tests/list/list20.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list20(x): 4 | if x == 0: 5 | d = [x + 1, 1] 6 | elif x == 1: 7 | d = [x + 2, 2] 8 | else: 9 | d = [x + 3, 3] 10 | return d[0] 11 | 12 | @testbench 13 | def test(): 14 | assert 1 == list20(0) 15 | assert 3 == list20(1) 16 | assert 5 == list20(2) 17 | -------------------------------------------------------------------------------- /tests/list/list21.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list21(x): 4 | data0 = [x, x, x] 5 | data1 = [x] 6 | if x: 7 | d = data0 8 | else: 9 | d = data1 10 | return len(d) 11 | 12 | @testbench 13 | def test(): 14 | assert 1 == list21(0) 15 | assert 3 == list21(1) 16 | -------------------------------------------------------------------------------- /tests/list/list22.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list22(x, y): 4 | data0 = [x+1] 5 | data1 = [x] 6 | if x: 7 | a = data0 8 | b = data1 9 | else: 10 | a = data1 11 | b = data0 12 | if y: 13 | c = a 14 | else: 15 | c = b 16 | return c[0] 17 | 18 | @testbench 19 | def test(): 20 | assert 1 == list22(0, 0) 21 | assert 1 == list22(1, 0) 22 | assert 0 == list22(0, 1) 23 | assert 2 == list22(1, 1) 24 | -------------------------------------------------------------------------------- /tests/list/list23.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list23(x): 4 | def f(x:list): 5 | def ff(x:list): 6 | return x[0] 7 | return ff(x) 8 | data0 = [x+1] 9 | data1 = [x] 10 | if x: 11 | d = data0 12 | else: 13 | d = data1 14 | return f(d) 15 | 16 | @testbench 17 | def test(): 18 | assert 0 == list23(0) 19 | assert 2 == list23(1) 20 | -------------------------------------------------------------------------------- /tests/list/list24.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list24(x): 4 | data0 = [x] 5 | data1 = [x, x] 6 | data2 = [x, x, x] 7 | data3 = [x, x, x, x] 8 | if x == 0: 9 | d = data0 10 | elif x == 1: 11 | d = data1 12 | elif x == 2: 13 | d = data2 14 | else: 15 | d = data3 16 | return len(d) 17 | 18 | @testbench 19 | def test(): 20 | assert 1 == list24(0) 21 | assert 2 == list24(1) 22 | assert 3 == list24(2) 23 | assert 4 == list24(3) 24 | -------------------------------------------------------------------------------- /tests/list/list25.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def list25(x): 4 | def f(x:list, y:list, i): 5 | if i: 6 | z = x 7 | else: 8 | z = y 9 | return len(z) 10 | data0 = [x] 11 | data1 = [x, x] 12 | return f(data0, data1, x) 13 | 14 | @testbench 15 | def test(): 16 | assert 2 == list25(0) 17 | assert 1 == list25(1) 18 | -------------------------------------------------------------------------------- /tests/list/list26.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list26_a(i): 5 | xs = [0, 1, 2] 6 | return xs[i] 7 | 8 | def list26_b(i): 9 | xs = [3, 4, 5] 10 | return xs[i] 11 | 12 | 13 | @testbench 14 | def test(): 15 | data1 = [0, 1, 2] 16 | data2 = [3, 4, 5] 17 | for i in range(len(data1)): 18 | d = data1[i] 19 | assert d == list26_a(i) 20 | 21 | for i in range(len(data2)): 22 | d = data2[i] 23 | assert d == list26_b(i) 24 | -------------------------------------------------------------------------------- /tests/list/list27.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list27(x): 5 | mem = [x] * 4096 6 | return mem[0] 7 | 8 | 9 | @testbench 10 | def test(): 11 | assert 10 == list27(10) 12 | -------------------------------------------------------------------------------- /tests/list/list28.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list28(): 5 | xs = [1, 2, 3] 6 | xs[0] = 10 7 | x0 = xs[0] 8 | xs[0] = 100 9 | xs[1] = 11 10 | x00 = xs[0] 11 | x1 = xs[1] 12 | assert x0 == 10 13 | assert x00 == 100 14 | assert x1 == 11 15 | 16 | 17 | @testbench 18 | def test(): 19 | list28() 20 | -------------------------------------------------------------------------------- /tests/list/list29.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list29(p, x, y): 5 | xs = [x] 6 | ys = [y] 7 | if p: 8 | d = xs 9 | else: 10 | d = ys 11 | return d[0] 12 | 13 | 14 | @testbench 15 | def test(): 16 | assert 1 == list29(True, 1, 2) 17 | assert 4 == list29(False, 3, 4) 18 | -------------------------------------------------------------------------------- /tests/list/list30.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list30(k0, v): 5 | buf0 = [v] * 100 6 | ref0 = buf0 7 | while True: 8 | buf0 = ref0 9 | ref0 = buf0 10 | if k0: 11 | break 12 | return ref0[0] 13 | 14 | 15 | @testbench 16 | def test(): 17 | assert 3 == list30(1, 3) 18 | -------------------------------------------------------------------------------- /tests/list/list33.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list33(a): 5 | xs = [a] * 4 6 | s = 0 7 | for x in xs: 8 | s += x 9 | return s 10 | 11 | 12 | @testbench 13 | def test(): 14 | assert 3 * 4 == list33(3) 15 | assert 6 * 4 == list33(6) 16 | -------------------------------------------------------------------------------- /tests/list/list34.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def list34(x): 5 | xs = [x] * 4 6 | ys = [None] * len(xs) 7 | s = 0 8 | for i in range(len(xs)): 9 | ys[i] = xs[i] + 1 10 | for i in range(len(ys)): 11 | s += ys[i] 12 | return s 13 | 14 | 15 | @testbench 16 | def test(): 17 | assert 4 * 4 == list34(3) 18 | assert 7 * 4 == list34(6) 19 | -------------------------------------------------------------------------------- /tests/list/rom01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def rom01(x): 4 | sum = 0 5 | rom = [1, 3, 5]*3 6 | for i in range(9): 7 | sum += rom[i]*x 8 | return sum 9 | 10 | @testbench 11 | def test(): 12 | assert 0 == rom01(0) 13 | assert 27 == rom01(1) 14 | assert 54 == rom01(2) 15 | -------------------------------------------------------------------------------- /tests/list/rom02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def rom02(x): 4 | sum = 0 5 | a = 2 6 | rom = [a, a*2, a*3]*3 7 | for i in range(9): 8 | sum += rom[i]*x 9 | return sum 10 | 11 | @testbench 12 | def test(): 13 | assert 0 == rom02(0) 14 | assert 36 == rom02(1) 15 | assert 72 == rom02(2) 16 | -------------------------------------------------------------------------------- /tests/list/rom03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def rom03(x, y): 4 | data0 = [1, 2, 3] 5 | data1 = [4, 5, 6] 6 | data2 = [7, 8, 9] 7 | if x == 0: 8 | if y == 1: 9 | e = data0 10 | else: 11 | e = data1 12 | d = e 13 | elif x == 1: 14 | d = data1 15 | else: 16 | d = data2 17 | return d[0] 18 | 19 | @testbench 20 | def test(): 21 | assert 1 == rom03(0, 1) 22 | assert 4 == rom03(1, 1) 23 | assert 7 == rom03(2, 1) 24 | assert 4 == rom03(0, 0) 25 | -------------------------------------------------------------------------------- /tests/list/rom04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def rom04(d): 4 | data = [d] * 8 5 | sum = 0 6 | for d in data: 7 | sum += d 8 | return sum 9 | 10 | @testbench 11 | def test(): 12 | assert 8 == rom04(1) 13 | assert 16 == rom04(2) 14 | assert 24 == rom04(3) 15 | -------------------------------------------------------------------------------- /tests/list/rom05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | data = (2, 4, 6, 8, 10) 4 | 5 | def rom05(i): 6 | return data[i] 7 | 8 | @testbench 9 | def test(): 10 | assert 2 == rom05(0) 11 | assert 4 == rom05(1) 12 | assert 6 == rom05(2) 13 | -------------------------------------------------------------------------------- /tests/list/rom06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | data = (2, 4, 6, 8, 10) 4 | index = (4, 3, 2, 1, 0) 5 | def idx(i): 6 | return index[i] 7 | def rom06(i): 8 | return data[idx(i)] 9 | 10 | @testbench 11 | def test(): 12 | assert 10 == rom06(0) 13 | assert 8 == rom06(1) 14 | assert 6 == rom06(2) 15 | -------------------------------------------------------------------------------- /tests/list/rom07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def rom07(d): 5 | data = [d] * 8 6 | return data[0] 7 | 8 | 9 | @testbench 10 | def test(): 11 | assert 1 == rom07(1) 12 | assert 2 == rom07(2) 13 | assert 3 == rom07(3) 14 | -------------------------------------------------------------------------------- /tests/list/rom10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def rom10(x, y, z): 4 | lst0 = [x, y, z] 5 | lst1 = [-x, -y, -z] 6 | lst2 = [0] * 4 7 | v = ~x 8 | print(v, ~v) 9 | lst2[0] = 1 10 | lst2[1] = 2 11 | lst2[2] = 3 12 | lst2[v] = 10 13 | print(lst2[0], lst2[1], lst2[2], lst2[3]) 14 | return lst0[0] + lst1[0] + lst1[0] 15 | 16 | 17 | @testbench 18 | def test(): 19 | assert -1 == rom10(1, 2, 3) 20 | -------------------------------------------------------------------------------- /tests/loop/for01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for01(x): 4 | s1 = 0 5 | s2 = 0 6 | for i in range(x): 7 | s1 += i 8 | s2 += i 9 | return s1 + s2 10 | 11 | @testbench 12 | def test(): 13 | assert 0 == for01(0) 14 | assert 0 == for01(1) 15 | assert 2 == for01(2) 16 | -------------------------------------------------------------------------------- /tests/loop/for02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for02(x): 4 | s1 = 0 5 | s2 = 0 6 | for i in range(x): 7 | s1 += 1 8 | if i == 5: 9 | break 10 | s2 += 2 11 | return s1 + s2 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == for02(0) 16 | assert 3 == for02(1) 17 | assert 16 == for02(10) 18 | -------------------------------------------------------------------------------- /tests/loop/for03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for03(x): 4 | s1 = 0 5 | s2 = 0 6 | for i in range(x): 7 | s1 += 1 8 | if i == 5: 9 | continue 10 | s2 += 2 11 | return s1 + s2 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == for03(0) 16 | assert 3 == for03(1) 17 | assert 28 == for03(10) 18 | -------------------------------------------------------------------------------- /tests/loop/for04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for04(x): 4 | s1 = 0 5 | s2 = 0 6 | for i in range(x): 7 | s1 += 1 8 | s2 += 2 9 | if i == 5: 10 | break 11 | return s1 + s2 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == for04(0) 16 | assert 3 == for04(1) 17 | assert 18 == for04(10) 18 | -------------------------------------------------------------------------------- /tests/loop/for05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for05(x): 4 | s1 = 0 5 | s2 = 0 6 | for i in range(x): 7 | s1 += 1 8 | s2 += 2 9 | if i == 5: 10 | continue 11 | return s1 + s2 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == for05(0) 16 | assert 3 == for05(1) 17 | assert 30 == for05(10) 18 | -------------------------------------------------------------------------------- /tests/loop/for06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for06(x, y): 4 | s1 = 0 5 | s2 = 0 6 | for i in range(x, y): 7 | s1 += 1 8 | s2 += 2 9 | if i > 5: 10 | break 11 | if i <= 5: 12 | continue 13 | return s1 + s2 14 | 15 | @testbench 16 | def test(): 17 | assert 0 == for06(0, 0) 18 | assert 3 == for06(0, 1) 19 | assert 6 == for06(5, 10) 20 | -------------------------------------------------------------------------------- /tests/loop/for07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for07(x): 4 | y = x 5 | z = 0 6 | for i in range(30): 7 | for j in range(30): 8 | z += i * j 9 | return y + z 10 | 11 | @testbench 12 | def test(): 13 | assert 189225 == for07(0) 14 | assert 189226 == for07(1) 15 | assert 189227 == for07(2) 16 | -------------------------------------------------------------------------------- /tests/loop/for08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for08(x): 4 | y = x 5 | z = 0 6 | for i in range(10+x): 7 | for j in range(10): 8 | for k in range(10): 9 | z += i + j + k 10 | return y + z 11 | 12 | @testbench 13 | def test(): 14 | assert 13500 == for08(0) 15 | assert 15401 == for08(1) 16 | assert 17402 == for08(2) 17 | -------------------------------------------------------------------------------- /tests/loop/for09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for09(x): 4 | y = x 5 | z = 0 6 | for i in range(10+x): 7 | if i > 5: 8 | z += 1 9 | if i > 6: 10 | z += 1 11 | if i > 7: 12 | z += 1 13 | if i > 8: 14 | z += 1 15 | 16 | return y + z 17 | 18 | @testbench 19 | def test(): 20 | assert 10 == for09(0) 21 | assert 35 == for09(5) 22 | assert 60 == for09(10) 23 | -------------------------------------------------------------------------------- /tests/loop/for10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for10(x): 4 | y = x 5 | z = 0 6 | for i in range(10+x): 7 | if i > 5: 8 | z += 1 9 | else: 10 | if i > 4: 11 | z += 1 12 | else: 13 | if i > 3: 14 | z += 1 15 | else: 16 | if i > 2: 17 | z += 1 18 | 19 | return y + z 20 | 21 | @testbench 22 | def test(): 23 | assert 7 == for10(0) 24 | assert 17 == for10(5) 25 | assert 27 == for10(10) 26 | -------------------------------------------------------------------------------- /tests/loop/for11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for11(x): 4 | data = [x, 2, 3, 4] 5 | sum = 0 6 | for d in data: 7 | sum += d 8 | return sum 9 | 10 | @testbench 11 | def test(): 12 | assert 9 == for11(0) 13 | assert 14 == for11(5) 14 | assert 19 == for11(10) 15 | -------------------------------------------------------------------------------- /tests/loop/for12.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for12(s, e, step): 4 | sum = 0 5 | for i in range(s, e, step): 6 | sum += i 7 | return sum 8 | 9 | @testbench 10 | def test(): 11 | assert 45 == for12(0, 10, 1) 12 | assert 20 == for12(0, 10, 2) 13 | # assert 55 == for12(10, 0, -1) 14 | # assert 45 == for12(9, -1, -1) 15 | # assert 25 == for12(9, -1, -2) 16 | -------------------------------------------------------------------------------- /tests/loop/for13.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def for13(x): 4 | sum = 0 5 | for d in [1+x, 3+x, 5+x, 7+x, 9+x]: 6 | sum += d 7 | return sum 8 | 9 | @testbench 10 | def test(): 11 | assert 1+3+5+7+9 == for13(0) 12 | assert 1+3+5+7+9+10 == for13(2) 13 | -------------------------------------------------------------------------------- /tests/loop/for14.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def func(xs:list, ys:list): 4 | sum_x = 0 5 | for x in xs: 6 | sum_x += x 7 | sum_y = 0 8 | for y in ys: 9 | sum_y += y 10 | 11 | return sum_x + sum_y 12 | 13 | 14 | def for14(a0, a1, a2, a3, a4, a5): 15 | return func([a0, a1, a2], [a3, a4, a5]) 16 | 17 | 18 | @testbench 19 | def test(): 20 | assert 1+2+3+4+5 == for14(0, 1, 2, 3, 4, 5) 21 | assert 5+6+7+8+9+10 == for14(5, 6, 7, 8, 9, 10) 22 | -------------------------------------------------------------------------------- /tests/loop/for15.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def for15(x): 5 | sum = 0 6 | for i in range(0, x): 7 | sum += i 8 | x = 5 9 | return sum 10 | 11 | 12 | @testbench 13 | def test(): 14 | assert 0 == for15(0) 15 | assert 6 == for15(4) 16 | assert 10 == for15(5) 17 | -------------------------------------------------------------------------------- /tests/loop/for16.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def for16(start, stop, step): 5 | sum = 0 6 | for j in range(start, stop, step): 7 | sum1 = 0 8 | for i in range(0, stop): 9 | sum1 += i 10 | stop = 2 11 | step = 1 12 | sum += sum1 13 | print(sum) 14 | return sum 15 | 16 | 17 | @testbench 18 | def test(): 19 | assert 0 == for16(0, 0, 1) 20 | assert 7 == for16(0, 4, 2) 21 | assert 47 == for16(3, 10, 3) 22 | -------------------------------------------------------------------------------- /tests/loop/for17.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def for17(stop, p): 5 | s = 0 6 | ss = 1 7 | for i in range(stop): 8 | if p == 0: 9 | pass 10 | elif p == 1: 11 | ss += 1 12 | elif p == 2: 13 | ss += 2 14 | else: 15 | continue 16 | s += ss 17 | return s 18 | 19 | 20 | @testbench 21 | def test(): 22 | assert 65 == for17(10, 1) 23 | -------------------------------------------------------------------------------- /tests/loop/for18.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def for18(stop, p): 5 | ss = 1 6 | s = 0 7 | if p: 8 | v = 0 9 | for i in range(10): 10 | v += 1 11 | while True: 12 | if p == 0: 13 | pass 14 | elif p == 1: 15 | ss += 1 16 | elif p == 2: 17 | ss += 2 18 | else: 19 | continue 20 | s += ss 21 | if s > stop: 22 | break 23 | return s 24 | 25 | 26 | @testbench 27 | def test(): 28 | assert 11 == for18(10, 0) 29 | assert 14 == for18(10, 1) 30 | assert 15 == for18(10, 2) 31 | -------------------------------------------------------------------------------- /tests/loop/while01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def while01(x): 4 | s1 = 0 5 | s2 = 0 6 | i = 0 7 | while i < x: 8 | s1 += i 9 | s2 += i 10 | i = i + 1 11 | return s1 + s2 12 | 13 | @testbench 14 | def test(): 15 | assert 0 == while01(0) 16 | assert 0 == while01(1) 17 | assert 2 == while01(2) 18 | -------------------------------------------------------------------------------- /tests/loop/while02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def while02(x): 4 | s1 = 0 5 | s2 = 0 6 | i = 0 7 | while i < x: 8 | s1 += 1 9 | if i == 5: 10 | break 11 | s2 += 2 12 | i += 1 13 | return s1 + s2 14 | 15 | @testbench 16 | def test(): 17 | assert 0 == while02(0) 18 | assert 3 == while02(1) 19 | assert 6 == while02(2) 20 | -------------------------------------------------------------------------------- /tests/loop/while03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def while03(x): 4 | s1 = 0 5 | s2 = 0 6 | i = 0 7 | while i < x: 8 | s1 += 1 9 | if i == 5: 10 | i += 1 11 | continue 12 | s2 += 2 13 | i += 1 14 | return s1 + s2 15 | 16 | @testbench 17 | def test(): 18 | assert 0 == while03(0) 19 | assert 3 == while03(1) 20 | assert 28 == while03(10) 21 | -------------------------------------------------------------------------------- /tests/loop/while04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def while04(x): 4 | s1 = 0 5 | s2 = 0 6 | i = x 7 | while True: 8 | s1 += 1 9 | if i >= 5: 10 | break 11 | s2 += 2 12 | i += 1 13 | return s1 + s2 14 | 15 | @testbench 16 | def test(): 17 | assert 16 == while04(0) 18 | assert 13 == while04(1) 19 | assert 10 == while04(2) 20 | -------------------------------------------------------------------------------- /tests/loop/while05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def while05(x): 4 | s1 = 0 5 | s2 = 0 6 | i = 0 7 | while True: 8 | s1 += 1 9 | while True: 10 | i += 1 + x 11 | s2 += 2 12 | if i < 5: 13 | continue 14 | elif i < 7: 15 | continue 16 | else: 17 | break 18 | s1 += 0 19 | if s1 > 5: 20 | break 21 | return s1 + s2 22 | 23 | @testbench 24 | def test(): 25 | assert 30 == while05(0) 26 | assert 24 == while05(1) 27 | assert 22 == while05(2) 28 | -------------------------------------------------------------------------------- /tests/loop/while06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def while06(x): 4 | i = 1 5 | j = 0 6 | while i: 7 | if j == x: 8 | i = 0 9 | j += 1 10 | return j 11 | 12 | @testbench 13 | def test(): 14 | assert 1 == while06(0) 15 | assert 2 == while06(1) 16 | assert 3 == while06(2) 17 | -------------------------------------------------------------------------------- /tests/loop/while07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | shift_n = 8 4 | 5 | 6 | def abs(x): 7 | if x < 0: 8 | return -x 9 | else: 10 | return x 11 | 12 | 13 | def i_square(x): 14 | return x * x 15 | 16 | 17 | def i_while(x): 18 | epsilon = 1 19 | new_x = x << shift_n 20 | guess = new_x >> 1 21 | old_guess = 0 22 | while (abs(i_square(guess) - new_x)) > epsilon: 23 | if old_guess == guess: 24 | break 25 | old_guess = guess 26 | guess = guess - 10 27 | 28 | return guess 29 | 30 | 31 | @testbench 32 | def test(): 33 | x = 25 34 | result = i_while(x) 35 | assert result == 80 36 | -------------------------------------------------------------------------------- /tests/loop/while08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def while08(n): 5 | x = 1 6 | y = 2 7 | while True: 8 | #z = y 9 | y = x 10 | x = 5 11 | n -= 1 12 | if n < 0: 13 | break 14 | print(x, y) 15 | return x + y 16 | 17 | 18 | @testbench 19 | def test(): 20 | assert 6 == while08(0) 21 | assert 10 == while08(1) 22 | -------------------------------------------------------------------------------- /tests/loop/while09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def while09(n): 5 | x = 1 6 | while True: 7 | y = x 8 | x = 2 9 | n -= 1 10 | if n < 0: 11 | break 12 | 13 | return y 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert 1 == while09(0) 19 | assert 2 == while09(1) 20 | -------------------------------------------------------------------------------- /tests/module/module01.py: -------------------------------------------------------------------------------- 1 | from polyphony import module 2 | from polyphony import testbench 3 | from polyphony.timing import clksleep 4 | 5 | 6 | @module 7 | class ModuleTest01: 8 | def __init__(self, mparam): 9 | self.append_worker(self.worker0, 1 * mparam) 10 | self.append_worker(self.worker1, 2 * mparam) 11 | 12 | def worker0(self, param): 13 | print('worker0', param) 14 | 15 | def worker1(self, param): 16 | for i in range(10): 17 | print('worker1', param) 18 | 19 | 20 | @testbench 21 | def test0(m): 22 | clksleep(10) 23 | 24 | 25 | @testbench 26 | def test1(m): 27 | clksleep(30) 28 | 29 | 30 | m0 = ModuleTest01(10) 31 | m1 = ModuleTest01(20) 32 | test0(m0) 33 | test1(m1) 34 | -------------------------------------------------------------------------------- /tests/module/module02.py: -------------------------------------------------------------------------------- 1 | from polyphony import module 2 | from polyphony import testbench 3 | from polyphony import is_worker_running 4 | from polyphony.timing import clksleep 5 | 6 | 7 | @module 8 | class ModuleTest02: 9 | def __init__(self, mparam): 10 | self.append_worker(self.worker, 1 * mparam) 11 | self.append_worker(self.worker, 2 * mparam) 12 | self.append_worker(self.worker, 3 * mparam) 13 | 14 | def worker(self, param): 15 | while is_worker_running(): 16 | print('worker', param) 17 | 18 | 19 | @testbench 20 | def test(m): 21 | clksleep(10) 22 | 23 | 24 | m = ModuleTest02(10) 25 | test(m) 26 | -------------------------------------------------------------------------------- /tests/pipeline/for01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe_func(xs, ys, a, b, c): 6 | for i in pipelined(range(len(xs))): 7 | x = xs[i] 8 | x += a 9 | x -= b 10 | x *= c 11 | print(x) 12 | ys[i] = x 13 | 14 | def for01(a, b, c): 15 | data = [1, 2, 3, 4] 16 | out_data = [0] * 4 17 | pipe_func(data, out_data, a, b, c) 18 | assert 0 == out_data[0] 19 | assert 3 == out_data[1] 20 | assert 6 == out_data[2] 21 | assert 9 == out_data[3] 22 | 23 | 24 | @testbench 25 | def test(): 26 | for01(1, 2, 3) 27 | -------------------------------------------------------------------------------- /tests/pipeline/for02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def loop(xs, ys, a, b, c): 6 | for i in pipelined(range(len(xs))): 7 | x = xs[i] 8 | x += a 9 | x -= b 10 | x *= c 11 | ys[i] = x 12 | 13 | 14 | def for02(a, b, c): 15 | data = [1, 2, 3, 4] 16 | out_data = [0] * 4 17 | loop(data, out_data, a, b, c) 18 | assert 0 == out_data[0] 19 | assert 3 == out_data[1] 20 | assert 6 == out_data[2] 21 | assert 9 == out_data[3] 22 | 23 | 24 | @testbench 25 | def test(): 26 | for02(1, 2, 3) 27 | -------------------------------------------------------------------------------- /tests/pipeline/for03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe_func(xs): 6 | s = 0 7 | for x in pipelined(xs): 8 | x = x + s 9 | s += x 10 | return s 11 | 12 | def for03_a(): 13 | xs = [1, 1, 1, 1, 1, 1, 1, 1] 14 | return pipe_func(xs) 15 | 16 | def for03_b(): 17 | xs = [1, 2, 3, 4, 5, 6, 7, 8] 18 | return pipe_func(xs) 19 | 20 | @testbench 21 | def test(): 22 | assert 255 == for03_a() 23 | assert 502 == for03_b() 24 | -------------------------------------------------------------------------------- /tests/pipeline/for04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe_func(a, b, c): 6 | for i in range(8): 7 | s = 0 8 | for k in pipelined(range(8)): 9 | s += a[k] * b[i] 10 | c[i] = s 11 | print(c[0], c[1], c[2], c[3]) 12 | 13 | 14 | def for04(): 15 | data_a = [1, 3, 5, 7, 5, 3, 1, 0] 16 | data_b = [1, -1, 2, -2, 3, -3, 4, -5] 17 | data_c = [None] * 8 18 | pipe_func(data_a, data_b, data_c) 19 | assert 25 == data_c[0] 20 | assert -25 == data_c[1] 21 | assert 50 == data_c[2] 22 | assert -50 == data_c[3] 23 | 24 | 25 | @testbench 26 | def test(): 27 | for04() 28 | -------------------------------------------------------------------------------- /tests/pipeline/for05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def for05(x): 6 | z = 0 7 | for i in pipelined(range(x)): 8 | if i > 5: 9 | z += 1 10 | else: 11 | z += 2 12 | return z 13 | 14 | 15 | @testbench 16 | def test(): 17 | assert 0 == for05(0) 18 | assert 10 == for05(5) 19 | assert 16 == for05(10) 20 | -------------------------------------------------------------------------------- /tests/pipeline/for08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe_func(xs, ys): 6 | s = 0 7 | for i in pipelined(range(len(xs))): 8 | idx = i + s 9 | if idx > 4: 10 | idx = 0 11 | v = xs[idx] 12 | ys[i] = v 13 | s = s + v 14 | 15 | 16 | def for08(): 17 | data = [0, 16, 32, -16, -64] 18 | out = [0] * 5 19 | pipe_func(data, out) 20 | #print(out) 21 | assert 0 == out[0] 22 | assert 16 == out[1] 23 | assert 0 == out[2] 24 | assert 0 == out[3] 25 | assert 0 == out[4] 26 | 27 | 28 | @testbench 29 | def test(): 30 | for08() 31 | -------------------------------------------------------------------------------- /tests/pipeline/for09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe_func(xs, w): 6 | for i in pipelined(range(len(xs))): 7 | v = xs[i] 8 | v *= w 9 | xs[i] = v 10 | 11 | 12 | def for09(): 13 | data = [1, 16, 32, -16, -64] 14 | pipe_func(data, 2) 15 | print(data[0]) 16 | print(data[1]) 17 | print(data[2]) 18 | print(data[3]) 19 | print(data[4]) 20 | assert 2 == data[0] 21 | assert 32 == data[1] 22 | assert 64 == data[2] 23 | assert -32 == data[3] 24 | assert -128 == data[4] 25 | 26 | 27 | @testbench 28 | def test(): 29 | for09() 30 | -------------------------------------------------------------------------------- /tests/pipeline/for10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe10_func(xs): 6 | for i in pipelined(range(len(xs) - 1), ii=-1): 7 | v = xs[i] + xs[i + 1] 8 | v >>= 1 9 | xs[i] = v 10 | 11 | 12 | def for10(): 13 | data = [0, 16, 32, -16, -64] 14 | pipe10_func(data) 15 | assert 8 == data[0] 16 | assert 24 == data[1] 17 | assert 8 == data[2] 18 | assert -40 == data[3] 19 | assert -64 == data[4] 20 | 21 | 22 | @testbench 23 | def test(): 24 | for10() 25 | -------------------------------------------------------------------------------- /tests/pipeline/for11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe11_func(xs, ys): 6 | for i in pipelined(range(len(xs) - 1), ii=2): 7 | v = xs[i] + xs[i + 1] 8 | v >>= 1 9 | ys[i] = v 10 | 11 | 12 | def for11(): 13 | idata = [0, 16, 32, -16, -64] 14 | odata = [0] * 5 15 | pipe11_func(idata, odata) 16 | assert 8 == odata[0] 17 | assert 24 == odata[1] 18 | assert 8 == odata[2] 19 | assert -40 == odata[3] 20 | assert 0 == odata[4] 21 | 22 | 23 | @testbench 24 | def test(): 25 | for11() 26 | -------------------------------------------------------------------------------- /tests/pipeline/for12.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def pipe12_func(xs0, xs1, ys0): 6 | for i in pipelined(range(len(xs0))): 7 | if i % 2 == 0: 8 | v = xs0[i] 9 | else: 10 | v = xs1[i] 11 | #print(i, v, xs0[i], xs1[i]) 12 | ys0[i] = v 13 | 14 | 15 | def for12(): 16 | idata0 = [0, 16, 32, -16, -64] 17 | idata1 = [32, -16, -64, 8, 24] 18 | odata0 = [0] * 5 19 | pipe12_func(idata0, idata1, odata0) 20 | assert 0 == odata0[0] 21 | assert -16 == odata0[1] 22 | assert 32 == odata0[2] 23 | assert 8 == odata0[3] 24 | assert -64 == odata0[4] 25 | 26 | 27 | @testbench 28 | def test(): 29 | for12() 30 | -------------------------------------------------------------------------------- /tests/pipeline/nested01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def nested01_func(xs, ys, w, h): 6 | for y in pipelined(range(h)): 7 | for x in range(w): 8 | idx = y * w + x 9 | v = xs[idx] 10 | ys[idx] = v + 1 11 | 12 | 13 | def nested01(): 14 | width = 16 15 | height = 16 16 | data0 = [0] * width * height 17 | data1 = [0] * width * height 18 | for i in range(width * height): 19 | data0[i] = i 20 | nested01_func(data0, data1, width, height) 21 | for i in range(width * height): 22 | assert data1[i] == i + 1 23 | 24 | 25 | @testbench 26 | def test(): 27 | nested01() 28 | -------------------------------------------------------------------------------- /tests/pipeline/nested02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def nested02(x): 6 | s = x 7 | for i in pipelined(range(4)): 8 | for j in range(4): 9 | s += 1 10 | return s 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 26 == nested02(10) 16 | -------------------------------------------------------------------------------- /tests/pipeline/nested03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def nested03(x): 6 | s = x 7 | for i in pipelined(range(4)): 8 | t = i 9 | s += i 10 | for j in range(4): 11 | s += 1 12 | t += 1 13 | t += 2 14 | s += t 15 | return s 16 | 17 | 18 | @testbench 19 | def test(): 20 | assert 62 == nested03(10) 21 | -------------------------------------------------------------------------------- /tests/pipeline/nested04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def nested04(x): 6 | s = x 7 | for i in pipelined(range(4)): 8 | for j in range(4): 9 | for k in range(4): 10 | s += 1 11 | return s 12 | 13 | 14 | @testbench 15 | def test(): 16 | assert 74 == nested04(10) 17 | -------------------------------------------------------------------------------- /tests/pipeline/nested05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def nested05(x): 6 | s = x 7 | for i in pipelined(range(4)): 8 | t = i 9 | for j in range(4): 10 | s += 1 11 | for k in range(4): 12 | t += 1 13 | s += t 14 | return s 15 | 16 | 17 | @testbench 18 | def test(): 19 | assert 96 == nested05(10) 20 | -------------------------------------------------------------------------------- /tests/pipeline/nested06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import pipelined 3 | 4 | 5 | def nested06(x): 6 | s = x 7 | for i in pipelined(range(4)): 8 | t = i 9 | for j in range(4): 10 | t += 1 11 | for k in range(4): 12 | s += 2 13 | for l in range(4): 14 | s += 3 15 | s += t 16 | return s 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 928 == nested06(10) 22 | -------------------------------------------------------------------------------- /tests/pure/module01.py: -------------------------------------------------------------------------------- 1 | from polyphony import module, pure 2 | from polyphony import testbench 3 | from polyphony.timing import clksleep 4 | 5 | 6 | @module 7 | class ModuleTest01: 8 | @pure 9 | def __init__(self, mparam): 10 | self.append_worker(self.worker0, 1 * mparam) 11 | self.append_worker(self.worker1, 2 * mparam) 12 | 13 | def worker0(self, param): 14 | print('worker0', param) 15 | 16 | def worker1(self, param): 17 | for i in range(10): 18 | print('worker1', param) 19 | 20 | 21 | @testbench 22 | def test0(m): 23 | clksleep(10) 24 | 25 | 26 | @testbench 27 | def test1(m): 28 | clksleep(30) 29 | 30 | 31 | m0 = ModuleTest01(10) 32 | m1 = ModuleTest01(20) 33 | test0(m0) 34 | test1(m1) 35 | -------------------------------------------------------------------------------- /tests/pure/module02.py: -------------------------------------------------------------------------------- 1 | from polyphony import module, pure 2 | from polyphony import testbench 3 | from polyphony import is_worker_running 4 | from polyphony.timing import clksleep 5 | 6 | 7 | @module 8 | class ModuleTest02: 9 | @pure 10 | def __init__(self, mparam): 11 | self.append_worker(self.worker, 1 * mparam) 12 | self.append_worker(self.worker, 2 * mparam) 13 | self.append_worker(self.worker, 3 * mparam) 14 | 15 | def worker(self, param): 16 | while is_worker_running(): 17 | print('worker', param) 18 | 19 | 20 | @testbench 21 | def test(m): 22 | clksleep(10) 23 | 24 | 25 | m = ModuleTest02(10) 26 | test(m) 27 | -------------------------------------------------------------------------------- /tests/pure/pure01.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def f(x): 7 | return sum([i for i in range(x)]) 8 | 9 | 10 | @testbench 11 | def test(): 12 | assert 4950 == f(100) 13 | assert 4950 + 4950 == f(100) + f(100) 14 | print(f(1000)) 15 | 16 | 17 | test() 18 | -------------------------------------------------------------------------------- /tests/pure/pure02.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def rand(seed, x, y): 7 | import random 8 | random.seed(seed) 9 | return random.randint(x, y) 10 | 11 | 12 | @testbench 13 | def test(): 14 | assert rand(0, 1, 1000) == rand(0, 1, 1000) 15 | assert rand(0, -1000, 1000) == rand(0, -1000, 1000) 16 | 17 | 18 | test() 19 | -------------------------------------------------------------------------------- /tests/pure/pure03.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def tuple_numbers(x): 7 | return tuple([i for i in range(x)]) 8 | 9 | 10 | @pure 11 | def list_numbers(x): 12 | return list([i for i in range(x)]) 13 | 14 | 15 | def pure03_a(): 16 | sum = 0 17 | data = list_numbers(5 + 5) 18 | for d in data: 19 | sum += d 20 | return sum 21 | 22 | 23 | def pure03_b(): 24 | sum = 0 25 | data = tuple_numbers(5 + 5) 26 | for d in data: 27 | sum += d 28 | return sum 29 | 30 | 31 | @testbench 32 | def test(): 33 | assert 45 == pure03_a() 34 | assert 45 == pure03_b() 35 | 36 | 37 | test() 38 | -------------------------------------------------------------------------------- /tests/pure/pure04.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def f(x): 7 | return x 8 | 9 | 10 | @pure 11 | def g(x): 12 | return x 13 | 14 | 15 | @pure 16 | def h(x): 17 | return x 18 | 19 | 20 | def pure04(): 21 | #return f(g(h(100))) + f(10) + g(10) + h(10) 22 | return f(10) + f(20) + f(30) 23 | 24 | 25 | @testbench 26 | def test(): 27 | assert 60 == pure04() 28 | 29 | 30 | test() 31 | -------------------------------------------------------------------------------- /tests/pure/pure05.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | value = 12345 6 | 7 | 8 | @pure 9 | def f(): 10 | return value 11 | 12 | 13 | def pure05(): 14 | return f() 15 | 16 | 17 | @testbench 18 | def test(): 19 | assert value == pure05() 20 | 21 | 22 | test() 23 | -------------------------------------------------------------------------------- /tests/pure/pure06.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | def g(x): 6 | return x 7 | 8 | 9 | @pure 10 | def f(x): 11 | return g(x) 12 | 13 | 14 | def pure06(): 15 | return f(100) 16 | 17 | 18 | @testbench 19 | def test(): 20 | assert 100 == pure06() 21 | 22 | 23 | test() 24 | -------------------------------------------------------------------------------- /tests/pure/pure07.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def f(x): 7 | return sum([i for i in range(x)]) 8 | 9 | 10 | value = f(100) 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 4950 == value 16 | 17 | 18 | test() 19 | -------------------------------------------------------------------------------- /tests/pure/pure08.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def data(op, x): 7 | if op == 'mul': 8 | return [i * i for i in range(x)] 9 | elif op == 'add': 10 | return [i + i for i in range(x)] 11 | 12 | 13 | class C: 14 | d0 = data('mul', 10) 15 | d1 = data('add', 10) 16 | 17 | 18 | def pure08_a(idx): 19 | return C.d0[idx] 20 | 21 | 22 | def pure08_b(idx): 23 | return C.d1[idx] 24 | 25 | 26 | @testbench 27 | def test(): 28 | assert 0 == pure08_a(0) 29 | assert 25 == pure08_a(5) 30 | assert 81 == pure08_a(9) 31 | 32 | assert 0 == pure08_b(0) 33 | assert 10 == pure08_b(5) 34 | assert 18 == pure08_b(9) 35 | 36 | 37 | test() 38 | -------------------------------------------------------------------------------- /tests/pure/pure09.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def mul(data): 7 | return [d * d for d in data] 8 | 9 | 10 | def pure09_a(i0, i1, i2): 11 | data = mul([1, 2, 3, 4, 5]) 12 | return data[i0] + data[i1] + data[i2] 13 | 14 | 15 | def pure09_b(i0, i1, i2): 16 | data = mul([2] * 10) 17 | return data[i0] + data[i1] + data[i2] 18 | 19 | 20 | @testbench 21 | def test(): 22 | assert 1 + 4 + 9 == pure09_a(0, 1, 2) 23 | assert 4 + 9 + 16 == pure09_a(1, 2, 3) 24 | assert 9 + 16 + 25 == pure09_a(2, 3, 4) 25 | 26 | assert 4 + 4 + 4 == pure09_b(0, 1, 2) 27 | 28 | 29 | test() 30 | -------------------------------------------------------------------------------- /tests/pure/pure11.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def f(xs): 7 | return sum(xs) 8 | 9 | 10 | def pure11(): 11 | a = (1, 3, 5) 12 | b = (2, 4, 6) 13 | a_sum, b_sum = f(a), f(b) 14 | 15 | return a_sum + b_sum 16 | 17 | 18 | @testbench 19 | def test(): 20 | assert 21 == pure11() 21 | assert 21 == pure11() 22 | assert 21 == pure11() 23 | 24 | test() 25 | -------------------------------------------------------------------------------- /tests/pure/pure12.py: -------------------------------------------------------------------------------- 1 | from polyphony import pure 2 | from polyphony import testbench 3 | 4 | 5 | @pure 6 | def f(xs): 7 | return sum(xs) 8 | 9 | 10 | @pure 11 | def ff(xs): 12 | return f(xs) 13 | 14 | 15 | def pure12(): 16 | a = (1, 3, 5) 17 | a0_sum = ff(a) 18 | 19 | a = (2, 4, 6) 20 | a1_sum = ff(a) 21 | 22 | return a0_sum + a1_sum 23 | 24 | 25 | @testbench 26 | def test(): 27 | assert 21 == pure12() 28 | 29 | 30 | test() 31 | -------------------------------------------------------------------------------- /tests/return/noreturn01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def fun(data): 5 | while True: 6 | for i in range(4): 7 | data[i] = i 8 | break 9 | 10 | 11 | def noreturn01(): 12 | data = [0] * 10 13 | fun(data) 14 | assert data[0] == 0 15 | assert data[1] == 1 16 | assert data[2] == 2 17 | assert data[3] == 3 18 | assert data[4] == 0 19 | 20 | 21 | @testbench 22 | def test(): 23 | noreturn01() 24 | -------------------------------------------------------------------------------- /tests/return/return01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def return01(x, y, z): 4 | if z == 1: 5 | x = x + y 6 | return x + y 7 | elif z == 2: 8 | x = x - y 9 | return x - y 10 | return x 11 | 12 | @testbench 13 | def test(): 14 | assert 0 == return01(0, 0, 0) 15 | assert 3 == return01(1, 1, 1) 16 | assert -2 == return01(2, 2, 2) 17 | -------------------------------------------------------------------------------- /tests/return/return02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def return02(x, y, z): 4 | s = 1 5 | for i in range(x): 6 | if i == y + z: 7 | break 8 | s = s * 2 9 | return s 10 | 11 | @testbench 12 | def test(): 13 | assert 1 == return02(0, 0, 0) 14 | assert 2 == return02(1, 1, 1) 15 | assert 32 == return02(10, 2, 3) 16 | -------------------------------------------------------------------------------- /tests/return/return03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def return03(x, y, z): 4 | s = 1 5 | for i in range(x): 6 | if i == y + z: 7 | return i 8 | s = s * 2 9 | return s 10 | 11 | @testbench 12 | def test(): 13 | assert 1 == return03(0, 0, 0) 14 | assert 2 == return03(1, 1, 1) 15 | assert 5 == return03(10, 2, 3) 16 | -------------------------------------------------------------------------------- /tests/return/return04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def return04(x, y, z): 4 | s = 1 5 | for i in range(x): 6 | for j in range(y): 7 | if i == j == z: 8 | return i 9 | s = s * 2 10 | return s 11 | 12 | @testbench 13 | def test(): 14 | assert 1 == return04(0, 0, 0) 15 | assert 2 == return04(1, 1, 1) 16 | assert 1024 == return04(10, 2, 3) 17 | -------------------------------------------------------------------------------- /tests/return/return05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def return05(x): 4 | if x == 0: return 0 5 | if x == 1: return 1 6 | if x == 2: return 2 7 | if x == 3: return 3 8 | return -1 9 | 10 | @testbench 11 | def test(): 12 | assert 0 == return05(0) 13 | assert 1 == return05(1) 14 | assert 2 == return05(2) 15 | assert 3 == return05(3) 16 | assert -1 == return05(4) 17 | -------------------------------------------------------------------------------- /tests/scope/call01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def f(): 4 | return 0 5 | 6 | def call01(): 7 | def f(): 8 | return 1 9 | return f() 10 | 11 | @testbench 12 | def test(): 13 | assert 1 == call01() 14 | -------------------------------------------------------------------------------- /tests/scope/call02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def f(): 4 | return 0 5 | 6 | def call02(): 7 | return f() 8 | 9 | @testbench 10 | def test(): 11 | assert 0 == call02() 12 | -------------------------------------------------------------------------------- /tests/scope/global01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | g = 1111 4 | g = 2222 5 | 6 | def global01(): 7 | return g 8 | 9 | @testbench 10 | def test(): 11 | assert 2222 == global01() 12 | -------------------------------------------------------------------------------- /tests/scope/global02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | g = 1111 4 | g = 2222 5 | g = 3333 6 | 7 | def global02(): 8 | def inner(): 9 | return g 10 | return inner() 11 | 12 | @testbench 13 | def test(): 14 | assert 3333 == global02() 15 | -------------------------------------------------------------------------------- /tests/scope/global03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | g1 = 1111 4 | g2 = 1111 + g1 5 | g1 = g2 + g2 6 | 7 | def global03(): 8 | def inner1(): 9 | return g1 10 | def inner2(): 11 | return g2 12 | 13 | return inner1() + inner2() 14 | 15 | @testbench 16 | def test(): 17 | assert 6666 == global03() 18 | -------------------------------------------------------------------------------- /tests/scope/global04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | g1 = (1111, 2222) 4 | g2 = g1[0] 5 | g3 = g1[1] 6 | 7 | def global04(): 8 | return g2 + g3 9 | 10 | @testbench 11 | def test(): 12 | assert 3333 == global04() 13 | -------------------------------------------------------------------------------- /tests/scope/global05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | gs = (1111, 2222, 3333) 4 | 5 | def global05(): 6 | sum = 0 7 | for g in gs: 8 | sum += g 9 | return sum 10 | 11 | @testbench 12 | def test(): 13 | assert 6666 == global05() 14 | -------------------------------------------------------------------------------- /tests/scope/global06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | a = 0 4 | b = a 5 | c = b 6 | 7 | if b: 8 | gs0 = (1, 2, 3) 9 | else: 10 | gs0 = (4, 5, 6) 11 | 12 | if c: 13 | gs = gs0 14 | else: 15 | gs = gs0 16 | 17 | 18 | def global06(): 19 | sum = 0 20 | for g in gs: 21 | sum += g 22 | return sum 23 | 24 | @testbench 25 | def test(): 26 | assert 15 == global06() 27 | -------------------------------------------------------------------------------- /tests/scope/lookup01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | foo = 1 4 | 5 | 6 | def f(): 7 | def ff(): 8 | print('ff.foo', foo) 9 | return foo == 2 10 | foo = 2 11 | return ff() 12 | 13 | 14 | @testbench 15 | def test(): 16 | assert f() == True 17 | -------------------------------------------------------------------------------- /tests/scope/lookup02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | foo = 1 4 | 5 | 6 | class C: 7 | foo = 2 8 | 9 | def __init__(self): 10 | print('foo', foo) 11 | a = foo == 1 12 | print('self.foo', self.foo) 13 | b = self.foo == 2 14 | print('C.foo', C.foo) 15 | c = C.foo == 2 16 | self.result = a and b and c 17 | 18 | 19 | def f(): 20 | c = C() 21 | return c.result 22 | 23 | 24 | @testbench 25 | def test(): 26 | assert f() == True 27 | -------------------------------------------------------------------------------- /tests/testbench/tb01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | @testbench 4 | def test(): 5 | a = 10 6 | b = 10 7 | c = 1 8 | assert a == 10 9 | assert 10 == b 10 | assert a == b 11 | assert a != c 12 | print(a) 13 | -------------------------------------------------------------------------------- /tests/testbench/tb02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def f(x): 4 | return x 5 | 6 | @testbench 7 | def test(): 8 | a = 10 9 | assert a == f(a) 10 | assert f(a) == a 11 | assert f(a) == f(a) 12 | print(f(a)) 13 | -------------------------------------------------------------------------------- /tests/testbench/tb03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def g(x): 4 | return x 5 | 6 | def f(x): 7 | return g(x) + g(x) 8 | 9 | @testbench 10 | def test(): 11 | a = 10 12 | assert a+a == f(a) 13 | b = f(1) + f(1) 14 | assert b == f(2) 15 | -------------------------------------------------------------------------------- /tests/testbench/tb04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | @testbench 5 | def test(): 6 | data = [0, 1, 2] 7 | assert 0 == data[0] 8 | assert 1 == data[1] 9 | assert 2 == data[2] 10 | data[0] = 11 11 | data[1] = 22 12 | data[2] = 33 13 | assert 11 == data[0] 14 | assert 22 == data[1] 15 | assert 33 == data[2] 16 | -------------------------------------------------------------------------------- /tests/testbench/tb05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | @testbench 5 | def test(): 6 | rom = (0, 1, 2) 7 | assert 0 == rom[0] 8 | assert 1 == rom[1] 9 | assert 2 == rom[2] 10 | -------------------------------------------------------------------------------- /tests/testbench/tb06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | @testbench 4 | def test(): 5 | rom = [10, 11, 12] 6 | for i in range(3): 7 | assert rom[i] == i + 10 8 | print(rom[i]) 9 | -------------------------------------------------------------------------------- /tests/testbench/tb07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | @testbench 4 | def test(): 5 | sum = 0 6 | for i in range(4): 7 | sum += i 8 | print(sum) 9 | assert sum == 6 10 | -------------------------------------------------------------------------------- /tests/testbench/tb08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | @testbench 4 | def test(): 5 | sum = 0 6 | for i in range(2, 5): 7 | sum += i-1 8 | print(sum) 9 | assert sum == 6 10 | -------------------------------------------------------------------------------- /tests/testbench/tb09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | @testbench 4 | def test(): 5 | xs = [10, 20, 30, 40] 6 | sum = 0 7 | for x in xs: 8 | sum += x 9 | print(sum) 10 | assert sum == 100 11 | -------------------------------------------------------------------------------- /tests/testbench/tb10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | @testbench 4 | def test(): 5 | xs = (10, 20, 30, 40) 6 | sum = 0 7 | for x in xs: 8 | sum += x 9 | print(sum) 10 | assert sum == 100 11 | -------------------------------------------------------------------------------- /tests/testbench/tb11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | @testbench 5 | def test(): 6 | rom = (0, 1, 2) 7 | assert 1 == rom[0] + rom[1] 8 | assert 3 == rom[1] + rom[2] 9 | assert 2 == rom[2] + rom[0] 10 | -------------------------------------------------------------------------------- /tests/timed/clkrange01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.timing import timed, clktime, clkrange 3 | 4 | 5 | @timed 6 | @testbench 7 | def test(): 8 | N = 10 9 | assert clktime() == 0 10 | for i in clkrange(N): 11 | assert clktime() == i + 1 12 | assert clktime() == N + 1 13 | -------------------------------------------------------------------------------- /tests/timed/clkrange02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.timing import timed, clktime, clkrange, clkfence 3 | 4 | 5 | @timed 6 | @testbench 7 | def test(): 8 | N = 10 9 | assert clktime() == 0 10 | clkfence() 11 | for i in clkrange(N): 12 | assert clktime() == i + 2 13 | clkfence() 14 | assert clktime() == N + 3 15 | -------------------------------------------------------------------------------- /tests/timed/clkrange03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.timing import timed, clktime, clkrange, clkfence 3 | 4 | 5 | @timed 6 | @testbench 7 | def test(): 8 | N = 10 9 | assert clktime() == 0 10 | clkfence() 11 | for i in clkrange(N): 12 | assert clktime() == (i * 2) + 2 13 | clkfence() 14 | clkfence() 15 | assert clktime() == N * 2 + 3 16 | -------------------------------------------------------------------------------- /tests/timed/clkrange04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.timing import timed, clktime, clkrange 3 | 4 | 5 | @timed 6 | @testbench 7 | def test(): 8 | N = 10 9 | M = 5 10 | assert clktime() == 0 11 | for i in clkrange(N): 12 | assert clktime() == i * (M + 2) + 1 13 | #print('i', ) 14 | for j in clkrange(M): 15 | assert clktime() == i * (M + 2) + 1 + j + 1 16 | assert clktime() == i * (M + 2) + 1 + M + 1 17 | assert clktime() == (N - 1) * (M + 2) + 1 + M + 2 18 | -------------------------------------------------------------------------------- /tests/timed/clkrange05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.timing import timed, clktime, clkrange 3 | 4 | 5 | @timed 6 | @testbench 7 | def test(): 8 | N = 10 9 | assert clktime() == 0 10 | for i in clkrange(N): 11 | assert clktime() == i + 1 12 | print(clktime()) 13 | #assert clktime() == N + 1 14 | for i in clkrange(N): 15 | assert clktime() == N + 1 + i + 1 16 | print(clktime()) 17 | assert clktime() == N + 1 + N + 1 18 | -------------------------------------------------------------------------------- /tests/timed/clktime01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.timing import timed, clktime, clkfence, clksleep 3 | 4 | 5 | @timed 6 | @testbench 7 | def test(): 8 | assert clktime() == 0 9 | clkfence() 10 | assert clktime() == 1 11 | clksleep(1) 12 | assert clktime() == 2 13 | clksleep(2) 14 | assert clktime() == 4 15 | 16 | prev = clktime() # meta: symbol=register 17 | clksleep(10) 18 | assert clktime() == 14 19 | elapsed = clktime() - prev 20 | assert elapsed == 10 21 | -------------------------------------------------------------------------------- /tests/timed/clktime02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.timing import timed, clktime, clkfence, clksleep 3 | 4 | 5 | @timed 6 | @testbench 7 | def test(): 8 | assert clktime() == 0 9 | clksleep(1000001) 10 | assert clktime() == 1000001 11 | -------------------------------------------------------------------------------- /tests/tuple/tuple01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple01(idx1, idx2): 4 | tup = (0, 10, 20, 30) 5 | return tup[idx1] + tup[idx2] 6 | 7 | @testbench 8 | def test(): 9 | assert 10 == tuple01(0, 1) 10 | assert 50 == tuple01(2, 3) 11 | assert 30 == tuple01(3, 0) 12 | -------------------------------------------------------------------------------- /tests/tuple/tuple02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple02(x, y, z): 4 | ts = (x, y, z)*3 5 | s = 0 6 | for t in ts: 7 | s += t 8 | return s 9 | 10 | @testbench 11 | def test(): 12 | assert 9 == tuple02(1, 1, 1) 13 | assert 18 == tuple02(1, 2, 3) 14 | assert 0 == tuple02(-1, 0, 1) 15 | -------------------------------------------------------------------------------- /tests/tuple/tuple03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple03(x, y): 4 | y, x = x, y 5 | return x 6 | 7 | @testbench 8 | def test(): 9 | assert 2 == tuple03(1, 2) 10 | assert 3 == tuple03(2, 3) 11 | -------------------------------------------------------------------------------- /tests/tuple/tuple04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple04(x, y): 4 | t = x, y 5 | y, x = t 6 | return x 7 | 8 | 9 | @testbench 10 | def test(): 11 | assert 2 == tuple04(1, 2) 12 | assert 3 == tuple04(2, 3) 13 | -------------------------------------------------------------------------------- /tests/tuple/tuple05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple05(p, x, y): 4 | if p: 5 | ts = (x,)*4 6 | else: 7 | ts = (y,)*4 8 | s = 0 9 | for t in ts: 10 | s += t 11 | return s 12 | 13 | 14 | @testbench 15 | def test(): 16 | assert 4 == tuple05(True, 1, 2) 17 | assert 12 == tuple05(False, 2, 3) 18 | -------------------------------------------------------------------------------- /tests/tuple/tuple06.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple06(x, i): 4 | def f(t, i): 5 | return t[i] 6 | return f((x, x+1, x+2), i) 7 | 8 | @testbench 9 | def test(): 10 | assert 11 == tuple06(10, 1) 11 | assert 12 == tuple06(10, 2) 12 | -------------------------------------------------------------------------------- /tests/tuple/tuple07.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple07(p, x, y, z): 4 | def f(p, x, y, z): 5 | if p: 6 | return x, y 7 | else: 8 | return y, z 9 | a, b = f(p, x, y, z) 10 | return a + b 11 | 12 | @testbench 13 | def test(): 14 | assert 1+2 == tuple07(True, 1, 2, 3) 15 | assert 2+3 == tuple07(False, 1, 2, 3) 16 | assert 4+5 == tuple07(True, 4, 5, 6) 17 | assert 5+6 == tuple07(False, 4, 5, 6) 18 | -------------------------------------------------------------------------------- /tests/tuple/tuple08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | def tuple08(p, x, y): 4 | if p: 5 | ts = (x,)*4 6 | else: 7 | ts = (y,)*4 8 | a, b, c, d = ts 9 | return a+b+c+d 10 | 11 | @testbench 12 | def test(): 13 | assert 4 == tuple08(True, 1, 2) 14 | assert 12 == tuple08(False, 2, 3) 15 | -------------------------------------------------------------------------------- /tests/tuple/tuple09.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def first(t): 5 | x, y, z = t 6 | return x 7 | 8 | 9 | def second(t): 10 | x, y, z = t 11 | return y 12 | 13 | 14 | def tuple09(x, y, z): 15 | t = (x, y, z) 16 | return first(t) + second(t) 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 1 + 2 == tuple09(1, 2, 3) 22 | assert 4 + 5 == tuple09(4, 5, 6) 23 | -------------------------------------------------------------------------------- /tests/tuple/tuple10.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def return_tuple(x, y): 5 | return x, y 6 | 7 | 8 | def tuple10(x, y): 9 | a, b = return_tuple(x, y) 10 | return a + b 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 1 + 2 == tuple10(1, 2) 16 | assert 4 + 5 == tuple10(4, 5) 17 | -------------------------------------------------------------------------------- /tests/tuple/tuple11.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def sum(ts:tuple): 5 | def sub_sum(ts:tuple): 6 | def sub_sub_sum(ts:tuple): 7 | s = 0 8 | for t in ts: 9 | s += t 10 | return s 11 | return sub_sub_sum(ts) 12 | return sub_sum(ts) 13 | 14 | 15 | def tuple11(x): 16 | data1 = (x, 1, 2) 17 | data2 = (x, 1, 2, 3, 4, 5) 18 | s1 = sum(data1) 19 | s2 = sum(data2) 20 | return s1 + s2 + x 21 | 22 | 23 | @testbench 24 | def test(): 25 | assert 18 == tuple11(0) 26 | assert 21 == tuple11(1) 27 | assert 24 == tuple11(2) 28 | -------------------------------------------------------------------------------- /tests/tuple/tuple12.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import Tuple, List, int8 3 | 4 | 5 | def f(x, y) -> Tuple[int8]: 6 | return x, y 7 | 8 | 9 | def tuple12_func(xs:list, ys:list, i, j): 10 | ys[j], xs[i] = f(xs[i], ys[j]) 11 | 12 | 13 | def tuple12(): 14 | xs:List[int8] = [1, 2, 3, 4] 15 | ys:List[int8] = [5, 6, 7, 8] 16 | tuple12_func(xs, ys, 0, 1) 17 | assert xs[0] == 6 and ys[1] == 1 18 | 19 | tuple12_func(ys, xs, 0, 2) 20 | assert xs[2] == 5 and ys[0] == 3 21 | 22 | 23 | @testbench 24 | def test(): 25 | tuple12() 26 | -------------------------------------------------------------------------------- /tests/tuple/tuple13.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | 3 | 4 | def tuple13(): 5 | t = (0, 1, 2) 6 | a, b, c = t 7 | return a + b + c 8 | 9 | 10 | @testbench 11 | def test(): 12 | assert 3 == tuple13() 13 | -------------------------------------------------------------------------------- /tests/typing/bitwidth01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import List, bit8, bit16 3 | 4 | 5 | def bitwidth01(a:bit16): 6 | buf:List[bit8] = [1, 2, 3, 4] 7 | buf[0] = a 8 | a = buf[0] 9 | return a 10 | 11 | 12 | @testbench 13 | def test(): 14 | assert 0x34 == bitwidth01(0x1234) # This should fail in Python interpreter 15 | 16 | -------------------------------------------------------------------------------- /tests/typing/bitwidth02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import Tuple, bit32, bit64 3 | 4 | 5 | def _32to64(xs:Tuple[bit32]) -> bit64: 6 | tmp0:bit64 = xs[0] 7 | tmp1:bit64 = xs[1] << 32 8 | return tmp0 | tmp1 9 | 10 | 11 | def bitwidth02(x, y) -> bit64: 12 | ret:bit64 = _32to64((x, y)) 13 | return ret 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert 0x8765432112345678 == bitwidth02(0x12345678, 0x87654321) 19 | -------------------------------------------------------------------------------- /tests/typing/signed01.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import int16 3 | 4 | 5 | def signed01(x:int16) -> int16: 6 | v = x 7 | if v < 0: 8 | vv = (v - 8) 9 | z = vv >> 4 10 | else: 11 | vv = (v + 8) 12 | z = vv >> 4 13 | return z 14 | 15 | 16 | @testbench 17 | def test(): 18 | assert signed01(32) == 2 19 | assert signed01(16) == 1 20 | assert signed01(0) == 0 21 | assert signed01(-16) == -2 22 | assert signed01(-64) == -5 23 | -------------------------------------------------------------------------------- /tests/typing/signed02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import int16, int64 3 | from polyphony.timing import clktime 4 | 5 | def signed02(x:int16) -> int64: 6 | v = x 7 | if v < 0: 8 | t = clktime() 9 | else: 10 | t = 100 11 | return t 12 | 13 | 14 | @testbench 15 | def test(): 16 | print(signed02(1)) 17 | print(signed02(0)) 18 | print(signed02(-1)) 19 | -------------------------------------------------------------------------------- /tests/typing/typing01.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from polyphony import testbench 3 | from polyphony.typing import bit, bit2 4 | 5 | 6 | def typing01_a(a:bit, b:polyphony.typing.bit) -> bit2: 7 | return a + b 8 | 9 | 10 | def typing01_b(a:bit, b:polyphony.typing.bit) -> bit: 11 | return a + b 12 | 13 | 14 | @testbench 15 | def test(): 16 | assert 0 == typing01_a(0, 0) 17 | assert 1 == typing01_a(0, 1) 18 | assert 1 == typing01_a(1, 0) 19 | assert 2 == typing01_a(1, 1) 20 | 21 | assert 0 == typing01_b(0, 0) 22 | assert 1 == typing01_b(0, 1) 23 | assert 1 == typing01_b(1, 0) 24 | assert 0 == typing01_b(1, 1) 25 | -------------------------------------------------------------------------------- /tests/typing/typing02.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import List, bit, int8 3 | 4 | 5 | def bitdata(xs:List[bit], i:int8) -> bit: 6 | return xs[i] 7 | 8 | 9 | def intdata(xs:List[int], i:int8) -> int: 10 | return xs[i] 11 | 12 | 13 | def typing02_a(i:int8) -> bit: 14 | data = [0, 1, 1, 0, 1, 0, 1, 0] # type: List[bit] 15 | return bitdata(data, i) 16 | 17 | 18 | def typing02_b(i:int8) -> int: 19 | data = [0, 1, 1, 0, 1, 0, 1, 0] # type: List[int] 20 | return intdata(data, i) 21 | 22 | 23 | @testbench 24 | def test(): 25 | data = [0, 1, 1, 0, 1, 0, 1, 0] 26 | for i in range(8): 27 | assert data[i] == typing02_a(i) 28 | assert data[i] == typing02_b(i) 29 | -------------------------------------------------------------------------------- /tests/typing/typing03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import List, bit, int8 3 | 4 | 5 | def bitdata(xs:List[bit], i:int8) -> bit: 6 | return xs[i] 7 | 8 | 9 | def intdata(xs:List[int], i:int8) -> int: 10 | return xs[i] 11 | 12 | 13 | def typing03_a(i:int8) -> bit: 14 | data = [0, 1, 1, 0, 1, 0, 1, 0] # type: List[bit] 15 | return bitdata(data, i) 16 | 17 | 18 | def typing03_b(i:int8) -> int: 19 | data = [0, 1, 1, 0, 1, 0, 1, 0] # type: List[int] 20 | return intdata(data, i) 21 | 22 | 23 | @testbench 24 | def test(): 25 | data = [0, 1, 1, 0, 1, 0, 1, 0] 26 | for i in range(8): 27 | assert data[i] == typing03_a(i) 28 | assert data[i] == typing03_b(i) 29 | -------------------------------------------------------------------------------- /tests/typing/typing04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import Tuple, bit, int8 3 | 4 | 5 | def typing04_a(xs:Tuple[bit, bit, bit], i:int8) -> bit: 6 | return xs[i] 7 | 8 | 9 | def typing04_b(xs:Tuple[int, int, int], i:int8) -> int: 10 | return xs[i] 11 | 12 | 13 | def typing04(): 14 | data = (0, 1, 1) # type: Tuple[bit, bit, bit] 15 | for i in range(len(data)): 16 | d = data[i] 17 | assert d == typing04_a(data, i) 18 | assert d == typing04_b(data, i) 19 | 20 | 21 | @testbench 22 | def test(): 23 | typing04() 24 | -------------------------------------------------------------------------------- /tests/typing/typing05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import Tuple, bit, int8 3 | 4 | 5 | def typing05_a(xs:Tuple[bit, ...], i:int8) -> bit: 6 | return xs[i] 7 | 8 | 9 | def typing05_b(xs:Tuple[int, ...], i:int8) -> int: 10 | return xs[i] 11 | 12 | 13 | def typing05(): 14 | data = (0, 1, 1) # type: Tuple[bit, ...] 15 | for i in range(len(data)): 16 | d = data[i] 17 | assert d == typing05_a(data, i) 18 | assert d == typing05_b(data, i) 19 | 20 | @testbench 21 | def test(): 22 | typing05() 23 | -------------------------------------------------------------------------------- /tests/typing/typing06.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from polyphony import testbench 3 | from polyphony.typing import int4, int8 4 | 5 | 6 | class UserClass: 7 | def __init__(self, x): 8 | self.x = x # type: int8 9 | self.y: int8 = x + 1 # This is legal in only Python3.6 or later 10 | 11 | 12 | def func(c:UserClass): 13 | return c.x * c.y 14 | 15 | 16 | def typing06(x:int4): 17 | c = UserClass(x) 18 | return func(c) 19 | 20 | 21 | @testbench 22 | def test(): 23 | assert 2 == typing06(1) 24 | assert 6 == typing06(2) 25 | -------------------------------------------------------------------------------- /tests/typing/typing08.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony.typing import bit8, int8 3 | 4 | 5 | def typing08(a:bit8) -> int8: 6 | assert a > 0 7 | b:int8 = a 8 | assert b < 0 9 | return b 10 | 11 | @testbench 12 | def test(): 13 | assert typing08(-1) < 0 14 | -------------------------------------------------------------------------------- /tests/typing/typing09.py: -------------------------------------------------------------------------------- 1 | import polyphony 2 | from polyphony import testbench 3 | from polyphony.typing import bit, Tuple 4 | 5 | 6 | bits = (1, 0) # type: Tuple[bit, bit] 7 | 8 | 9 | def typing09(i): 10 | return bits[i] 11 | 12 | 13 | @testbench 14 | def test(): 15 | assert 1 == typing09(0) 16 | assert 0 == typing09(1) 17 | -------------------------------------------------------------------------------- /tests/unroll/unroll03.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import unroll as ur 3 | 4 | 5 | def unroll03_a(x): 6 | sum = 0 7 | for i in ur(range(10), 2): 8 | for j in ur(range(10)): 9 | sum += (i * j * x) 10 | return sum 11 | 12 | 13 | def unroll03_b(x): 14 | sum = 0 15 | for i in ur(range(10)): 16 | for j in ur(range(10)): 17 | sum += (i * j * x) 18 | return sum 19 | 20 | 21 | @testbench 22 | def test(): 23 | assert 2025 == unroll03_a(1) 24 | assert 4050 == unroll03_a(2) 25 | assert 2025 == unroll03_b(1) 26 | assert 4050 == unroll03_b(2) 27 | -------------------------------------------------------------------------------- /tests/unroll/unroll04.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import unroll 3 | 4 | 5 | def unroll04_a(): 6 | xs = [10, 20, 30, 40] 7 | sum = 0 8 | for x in unroll(xs, 4): 9 | sum += x 10 | return sum 11 | 12 | 13 | def unroll04_b(): 14 | xs = [10, 20, 30, 40] 15 | sum = 0 16 | for x in unroll(xs, 2): 17 | sum += x 18 | return sum 19 | 20 | 21 | @testbench 22 | def test(): 23 | assert 100 == unroll04_a() 24 | assert 100 == unroll04_b() 25 | -------------------------------------------------------------------------------- /tests/unroll/unroll05.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import unroll 3 | 4 | 5 | def unroll05_a(): 6 | xs = [1, 2, 3, 4] 7 | sum = 0 8 | for i in unroll(range(2, 4)): 9 | sum += xs[i] 10 | return sum 11 | 12 | 13 | def unroll05_b(): 14 | xs = [10, 20, 30, 40] 15 | sum = 0 16 | for i in unroll(range(3, 4)): 17 | sum += xs[i] 18 | return sum 19 | 20 | 21 | def unroll05_c(): 22 | xs = [10, 20, 30, 40] 23 | sum = 0 24 | for k in range(4, 4): 25 | for i in unroll(range(4, 4)): 26 | sum += xs[i] 27 | return sum 28 | 29 | 30 | @testbench 31 | def test(): 32 | assert 7 == unroll05_a() 33 | assert 40 == unroll05_b() 34 | assert 0 == unroll05_c() 35 | -------------------------------------------------------------------------------- /tests/unroll/unroll14.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import unroll 3 | 4 | 5 | def unroll14_a(stop): 6 | sum = 0 7 | for i in unroll(range(stop), 4): 8 | sum += i 9 | return sum 10 | 11 | 12 | def unroll14_b(stop): 13 | sum = 0 14 | for i in unroll(range(stop), 5): 15 | sum += i 16 | return sum 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 0 == unroll14_a(0) 22 | assert 0 == unroll14_a(1) 23 | assert 1 == unroll14_a(2) 24 | assert 6 == unroll14_a(4) 25 | assert 45 == unroll14_a(10) 26 | 27 | assert 0 == unroll14_b(0) 28 | assert 0 == unroll14_b(1) 29 | assert 1 == unroll14_b(2) 30 | assert 6 == unroll14_b(4) 31 | assert 45 == unroll14_b(10) 32 | -------------------------------------------------------------------------------- /tests/unroll/unroll15.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import unroll 3 | 4 | 5 | def unroll15_a(start, stop): 6 | sum = 0 7 | for i in unroll(range(start, stop), 4): 8 | sum += i 9 | return sum 10 | 11 | 12 | def unroll15_b(start, stop): 13 | sum = 0 14 | for i in unroll(range(start, stop), 5): 15 | sum += i 16 | return sum 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 0 == unroll15_a(0, 0) 22 | assert 45 == unroll15_a(0, 10) 23 | assert 35 == unroll15_a(5, 10) 24 | 25 | assert 0 == unroll15_b(0, 0) 26 | assert 45 == unroll15_b(0, 10) 27 | assert 35 == unroll15_b(5, 10) 28 | -------------------------------------------------------------------------------- /tests/unroll/unroll16.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import unroll 3 | 4 | 5 | def unroll16(start, stop): 6 | sum = 0 7 | for i in unroll(range(4)): 8 | sum += i 9 | 10 | for i in range(10): 11 | sum += i 12 | 13 | for i in unroll(range(4)): 14 | sum += i 15 | 16 | return sum 17 | 18 | 19 | @testbench 20 | def test(): 21 | print(unroll16(0, 10)) 22 | assert 57 == unroll16(0, 10) 23 | -------------------------------------------------------------------------------- /tests/unroll/unroll17.py: -------------------------------------------------------------------------------- 1 | from polyphony import testbench 2 | from polyphony import unroll 3 | 4 | 5 | def f(a): 6 | if (a < 0): 7 | return -a 8 | else: 9 | return a 10 | 11 | def unroll17(): 12 | sum = 0 13 | for i in unroll(range(-10, 10)): 14 | sum += f(i) 15 | 16 | return sum 17 | 18 | 19 | @testbench 20 | def test(): 21 | assert 100 == unroll17() 22 | -------------------------------------------------------------------------------- /tests/warning/pipeline_hazard01.py: -------------------------------------------------------------------------------- 1 | #The pipeline may not work correctly if there is both read and write access to the same memory 'xs0' 2 | from polyphony import testbench 3 | from polyphony import pipelined 4 | 5 | 6 | def pipeline_hazard01(xs0, xs1, xs2): 7 | for i in pipelined(range(len(xs0) - 1)): 8 | xs1[i] = xs0[i] 9 | xs0[i + 1] = xs2[i] 10 | 11 | 12 | @testbench 13 | def test(): 14 | data0 = [1, 2, 3] 15 | data1 = [0, 0, 0] 16 | data2 = [1, 2, 1] 17 | pipeline_hazard01(data0, data1, data2) 18 | assert 1 == data0[0] 19 | assert 1 == data0[1] 20 | assert 2 == data0[2] 21 | assert 1 == data1[0] 22 | assert 1 == data1[1] 23 | assert 0 == data1[2] 24 | 25 | 26 | test() 27 | -------------------------------------------------------------------------------- /tests/warning/pipeline_resource01.py: -------------------------------------------------------------------------------- 1 | #There is a read conflict at 'xs' in a pipeline, II will be adjusted 2 | from polyphony import testbench 3 | from polyphony import rule 4 | 5 | 6 | def pipeline_resource01(xs, ys): 7 | with rule(scheduling='pipeline'): 8 | for i in range(4): 9 | a = xs[i] 10 | b = xs[i + 1] 11 | ys[i] = (a + b) >> 1 12 | return 13 | 14 | 15 | @testbench 16 | def test(): 17 | out = [None] * 400 18 | pipeline_resource01([1, 2, 3, 4] * 100, out) 19 | 20 | 21 | test() 22 | -------------------------------------------------------------------------------- /tests/warning/pipeline_resource02.py: -------------------------------------------------------------------------------- 1 | #There is a write conflict at 'ys' in a pipeline, II will be adjusted 2 | from polyphony import testbench 3 | from polyphony import rule 4 | 5 | 6 | def pipeline_resource02(xs, ys): 7 | with rule(scheduling='pipeline'): 8 | for i in range(4): 9 | a = xs[i] 10 | ys[i] = a 11 | ys[i + 1] = a << 1 12 | return 13 | 14 | 15 | @testbench 16 | def test(): 17 | out = [None] * 400 18 | pipeline_resource02([1, 2, 3, 4] * 100, out) 19 | 20 | 21 | test() 22 | -------------------------------------------------------------------------------- /tests/warning/port_is_not_used01.py: -------------------------------------------------------------------------------- 1 | #Port 'p' is not used at all 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class port_is_not_used01: 8 | def __init__(self): 9 | self.p = Port(bool, 'out') 10 | self.q = Port(bool, 'out') 11 | self.append_worker(self.w) 12 | 13 | def w(self): 14 | self.q.wr(1) 15 | 16 | 17 | m = port_is_not_used01() 18 | -------------------------------------------------------------------------------- /tests/warning/port_is_not_used02.py: -------------------------------------------------------------------------------- 1 | #Port 'p' is not used at all 2 | from polyphony import module 3 | from polyphony.io import Port 4 | 5 | 6 | @module 7 | class port_is_not_used02: 8 | def __init__(self): 9 | self.p = Port(bool, 'out') 10 | self.q = Port(bool, 'out') 11 | self.append_worker(self.w, self.p, self.q) 12 | 13 | def w(self, p, q): 14 | q.wr(1) 15 | 16 | 17 | m = port_is_not_used02() 18 | -------------------------------------------------------------------------------- /tests/warning/static_assert.py: -------------------------------------------------------------------------------- 1 | #The expression of assert always evaluates to False 2 | from polyphony import testbench 3 | from polyphony import __version__ 4 | 5 | 6 | @testbench 7 | def test(): 8 | assert __version__ == '' 9 | 10 | 11 | test() --------------------------------------------------------------------------------