sorting-race 0.2.0

Terminal-based sorting algorithm race visualization tool with real-time metrics
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
//! Contract tests for Sparkline component
//!
//! These tests verify the expected interface and behavior of the Sparkline visualization component.
//! The tests are designed to work with the existing Sparkline implementation from src/lib/sparkline.rs
//! but also test additional expected features.

use std::collections::VecDeque;

/// Extended Sparkline component for visualization metrics
/// This extends the existing Sparkline with features expected by the visualization system
#[derive(Debug)]
pub struct MetricsSparkline {
    data: VecDeque<f64>,
    capacity: usize,
    width: usize,
    _height: usize,
    title: String,
    unit: String,
}

impl MetricsSparkline {
    /// Create a new sparkline with specified capacity for data points
    pub fn new(capacity: usize) -> Self {
        Self {
            data: VecDeque::with_capacity(capacity),
            capacity,
            width: capacity.min(100), // Default visual width
            _height: 1,
            title: String::new(),
            unit: String::new(),
        }
    }

    /// Add a data point and maintain the rolling window
    pub fn add_point(&mut self, value: f64) {
        if self.data.len() >= self.capacity {
            self.data.pop_front();
        }
        self.data.push_back(value);
    }

    /// Render the sparkline using Unicode block characters
    pub fn render(&self) -> String {
        if self.data.is_empty() {
            return " ".repeat(self.width);
        }

        let min_val = self.data.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max_val = self.data.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        // Avoid division by zero
        let range = if (max_val - min_val).abs() < f64::EPSILON {
            1.0
        } else {
            max_val - min_val
        };

        // Unicode block characters for different heights
        let chars = ['', '', '', '', '', '', '', ''];
        let levels = chars.len() as f64;

        let mut result = String::new();
        let _points_to_show = self.data.len().min(self.width);

        // Take the most recent data points that fit in the width
        let start_index = if self.data.len() > self.width {
            self.data.len() - self.width
        } else {
            0
        };

        for i in start_index..self.data.len() {
            let value = self.data[i];
            let normalized = ((value - min_val) / range).clamp(0.0, 1.0);
            let level = (normalized * (levels - 1.0)).round() as usize;
            result.push(chars[level]);
        }

        // Pad with spaces if needed
        while result.chars().count() < self.width {
            result.push(' ');
        }

        result
    }

    /// Get the current data points
    pub fn get_data(&self) -> Vec<f64> {
        self.data.iter().copied().collect()
    }

    /// Get the capacity (maximum number of points)
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Get current number of data points
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Set the title for this sparkline
    pub fn with_title(mut self, title: &str) -> Self {
        self.title = title.to_string();
        self
    }

    /// Set the unit for values
    pub fn with_unit(mut self, unit: &str) -> Self {
        self.unit = unit.to_string();
        self
    }

    /// Set the visual width
    pub fn with_width(mut self, width: usize) -> Self {
        self.width = width;
        self
    }

    /// Clear all data points
    pub fn clear(&mut self) {
        self.data.clear();
    }

    /// Get statistics for the current data
    pub fn get_stats(&self) -> SparklineStats {
        if self.data.is_empty() {
            return SparklineStats::default();
        }

        let min = self.data.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max = self.data.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        let sum: f64 = self.data.iter().sum();
        let avg = sum / self.data.len() as f64;

        // Calculate standard deviation
        let variance: f64 =
            self.data.iter().map(|&x| (x - avg).powi(2)).sum::<f64>() / self.data.len() as f64;
        let std_dev = variance.sqrt();

        SparklineStats {
            min,
            max,
            avg,
            std_dev,
            count: self.data.len(),
        }
    }

    /// Render with full context (title, sparkline, stats)
    pub fn render_with_context(&self) -> String {
        let sparkline = self.render();
        let stats = self.get_stats();

        if self.title.is_empty() {
            format!(
                "{} (min: {:.1}, max: {:.1}, avg: {:.1}{})",
                sparkline, stats.min, stats.max, stats.avg, self.unit
            )
        } else {
            format!(
                "{}: {} (min: {:.1}, max: {:.1}, avg: {:.1}{})",
                self.title, sparkline, stats.min, stats.max, stats.avg, self.unit
            )
        }
    }
}

/// Statistics for sparkline data
#[derive(Debug, Clone, Default)]
pub struct SparklineStats {
    pub min: f64,
    pub max: f64,
    pub avg: f64,
    pub std_dev: f64,
    pub count: usize,
}

/// Collection of sparklines for different metrics
#[derive(Debug)]
pub struct SparklineCollection {
    sparklines: std::collections::HashMap<String, MetricsSparkline>,
    default_capacity: usize,
    default_width: usize,
}

impl SparklineCollection {
    /// Create a new collection with default settings
    pub fn new(default_capacity: usize, default_width: usize) -> Self {
        Self {
            sparklines: std::collections::HashMap::new(),
            default_capacity,
            default_width,
        }
    }

    /// Add or update a metric sparkline
    pub fn update_metric(&mut self, name: &str, value: f64) {
        let sparkline = self.sparklines.entry(name.to_string()).or_insert_with(|| {
            MetricsSparkline::new(self.default_capacity)
                .with_width(self.default_width)
                .with_title(name)
        });
        sparkline.add_point(value);
    }

    /// Get a sparkline by name
    pub fn get(&self, name: &str) -> Option<&MetricsSparkline> {
        self.sparklines.get(name)
    }

    /// Get a mutable sparkline by name
    pub fn get_mut(&mut self, name: &str) -> Option<&mut MetricsSparkline> {
        self.sparklines.get_mut(name)
    }

    /// Render all sparklines
    pub fn render_all(&self) -> Vec<String> {
        let mut result = Vec::new();
        let mut keys: Vec<_> = self.sparklines.keys().collect();
        keys.sort();

        for key in keys {
            if let Some(sparkline) = self.sparklines.get(key) {
                result.push(sparkline.render_with_context());
            }
        }

        result
    }

    /// Get sparkline names
    pub fn get_names(&self) -> Vec<String> {
        let mut names: Vec<_> = self.sparklines.keys().cloned().collect();
        names.sort();
        names
    }

    /// Clear all sparklines
    pub fn clear(&mut self) {
        self.sparklines.clear();
    }

    /// Get number of sparklines
    pub fn len(&self) -> usize {
        self.sparklines.len()
    }

    /// Check if collection is empty
    pub fn is_empty(&self) -> bool {
        self.sparklines.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sparkline_data_collection_100_point_history() {
        let mut sparkline = MetricsSparkline::new(100);

        // Add 100 points
        for i in 0..100 {
            sparkline.add_point(i as f64);
        }

        assert_eq!(sparkline.len(), 100);
        assert_eq!(sparkline.capacity(), 100);

        let data = sparkline.get_data();
        assert_eq!(data[0], 0.0);
        assert_eq!(data[99], 99.0);
    }

    #[test]
    fn test_data_point_addition_and_overflow() {
        let mut sparkline = MetricsSparkline::new(5); // Small capacity for testing overflow

        // Add more points than capacity
        for i in 0..10 {
            sparkline.add_point(i as f64);
        }

        // Should only keep the last 5 points
        assert_eq!(sparkline.len(), 5);
        let data = sparkline.get_data();
        assert_eq!(data, vec![5.0, 6.0, 7.0, 8.0, 9.0]);
    }

    #[test]
    fn test_rendering_with_different_scales() {
        let mut sparkline = MetricsSparkline::new(10).with_width(10);

        // Test linear scale
        for i in 1..=10 {
            sparkline.add_point(i as f64);
        }
        let linear_render = sparkline.render();
        assert_eq!(linear_render.chars().count(), 10);

        // Test with different value ranges
        sparkline.clear();
        sparkline.add_point(1000.0);
        sparkline.add_point(2000.0);
        sparkline.add_point(3000.0);

        let scaled_render = sparkline.render();
        assert_eq!(scaled_render.chars().count(), 10);
        // Should contain Unicode block characters
        assert!(scaled_render.chars().any(|c| "▁▂▃▄▅▆▇█".contains(c)));
    }

    #[test]
    fn test_multiple_sparklines_for_different_metrics() {
        let mut collection = SparklineCollection::new(50, 20);

        // Add data for different metrics
        collection.update_metric("comparisons", 100.0);
        collection.update_metric("swaps", 50.0);
        collection.update_metric("memory_usage", 1024.0);

        assert_eq!(collection.len(), 3);

        let names = collection.get_names();
        assert_eq!(names, vec!["comparisons", "memory_usage", "swaps"]);

        // Verify each sparkline exists and has data
        assert!(collection.get("comparisons").is_some());
        assert!(collection.get("swaps").is_some());
        assert!(collection.get("memory_usage").is_some());

        let comparisons_sparkline = collection.get("comparisons").unwrap();
        assert_eq!(comparisons_sparkline.len(), 1);
        assert_eq!(comparisons_sparkline.get_data(), vec![100.0]);
    }

    #[test]
    fn test_sparkline_stats_calculation() {
        let mut sparkline = MetricsSparkline::new(10);

        // Add known values for testing stats
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        for value in &values {
            sparkline.add_point(*value);
        }

        let stats = sparkline.get_stats();
        assert_eq!(stats.min, 1.0);
        assert_eq!(stats.max, 5.0);
        assert_eq!(stats.avg, 3.0); // (1+2+3+4+5)/5 = 3
        assert_eq!(stats.count, 5);

        // Standard deviation for [1,2,3,4,5] is sqrt(2) ≈ 1.414
        // Variance = ((1-3)² + (2-3)² + (3-3)² + (4-3)² + (5-3)²) / 5
        //          = (4 + 1 + 0 + 1 + 4) / 5 = 2
        // Std dev = sqrt(2) ≈ 1.414
        assert!((stats.std_dev - 1.414).abs() < 0.01);
    }

    #[test]
    fn test_sparkline_rendering_unicode_blocks() {
        let mut sparkline = MetricsSparkline::new(8).with_width(8);

        // Add ascending values to test all Unicode blocks
        for i in 0..8 {
            sparkline.add_point(i as f64);
        }

        let rendered = sparkline.render();
        assert_eq!(rendered.chars().count(), 8);

        // Should contain the lowest and highest block characters
        assert!(rendered.contains('')); // Lowest block
        assert!(rendered.contains('')); // Highest block
    }

    #[test]
    fn test_empty_sparkline_rendering() {
        let sparkline = MetricsSparkline::new(10).with_width(5);

        let rendered = sparkline.render();
        assert_eq!(rendered, "     "); // Should be spaces
        assert_eq!(rendered.len(), 5);
    }

    #[test]
    fn test_sparkline_with_identical_values() {
        let mut sparkline = MetricsSparkline::new(5).with_width(5);

        // Add identical values
        for _ in 0..5 {
            sparkline.add_point(42.0);
        }

        let rendered = sparkline.render();
        assert_eq!(rendered.chars().count(), 5);
        // When all values are identical, they should all render as the same character
        // The exact character depends on the implementation's normalization approach
        // For identical values, it should pick a consistent middle character
        let first_char = rendered.chars().next().unwrap();
        assert!(rendered.chars().all(|c| c == first_char));
    }

    #[test]
    fn test_sparkline_width_adjustment() {
        let mut sparkline = MetricsSparkline::new(100);

        // Add more data points than the display width
        for i in 0..50 {
            sparkline.add_point(i as f64);
        }

        // Set narrow width
        sparkline = sparkline.with_width(10);
        let rendered = sparkline.render();
        assert_eq!(rendered.chars().count(), 10);

        // Should show only the most recent data points
        let data = sparkline.get_data();
        assert_eq!(data.len(), 50); // All data still stored
    }

    #[test]
    fn test_sparkline_collection_updates() {
        let mut collection = SparklineCollection::new(10, 15);

        // Update the same metric multiple times
        for i in 1..=5 {
            collection.update_metric("test_metric", i as f64);
        }

        let sparkline = collection.get("test_metric").unwrap();
        assert_eq!(sparkline.len(), 5);
        assert_eq!(sparkline.get_data(), vec![1.0, 2.0, 3.0, 4.0, 5.0]);
    }

    #[test]
    fn test_sparkline_render_with_context() {
        let mut sparkline = MetricsSparkline::new(5)
            .with_width(5)
            .with_title("Test Metric")
            .with_unit("ms");

        sparkline.add_point(10.0);
        sparkline.add_point(20.0);
        sparkline.add_point(30.0);

        let rendered = sparkline.render_with_context();
        assert!(rendered.contains("Test Metric"));
        assert!(rendered.contains("ms"));
        assert!(rendered.contains("min: 10.0"));
        assert!(rendered.contains("max: 30.0"));
        assert!(rendered.contains("avg: 20.0"));
    }

    #[test]
    fn test_collection_render_all() {
        let mut collection = SparklineCollection::new(5, 8);

        collection.update_metric("metric_a", 10.0);
        collection.update_metric("metric_b", 20.0);
        collection.update_metric("metric_a", 15.0);

        let all_rendered = collection.render_all();
        assert_eq!(all_rendered.len(), 2);

        // Should be sorted by name
        assert!(all_rendered[0].starts_with("metric_a:"));
        assert!(all_rendered[1].starts_with("metric_b:"));
    }

    #[test]
    fn test_sparkline_clear_functionality() {
        let mut sparkline = MetricsSparkline::new(10);

        sparkline.add_point(1.0);
        sparkline.add_point(2.0);
        sparkline.add_point(3.0);

        assert_eq!(sparkline.len(), 3);

        sparkline.clear();
        assert_eq!(sparkline.len(), 0);
        assert!(sparkline.is_empty());

        let stats = sparkline.get_stats();
        assert_eq!(stats.count, 0);
    }

    #[test]
    fn test_sparkline_extreme_values() {
        let mut sparkline = MetricsSparkline::new(5).with_width(5);

        // Test with very large and very small values
        sparkline.add_point(f64::MIN / 2.0);
        sparkline.add_point(0.0);
        sparkline.add_point(f64::MAX / 2.0);

        let rendered = sparkline.render();
        assert_eq!(rendered.chars().count(), 5);

        // Should handle extreme values without panicking
        let stats = sparkline.get_stats();
        assert!(stats.min.is_finite());
        assert!(stats.max.is_finite());
        assert!(stats.avg.is_finite());
    }
}