Skip to main content

reifydb_value/value/number/safe/convert/
u32.rs

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