|
58 | 58 | import mathics.builtin.drawing.plot as plot |
59 | 59 | from mathics.core.util import print_expression_tree |
60 | 60 |
|
61 | | -both = [ |
62 | | - ("test-plot3d", "Plot3D[x y, {x,0,1}, {y,0,1}]"), |
63 | | -] |
64 | | - |
| 61 | +# non-vectorized available, vectorized not available, |
65 | 62 | classic = [ |
66 | | - ("test-plot", "Plot[x, {x,0,1}]"), |
| 63 | + ("barchart", "BarChart[{3,5,2,7}]"), |
| 64 | + ("discreteplot", "DiscretePlot[n^2,{n,1,10}]"), |
| 65 | + ("histogram", "Histogram[{1,1,1,5,5,7,8,8,8}]"), |
| 66 | + ("listlineplot", "ListLinePlot[{1,4,2,5,3}]"), |
| 67 | + ("listplot", "ListPlot[{1,4,2,5,3}]"), |
| 68 | + ("liststepplot", "ListStepPlot[{1,4,2,5,3}]"), |
| 69 | + #("manipulate", "Manipulate[Plot[a x,{x,0,1}],{a,0,5}]"), |
| 70 | + ("numberlineplot", "NumberLinePlot[{1,3,4}]"), |
| 71 | + ("parametricplot", "ParametricPlot[{Cos[t],Sin[t]},{t,0,2 Pi}]"), |
| 72 | + ("piechart", "PieChart[{3,2,5}]"), |
| 73 | + ("plot", "Plot[Sin[x], {x, 0, 2 Pi}]"), |
| 74 | + ("polarplot", "PolarPlot[1+0.5 Cos[3 θ],{θ,0,2 Pi}]"), |
67 | 75 | ] |
68 | 76 |
|
| 77 | +# vectorized available, non-vectorized not available |
69 | 78 | vectorized = [ |
70 | | - ("test-density", "DensityPlot[x y, {x,0,1}, {y,0,1}]"), |
| 79 | + ("complexplot", "ComplexPlot[Exp[I z],{z,-2-2 I,2+2 I}]"), |
| 80 | + ("complexplot3d", "ComplexPlot3D[Exp[I z],{z,-2-2 I,2+2 I}]"), |
| 81 | + ("contourplot-1", "ContourPlot[x^2-y^2,{x,-2,2},{y,-2,2}]"), |
| 82 | + ("contourplot-2", "ContourPlot[x^2+y^2==1,{x,-2,2},{y,-2,2}]"), |
71 | 83 | ] |
72 | 84 |
|
| 85 | +# both vectorized and non-vectorized available |
| 86 | +both = [ |
| 87 | + ("densityplot", "DensityPlot[Sin[x y],{x,-2,2},{y,-2,2}]"), |
| 88 | + ("plot3d", "Plot3D[Sin[x y],{x,-2,2},{y,-2,2}]"), |
| 89 | +] |
73 | 90 |
|
74 | | -def ref_dir(): |
75 | | - dir, _ = os.path.splitext(__file__) |
76 | | - return dir + "_ref" |
| 91 | +# common plotting options to test with and without |
| 92 | +opts = """ |
| 93 | + AspectRatio -> 2, |
| 94 | + Axes -> False, |
| 95 | + BoxRatios -> {1, 2, 3}, |
| 96 | + Frame -> False, |
| 97 | + Mesh -> Full, |
| 98 | + PlotPoints -> 10 |
| 99 | +""" |
77 | 100 |
|
| 101 | +# compute reference dir, which is this file minus .py plus _ref |
| 102 | +path, _ = os.path.splitext(__file__) |
| 103 | +ref_dir = path + "_ref" |
| 104 | +print(f"ref_dir {ref_dir}") |
78 | 105 |
|
79 | | -def one_test(name, str_expr, act_dir="/tmp"): |
80 | | - print(f"=== running {name} {str_expr}") |
81 | 106 |
|
82 | | - # evaluate the expression to be tested |
83 | | - expr = session.parse(str_expr) |
84 | | - act_expr = expr.evaluate(session.evaluation) |
| 107 | +def one_test(name, str_expr, vec, opt, act_dir="/tmp"): |
85 | 108 |
|
86 | | - # write the results to act_fn in act_dir |
87 | | - act_fn = os.path.join(act_dir, f"{name}.txt") |
88 | | - with open(act_fn, "w") as act_f: |
89 | | - print_expression_tree(act_expr, file=act_f) |
| 109 | + # update name and set use_vectorized_plot depending on |
| 110 | + # whether vectorized test |
| 111 | + if vec: |
| 112 | + name += "-vec" |
| 113 | + plot.use_vectorized_plot = vec |
| 114 | + else: |
| 115 | + name += "-cls" |
90 | 116 |
|
91 | | - # use diff to compare the actual result in act_fn to reference result in ref_fn |
92 | | - ref_fn = os.path.join(ref_dir(), f"{name}.txt") |
93 | | - result = subprocess.run(["diff", "-u", ref_fn, act_fn], capture_output=False) |
94 | | - assert result.returncode == 0, "reference and actual output differ" |
95 | | - os.remove(act_fn) |
| 117 | + # update name and splice in options depending on |
| 118 | + # whether default or with-options test |
| 119 | + if opt: |
| 120 | + name += "-opt" |
| 121 | + str_expr = f"{str_expr[:-1]}, {opts}]" |
| 122 | + else: |
| 123 | + name += "-def" |
| 124 | + |
| 125 | + print(f"=== running {name} {str_expr}") |
96 | 126 |
|
97 | | -def test_all(act_dir="/tmp"): |
98 | | - # run vectorized tests |
99 | 127 | try: |
100 | | - plot.use_vectorized_plot = True |
101 | | - for name, str_expr in vectorized + both: |
102 | | - one_test(name + "-vectorized", str_expr, act_dir) |
| 128 | + # evaluate the expression to be tested |
| 129 | + expr = session.parse(str_expr) |
| 130 | + act_expr = expr.evaluate(session.evaluation) |
| 131 | + |
| 132 | + # write the results to act_fn in act_dir |
| 133 | + act_fn = os.path.join(act_dir, f"{name}.txt") |
| 134 | + with open(act_fn, "w") as act_f: |
| 135 | + print_expression_tree(act_expr, file=act_f) |
| 136 | + |
| 137 | + # use diff to compare the actual result in act_fn to reference result in ref_fn |
| 138 | + ref_fn = os.path.join(ref_dir, f"{name}.txt") |
| 139 | + result = subprocess.run(["diff", "-U", "5", ref_fn, act_fn], capture_output=False) |
| 140 | + assert result.returncode == 0, "reference and actual result differ" |
| 141 | + if act_fn != ref_fn: |
| 142 | + os.remove(act_fn) |
| 143 | + |
103 | 144 | finally: |
104 | 145 | plot.use_vectorized_plot = False |
105 | 146 |
|
106 | | - # run classic tests |
107 | | - for name, str_expr in classic + both: |
108 | | - one_test(name, str_expr, act_dir) |
| 147 | + |
| 148 | +def test_all(act_dir="/tmp", opt=None): |
| 149 | + |
| 150 | + # run twice, once without and once with options |
| 151 | + for opt in [False, True]: |
| 152 | + |
| 153 | + # run classic tests |
| 154 | + for name, str_expr in classic + both: |
| 155 | + one_test(name, str_expr, False, opt, act_dir) |
| 156 | + |
| 157 | + # run vectorized tests |
| 158 | + for name, str_expr in vectorized + both: |
| 159 | + one_test(name, str_expr, True, opt, act_dir) |
109 | 160 |
|
110 | 161 |
|
111 | 162 | if __name__ == "__main__": |
112 | 163 | # reference files can be generated by pointing saved actual |
113 | 164 | # output at reference dir instead of /tmp |
114 | 165 | def make_ref_files(): |
115 | | - test_all(ref_dir()) |
| 166 | + test_all(ref_dir) |
116 | 167 |
|
117 | 168 | def run_tests(): |
118 | 169 | try: |
119 | 170 | test_all() |
120 | 171 | except AssertionError: |
121 | 172 | print("FAIL") |
122 | 173 |
|
123 | | - # make_ref_files() |
| 174 | + #make_ref_files() |
124 | 175 | run_tests() |
0 commit comments