reifydb_value/value/number/safe/convert/
u8.rs1use super::*;
5
6impl_safe_convert_promote!(u8 => u16, u32, u64, u128);
7
8impl_safe_unsigned_convert!(u8 => i8, i16, i32, i64, i128);
9
10impl_safe_convert_unsigned_to_float!(24; u8 => f32);
11impl_safe_convert_unsigned_to_float!(53; u8 => f64);
12
13impl_safe_convert_to_decimal_from_int!(u8);
14
15#[cfg(test)]
16pub mod tests {
17 use super::SafeConvert;
18
19 mod i8 {
20 use super::*;
21
22 #[test]
23 fn test_checked_convert_happy() {
24 let x: u8 = 42;
25 let y: Option<i8> = x.checked_convert();
26 assert_eq!(y, Some(42i8));
27 }
28
29 #[test]
30 fn test_checked_convert_unhappy() {
31 let x: u8 = 200;
32 let y: Option<i8> = x.checked_convert();
33 assert_eq!(y, None);
34 }
35
36 #[test]
37 fn test_saturating_convert() {
38 let x: u8 = 200;
39 let y: i8 = x.saturating_convert();
40 assert_eq!(y, i8::MAX);
41 }
42
43 #[test]
44 fn test_wrapping_convert() {
45 let x: u8 = 200;
46 let y: i8 = x.wrapping_convert();
47 assert_eq!(y, -56i8);
48 }
49 }
50
51 mod i16 {
52 use super::*;
53
54 #[test]
55 fn test_checked_convert_happy() {
56 let x: u8 = 42;
57 let y: Option<i16> = x.checked_convert();
58 assert_eq!(y, Some(42i16));
59 }
60
61 #[test]
62 fn test_checked_convert_max() {
63 let x: u8 = u8::MAX;
64 let y: Option<i16> = x.checked_convert();
65 assert_eq!(y, Some(255i16));
66 }
67
68 #[test]
69 fn test_saturating_convert() {
70 let x: u8 = u8::MAX;
71 let y: i16 = x.saturating_convert();
72 assert_eq!(y, 255i16);
73 }
74
75 #[test]
76 fn test_wrapping_convert() {
77 let x: u8 = u8::MAX;
78 let y: i16 = x.wrapping_convert();
79 assert_eq!(y, 255i16);
80 }
81 }
82
83 mod i32 {
84 use super::*;
85
86 #[test]
87 fn test_checked_convert_happy() {
88 let x: u8 = 42;
89 let y: Option<i32> = x.checked_convert();
90 assert_eq!(y, Some(42i32));
91 }
92
93 #[test]
94 fn test_checked_convert_max() {
95 let x: u8 = u8::MAX;
96 let y: Option<i32> = x.checked_convert();
97 assert_eq!(y, Some(255i32));
98 }
99
100 #[test]
101 fn test_saturating_convert() {
102 let x: u8 = u8::MAX;
103 let y: i32 = x.saturating_convert();
104 assert_eq!(y, 255i32);
105 }
106
107 #[test]
108 fn test_wrapping_convert() {
109 let x: u8 = u8::MAX;
110 let y: i32 = x.wrapping_convert();
111 assert_eq!(y, 255i32);
112 }
113 }
114
115 mod i64 {
116 use super::*;
117
118 #[test]
119 fn test_checked_convert_happy() {
120 let x: u8 = 42;
121 let y: Option<i64> = x.checked_convert();
122 assert_eq!(y, Some(42i64));
123 }
124
125 #[test]
126 fn test_checked_convert_max() {
127 let x: u8 = u8::MAX;
128 let y: Option<i64> = x.checked_convert();
129 assert_eq!(y, Some(255i64));
130 }
131
132 #[test]
133 fn test_saturating_convert() {
134 let x: u8 = u8::MAX;
135 let y: i64 = x.saturating_convert();
136 assert_eq!(y, 255i64);
137 }
138
139 #[test]
140 fn test_wrapping_convert() {
141 let x: u8 = u8::MAX;
142 let y: i64 = x.wrapping_convert();
143 assert_eq!(y, 255i64);
144 }
145 }
146
147 mod i128 {
148 use super::*;
149
150 #[test]
151 fn test_checked_convert_happy() {
152 let x: u8 = 42;
153 let y: Option<i128> = x.checked_convert();
154 assert_eq!(y, Some(42i128));
155 }
156
157 #[test]
158 fn test_checked_convert_max() {
159 let x: u8 = u8::MAX;
160 let y: Option<i128> = x.checked_convert();
161 assert_eq!(y, Some(255i128));
162 }
163
164 #[test]
165 fn test_saturating_convert() {
166 let x: u8 = u8::MAX;
167 let y: i128 = x.saturating_convert();
168 assert_eq!(y, 255i128);
169 }
170
171 #[test]
172 fn test_wrapping_convert() {
173 let x: u8 = u8::MAX;
174 let y: i128 = x.wrapping_convert();
175 assert_eq!(y, 255i128);
176 }
177 }
178
179 mod f32 {
180 use super::*;
181
182 #[test]
183 fn test_checked_convert() {
184 let x: u8 = 42;
185 let y: Option<f32> = x.checked_convert();
186 assert_eq!(y, Some(42.0f32));
187 }
188
189 #[test]
190 fn test_saturating_convert() {
191 let x: u8 = 100;
192 let y: f32 = x.saturating_convert();
193 assert_eq!(y, 100.0f32);
194 }
195
196 #[test]
197 fn test_wrapping_convert() {
198 let x: u8 = u8::MAX;
199 let y: f32 = x.wrapping_convert();
200 assert_eq!(y, 255.0f32);
201 }
202 }
203
204 mod f64 {
205 use super::*;
206
207 #[test]
208 fn test_checked_convert() {
209 let x: u8 = 42;
210 let y: Option<f64> = x.checked_convert();
211 assert_eq!(y, Some(42.0f64));
212 }
213
214 #[test]
215 fn test_saturating_convert() {
216 let x: u8 = 100;
217 let y: f64 = x.saturating_convert();
218 assert_eq!(y, 100.0f64);
219 }
220
221 #[test]
222 fn test_wrapping_convert() {
223 let x: u8 = u8::MAX;
224 let y: f64 = x.wrapping_convert();
225 assert_eq!(y, 255.0f64);
226 }
227 }
228
229 mod decimal {
230 use super::*;
231 use crate::value::decimal::Decimal;
232
233 #[test]
234 fn test_checked_convert() {
235 let x: u8 = 42;
236 let y: Option<Decimal> = x.checked_convert();
237 assert!(y.is_some());
238 let decimal = y.unwrap();
239 assert_eq!(decimal.to_string(), "42");
240 }
241
242 #[test]
243 fn test_checked_convert_zero() {
244 let x: u8 = 0;
245 let y: Option<Decimal> = x.checked_convert();
246 assert!(y.is_some());
247 let decimal = y.unwrap();
248 assert_eq!(decimal.to_string(), "0");
249 }
250
251 #[test]
252 fn test_checked_convert_max() {
253 let x: u8 = u8::MAX;
254 let y: Option<Decimal> = x.checked_convert();
255 assert!(y.is_some());
256 let decimal = y.unwrap();
257 assert_eq!(decimal.to_string(), "255");
258 }
259
260 #[test]
261 fn test_saturating_convert() {
262 let x: u8 = 100;
263 let y: Decimal = x.saturating_convert();
264 assert_eq!(y.to_string(), "100");
265 }
266
267 #[test]
268 fn test_wrapping_convert() {
269 let x: u8 = 99;
270 let y: Decimal = x.wrapping_convert();
271 assert_eq!(y.to_string(), "99");
272 }
273 }
274}