reifydb_value/value/number/safe/convert/
u32.rs1use super::*;
5
6impl_safe_convert_unsigned_demote!(u32 => u8, u16);
7impl_safe_convert_promote!(u32 => u64, u128);
8
9impl_safe_unsigned_convert!(u32 => i8, i16, i32, i64, i128);
10
11impl_safe_convert_unsigned_to_float!(24; u32 => f32);
12impl_safe_convert_unsigned_to_float!(53; u32 => f64);
13
14impl_safe_convert_to_decimal_from_int!(u32);
15
16#[cfg(test)]
17pub mod tests {
18 use super::SafeConvert;
19
20 mod i8 {
21 use super::*;
22
23 #[test]
24 fn test_checked_convert_happy() {
25 let x: u32 = 42;
26 let y: Option<i8> = x.checked_convert();
27 assert_eq!(y, Some(42i8));
28 }
29
30 #[test]
31 fn test_checked_convert_unhappy() {
32 let x: u32 = 500;
33 let y: Option<i8> = x.checked_convert();
34 assert_eq!(y, None);
35 }
36
37 #[test]
38 fn test_saturating_convert() {
39 let x: u32 = 500;
40 let y: i8 = x.saturating_convert();
41 assert_eq!(y, i8::MAX);
42 }
43
44 #[test]
45 fn test_wrapping_convert() {
46 let x: u32 = 500;
47 let y: i8 = x.wrapping_convert();
48 assert_eq!(y, -12i8);
49 }
50 }
51
52 mod i16 {
53 use super::*;
54
55 #[test]
56 fn test_checked_convert_happy() {
57 let x: u32 = 42;
58 let y: Option<i16> = x.checked_convert();
59 assert_eq!(y, Some(42i16));
60 }
61
62 #[test]
63 fn test_checked_convert_unhappy() {
64 let x: u32 = 100000;
65 let y: Option<i16> = x.checked_convert();
66 assert_eq!(y, None);
67 }
68
69 #[test]
70 fn test_saturating_convert() {
71 let x: u32 = 100000;
72 let y: i16 = x.saturating_convert();
73 assert_eq!(y, i16::MAX);
74 }
75
76 #[test]
77 fn test_wrapping_convert() {
78 let x: u32 = 100000;
79 let y: i16 = x.wrapping_convert();
80 assert_eq!(y, -31072i16);
81 }
82 }
83
84 mod i32 {
85 use super::*;
86
87 #[test]
88 fn test_checked_convert_happy() {
89 let x: u32 = 42;
90 let y: Option<i32> = x.checked_convert();
91 assert_eq!(y, Some(42i32));
92 }
93
94 #[test]
95 fn test_checked_convert_unhappy() {
96 let x: u32 = 3000000000u32;
97 let y: Option<i32> = x.checked_convert();
98 assert_eq!(y, None);
99 }
100
101 #[test]
102 fn test_saturating_convert() {
103 let x: u32 = 3000000000u32;
104 let y: i32 = x.saturating_convert();
105 assert_eq!(y, i32::MAX);
106 }
107
108 #[test]
109 fn test_wrapping_convert() {
110 let x: u32 = 3000000000u32;
111 let y: i32 = x.wrapping_convert();
112 assert_eq!(y, -1294967296i32);
113 }
114 }
115
116 mod i64 {
117 use super::*;
118
119 #[test]
120 fn test_checked_convert_happy() {
121 let x: u32 = 42;
122 let y: Option<i64> = x.checked_convert();
123 assert_eq!(y, Some(42i64));
124 }
125
126 #[test]
127 fn test_checked_convert_max() {
128 let x: u32 = u32::MAX;
129 let y: Option<i64> = x.checked_convert();
130 assert_eq!(y, Some(4294967295i64));
131 }
132
133 #[test]
134 fn test_saturating_convert() {
135 let x: u32 = u32::MAX;
136 let y: i64 = x.saturating_convert();
137 assert_eq!(y, 4294967295i64);
138 }
139
140 #[test]
141 fn test_wrapping_convert() {
142 let x: u32 = u32::MAX;
143 let y: i64 = x.wrapping_convert();
144 assert_eq!(y, 4294967295i64);
145 }
146 }
147
148 mod i128 {
149 use super::*;
150
151 #[test]
152 fn test_checked_convert_happy() {
153 let x: u32 = 42;
154 let y: Option<i128> = x.checked_convert();
155 assert_eq!(y, Some(42i128));
156 }
157
158 #[test]
159 fn test_checked_convert_max() {
160 let x: u32 = u32::MAX;
161 let y: Option<i128> = x.checked_convert();
162 assert_eq!(y, Some(4294967295i128));
163 }
164
165 #[test]
166 fn test_saturating_convert() {
167 let x: u32 = u32::MAX;
168 let y: i128 = x.saturating_convert();
169 assert_eq!(y, 4294967295i128);
170 }
171
172 #[test]
173 fn test_wrapping_convert() {
174 let x: u32 = u32::MAX;
175 let y: i128 = x.wrapping_convert();
176 assert_eq!(y, 4294967295i128);
177 }
178 }
179
180 mod f32 {
181 use super::*;
182
183 #[test]
184 fn test_checked_convert() {
185 let x: u32 = 42;
186 let y: Option<f32> = x.checked_convert();
187 assert_eq!(y, Some(42.0f32));
188 }
189
190 #[test]
191 fn test_saturating_convert() {
192 let x: u32 = 100;
193 let y: f32 = x.saturating_convert();
194 assert_eq!(y, 100.0f32);
195 }
196
197 #[test]
198 fn test_wrapping_convert() {
199 let x: u32 = u32::MAX;
200 let y: f32 = x.wrapping_convert();
201 assert_eq!(y, u32::MAX as f32);
202 }
203
204 #[test]
205 fn test_checked_convert_overflow() {
206 let x: u32 = (1u32 << 24) + 1;
207 let y: Option<f32> = x.checked_convert();
208 assert_eq!(y, None);
209 }
210
211 #[test]
212 fn test_saturating_convert_overflow() {
213 let x: u32 = u32::MAX;
214 let y: f32 = x.saturating_convert();
215 assert_eq!(y, (1u64 << 24) as f32);
216 }
217
218 #[test]
219 fn test_wrapping_convert_overflow() {
220 let x: u32 = u32::MAX;
221 let y: f32 = x.wrapping_convert();
222 assert_eq!(y, u32::MAX as f32);
223 }
224 }
225
226 mod f64 {
227 use super::*;
228
229 #[test]
230 fn test_checked_convert() {
231 let x: u32 = 42;
232 let y: Option<f64> = x.checked_convert();
233 assert_eq!(y, Some(42.0f64));
234 }
235
236 #[test]
237 fn test_saturating_convert() {
238 let x: u32 = 100;
239 let y: f64 = x.saturating_convert();
240 assert_eq!(y, 100.0f64);
241 }
242
243 #[test]
244 fn test_wrapping_convert() {
245 let x: u32 = u32::MAX;
246 let y: f64 = x.wrapping_convert();
247 assert_eq!(y, u32::MAX as f64);
248 }
249 }
250
251 mod u8 {
252 use super::*;
253
254 #[test]
255 fn test_checked_convert_happy() {
256 let x: u32 = 255;
257 let y: Option<u8> = x.checked_convert();
258 assert_eq!(y, Some(255u8));
259 }
260
261 #[test]
262 fn test_checked_convert_unhappy() {
263 let x: u32 = 256;
264 let y: Option<u8> = x.checked_convert();
265 assert_eq!(y, None);
266 }
267
268 #[test]
269 fn test_saturating_convert() {
270 let x: u32 = 1000;
271 let y: u8 = x.saturating_convert();
272 assert_eq!(y, u8::MAX);
273 }
274
275 #[test]
276 fn test_wrapping_convert() {
277 let x: u32 = 256;
278 let y: u8 = x.wrapping_convert();
279 assert_eq!(y, 0u8);
280 }
281 }
282
283 mod u16 {
284 use super::*;
285
286 #[test]
287 fn test_checked_convert_happy() {
288 let x: u32 = 65535;
289 let y: Option<u16> = x.checked_convert();
290 assert_eq!(y, Some(65535u16));
291 }
292
293 #[test]
294 fn test_checked_convert_unhappy() {
295 let x: u32 = 65536;
296 let y: Option<u16> = x.checked_convert();
297 assert_eq!(y, None);
298 }
299
300 #[test]
301 fn test_saturating_convert() {
302 let x: u32 = 100000;
303 let y: u16 = x.saturating_convert();
304 assert_eq!(y, u16::MAX);
305 }
306
307 #[test]
308 fn test_wrapping_convert() {
309 let x: u32 = 65536;
310 let y: u16 = x.wrapping_convert();
311 assert_eq!(y, 0u16);
312 }
313 }
314
315 mod u64 {
316 use super::*;
317
318 #[test]
319 fn test_checked_convert() {
320 let x: u32 = u32::MAX;
321 let y: Option<u64> = x.checked_convert();
322 assert_eq!(y, Some(4294967295u64));
323 }
324
325 #[test]
326 fn test_saturating_convert() {
327 let x: u32 = u32::MAX;
328 let y: u64 = x.saturating_convert();
329 assert_eq!(y, 4294967295u64);
330 }
331
332 #[test]
333 fn test_wrapping_convert() {
334 let x: u32 = 42;
335 let y: u64 = x.wrapping_convert();
336 assert_eq!(y, 42u64);
337 }
338 }
339
340 mod u128 {
341 use super::*;
342
343 #[test]
344 fn test_checked_convert() {
345 let x: u32 = u32::MAX;
346 let y: Option<u128> = x.checked_convert();
347 assert_eq!(y, Some(4294967295u128));
348 }
349
350 #[test]
351 fn test_saturating_convert() {
352 let x: u32 = u32::MAX;
353 let y: u128 = x.saturating_convert();
354 assert_eq!(y, 4294967295u128);
355 }
356
357 #[test]
358 fn test_wrapping_convert() {
359 let x: u32 = 42;
360 let y: u128 = x.wrapping_convert();
361 assert_eq!(y, 42u128);
362 }
363 }
364
365 mod decimal {
366 use super::*;
367 use crate::value::decimal::Decimal;
368
369 #[test]
370 fn test_checked_convert() {
371 let x: u32 = 42;
372 let y: Option<Decimal> = x.checked_convert();
373 assert!(y.is_some());
374 let decimal = y.unwrap();
375 assert_eq!(decimal.to_string(), "42");
376 }
377
378 #[test]
379 fn test_saturating_convert() {
380 let x: u32 = u32::MAX;
381 let y: Decimal = x.saturating_convert();
382 assert_eq!(y.to_string(), "4294967295");
383 }
384
385 #[test]
386 fn test_wrapping_convert() {
387 let x: u32 = 1000000;
388 let y: Decimal = x.wrapping_convert();
389 assert_eq!(y.to_string(), "1000000");
390 }
391 }
392}