1+ from __future__ import division
2+
3+ import math
4+
5+ from variants import variants
6+
7+ from ._division_data import DivisionData
8+
9+ import pytest
10+
11+ ###
12+ # Example implementation - division function
13+ @variants
14+ def divide (x , y ):
15+ """A function that divides x by y."""
16+ return x / y
17+
18+
19+ @divide .variant ('round' )
20+ def divide (x , y ):
21+ return round (x / y )
22+
23+
24+ @divide .variant ('round_callmain' )
25+ def divide (x , y ):
26+ return round (divide (x , y ))
27+
28+
29+ @divide .variant ('floor' )
30+ def divide (x , y ):
31+ return math .floor (divide (x , y ))
32+
33+
34+ @divide .variant ('ceil' )
35+ def divide (x , y ):
36+ return math .ceil (divide (x , y ))
37+
38+
39+ @divide .variant ('mode' )
40+ def divide (x , y , mode = None ):
41+ funcs = {
42+ None : divide ,
43+ 'round' : divide .round ,
44+ 'floor' : divide .floor ,
45+ 'ceil' : divide .ceil
46+ }
47+
48+ return funcs [mode ](x , y )
49+
50+ ###
51+ # Division Function Tests
52+ @pytest .mark .parametrize ('x, y, expected' , DivisionData .DIV_VALS )
53+ def test_main (x , y , expected ):
54+ assert divide (x , y ) == expected
55+
56+
57+ @pytest .mark .parametrize ('x,y,expected' , DivisionData .ROUND_VALS )
58+ def test_round (x , y , expected ):
59+ assert divide .round (x , y ) == expected
60+
61+
62+ @pytest .mark .parametrize ('x,y,expected' , DivisionData .ROUND_VALS )
63+ def test_round_callmain (x , y , expected ):
64+ assert divide .round_callmain (x , y ) == expected
65+
66+
67+ @pytest .mark .parametrize ('x,y,expected' , DivisionData .FLOOR_VALS )
68+ def test_round_callmain (x , y , expected ):
69+ assert divide .floor (x , y ) == expected
70+
71+ @pytest .mark .parametrize ('x,y,expected' , DivisionData .CEIL_VALS )
72+ def test_round_callmain (x , y , expected ):
73+ assert divide .ceil (x , y ) == expected
74+
75+
76+ @pytest .mark .parametrize ('x,y,expected,mode' , DivisionData .MODE_VALS )
77+ def test_mode (x , y , expected , mode ):
78+ assert divide .mode (x , y , mode ) == expected
79+
80+
81+ ###
82+ # Division function metadata tests
83+ def test_name ():
84+ assert divide .__name__ == 'divide'
85+
86+
87+ def test_docstring ():
88+ assert divide .__doc__ == """A function that divides x by y."""
89+
90+
91+ def test_repr ():
92+ assert repr (divide ) == '<VariantFunction divide>'
0 commit comments