1 // SPDX-FileCopyrightText: 2020 Robin Krahl <[email protected]>
2 // SPDX-License-Identifier: Apache-2.0 or MIT
3 
4 #![cfg(feature = "derive")]
5 
6 use merge::Merge;
7 
test<T: std::fmt::Debug + Merge + PartialEq>(expected: T, mut left: T, right: T)8 fn test<T: std::fmt::Debug + Merge + PartialEq>(expected: T, mut left: T, right: T) {
9     left.merge(right);
10     assert_eq!(expected, left);
11 }
12 
13 #[test]
test_bool_overwrite_false()14 fn test_bool_overwrite_false() {
15     #[derive(Debug, Merge, PartialEq)]
16     struct S(#[merge(strategy = merge::bool::overwrite_false)] bool);
17 
18     test(S(false), S(false), S(false));
19     test(S(true), S(false), S(true));
20     test(S(true), S(true), S(false));
21     test(S(true), S(true), S(true));
22 }
23 
24 #[test]
test_bool_overwrite_true()25 fn test_bool_overwrite_true() {
26     #[derive(Debug, Merge, PartialEq)]
27     struct S(#[merge(strategy = merge::bool::overwrite_true)] bool);
28 
29     test(S(false), S(false), S(false));
30     test(S(false), S(false), S(true));
31     test(S(false), S(true), S(false));
32     test(S(true), S(true), S(true));
33 }
34 
35 #[cfg(feature = "num")]
36 #[test]
test_num_saturating_add()37 fn test_num_saturating_add() {
38     #[derive(Debug, Merge, PartialEq)]
39     struct S(#[merge(strategy = merge::num::saturating_add)] u8);
40 
41     test(S(0), S(0), S(0));
42     test(S(1), S(0), S(1));
43     test(S(255), S(255), S(10));
44     test(S(40), S(30), S(10));
45 }
46 
47 #[cfg(feature = "num")]
48 #[test]
test_num_overwrite_zero()49 fn test_num_overwrite_zero() {
50     #[derive(Debug, Merge, PartialEq)]
51     struct S(#[merge(strategy = merge::num::overwrite_zero)] u8);
52 
53     test(S(0), S(0), S(0));
54     test(S(1), S(0), S(1));
55     test(S(255), S(255), S(10));
56 }
57 
58 #[test]
test_ord_max()59 fn test_ord_max() {
60     #[derive(Debug, Merge, PartialEq)]
61     struct S(#[merge(strategy = merge::ord::max)] u8);
62 
63     test(S(2), S(1), S(2));
64     test(S(2), S(2), S(1));
65     test(S(2), S(2), S(2));
66     test(S(2), S(2), S(0));
67     test(S(2), S(0), S(2));
68     test(S(33), S(33), S(11));
69 }
70 
71 #[test]
test_ord_min()72 fn test_ord_min() {
73     #[derive(Debug, Merge, PartialEq)]
74     struct S(#[merge(strategy = merge::ord::min)] u8);
75 
76     test(S(1), S(1), S(2));
77     test(S(1), S(2), S(1));
78     test(S(2), S(2), S(2));
79     test(S(0), S(2), S(0));
80     test(S(0), S(0), S(2));
81     test(S(11), S(33), S(11));
82 }
83 
84 #[cfg(feature = "std")]
85 #[test]
test_vec_overwrite_empty()86 fn test_vec_overwrite_empty() {
87     #[derive(Debug, Merge, PartialEq)]
88     struct S(#[merge(strategy = merge::vec::overwrite_empty)] Vec<u8>);
89 
90     test(S(vec![]), S(vec![]), S(vec![]));
91     test(S(vec![1]), S(vec![]), S(vec![1]));
92     test(S(vec![0]), S(vec![0]), S(vec![1]));
93     test(S(vec![255]), S(vec![255]), S(vec![10]));
94 }
95 
96 #[cfg(feature = "std")]
97 #[test]
test_vec_append()98 fn test_vec_append() {
99     #[derive(Debug, Merge, PartialEq)]
100     struct S(#[merge(strategy = merge::vec::append)] Vec<u8>);
101 
102     test(S(vec![]), S(vec![]), S(vec![]));
103     test(S(vec![1]), S(vec![]), S(vec![1]));
104     test(S(vec![0, 1]), S(vec![0]), S(vec![1]));
105     test(S(vec![255, 10]), S(vec![255]), S(vec![10]));
106     test(S(vec![0, 1, 2, 3, 4]), S(vec![0, 1, 2]), S(vec![3, 4]));
107     test(S(vec![3, 4, 0, 1, 2]), S(vec![3, 4]), S(vec![0, 1, 2]));
108 }
109 
110 #[cfg(feature = "std")]
111 #[test]
test_vec_prepend()112 fn test_vec_prepend() {
113     #[derive(Debug, Merge, PartialEq)]
114     struct S(#[merge(strategy = merge::vec::prepend)] Vec<u8>);
115 
116     test(S(vec![]), S(vec![]), S(vec![]));
117     test(S(vec![1]), S(vec![]), S(vec![1]));
118     test(S(vec![1, 0]), S(vec![0]), S(vec![1]));
119     test(S(vec![10, 255]), S(vec![255]), S(vec![10]));
120     test(S(vec![3, 4, 0, 1, 2]), S(vec![0, 1, 2]), S(vec![3, 4]));
121     test(S(vec![0, 1, 2, 3, 4]), S(vec![3, 4]), S(vec![0, 1, 2]));
122 }
123