-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvplot_test.v
More file actions
120 lines (112 loc) · 2.29 KB
/
Copy pathvplot_test.v
File metadata and controls
120 lines (112 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module vplot
import os
fn gnuplot_available() bool {
if e := os.getenv_opt('VPLOT_GNUPLOT') {
return os.is_executable(e)
}
_ := os.find_abs_path_of_executable('gnuplot') or { return false }
return true
}
fn tmp_out(name string) string {
return os.join_path(os.temp_dir(), 'vplot_test_${name}')
}
fn test_plot_fn_and_save_png() {
if !gnuplot_available() {
eprintln('gnuplot not found, skipping test')
return
}
out := tmp_out('fn.png')
os.rm(out) or {}
mut p := new()!
p.set_title('sine wave')!
p.plot_fn('sin(x)', label: 'sin(x)', samples: 200)!
p.save(out, 640, 480)!
p.close()
assert os.exists(out)
assert os.file_size(out) > 0
os.rm(out) or {}
}
fn test_plot_xy_series() {
if !gnuplot_available() {
eprintln('gnuplot not found, skipping test')
return
}
out := tmp_out('xy.png')
os.rm(out) or {}
x := [0.0, 1, 2, 3, 4, 5]
mut y1 := []f64{cap: x.len}
mut y2 := []f64{cap: x.len}
for v in x {
y1 << v * v
y2 << 2 * v + 1
}
mut p := new()!
p.plot_series([
Series{
x: x
y: y1
cfg: SeriesCfg{
label: 'x^2'
style: .lines_points
color: '#d62728'
}
},
Series{
x: x
y: y2
cfg: SeriesCfg{
label: '2x+1'
style: .lines
}
},
])!
p.save(out, 640, 480)!
p.close()
assert os.exists(out)
assert os.file_size(out) > 0
os.rm(out) or {}
}
fn test_plot3_fn() {
if !gnuplot_available() {
eprintln('gnuplot not found, skipping test')
return
}
out := tmp_out('3d.png')
os.rm(out) or {}
mut p := new()!
p.plot3_fn('sin(sqrt(x**2 + y**2))', 'ripple')!
p.save(out, 640, 480)!
p.close()
assert os.exists(out)
assert os.file_size(out) > 0
os.rm(out) or {}
}
fn test_save_rejects_unknown_extension() {
if !gnuplot_available() {
eprintln('gnuplot not found, skipping test')
return
}
mut p := new()!
p.plot_fn('x')!
p.save(tmp_out('bad.bmp'), 640, 480) or {
assert err.msg().contains('unsupported output extension')
p.close()
return
}
p.close()
assert false, 'expected error for .bmp output'
}
fn test_plot_series_rejects_mismatched_lengths() {
if !gnuplot_available() {
eprintln('gnuplot not found, skipping test')
return
}
mut p := new()!
p.plot_xy([0.0, 1, 2], [1.0, 2]) or {
assert err.msg().contains('equal length')
p.close()
return
}
p.close()
assert false, 'expected error for mismatched x/y lengths'
}