Skip to main content

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

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use super::*;
5
6impl_safe_convert_unsigned_demote!(u64 => u8, u16, u32);
7impl_safe_convert_promote!(u64 => u128);
8
9impl_safe_unsigned_convert!(u64 => i8, i16, i32, i64, i128);
10
11impl_safe_convert_unsigned_to_float!(24; u64 => f32);
12impl_safe_convert_unsigned_to_float!(53; u64 => f64);
13
14impl_safe_convert_to_decimal_from_uint!(u64);
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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 5000000000u64;
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: u64 = 5000000000u64;
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: u64 = 5000000000u64;
111			let y: i32 = x.wrapping_convert();
112			assert_eq!(y, 705032704i32);
113		}
114	}
115
116	mod i64 {
117		use super::*;
118
119		#[test]
120		fn test_checked_convert_happy() {
121			let x: u64 = 42;
122			let y: Option<i64> = x.checked_convert();
123			assert_eq!(y, Some(42i64));
124		}
125
126		#[test]
127		fn test_checked_convert_unhappy() {
128			let x: u64 = 10000000000000000000u64;
129			let y: Option<i64> = x.checked_convert();
130			assert_eq!(y, None);
131		}
132
133		#[test]
134		fn test_saturating_convert() {
135			let x: u64 = 10000000000000000000u64;
136			let y: i64 = x.saturating_convert();
137			assert_eq!(y, i64::MAX);
138		}
139
140		#[test]
141		fn test_wrapping_convert() {
142			let x: u64 = 10000000000000000000u64;
143			let y: i64 = x.wrapping_convert();
144			assert_eq!(y, -8446744073709551616i64);
145		}
146	}
147
148	mod i128 {
149		use super::*;
150
151		#[test]
152		fn test_checked_convert_happy() {
153			let x: u64 = 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: u64 = u64::MAX;
161			let y: Option<i128> = x.checked_convert();
162			assert_eq!(y, Some(18446744073709551615i128));
163		}
164
165		#[test]
166		fn test_saturating_convert() {
167			let x: u64 = u64::MAX;
168			let y: i128 = x.saturating_convert();
169			assert_eq!(y, 18446744073709551615i128);
170		}
171
172		#[test]
173		fn test_wrapping_convert() {
174			let x: u64 = u64::MAX;
175			let y: i128 = x.wrapping_convert();
176			assert_eq!(y, 18446744073709551615i128);
177		}
178	}
179
180	mod f32 {
181		use super::*;
182
183		#[test]
184		fn test_checked_convert() {
185			let x: u64 = 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: u64 = 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: u64 = 1000;
200			let y: f32 = x.wrapping_convert();
201			assert_eq!(y, 1000.0f32);
202		}
203
204		#[test]
205		fn test_checked_convert_overflow() {
206			let x: u64 = u64::MAX;
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: u64 = u64::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: u64 = u64::MAX;
221			let y: f32 = x.wrapping_convert();
222			assert_eq!(y, u64::MAX as f32);
223		}
224	}
225
226	mod f64 {
227		use super::*;
228
229		#[test]
230		fn test_checked_convert() {
231			let x: u64 = 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: u64 = 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: u64 = 1000;
246			let y: f64 = x.wrapping_convert();
247			assert_eq!(y, 1000.0f64);
248		}
249
250		#[test]
251		fn test_checked_convert_overflow() {
252			let x: u64 = u64::MAX;
253			let y: Option<f64> = x.checked_convert();
254			assert_eq!(y, None);
255		}
256
257		#[test]
258		fn test_saturating_convert_overflow() {
259			let x: u64 = u64::MAX;
260			let y: f64 = x.saturating_convert();
261			assert_eq!(y, (1u64 << 53) as f64);
262		}
263
264		#[test]
265		fn test_wrapping_convert_overflow() {
266			let x: u64 = u64::MAX;
267			let y: f64 = x.wrapping_convert();
268			assert_eq!(y, u64::MAX as f64);
269		}
270	}
271
272	mod u8 {
273		use super::*;
274
275		#[test]
276		fn test_checked_convert_happy() {
277			let x: u64 = 255;
278			let y: Option<u8> = x.checked_convert();
279			assert_eq!(y, Some(255u8));
280		}
281
282		#[test]
283		fn test_checked_convert_unhappy() {
284			let x: u64 = 256;
285			let y: Option<u8> = x.checked_convert();
286			assert_eq!(y, None);
287		}
288
289		#[test]
290		fn test_saturating_convert() {
291			let x: u64 = 1000;
292			let y: u8 = x.saturating_convert();
293			assert_eq!(y, u8::MAX);
294		}
295
296		#[test]
297		fn test_wrapping_convert() {
298			let x: u64 = 256;
299			let y: u8 = x.wrapping_convert();
300			assert_eq!(y, 0u8);
301		}
302	}
303
304	mod u16 {
305		use super::*;
306
307		#[test]
308		fn test_checked_convert_happy() {
309			let x: u64 = 65535;
310			let y: Option<u16> = x.checked_convert();
311			assert_eq!(y, Some(65535u16));
312		}
313
314		#[test]
315		fn test_checked_convert_unhappy() {
316			let x: u64 = 65536;
317			let y: Option<u16> = x.checked_convert();
318			assert_eq!(y, None);
319		}
320
321		#[test]
322		fn test_saturating_convert() {
323			let x: u64 = 100000;
324			let y: u16 = x.saturating_convert();
325			assert_eq!(y, u16::MAX);
326		}
327
328		#[test]
329		fn test_wrapping_convert() {
330			let x: u64 = 65536;
331			let y: u16 = x.wrapping_convert();
332			assert_eq!(y, 0u16);
333		}
334	}
335
336	mod u32 {
337		use super::*;
338
339		#[test]
340		fn test_checked_convert_happy() {
341			let x: u64 = 4294967295;
342			let y: Option<u32> = x.checked_convert();
343			assert_eq!(y, Some(4294967295u32));
344		}
345
346		#[test]
347		fn test_checked_convert_unhappy() {
348			let x: u64 = 4294967296;
349			let y: Option<u32> = x.checked_convert();
350			assert_eq!(y, None);
351		}
352
353		#[test]
354		fn test_saturating_convert() {
355			let x: u64 = u64::MAX;
356			let y: u32 = x.saturating_convert();
357			assert_eq!(y, u32::MAX);
358		}
359
360		#[test]
361		fn test_wrapping_convert() {
362			let x: u64 = 4294967296;
363			let y: u32 = x.wrapping_convert();
364			assert_eq!(y, 0u32);
365		}
366	}
367
368	mod u128 {
369		use super::*;
370
371		#[test]
372		fn test_checked_convert() {
373			let x: u64 = u64::MAX;
374			let y: Option<u128> = x.checked_convert();
375			assert_eq!(y, Some(18446744073709551615u128));
376		}
377
378		#[test]
379		fn test_saturating_convert() {
380			let x: u64 = u64::MAX;
381			let y: u128 = x.saturating_convert();
382			assert_eq!(y, 18446744073709551615u128);
383		}
384
385		#[test]
386		fn test_wrapping_convert() {
387			let x: u64 = 42;
388			let y: u128 = x.wrapping_convert();
389			assert_eq!(y, 42u128);
390		}
391	}
392
393	mod decimal {
394		use super::*;
395		use crate::value::decimal::Decimal;
396
397		#[test]
398		fn test_checked_convert() {
399			let x: u64 = 42;
400			let y: Option<Decimal> = x.checked_convert();
401			assert!(y.is_some());
402			let decimal = y.unwrap();
403			assert_eq!(decimal.to_string(), "42");
404		}
405
406		#[test]
407		fn test_saturating_convert() {
408			let x: u64 = u64::MAX;
409			let y: Decimal = x.saturating_convert();
410			assert_eq!(y.to_string(), "18446744073709551615");
411		}
412
413		#[test]
414		fn test_wrapping_convert() {
415			let x: u64 = 1000000000;
416			let y: Decimal = x.wrapping_convert();
417			assert_eq!(y.to_string(), "1000000000");
418		}
419	}
420}