plotkit 0.5.0

A matplotlib-shaped, publication-quality plotting library for Rust
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Edge case tests for plotkit.
//!
//! Each test documents whether the edge case panics, returns an error,
//! or produces valid output.

use plotkit::{Figure, FigureExt};

// ============================================================================
// Helper: renders a Figure to SVG bytes so we can verify "produces output"
// without writing to disk.
// ============================================================================

fn render_svg(fig: &Figure) -> String {
    fig.to_svg_string().expect("SVG render should succeed")
}

// ============================================================================
// 1. Empty data — plot with empty x/y vectors
// ============================================================================

#[test]
fn edge_empty_data_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let empty: Vec<f64> = vec![];
    let result = ax.plot(&empty, &empty);
    assert!(result.is_err(), "plot() with empty data should return an error");
    match result.unwrap_err() {
        plotkit::error::PlotError::EmptyData => {} // expected
        other => panic!("Expected EmptyData, got: {:?}", other),
    }
}

#[test]
fn edge_empty_data_scatter() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let empty: Vec<f64> = vec![];
    let result = ax.scatter(&empty, &empty);
    assert!(result.is_err(), "scatter() with empty data should return an error");
}

#[test]
fn edge_empty_data_bar() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let cats: Vec<&str> = vec![];
    let heights: Vec<f64> = vec![];
    let result = ax.bar(cats.as_slice(), &heights);
    assert!(result.is_err(), "bar() with empty data should return an error");
}

#[test]
fn edge_empty_data_hist() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let empty: Vec<f64> = vec![];
    let result = ax.hist(&empty, 10);
    assert!(result.is_err(), "hist() with empty data should return an error");
}

// ============================================================================
// 2. Single point — plot with exactly 1 data point
// ============================================================================

#[test]
fn edge_single_point_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([42.0], [7.0]).expect("single-point plot should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "SVG output should not be empty");
    assert!(svg.contains("<svg"), "output should be valid SVG");
}

#[test]
fn edge_single_point_scatter() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.scatter([1.0], [2.0]).expect("single-point scatter should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_single_point_hist() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.hist([5.0], 10).expect("single-point hist should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 3. Very large values — x/y values like 1e15
// ============================================================================

#[test]
fn edge_very_large_values() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x = vec![1e15, 2e15, 3e15];
    let y = vec![4e15, 5e15, 6e15];
    ax.plot(&x, &y).expect("large-value plot should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "should produce output with large values");
    assert!(svg.contains("<svg"), "should be valid SVG");
}

#[test]
fn edge_very_large_values_scatter() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.scatter([1e15, 2e15], [3e15, 4e15]).expect("large-value scatter should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 4. Very small values — x/y values like 1e-15
// ============================================================================

#[test]
fn edge_very_small_values() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x = vec![1e-15, 2e-15, 3e-15];
    let y = vec![4e-15, 5e-15, 6e-15];
    ax.plot(&x, &y).expect("small-value plot should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "should produce output with small values");
}

#[test]
fn edge_very_small_values_hist() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let data = vec![1e-15, 2e-15, 3e-15, 4e-15, 5e-15];
    ax.hist(&data, 5).expect("small-value hist should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 5. Negative values — all negative data
// ============================================================================

#[test]
fn edge_all_negative_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x = vec![-10.0, -5.0, -1.0];
    let y = vec![-100.0, -50.0, -10.0];
    ax.plot(&x, &y).expect("negative-value plot should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "should produce output with negative values");
}

#[test]
fn edge_all_negative_bar() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let cats: &[&str] = &["a", "b", "c"];
    let heights = vec![-3.0, -5.0, -1.0];
    ax.bar(cats, &heights).expect("negative-height bar should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_all_negative_hist() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let data = vec![-10.0, -5.0, -3.0, -1.0, -0.5];
    ax.hist(&data, 5).expect("negative-value hist should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 6. NaN/Inf values — data containing f64::NAN or f64::INFINITY
// ============================================================================

#[test]
fn edge_nan_inf_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x = vec![1.0, f64::NAN, 3.0, f64::INFINITY, 5.0];
    let y = vec![2.0, 4.0, f64::NEG_INFINITY, 6.0, f64::NAN];
    ax.plot(&x, &y).expect("NaN/Inf plot should succeed (data accepted)");
    // Rendering should not panic even with NaN/Inf values
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "should produce output even with NaN/Inf");
}

#[test]
fn edge_all_nan_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x = vec![f64::NAN, f64::NAN, f64::NAN];
    let y = vec![f64::NAN, f64::NAN, f64::NAN];
    ax.plot(&x, &y).expect("all-NaN plot should succeed (data accepted)");
    // Rendering should not panic
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_all_inf_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x = vec![f64::INFINITY, f64::NEG_INFINITY];
    let y = vec![f64::INFINITY, f64::NEG_INFINITY];
    ax.plot(&x, &y).expect("all-Inf plot should succeed (data accepted)");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_nan_inf_hist() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    // Mix of finite and non-finite values. hist should succeed on finite subset.
    let data = vec![1.0, f64::NAN, 3.0, f64::INFINITY, 5.0];
    ax.hist(&data, 5).expect("hist with NaN/Inf should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_all_nan_hist() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    // All NaN: series is not empty (len=3), but no finite values.
    // hist may behave differently here -- let's find out.
    let data = vec![f64::NAN, f64::NAN, f64::NAN];
    // This should NOT panic regardless of whether it returns Ok or Err.
    let result = ax.hist(&data, 5);
    // Document the behavior:
    match result {
        Ok(_) => {
            // If it succeeds, rendering should not panic
            let svg = render_svg(&fig);
            assert!(!svg.is_empty());
        }
        Err(e) => {
            eprintln!("all-NaN hist returned error (acceptable): {:?}", e);
        }
    }
}

// ============================================================================
// 7. Zero-size figure — Figure with width=0 or height=0
// ============================================================================

#[test]
fn edge_zero_width_figure() {
    let mut fig = Figure::with_size(0, 600);
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0], [3.0, 4.0]).unwrap();
    // Should not panic during rendering
    let svg = fig.to_svg_string().expect("zero-width SVG should not fail");
    assert!(svg.contains("<svg"), "should still produce SVG markup");
}

#[test]
fn edge_zero_height_figure() {
    let mut fig = Figure::with_size(800, 0);
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0], [3.0, 4.0]).unwrap();
    let svg = fig.to_svg_string().expect("zero-height SVG should not fail");
    assert!(svg.contains("<svg"));
}

#[test]
fn edge_zero_both_figure() {
    let mut fig = Figure::with_size(0, 0);
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0], [3.0, 4.0]).unwrap();
    let svg = fig.to_svg_string().expect("zero-size SVG should not fail");
    assert!(svg.contains("<svg"));
}

// ============================================================================
// 8. Huge data — 100,000 data points
// ============================================================================

#[test]
fn edge_huge_data_plot() {
    let n = 100_000;
    let x: Vec<f64> = (0..n).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|v| v.sin()).collect();
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot(&x, &y).expect("100k-point plot should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "should produce SVG for 100k points");
    // SVG should be substantial (many path segments)
    assert!(svg.len() > 1000, "SVG for 100k points should be sizable");
}

#[test]
fn edge_huge_data_scatter() {
    let n = 100_000;
    let x: Vec<f64> = (0..n).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|v| v.cos()).collect();
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.scatter(&x, &y).expect("100k-point scatter should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_huge_data_hist() {
    let n = 100_000;
    let data: Vec<f64> = (0..n).map(|i| (i as f64 * 0.001).sin()).collect();
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.hist(&data, 50).expect("100k-point hist should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 9. Mismatched lengths — x has 10 elements, y has 5
// ============================================================================

#[test]
fn edge_mismatched_lengths_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x: Vec<f64> = (0..10).map(|i| i as f64).collect();
    let y: Vec<f64> = (0..5).map(|i| i as f64).collect();
    let result = ax.plot(&x, &y);
    assert!(result.is_err(), "mismatched lengths should return an error");
    match result.unwrap_err() {
        plotkit::error::PlotError::SeriesLengthMismatch { expected, got } => {
            assert_eq!(expected, 10);
            assert_eq!(got, 5);
        }
        other => panic!("Expected SeriesLengthMismatch, got: {:?}", other),
    }
}

#[test]
fn edge_mismatched_lengths_scatter() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let x: Vec<f64> = (0..10).map(|i| i as f64).collect();
    let y: Vec<f64> = (0..5).map(|i| i as f64).collect();
    let result = ax.scatter(&x, &y);
    assert!(result.is_err());
    match result.unwrap_err() {
        plotkit::error::PlotError::SeriesLengthMismatch { expected: 10, got: 5 } => {}
        other => panic!("Expected SeriesLengthMismatch(10,5), got: {:?}", other),
    }
}

#[test]
fn edge_mismatched_lengths_bar() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let cats: &[&str] = &["a", "b", "c"];
    let heights = vec![1.0, 2.0]; // only 2 heights for 3 categories
    let result = ax.bar(cats, &heights);
    assert!(result.is_err());
}

// ============================================================================
// 10. Unicode text — title/labels with Unicode chars like "μ ± σ"
// ============================================================================

#[test]
fn edge_unicode_title() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0, 3.0], [1.0, 4.0, 9.0]).unwrap();
    ax.set_title("μ ± σ — Gaussian distribution (日本語)");
    ax.set_xlabel("Δx (μm)");
    ax.set_ylabel("Ω (Ω·m⁻¹)");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "Unicode labels should not break rendering");
    // Check that Unicode text appears in the SVG (possibly HTML-escaped)
    assert!(svg.contains("Gaussian") || svg.contains("μ"), "Unicode text should appear in SVG");
}

#[test]
fn edge_unicode_suptitle() {
    let mut fig = Figure::new();
    fig.suptitle("Ωmega — α β γ δ ε ζ η θ ι κ λ μ");
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0], [1.0, 2.0]).unwrap();
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_unicode_legend_labels() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0], [1.0, 2.0]).unwrap().label("α-series");
    ax.plot([1.0, 2.0], [2.0, 3.0]).unwrap().label("β-series");
    ax.legend();
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 11. Empty string labels — empty title, xlabel, ylabel
// ============================================================================

#[test]
fn edge_empty_string_labels() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0, 3.0], [1.0, 4.0, 9.0]).unwrap();
    ax.set_title("");
    ax.set_xlabel("");
    ax.set_ylabel("");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "empty-string labels should not break rendering");
}

#[test]
fn edge_empty_string_suptitle() {
    let mut fig = Figure::new();
    fig.suptitle("");
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([1.0, 2.0], [1.0, 2.0]).unwrap();
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 12. Multiple subplots — 3x3 grid of subplots
// ============================================================================

#[test]
fn edge_3x3_subplots() {
    let mut fig = Figure::new();
    for i in 1..=9 {
        let ax = fig.add_subplot(3, 3, i);
        let x = vec![0.0, 1.0, 2.0];
        let y = vec![i as f64, (i * 2) as f64, (i * 3) as f64];
        ax.plot(&x, &y).expect("subplot plot should succeed");
        ax.set_title(&format!("Plot {}", i));
    }
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "3x3 grid should produce valid SVG");
    // Should have at least 9 plot areas (clip paths)
    assert!(svg.len() > 500, "3x3 SVG should be substantial");
}

#[test]
fn edge_subplots_mixed_chart_types() {
    let mut fig = Figure::with_size(1200, 900);
    // Subplot 1: line
    let ax1 = fig.add_subplot(2, 2, 1);
    ax1.plot([1.0, 2.0, 3.0], [1.0, 4.0, 9.0]).unwrap();
    // Subplot 2: scatter
    let ax2 = fig.add_subplot(2, 2, 2);
    ax2.scatter([1.0, 2.0, 3.0], [9.0, 4.0, 1.0]).unwrap();
    // Subplot 3: bar
    let ax3 = fig.add_subplot(2, 2, 3);
    ax3.bar(&["a", "b", "c"][..], [3.0, 7.0, 5.0]).unwrap();
    // Subplot 4: hist
    let ax4 = fig.add_subplot(2, 2, 4);
    ax4.hist([1.0, 2.0, 2.0, 3.0, 3.0, 3.0], 3).unwrap();
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 13. Bar chart with 0 values — all bars height 0
// ============================================================================

#[test]
fn edge_bar_all_zero_heights() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let cats: &[&str] = &["a", "b", "c", "d"];
    let heights = vec![0.0, 0.0, 0.0, 0.0];
    ax.bar(cats, &heights).expect("zero-height bars should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "zero-height bar chart should produce valid SVG");
}

#[test]
fn edge_bar_mixed_zero_and_nonzero() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let cats: &[&str] = &["a", "b", "c"];
    let heights = vec![0.0, 5.0, 0.0];
    ax.bar(cats, &heights).expect("mixed zero/non-zero bars should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// 14. Histogram with single value — all values the same
// ============================================================================

#[test]
fn edge_hist_all_same_value() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let data = vec![5.0, 5.0, 5.0, 5.0, 5.0];
    ax.hist(&data, 10).expect("all-same-value hist should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "constant-data histogram should produce valid SVG");
}

#[test]
fn edge_hist_all_same_value_single_bin() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let data = vec![42.0; 100];
    ax.hist(&data, 1).expect("all-same-value, 1 bin should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

#[test]
fn edge_hist_all_same_value_many_bins() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    let data = vec![3.125; 50];
    ax.hist(&data, 100).expect("all-same-value, 100 bins should succeed");
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}

// ============================================================================
// Additional edge cases worth checking
// ============================================================================

#[test]
fn edge_figure_no_axes_render() {
    // Render a figure with no axes at all
    let fig = Figure::new();
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "empty figure should still produce SVG wrapper");
    assert!(svg.contains("<svg"));
}

#[test]
fn edge_axes_no_data_render() {
    // Axes exist but have no artists
    let mut fig = Figure::new();
    fig.add_subplot(1, 1, 1);
    let svg = render_svg(&fig);
    assert!(!svg.is_empty(), "axes with no data should render background/spines");
}

#[test]
fn edge_two_points_plot() {
    let mut fig = Figure::new();
    let ax = fig.add_subplot(1, 1, 1);
    ax.plot([0.0, 1.0], [0.0, 1.0]).unwrap();
    let svg = render_svg(&fig);
    assert!(!svg.is_empty());
}