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
use crate::CreatedDelta;
use crate::{number_diff_impl_option_wrapped, number_patch_impl_option_wrapped};

number_diff_impl_option_wrapped!(f32, f32);
number_patch_impl_option_wrapped!(f32, Option<f32>);

number_diff_impl_option_wrapped!(f64, f64);
number_patch_impl_option_wrapped!(f64, Option<f64>);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dipa_impl_tester::DipaImplTester;
    use crate::{Diffable, Patchable};

    /// We wrap f32 so that we can impl Eq and PartialEq
    #[derive(Debug, Deserialize, Serialize, Copy, Clone)]
    struct F32TestWrapper(f32);
    /// We wrap f64 so that we can impl Eq and PartialEq
    #[derive(Debug, Deserialize, Serialize, Copy, Clone)]
    struct F64TestWrapper(f64);

    impl<'s, 'e> Diffable<'s, 'e, F32TestWrapper> for F32TestWrapper {
        type Delta = Option<f32>;
        type DeltaOwned = Option<f32>;

        fn create_delta_towards(&self, end_state: &Self) -> CreatedDelta<Self::Delta> {
            self.0.create_delta_towards(&end_state.0)
        }
    }

    impl Patchable<Option<f32>> for F32TestWrapper {
        fn apply_patch(&mut self, patch: Option<f32>) {
            self.0.apply_patch(patch)
        }
    }

    impl<'s, 'e> Diffable<'s, 'e, F64TestWrapper> for F64TestWrapper {
        type Delta = Option<f64>;
        type DeltaOwned = Option<f64>;

        fn create_delta_towards(&self, end_state: &Self) -> CreatedDelta<Self::Delta> {
            self.0.create_delta_towards(&end_state.0)
        }
    }

    impl Patchable<Option<f64>> for F64TestWrapper {
        fn apply_patch(&mut self, patch: Option<f64>) {
            self.0.apply_patch(patch)
        }
    }

    impl Eq for F32TestWrapper {}
    impl Eq for F64TestWrapper {}

    impl PartialEq for F32TestWrapper {
        fn eq(&self, other: &Self) -> bool {
            (self.0 - other.0).abs() < f32::EPSILON
        }
    }

    impl PartialEq for F64TestWrapper {
        fn eq(&self, other: &Self) -> bool {
            (self.0 - other.0).abs() < f64::EPSILON
        }
    }

    #[test]
    fn f32_unchanged() {
        DipaImplTester {
            label: Some("Diff patch same f32"),
            start: &mut F32TestWrapper(0.),
            end: &F32TestWrapper(0.),
            expected_delta: None,
            expected_serialized_patch_size: 1,
            expected_did_change: false,
        }
        .test();
    }

    #[test]
    fn f32_changed() {
        DipaImplTester {
            label: Some("Diff patch different f32"),
            start: &mut F32TestWrapper(0.),
            end: &F32TestWrapper(5.),
            expected_delta: Some(5.),
            expected_serialized_patch_size: 5,
            expected_did_change: true,
        }
        .test();
    }

    #[test]
    fn f64_unchanged() {
        DipaImplTester {
            label: Some("Diff patch different f64"),
            start: &mut F64TestWrapper(0.),
            end: &F64TestWrapper(0.),
            expected_delta: None,
            expected_serialized_patch_size: 1,
            expected_did_change: false,
        }
        .test();
    }

    #[test]
    fn f64_changed() {
        DipaImplTester {
            label: Some("Diff patch different f64"),
            start: &mut F64TestWrapper(0.),
            end: &F64TestWrapper(5.),
            expected_delta: Some(5.),
            expected_serialized_patch_size: 9,
            expected_did_change: true,
        }
        .test();
    }
}