Skip to main content

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

1// SPDX-License-Identifier: MIT
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_int!(u64);
15impl_safe_convert_unsigned_to_uint!(u64);
16
17impl_safe_convert_to_decimal_from_uint!(u64);
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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 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: u64 = 5000000000u64;
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: u64 = 5000000000u64;
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: u64 = 5000000000u64;
114			let y: i32 = x.wrapping_convert();
115			assert_eq!(y, 705032704i32);
116		}
117	}
118
119	mod i64 {
120		use super::*;
121
122		#[test]
123		fn test_checked_convert_happy() {
124			let x: u64 = 42;
125			let y: Option<i64> = x.checked_convert();
126			assert_eq!(y, Some(42i64));
127		}
128
129		#[test]
130		fn test_checked_convert_unhappy() {
131			let x: u64 = 10000000000000000000u64;
132			let y: Option<i64> = x.checked_convert();
133			assert_eq!(y, None);
134		}
135
136		#[test]
137		fn test_saturating_convert() {
138			let x: u64 = 10000000000000000000u64;
139			let y: i64 = x.saturating_convert();
140			assert_eq!(y, i64::MAX);
141		}
142
143		#[test]
144		fn test_wrapping_convert() {
145			let x: u64 = 10000000000000000000u64;
146			let y: i64 = x.wrapping_convert();
147			assert_eq!(y, -8446744073709551616i64);
148		}
149	}
150
151	mod i128 {
152		use super::*;
153
154		#[test]
155		fn test_checked_convert_happy() {
156			let x: u64 = 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: u64 = u64::MAX;
164			let y: Option<i128> = x.checked_convert();
165			assert_eq!(y, Some(18446744073709551615i128));
166		}
167
168		#[test]
169		fn test_saturating_convert() {
170			let x: u64 = u64::MAX;
171			let y: i128 = x.saturating_convert();
172			assert_eq!(y, 18446744073709551615i128);
173		}
174
175		#[test]
176		fn test_wrapping_convert() {
177			let x: u64 = u64::MAX;
178			let y: i128 = x.wrapping_convert();
179			assert_eq!(y, 18446744073709551615i128);
180		}
181	}
182
183	mod f32 {
184		use super::*;
185
186		#[test]
187		fn test_checked_convert() {
188			let x: u64 = 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: u64 = 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: u64 = 1000;
203			let y: f32 = x.wrapping_convert();
204			assert_eq!(y, 1000.0f32);
205		}
206
207		#[test]
208		fn test_checked_convert_overflow() {
209			let x: u64 = u64::MAX;
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: u64 = u64::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: u64 = u64::MAX;
224			let y: f32 = x.wrapping_convert();
225			assert_eq!(y, u64::MAX as f32);
226		}
227	}
228
229	mod f64 {
230		use super::*;
231
232		#[test]
233		fn test_checked_convert() {
234			let x: u64 = 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: u64 = 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: u64 = 1000;
249			let y: f64 = x.wrapping_convert();
250			assert_eq!(y, 1000.0f64);
251		}
252
253		#[test]
254		fn test_checked_convert_overflow() {
255			let x: u64 = u64::MAX;
256			let y: Option<f64> = x.checked_convert();
257			assert_eq!(y, None);
258		}
259
260		#[test]
261		fn test_saturating_convert_overflow() {
262			let x: u64 = u64::MAX;
263			let y: f64 = x.saturating_convert();
264			assert_eq!(y, (1u64 << 53) as f64);
265		}
266
267		#[test]
268		fn test_wrapping_convert_overflow() {
269			let x: u64 = u64::MAX;
270			let y: f64 = x.wrapping_convert();
271			assert_eq!(y, u64::MAX as f64);
272		}
273	}
274
275	mod u8 {
276		use super::*;
277
278		#[test]
279		fn test_checked_convert_happy() {
280			let x: u64 = 255;
281			let y: Option<u8> = x.checked_convert();
282			assert_eq!(y, Some(255u8));
283		}
284
285		#[test]
286		fn test_checked_convert_unhappy() {
287			let x: u64 = 256;
288			let y: Option<u8> = x.checked_convert();
289			assert_eq!(y, None);
290		}
291
292		#[test]
293		fn test_saturating_convert() {
294			let x: u64 = 1000;
295			let y: u8 = x.saturating_convert();
296			assert_eq!(y, u8::MAX);
297		}
298
299		#[test]
300		fn test_wrapping_convert() {
301			let x: u64 = 256;
302			let y: u8 = x.wrapping_convert();
303			assert_eq!(y, 0u8);
304		}
305	}
306
307	mod u16 {
308		use super::*;
309
310		#[test]
311		fn test_checked_convert_happy() {
312			let x: u64 = 65535;
313			let y: Option<u16> = x.checked_convert();
314			assert_eq!(y, Some(65535u16));
315		}
316
317		#[test]
318		fn test_checked_convert_unhappy() {
319			let x: u64 = 65536;
320			let y: Option<u16> = x.checked_convert();
321			assert_eq!(y, None);
322		}
323
324		#[test]
325		fn test_saturating_convert() {
326			let x: u64 = 100000;
327			let y: u16 = x.saturating_convert();
328			assert_eq!(y, u16::MAX);
329		}
330
331		#[test]
332		fn test_wrapping_convert() {
333			let x: u64 = 65536;
334			let y: u16 = x.wrapping_convert();
335			assert_eq!(y, 0u16);
336		}
337	}
338
339	mod u32 {
340		use super::*;
341
342		#[test]
343		fn test_checked_convert_happy() {
344			let x: u64 = 4294967295;
345			let y: Option<u32> = x.checked_convert();
346			assert_eq!(y, Some(4294967295u32));
347		}
348
349		#[test]
350		fn test_checked_convert_unhappy() {
351			let x: u64 = 4294967296;
352			let y: Option<u32> = x.checked_convert();
353			assert_eq!(y, None);
354		}
355
356		#[test]
357		fn test_saturating_convert() {
358			let x: u64 = u64::MAX;
359			let y: u32 = x.saturating_convert();
360			assert_eq!(y, u32::MAX);
361		}
362
363		#[test]
364		fn test_wrapping_convert() {
365			let x: u64 = 4294967296;
366			let y: u32 = x.wrapping_convert();
367			assert_eq!(y, 0u32);
368		}
369	}
370
371	mod u128 {
372		use super::*;
373
374		#[test]
375		fn test_checked_convert() {
376			let x: u64 = u64::MAX;
377			let y: Option<u128> = x.checked_convert();
378			assert_eq!(y, Some(18446744073709551615u128));
379		}
380
381		#[test]
382		fn test_saturating_convert() {
383			let x: u64 = u64::MAX;
384			let y: u128 = x.saturating_convert();
385			assert_eq!(y, 18446744073709551615u128);
386		}
387
388		#[test]
389		fn test_wrapping_convert() {
390			let x: u64 = 42;
391			let y: u128 = x.wrapping_convert();
392			assert_eq!(y, 42u128);
393		}
394	}
395
396	mod decimal {
397		use super::*;
398		use crate::value::decimal::Decimal;
399
400		#[test]
401		fn test_checked_convert() {
402			let x: u64 = 42;
403			let y: Option<Decimal> = x.checked_convert();
404			assert!(y.is_some());
405			let decimal = y.unwrap();
406			assert_eq!(decimal.to_string(), "42");
407		}
408
409		#[test]
410		fn test_saturating_convert() {
411			let x: u64 = u64::MAX;
412			let y: Decimal = x.saturating_convert();
413			assert_eq!(y.to_string(), "18446744073709551615");
414		}
415
416		#[test]
417		fn test_wrapping_convert() {
418			let x: u64 = 1000000000;
419			let y: Decimal = x.wrapping_convert();
420			assert_eq!(y.to_string(), "1000000000");
421		}
422	}
423
424	mod int {
425		use super::*;
426		use crate::value::int::Int;
427
428		#[test]
429		fn test_checked_convert() {
430			let x: u64 = u64::MAX;
431			let y: Option<Int> = x.checked_convert();
432			assert!(y.is_some());
433			assert_eq!(y.unwrap().to_string(), "18446744073709551615");
434		}
435
436		#[test]
437		fn test_saturating_convert() {
438			let x: u64 = i64::MAX as u64;
439			let y: Int = x.saturating_convert();
440			assert_eq!(y.to_string(), "9223372036854775807");
441		}
442
443		#[test]
444		fn test_wrapping_convert() {
445			let x: u64 = 0;
446			let y: Int = x.wrapping_convert();
447			assert_eq!(y.to_string(), "0");
448		}
449	}
450
451	mod uint {
452		use super::*;
453		use crate::value::uint::Uint;
454
455		#[test]
456		fn test_checked_convert() {
457			let x: u64 = 42;
458			let y: Option<Uint> = x.checked_convert();
459			assert!(y.is_some());
460			assert_eq!(y.unwrap().to_string(), "42");
461		}
462
463		#[test]
464		fn test_saturating_convert() {
465			let x: u64 = u64::MAX;
466			let y: Uint = x.saturating_convert();
467			assert_eq!(y.to_string(), "18446744073709551615");
468		}
469
470		#[test]
471		fn test_wrapping_convert() {
472			let x: u64 = 9876543210;
473			let y: Uint = x.wrapping_convert();
474			assert_eq!(y.to_string(), "9876543210");
475		}
476	}
477}