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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//! Safety net for 0.23 (per-panel dodge/jitter, negative stacking, legend
//! wrap for multiple guides, vertical legend fit): a battery of fixed
//! plots exercising the four deliberate changes plus their unaffected
//! (byte-identical) counterparts, rendered through the plotters-free
//! native SVG path, hashed with FNV-1a.
//!
//! If a hash here needs to change, it must be caused by one of #153,
//! #154, #155, #159 (or a documented follow-up), and the change must be
//! listed under CHANGELOG's `[0.23.0]` "Changed". Anything else that
//! flips a hash here is a regression — fix the code, not the test.
//!
//! `all_positive_stack`'s hash changed under #169 (0.24): see that case's
//! own data-fn comment below and CHANGELOG's `[0.24.0]` entry — a
//! documented follow-up, not a regression.
//!
//! Runs in every feature configuration (no polars/plotters/sf-only API), so
//! it is part of every `cargo test` job, including `--no-default-features`
//! and `--no-default-features --features sf`.
use ggplot_rs::data::Value;
use ggplot_rs::guide::config::GuideLegend;
use ggplot_rs::prelude::*;
/// FNV-1a, 64-bit. No dependency; good enough to catch byte-for-byte drift.
fn fnv1a(bytes: &[u8]) -> u64 {
let mut hash: u64 = 0xcbf29ce484222325;
for &b in bytes {
hash ^= b as u64;
hash = hash.wrapping_mul(0x100000001b3);
}
hash
}
fn hash_svg(plot: GGPlot, w: u32, h: u32, what: &str) -> u64 {
let svg = plot
.render_svg_native_with_size(w, h)
.unwrap_or_else(|e| panic!("{what}: render failed: {e}"));
fnv1a(svg.as_bytes())
}
type Cols = Vec<(String, Vec<Value>)>;
fn f(v: &[f64]) -> Vec<Value> {
v.iter().map(|x| Value::Float(*x)).collect()
}
fn s(v: &[&str]) -> Vec<Value> {
v.iter().map(|x| Value::Str(x.to_string())).collect()
}
/// #153: 2 panels sharing x = 1; panel P has 3 groups there, panel Q has
/// only 1 — the per-panel dodge fix changes panel Q's offset.
fn dodge_facet_data() -> Cols {
vec![
("panel".into(), s(&["P", "P", "P", "Q"])),
("x".into(), f(&[1.0, 1.0, 1.0, 1.0])),
("grp".into(), s(&["g1", "g2", "g3", "g1"])),
("y".into(), f(&[1.0, 2.0, 3.0, 1.0])),
]
}
/// A single-panel dodge: unaffected by #153 (nothing to partition).
fn dodge_single_panel_data() -> Cols {
vec![
("x".into(), f(&[1.0, 1.0, 1.0])),
("grp".into(), s(&["g1", "g2", "g3"])),
("y".into(), f(&[1.0, 2.0, 3.0])),
]
}
/// #153: 2 panels with very different x spacing (resolution 1 vs 0.01),
/// same fixed facet scale — the per-panel jitter fix scales each panel's
/// default jitter by its own resolution.
fn jitter_facet_data() -> Cols {
vec![
("panel".into(), s(&["P", "P", "P", "Q", "Q", "Q"])),
("x".into(), f(&[1.0, 2.0, 3.0, 10.00, 10.01, 10.02])),
("y".into(), f(&[1.0, 2.0, 3.0, 10.00, 10.01, 10.02])),
]
}
/// #154: a bar chart mixing positive and negative values at one x. Up
/// through 0.23, `geom_col()` defaulted to `PositionIdentity` (review
/// #167 finding 3 — a pre-existing parity gap from ggplot2, which
/// defaults `geom_col` to `position_stack`; tracked in #169), so
/// exercising #154 needed an explicit `.position(PositionStack)` /
/// `.position(PositionFill)` below. #169 (0.24) fixed that gap, so this
/// data is no longer load-bearing for reaching #154's code through the
/// default path — the explicit `.position(...)` calls below are kept
/// anyway: they're what document *which* position variant (`Stack` vs
/// `Fill`) each case exercises, and this fixture is unaffected by #169
/// either way (every row explicitly overrides the default).
fn mixed_sign_data() -> Cols {
vec![
("x".into(), f(&[1.0, 1.0, 1.0, 1.0])),
("grp".into(), s(&["a", "b", "c", "d"])),
("y".into(), f(&[3.0, -2.0, 4.0, -1.0])),
]
}
/// A bar chart with only positive values at each x, rendered with
/// `geom_col()`'s *default* position (no explicit `.position(...)`) —
/// unaffected by #154 (the negative bucket is simply empty either way),
/// but, as of #169 (0.24), no longer byte-identical to 0.22.2/0.23: two
/// groups at a shared x now stack instead of drawing on top of each
/// other from `y = 0` (ggplot2 parity — `geom_col()`'s default position
/// is `position_stack()`, matching `geom_bar()`'s, see
/// `tests/issue169_geom_col_default_stack.rs`).
fn all_positive_stack_data() -> Cols {
vec![
("x".into(), f(&[1.0, 1.0])),
("grp".into(), s(&["a", "b"])),
("y".into(), f(&[3.0, 5.0])),
]
}
/// #155: `colour` and `shape` both mapped to discrete variables, with
/// enough entries that a bottom legend overflows a one-line layout.
fn two_discrete_guides_data() -> Cols {
let n = 8;
let x: Vec<Value> = (0..n).map(|i| Value::Float(i as f64)).collect();
let y: Vec<Value> = (0..n).map(|i| Value::Float((i % 3) as f64)).collect();
let g: Vec<Value> = (0..n)
.map(|i| Value::Str(format!("Colour {:02}", i + 1)))
.collect();
let sh: Vec<Value> = (0..n)
.map(|i| Value::Str(format!("Shape {:02}", i + 1)))
.collect();
vec![
("x".into(), x),
("y".into(), y),
("g".into(), g),
("sh".into(), sh),
]
}
/// A single discrete guide, overflowing a one-line bottom legend: the
/// 0.22 auto-wrap path, unaffected by #155 (only one guide present).
fn one_discrete_guide_data() -> Cols {
let n = 10;
let x: Vec<Value> = (0..n).map(|i| Value::Float(i as f64)).collect();
let y: Vec<Value> = (0..n).map(|i| Value::Float((i % 3) as f64)).collect();
let g: Vec<Value> = (0..n)
.map(|i| Value::Str(format!("Group {:02}", i + 1)))
.collect();
vec![("x".into(), x), ("y".into(), y), ("g".into(), g)]
}
/// #159: a vertical `ncol(2)` grid that already fits — unaffected (no
/// reduction, no truncation, no warning).
fn ncol_fits_data() -> Cols {
let x: Vec<Value> = (0..4).map(|i| Value::Float(i as f64)).collect();
let y: Vec<Value> = (0..4).map(|i| Value::Float((i % 3) as f64)).collect();
let g: Vec<Value> = (0..4).map(|i| Value::Str(format!("G{}", i + 1))).collect();
vec![("x".into(), x), ("y".into(), y), ("g".into(), g)]
}
/// #159: an `ncol(4)` grid with long labels on a narrow canvas — column
/// reduction alone resolves the overflow.
fn ncol_reduces_data() -> Cols {
let n = 12;
let x: Vec<Value> = (0..n).map(|i| Value::Float(i as f64)).collect();
let y: Vec<Value> = (0..n).map(|i| Value::Float((i % 3) as f64)).collect();
let g: Vec<Value> = (0..n)
.map(|i| Value::Str(format!("A Very Long Group Label Number {}", i + 1)))
.collect();
vec![("x".into(), x), ("y".into(), y), ("g".into(), g)]
}
/// #159: an `ncol(6)` grid with extremely long labels — column reduction
/// alone isn't enough, so labels truncate with an ellipsis.
fn ncol_truncates_data() -> Cols {
let n = 12;
let x: Vec<Value> = (0..n).map(|i| Value::Float(i as f64)).collect();
let y: Vec<Value> = (0..n).map(|i| Value::Float((i % 3) as f64)).collect();
let g: Vec<Value> = (0..n)
.map(|i| {
Value::Str(format!(
"An Extremely Very Super Duper Long Group Label Number {} \
With Lots More Text Appended Here To Make It Even Longer",
i + 1
))
})
.collect();
vec![("x".into(), x), ("y".into(), y), ("g".into(), g)]
}
fn expected() -> Vec<(&'static str, u64)> {
vec![
("dodge_facet", 0x87ca741c10e5d81f),
("dodge_single_panel", 0x60eede539d041c9f),
("jitter_facet", 0x2041b5e10e321c52),
("stack_mixed_sign", 0xd89d244ba7f70ae2),
("fill_mixed_sign", 0xf1c8fa0e2df7203a),
// #169 (0.24): geom_col()'s default position changed from
// PositionIdentity to PositionStack (ggplot2 parity) — two
// groups at a shared x now stack instead of overlapping from
// y=0. See this case's data-fn comment above and CHANGELOG
// [0.24.0].
("all_positive_stack", 0x236f799c5fa67957),
// #237 (fix/size-legend-keys-236): the `shape` legend's key glyph
// has no mapped colour of its own, so it now falls back to
// `theme.geom.ink`, then `theme.legend_text.color` (pure black in
// the base theme) instead of a fixed dark grey — a documented
// follow-up, not a regression. See CHANGELOG [Unreleased].
("two_discrete_guides", 0xa2c3eed5edc8e2e2),
("one_discrete_guide", 0x98da47e69ace53db),
("ncol_fits", 0x4bbbea598fcaaceb),
("ncol_reduces", 0xfb022ae18760d3b7),
("ncol_truncates", 0x74f922dad5ba6460),
]
}
#[test]
fn default_output_023_battery() {
let cases: Vec<(&str, u64)> = vec![
(
"dodge_facet",
hash_svg(
GGPlot::new(dodge_facet_data())
.aes(Aes::new().x("x").y("y").color("grp"))
.geom_point()
.position(ggplot_rs::position::dodge::position_dodge(0.9))
.facet_wrap("panel", None),
600,
300,
"dodge_facet",
),
),
(
"dodge_single_panel",
hash_svg(
GGPlot::new(dodge_single_panel_data())
.aes(Aes::new().x("x").y("y").color("grp"))
.geom_point()
.position(ggplot_rs::position::dodge::position_dodge(0.9)),
480,
320,
"dodge_single_panel",
),
),
(
"jitter_facet",
hash_svg(
GGPlot::new(jitter_facet_data())
.aes(Aes::new().x("x").y("y"))
.geom_jitter()
.facet_wrap("panel", None),
600,
300,
"jitter_facet",
),
),
(
"stack_mixed_sign",
hash_svg(
GGPlot::new(mixed_sign_data())
.aes(Aes::new().x("x").y("y").fill("grp"))
.geom_col()
.position(PositionStack),
320,
400,
"stack_mixed_sign",
),
),
(
"fill_mixed_sign",
hash_svg(
GGPlot::new(mixed_sign_data())
.aes(Aes::new().x("x").y("y").fill("grp"))
.geom_col()
.position(PositionFill),
320,
400,
"fill_mixed_sign",
),
),
(
"all_positive_stack",
hash_svg(
GGPlot::new(all_positive_stack_data())
.aes(Aes::new().x("x").y("y").fill("grp"))
.geom_col(),
320,
400,
"all_positive_stack",
),
),
(
"two_discrete_guides",
hash_svg(
GGPlot::new(two_discrete_guides_data())
.aes(Aes::new().x("x").y("y").color("g").shape("sh"))
.geom_point()
.legend_position(LegendPosition::Bottom),
640,
500,
"two_discrete_guides",
),
),
(
"one_discrete_guide",
hash_svg(
GGPlot::new(one_discrete_guide_data())
.aes(Aes::new().x("x").y("y").color("g"))
.geom_point()
.legend_position(LegendPosition::Bottom),
640,
400,
"one_discrete_guide",
),
),
(
"ncol_fits",
hash_svg(
GGPlot::new(ncol_fits_data())
.aes(Aes::new().x("x").y("y").color("g"))
.geom_point()
.guides(GuideLegend::new().with_ncol(2)),
500,
400,
"ncol_fits",
),
),
(
"ncol_reduces",
hash_svg(
GGPlot::new(ncol_reduces_data())
.aes(Aes::new().x("x").y("y").color("g"))
.geom_point()
.guides(GuideLegend::new().with_ncol(4)),
300,
400,
"ncol_reduces",
),
),
(
"ncol_truncates",
hash_svg(
GGPlot::new(ncol_truncates_data())
.aes(Aes::new().x("x").y("y").color("g"))
.geom_point()
.guides(GuideLegend::new().with_ncol(6)),
200,
400,
"ncol_truncates",
),
),
];
let expected = expected();
assert_eq!(
cases.len(),
expected.len(),
"case count drifted from the recorded table"
);
let mut mismatches = Vec::new();
for ((name, got), (exp_name, exp_hash)) in cases.iter().zip(expected.iter()) {
assert_eq!(name, exp_name, "case order drifted from the recorded table");
if got != exp_hash {
mismatches.push(format!(
"{name}: expected {exp_hash:#018x}, got {got:#018x}"
));
}
}
assert!(
mismatches.is_empty(),
"default-output hash mismatch — must be caused by #153/#154/#155/#159 and \
documented in CHANGELOG [0.23.0]:\n{}",
mismatches.join("\n")
);
}