crdtosphere 0.1.0

Universal embedded CRDTs for distributed coordination across automotive, robotics, IoT, and industrial applications
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Sensor Fusion CRDTs for Automotive Applications
//!
//! This module implements CRDTs for multi-sensor data fusion with reliability
//! weighting and automotive-specific sensor coordination patterns.

use crate::automotive::safety::{ASILLevel, SafetyLevel};
use crate::clock::CompactTimestamp;
use crate::error::{CRDTError, CRDTResult};
use crate::memory::{MemoryConfig, NodeId};
use crate::traits::{BoundedCRDT, CRDT, RealTimeCRDT};

/// Sensor reliability levels for automotive applications
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ReliabilityLevel {
    /// Low reliability sensor (e.g., single point sensor)
    Low = 1,
    /// Medium reliability sensor (e.g., redundant sensor)
    Medium = 2,
    /// High reliability sensor (e.g., safety-critical redundant)
    High = 3,
    /// Ultra-high reliability (e.g., triple redundant safety sensor)
    UltraHigh = 4,
}

impl ReliabilityLevel {
    /// Returns the weight factor for this reliability level
    pub fn weight(&self) -> f32 {
        match self {
            ReliabilityLevel::Low => 1.0,
            ReliabilityLevel::Medium => 2.0,
            ReliabilityLevel::High => 4.0,
            ReliabilityLevel::UltraHigh => 8.0,
        }
    }

    /// Returns true if this reliability level is suitable for safety-critical applications
    pub fn is_safety_suitable(&self) -> bool {
        *self >= ReliabilityLevel::High
    }
}

/// Individual sensor reading with metadata
#[derive(Debug, Clone, Copy)]
pub struct SensorReading<T> {
    /// Sensor value
    pub value: T,
    /// Timestamp when reading was taken
    pub timestamp: CompactTimestamp,
    /// Node ID of the sensor
    pub node_id: NodeId,
    /// Reliability level of this sensor
    pub reliability: ReliabilityLevel,
    /// Safety level of this sensor
    pub safety_level: SafetyLevel,
}

impl<T> SensorReading<T> {
    /// Creates a new sensor reading
    pub fn new(
        value: T,
        timestamp: u64,
        node_id: NodeId,
        reliability: ReliabilityLevel,
        safety_level: SafetyLevel,
    ) -> Self {
        Self {
            value,
            timestamp: CompactTimestamp::new(timestamp),
            node_id,
            reliability,
            safety_level,
        }
    }

    /// Returns the effective weight of this reading
    pub fn effective_weight(&self) -> f32 {
        let reliability_weight = self.reliability.weight();
        let safety_weight = match self.safety_level {
            SafetyLevel::Automotive(ASILLevel::QM) => 1.0,
            SafetyLevel::Automotive(ASILLevel::AsilA) => 1.5,
            SafetyLevel::Automotive(ASILLevel::AsilB) => 2.0,
            SafetyLevel::Automotive(ASILLevel::AsilC) => 3.0,
            SafetyLevel::Automotive(ASILLevel::AsilD) => 4.0,
            _ => 1.0,
        };
        reliability_weight * safety_weight
    }
}

/// Multi-sensor fusion CRDT for automotive applications
///
/// This CRDT aggregates sensor readings from multiple sources with
/// reliability weighting and safety-level prioritization.
///
/// # Type Parameters
/// - `T`: The sensor value type (must support arithmetic operations)
/// - `C`: Memory configuration
///
/// # Features
/// - Reliability-weighted averaging
/// - Safety-level prioritization
/// - Outlier detection and rejection
/// - Temporal consistency checking
///
/// # Example
/// ```rust
/// use crdtosphere::prelude::*;
/// use crdtosphere::automotive::{SensorFusion, SensorReading, ReliabilityLevel, SafetyLevel, ASILLevel};
///
/// // Create sensor fusion for temperature readings
/// let mut temp_fusion = SensorFusion::<f32, DefaultConfig>::new(1);
///
/// // Add readings from different sensors
/// let reading1 = SensorReading::new(
///     23.5, 1000, 1,
///     ReliabilityLevel::High,
///     SafetyLevel::automotive(ASILLevel::AsilC)
/// );
/// temp_fusion.add_reading(reading1)?;
///
/// let reading2 = SensorReading::new(
///     24.1, 1001, 2,
///     ReliabilityLevel::Medium,
///     SafetyLevel::automotive(ASILLevel::AsilB)
/// );
/// temp_fusion.add_reading(reading2)?;
///
/// // Get fused result
/// let fused_temp = temp_fusion.fused_value();
/// # Ok::<(), crdtosphere::error::CRDTError>(())
/// ```
#[derive(Debug, Clone)]
pub struct SensorFusion<T, C: MemoryConfig> {
    /// Fused sensor readings
    readings: [Option<SensorReading<T>>; 8],
    /// Current count of readings
    reading_count: usize,
    /// Node ID for this fusion unit
    #[allow(dead_code)]
    node_id: NodeId,
    /// Phantom data for memory config
    _phantom: core::marker::PhantomData<C>,
}

impl<T, C: MemoryConfig> SensorFusion<T, C>
where
    T: Clone + PartialEq + Copy,
{
    /// Creates a new sensor fusion CRDT
    ///
    /// # Arguments
    /// * `node_id` - The ID of this node
    ///
    /// # Returns
    /// A new empty sensor fusion CRDT
    pub fn new(node_id: NodeId) -> Self {
        Self {
            readings: [const { None }; 8],
            reading_count: 0,
            node_id,
            _phantom: core::marker::PhantomData,
        }
    }

    /// Adds a new sensor reading
    ///
    /// # Arguments
    /// * `reading` - The sensor reading to add
    ///
    /// # Returns
    /// Ok(()) if successful, error if fusion is full
    pub fn add_reading(&mut self, reading: SensorReading<T>) -> CRDTResult<()> {
        // Check if we already have a reading from this node
        for i in 0..self.reading_count {
            if let Some(ref mut existing) = self.readings[i] {
                if existing.node_id == reading.node_id {
                    // Update if newer timestamp or higher safety level
                    let should_update = reading.safety_level > existing.safety_level
                        || (reading.safety_level == existing.safety_level
                            && reading.timestamp > existing.timestamp);

                    if should_update {
                        *existing = reading;
                    }
                    return Ok(());
                }
            }
        }

        // Add new reading if we have space
        if self.reading_count >= 8 {
            return Err(CRDTError::BufferOverflow);
        }

        self.readings[self.reading_count] = Some(reading);
        self.reading_count += 1;
        Ok(())
    }

    /// Gets all current sensor readings
    ///
    /// # Returns
    /// Iterator over current sensor readings
    pub fn readings(&self) -> impl Iterator<Item = &SensorReading<T>> {
        self.readings
            .iter()
            .take(self.reading_count)
            .filter_map(|r| r.as_ref())
    }

    /// Returns the number of sensor readings
    ///
    /// # Returns
    /// Count of sensor readings
    pub fn reading_count(&self) -> usize {
        self.reading_count
    }

    /// Checks if the fusion has any readings
    ///
    /// # Returns
    /// true if no readings are present
    pub fn is_empty(&self) -> bool {
        self.reading_count == 0
    }

    /// Gets the highest safety level among all readings
    ///
    /// # Returns
    /// The highest safety level, or None if no readings
    pub fn max_safety_level(&self) -> Option<SafetyLevel> {
        self.readings().map(|r| r.safety_level).max()
    }

    /// Filters readings by minimum safety level
    ///
    /// # Arguments
    /// * `min_safety` - Minimum safety level to include
    ///
    /// # Returns
    /// Iterator over readings meeting the safety requirement
    pub fn safety_filtered_readings(
        &self,
        min_safety: SafetyLevel,
    ) -> impl Iterator<Item = &SensorReading<T>> {
        self.readings()
            .filter(move |r| r.safety_level >= min_safety)
    }

    /// Validates sensor readings for consistency
    ///
    /// # Returns
    /// Ok(()) if readings are consistent, error otherwise
    pub fn validate_readings(&self) -> CRDTResult<()> {
        // Check for valid node IDs
        for reading in self.readings() {
            if reading.node_id as usize >= C::MAX_NODES {
                return Err(CRDTError::InvalidNodeId);
            }
        }

        // Check for temporal consistency (readings should be reasonably recent)
        if let Some(latest) = self.readings().map(|r| r.timestamp).max() {
            for reading in self.readings() {
                let time_diff = latest.as_u64().saturating_sub(reading.timestamp.as_u64());
                if time_diff > 10000 {
                    // 10 second threshold
                    return Err(CRDTError::InvalidState);
                }
            }
        }

        Ok(())
    }
}

// Numeric operations for sensor fusion
impl<C: MemoryConfig> SensorFusion<f32, C> {
    /// Computes the reliability-weighted average of all readings
    ///
    /// # Returns
    /// The fused sensor value, or None if no readings
    pub fn fused_value(&self) -> Option<f32> {
        if self.reading_count == 0 {
            return None;
        }

        let mut weighted_sum = 0.0;
        let mut total_weight = 0.0;

        for reading in self.readings() {
            let weight = reading.effective_weight();
            weighted_sum += reading.value * weight;
            total_weight += weight;
        }

        if total_weight > 0.0 {
            Some(weighted_sum / total_weight)
        } else {
            None
        }
    }

    /// Computes the safety-critical fused value (ASIL-C and above only)
    ///
    /// # Returns
    /// The safety-critical fused value, or None if no safety-critical readings
    pub fn safety_critical_value(&self) -> Option<f32> {
        let mut weighted_sum = 0.0;
        let mut total_weight = 0.0;
        let mut has_safety_readings = false;

        for reading in self.safety_filtered_readings(SafetyLevel::automotive(ASILLevel::AsilC)) {
            has_safety_readings = true;
            let weight = reading.effective_weight();
            weighted_sum += reading.value * weight;
            total_weight += weight;
        }

        if has_safety_readings && total_weight > 0.0 {
            Some(weighted_sum / total_weight)
        } else {
            None
        }
    }

    /// Computes the variance of sensor readings
    ///
    /// # Returns
    /// The variance of readings, or None if insufficient data
    pub fn variance(&self) -> Option<f32> {
        if self.reading_count < 2 {
            return None;
        }

        let mean = self.fused_value()?;
        let mut variance_sum = 0.0;
        let mut total_weight = 0.0;

        for reading in self.readings() {
            let weight = reading.effective_weight();
            let diff = reading.value - mean;
            variance_sum += weight * diff * diff;
            total_weight += weight;
        }

        if total_weight > 0.0 {
            Some(variance_sum / total_weight)
        } else {
            None
        }
    }

    /// Detects outlier readings based on statistical analysis
    ///
    /// # Arguments
    /// * `threshold` - Standard deviation threshold for outlier detection
    ///
    /// # Returns
    /// Vector of node IDs with outlier readings
    pub fn detect_outliers(&self, threshold: f32) -> [Option<NodeId>; 8] {
        let mean = match self.fused_value() {
            Some(m) => m,
            None => return [const { None }; 8],
        };

        let variance = match self.variance() {
            Some(v) => v,
            None => return [const { None }; 8],
        };

        // Simple sqrt approximation for no_std
        let std_dev = {
            let mut x = variance;
            if x == 0.0 {
                0.0
            } else {
                // Newton's method for sqrt approximation
                for _ in 0..10 {
                    x = 0.5 * (x + variance / x);
                }
                x
            }
        };

        let mut outliers = [const { None }; 8];
        let mut outlier_count = 0;

        for reading in self.readings() {
            if outlier_count >= 8 {
                break;
            }
            let deviation = (reading.value - mean).abs();
            if deviation > threshold * std_dev {
                outliers[outlier_count] = Some(reading.node_id);
                outlier_count += 1;
            }
        }

        outliers
    }
}

impl<T, C: MemoryConfig> CRDT<C> for SensorFusion<T, C>
where
    T: Clone + PartialEq + Copy + core::fmt::Debug,
{
    type Error = CRDTError;

    fn merge(&mut self, other: &Self) -> CRDTResult<()> {
        // Merge all readings from other
        for reading in other.readings() {
            self.add_reading(*reading)?;
        }
        Ok(())
    }

    fn eq(&self, other: &Self) -> bool {
        if self.reading_count != other.reading_count {
            return false;
        }

        // Check that all readings match (order doesn't matter)
        for reading in self.readings() {
            let mut found = false;
            for other_reading in other.readings() {
                if reading.node_id == other_reading.node_id
                    && reading.timestamp == other_reading.timestamp
                    && reading.value == other_reading.value
                {
                    found = true;
                    break;
                }
            }
            if !found {
                return false;
            }
        }

        true
    }

    fn size_bytes(&self) -> usize {
        core::mem::size_of::<Self>()
    }

    fn validate(&self) -> CRDTResult<()> {
        self.validate_readings()
    }

    fn state_hash(&self) -> u32 {
        let mut hash = 0u32;
        for reading in self.readings() {
            let value_ptr = &reading.value as *const T as usize;
            hash ^=
                (value_ptr as u32) ^ (reading.timestamp.as_u64() as u32) ^ (reading.node_id as u32);
        }
        hash ^= self.reading_count as u32;
        hash
    }

    fn can_merge(&self, other: &Self) -> bool {
        // Check if merging would exceed capacity
        let mut new_nodes = 0;
        for other_reading in other.readings() {
            let mut found = false;
            for our_reading in self.readings() {
                if our_reading.node_id == other_reading.node_id {
                    found = true;
                    break;
                }
            }
            if !found {
                new_nodes += 1;
            }
        }

        self.reading_count + new_nodes <= 8
    }
}

impl<T, C: MemoryConfig> BoundedCRDT<C> for SensorFusion<T, C>
where
    T: Clone + PartialEq + Copy + core::fmt::Debug,
{
    const MAX_SIZE_BYTES: usize = core::mem::size_of::<Self>();
    const MAX_ELEMENTS: usize = 8; // Maximum number of sensor readings

    fn memory_usage(&self) -> usize {
        core::mem::size_of::<Self>()
    }

    fn element_count(&self) -> usize {
        self.reading_count
    }

    fn compact(&mut self) -> CRDTResult<usize> {
        // Remove oldest readings if we're at capacity
        // This is a simple compaction strategy
        Ok(0) // No compaction for now
    }

    fn can_add_element(&self) -> bool {
        self.reading_count < Self::MAX_ELEMENTS
    }
}

impl<T, C: MemoryConfig> RealTimeCRDT<C> for SensorFusion<T, C>
where
    T: Clone + PartialEq + Copy + core::fmt::Debug,
{
    const MAX_MERGE_CYCLES: u32 = 200; // Bounded by number of readings
    const MAX_VALIDATE_CYCLES: u32 = 100;
    const MAX_SERIALIZE_CYCLES: u32 = 150;

    fn merge_bounded(&mut self, other: &Self) -> CRDTResult<()> {
        // Sensor fusion merge is bounded by the number of readings
        self.merge(other)
    }

    fn validate_bounded(&self) -> CRDTResult<()> {
        // Validation is bounded by the number of readings
        self.validate()
    }

    fn remaining_budget(&self) -> Option<u32> {
        // For automotive systems, we don't track budget
        None
    }

    fn set_budget(&mut self, _cycles: u32) {
        // For automotive systems, we don't limit budget
    }
}

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

    #[test]
    fn test_reliability_level_weights() {
        assert_eq!(ReliabilityLevel::Low.weight(), 1.0);
        assert_eq!(ReliabilityLevel::Medium.weight(), 2.0);
        assert_eq!(ReliabilityLevel::High.weight(), 4.0);
        assert_eq!(ReliabilityLevel::UltraHigh.weight(), 8.0);

        assert!(ReliabilityLevel::High.is_safety_suitable());
        assert!(!ReliabilityLevel::Low.is_safety_suitable());
    }

    #[test]
    fn test_sensor_reading_creation() {
        let reading = SensorReading::new(
            25.0,
            1000,
            1,
            ReliabilityLevel::High,
            SafetyLevel::automotive(ASILLevel::AsilC),
        );

        assert_eq!(reading.value, 25.0);
        assert_eq!(reading.node_id, 1);
        assert!(reading.effective_weight() > 4.0); // High reliability * ASIL-C
    }

    #[test]
    fn test_sensor_fusion_creation() {
        let fusion = SensorFusion::<f32, DefaultConfig>::new(1);
        assert!(fusion.is_empty());
        assert_eq!(fusion.reading_count(), 0);
        assert_eq!(fusion.fused_value(), None);
    }

    #[test]
    fn test_add_sensor_readings() {
        let mut fusion = SensorFusion::<f32, DefaultConfig>::new(1);

        let reading1 = SensorReading::new(
            23.0,
            1000,
            1,
            ReliabilityLevel::High,
            SafetyLevel::automotive(ASILLevel::AsilC),
        );

        let reading2 = SensorReading::new(
            25.0,
            1001,
            2,
            ReliabilityLevel::Medium,
            SafetyLevel::automotive(ASILLevel::AsilB),
        );

        assert!(fusion.add_reading(reading1).is_ok());
        assert!(fusion.add_reading(reading2).is_ok());

        assert_eq!(fusion.reading_count(), 2);
        assert!(!fusion.is_empty());
    }

    #[test]
    fn test_weighted_fusion() {
        let mut fusion = SensorFusion::<f32, DefaultConfig>::new(1);

        // High reliability, high safety reading
        let reading1 = SensorReading::new(
            20.0,
            1000,
            1,
            ReliabilityLevel::High,
            SafetyLevel::automotive(ASILLevel::AsilD),
        );

        // Low reliability, low safety reading
        let reading2 = SensorReading::new(
            30.0,
            1001,
            2,
            ReliabilityLevel::Low,
            SafetyLevel::automotive(ASILLevel::QM),
        );

        fusion.add_reading(reading1).unwrap();
        fusion.add_reading(reading2).unwrap();

        let fused = fusion.fused_value().unwrap();
        // Should be closer to 20.0 due to higher weight
        assert!(fused < 25.0);
    }

    #[test]
    fn test_safety_critical_filtering() {
        let mut fusion = SensorFusion::<f32, DefaultConfig>::new(1);

        let reading1 = SensorReading::new(
            20.0,
            1000,
            1,
            ReliabilityLevel::High,
            SafetyLevel::automotive(ASILLevel::AsilD),
        );

        let reading2 = SensorReading::new(
            30.0,
            1001,
            2,
            ReliabilityLevel::Low,
            SafetyLevel::automotive(ASILLevel::QM),
        );

        fusion.add_reading(reading1).unwrap();
        fusion.add_reading(reading2).unwrap();

        let safety_critical = fusion.safety_critical_value().unwrap();
        assert_eq!(safety_critical, 20.0); // Only ASIL-D reading

        let max_safety = fusion.max_safety_level().unwrap();
        assert_eq!(max_safety, SafetyLevel::automotive(ASILLevel::AsilD));
    }

    #[test]
    fn test_outlier_detection() {
        let mut fusion = SensorFusion::<f32, DefaultConfig>::new(1);

        // Normal readings
        fusion
            .add_reading(SensorReading::new(
                20.0,
                1000,
                1,
                ReliabilityLevel::High,
                SafetyLevel::automotive(ASILLevel::AsilC),
            ))
            .unwrap();

        fusion
            .add_reading(SensorReading::new(
                21.0,
                1001,
                2,
                ReliabilityLevel::High,
                SafetyLevel::automotive(ASILLevel::AsilC),
            ))
            .unwrap();

        // Outlier reading
        fusion
            .add_reading(SensorReading::new(
                100.0,
                1002,
                3,
                ReliabilityLevel::Low,
                SafetyLevel::automotive(ASILLevel::QM),
            ))
            .unwrap();

        let outliers = fusion.detect_outliers(2.0);
        assert!(outliers.contains(&Some(3))); // Node 3 should be detected as outlier
    }

    #[test]
    fn test_sensor_fusion_merge() {
        let mut fusion1 = SensorFusion::<f32, DefaultConfig>::new(1);
        let mut fusion2 = SensorFusion::<f32, DefaultConfig>::new(2);

        fusion1
            .add_reading(SensorReading::new(
                20.0,
                1000,
                1,
                ReliabilityLevel::High,
                SafetyLevel::automotive(ASILLevel::AsilC),
            ))
            .unwrap();

        fusion2
            .add_reading(SensorReading::new(
                25.0,
                1001,
                2,
                ReliabilityLevel::Medium,
                SafetyLevel::automotive(ASILLevel::AsilB),
            ))
            .unwrap();

        fusion1.merge(&fusion2).unwrap();
        assert_eq!(fusion1.reading_count(), 2);
    }

    #[test]
    fn test_bounded_crdt_implementation() {
        let mut fusion = SensorFusion::<f32, DefaultConfig>::new(1);

        assert_eq!(fusion.element_count(), 0);
        assert!(fusion.can_add_element());

        fusion
            .add_reading(SensorReading::new(
                20.0,
                1000,
                1,
                ReliabilityLevel::High,
                SafetyLevel::automotive(ASILLevel::AsilC),
            ))
            .unwrap();

        assert_eq!(fusion.element_count(), 1);
        assert!(fusion.memory_usage() > 0);
    }

    #[test]
    fn test_real_time_crdt_implementation() {
        let mut fusion1 = SensorFusion::<f32, DefaultConfig>::new(1);
        let fusion2 = SensorFusion::<f32, DefaultConfig>::new(2);

        assert!(fusion1.merge_bounded(&fusion2).is_ok());
        assert!(fusion1.validate_bounded().is_ok());
    }
}