Skip to main content

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

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 ReifyDB
3
4use super::*;
5
6impl_safe_convert_demote!(i128 => i8, i16, i32, i64);
7
8impl_safe_convert!(i128 => u8, u16, u32, u64, u128);
9
10impl_safe_convert_signed_to_float!(24; i128 => f32);
11impl_safe_convert_signed_to_float!(53; i128 => f64);
12
13impl_safe_convert_to_int!(i128);
14impl SafeConvert<Uint> for i128 {
15	fn checked_convert(self) -> Option<Uint> {
16		if self >= 0 {
17			Some(Uint(BigInt::from(self)))
18		} else {
19			None
20		}
21	}
22
23	fn saturating_convert(self) -> Uint {
24		if self >= 0 {
25			Uint(BigInt::from(self))
26		} else {
27			Uint::zero()
28		}
29	}
30
31	fn wrapping_convert(self) -> Uint {
32		Uint(BigInt::from(self as u128))
33	}
34}
35
36impl_safe_convert_to_decimal_from_large_int!(i128);
37
38#[cfg(test)]
39pub mod tests {
40	use super::SafeConvert;
41
42	mod u8 {
43		use crate::value::number::safe::convert::SafeConvert;
44
45		#[test]
46		fn test_checked_convert_happy() {
47			let x: i128 = 42;
48			let y: Option<u8> = SafeConvert::checked_convert(x);
49			assert_eq!(y, Some(42u8));
50		}
51
52		#[test]
53		fn test_checked_convert_unhappy() {
54			let x: i128 = -1;
55			let y: Option<u8> = SafeConvert::checked_convert(x);
56			assert_eq!(y, None);
57		}
58
59		#[test]
60		fn test_saturating_convert() {
61			let x: i128 = -1;
62			let y: u8 = SafeConvert::saturating_convert(x);
63			assert_eq!(y, 0u8);
64		}
65
66		#[test]
67		fn test_wrapping_convert() {
68			let x: i128 = -1;
69			let y: u8 = x.wrapping_convert();
70			assert_eq!(y, 255u8);
71		}
72	}
73	mod u16 {
74		use crate::value::number::safe::convert::SafeConvert;
75
76		#[test]
77		fn test_checked_convert_happy() {
78			let x: i128 = 42;
79			let y: Option<u16> = x.checked_convert();
80			assert_eq!(y, Some(42u16));
81		}
82
83		#[test]
84		fn test_checked_convert_unhappy() {
85			let x: i128 = -1;
86			let y: Option<u16> = x.checked_convert();
87			assert_eq!(y, None);
88		}
89
90		#[test]
91		fn test_saturating_convert() {
92			let x: i128 = -1;
93			let y: u16 = x.saturating_convert();
94			assert_eq!(y, 0u16);
95		}
96
97		#[test]
98		fn test_wrapping_convert() {
99			let x: i128 = -1;
100			let y: u16 = x.wrapping_convert();
101			assert_eq!(y, 65535u16);
102		}
103	}
104	mod u32 {
105		use crate::value::number::safe::convert::SafeConvert;
106
107		#[test]
108		fn test_checked_convert_happy() {
109			let x: i128 = 42;
110			let y: Option<u32> = x.checked_convert();
111			assert_eq!(y, Some(42u32));
112		}
113
114		#[test]
115		fn test_checked_convert_unhappy() {
116			let x: i128 = -1;
117			let y: Option<u32> = x.checked_convert();
118			assert_eq!(y, None);
119		}
120
121		#[test]
122		fn test_saturating_convert() {
123			let x: i128 = -1;
124			let y: u32 = x.saturating_convert();
125			assert_eq!(y, 0u32);
126		}
127
128		#[test]
129		fn test_wrapping_convert() {
130			let x: i128 = -1;
131			let y: u32 = x.wrapping_convert();
132			assert_eq!(y, 4294967295u32);
133		}
134	}
135	mod u64 {
136		use crate::value::number::safe::convert::SafeConvert;
137
138		#[test]
139		fn test_checked_convert_happy() {
140			let x: i128 = 42;
141			let y: Option<u64> = x.checked_convert();
142			assert_eq!(y, Some(42u64));
143		}
144
145		#[test]
146		fn test_checked_convert_unhappy() {
147			let x: i128 = -1;
148			let y: Option<u64> = x.checked_convert();
149			assert_eq!(y, None);
150		}
151
152		#[test]
153		fn test_saturating_convert() {
154			let x: i128 = -1;
155			let y: u64 = x.saturating_convert();
156			assert_eq!(y, 0u64);
157		}
158
159		#[test]
160		fn test_wrapping_convert() {
161			let x: i128 = -1;
162			let y: u64 = x.wrapping_convert();
163			assert_eq!(y, 18446744073709551615u64);
164		}
165	}
166	mod u128 {
167		use crate::value::number::safe::convert::SafeConvert;
168
169		#[test]
170		fn test_checked_convert_happy() {
171			let x: i128 = 42;
172			let y: Option<u128> = x.checked_convert();
173			assert_eq!(y, Some(42u128));
174		}
175
176		#[test]
177		fn test_checked_convert_unhappy() {
178			let x: i128 = -1;
179			let y: Option<u128> = x.checked_convert();
180			assert_eq!(y, None);
181		}
182
183		#[test]
184		fn test_saturating_convert() {
185			let x: i128 = -1;
186			let y: u128 = x.saturating_convert();
187			assert_eq!(y, 0u128);
188		}
189
190		#[test]
191		fn test_wrapping_convert() {
192			let x: i128 = -1;
193			let y: u128 = x.wrapping_convert();
194			assert_eq!(y, 340282366920938463463374607431768211455u128);
195		}
196	}
197	mod f32 {
198		use crate::value::number::safe::convert::SafeConvert;
199
200		#[test]
201		fn test_checked_convert() {
202			let x: i128 = 42;
203			let y: Option<f32> = x.checked_convert();
204			assert_eq!(y, Some(42.0f32));
205		}
206
207		#[test]
208		fn test_saturating_convert() {
209			let x: i128 = 100;
210			let y: f32 = x.saturating_convert();
211			assert_eq!(y, 100.0f32);
212		}
213
214		#[test]
215		fn test_wrapping_convert() {
216			let x: i128 = -1;
217			let y: f32 = x.wrapping_convert();
218			assert_eq!(y, -1.0f32);
219		}
220
221		#[test]
222		fn test_checked_convert_overflow() {
223			let x: i128 = i128::MAX;
224			let y: Option<f32> = x.checked_convert();
225			assert_eq!(y, None); // too large for f32's exact range
226		}
227
228		#[test]
229		fn test_saturating_convert_overflow() {
230			let x: i128 = i128::MAX;
231			let y: f32 = x.saturating_convert();
232			assert_eq!(y, (1i64 << 24) as f32);
233		}
234
235		#[test]
236		fn test_saturating_convert_underflow() {
237			let x: i128 = i128::MIN;
238			let y: f32 = x.saturating_convert();
239			assert_eq!(y, -(1i64 << 24) as f32);
240		}
241
242		#[test]
243		fn test_wrapping_convert_overflow() {
244			let x: i128 = i128::MAX;
245			let y: f32 = x.wrapping_convert();
246			assert!(y.is_finite());
247		}
248
249		#[test]
250		fn test_wrapping_convert_underflow() {
251			let x: i128 = i128::MIN;
252			let y: f32 = x.wrapping_convert();
253			assert!(y.is_finite());
254		}
255	}
256	mod i8 {
257		use super::*;
258
259		#[test]
260		fn test_checked_convert_happy() {
261			let x: i128 = 127;
262			let y: Option<i8> = x.checked_convert();
263			assert_eq!(y, Some(127i8));
264		}
265
266		#[test]
267		fn test_checked_convert_unhappy() {
268			let x: i128 = 128;
269			let y: Option<i8> = x.checked_convert();
270			assert_eq!(y, None);
271		}
272
273		#[test]
274		fn test_saturating_convert_min() {
275			let x: i128 = -129;
276			let y: i8 = x.saturating_convert();
277			assert_eq!(y, i8::MIN);
278		}
279
280		#[test]
281		fn test_saturating_convert_max() {
282			let x: i128 = 128;
283			let y: i8 = x.saturating_convert();
284			assert_eq!(y, i8::MAX);
285		}
286
287		#[test]
288		fn test_wrapping_convert() {
289			let x: i128 = 128;
290			let y: i8 = x.wrapping_convert();
291			assert_eq!(y, -128);
292		}
293	}
294
295	mod i16 {
296		use super::*;
297
298		#[test]
299		fn test_checked_convert_happy() {
300			let x: i128 = 32767;
301			let y: Option<i16> = x.checked_convert();
302			assert_eq!(y, Some(32767i16));
303		}
304
305		#[test]
306		fn test_checked_convert_unhappy() {
307			let x: i128 = 32768;
308			let y: Option<i16> = x.checked_convert();
309			assert_eq!(y, None);
310		}
311
312		#[test]
313		fn test_saturating_convert_min() {
314			let x: i128 = -32769;
315			let y: i16 = x.saturating_convert();
316			assert_eq!(y, i16::MIN);
317		}
318
319		#[test]
320		fn test_saturating_convert_max() {
321			let x: i128 = 32768;
322			let y: i16 = x.saturating_convert();
323			assert_eq!(y, i16::MAX);
324		}
325
326		#[test]
327		fn test_wrapping_convert() {
328			let x: i128 = 32768;
329			let y: i16 = x.wrapping_convert();
330			assert_eq!(y, -32768);
331		}
332	}
333
334	mod i32 {
335		use super::*;
336
337		#[test]
338		fn test_checked_convert_happy() {
339			let x: i128 = 2147483647;
340			let y: Option<i32> = x.checked_convert();
341			assert_eq!(y, Some(2147483647i32));
342		}
343
344		#[test]
345		fn test_checked_convert_unhappy() {
346			let x: i128 = 2147483648;
347			let y: Option<i32> = x.checked_convert();
348			assert_eq!(y, None);
349		}
350
351		#[test]
352		fn test_saturating_convert_min() {
353			let x: i128 = -2147483649;
354			let y: i32 = x.saturating_convert();
355			assert_eq!(y, i32::MIN);
356		}
357
358		#[test]
359		fn test_saturating_convert_max() {
360			let x: i128 = 2147483648;
361			let y: i32 = x.saturating_convert();
362			assert_eq!(y, i32::MAX);
363		}
364
365		#[test]
366		fn test_wrapping_convert() {
367			let x: i128 = 2147483648;
368			let y: i32 = x.wrapping_convert();
369			assert_eq!(y, -2147483648);
370		}
371	}
372
373	mod i64 {
374		use super::*;
375
376		#[test]
377		fn test_checked_convert_happy() {
378			let x: i128 = i64::MAX as i128;
379			let y: Option<i64> = x.checked_convert();
380			assert_eq!(y, Some(i64::MAX));
381		}
382
383		#[test]
384		fn test_checked_convert_unhappy() {
385			let x: i128 = (i64::MAX as i128) + 1;
386			let y: Option<i64> = x.checked_convert();
387			assert_eq!(y, None);
388		}
389
390		#[test]
391		fn test_saturating_convert_min() {
392			let x: i128 = (i64::MIN as i128) - 1;
393			let y: i64 = x.saturating_convert();
394			assert_eq!(y, i64::MIN);
395		}
396
397		#[test]
398		fn test_saturating_convert_max() {
399			let x: i128 = (i64::MAX as i128) + 1;
400			let y: i64 = x.saturating_convert();
401			assert_eq!(y, i64::MAX);
402		}
403
404		#[test]
405		fn test_wrapping_convert() {
406			let x: i128 = (i64::MAX as i128) + 1;
407			let y: i64 = x.wrapping_convert();
408			assert_eq!(y, i64::MIN);
409		}
410	}
411
412	mod f64 {
413		use crate::value::number::safe::convert::SafeConvert;
414
415		#[test]
416		fn test_checked_convert() {
417			let x: i128 = 42;
418			let y: Option<f64> = x.checked_convert();
419			assert_eq!(y, Some(42.0f64));
420		}
421
422		#[test]
423		fn test_saturating_convert() {
424			let x: i128 = 100;
425			let y: f64 = x.saturating_convert();
426			assert_eq!(y, 100.0f64);
427		}
428
429		#[test]
430		fn test_wrapping_convert() {
431			let x: i128 = -1;
432			let y: f64 = x.wrapping_convert();
433			assert_eq!(y, -1.0f64);
434		}
435
436		#[test]
437		fn test_checked_convert_overflow() {
438			let x: i128 = i128::MAX;
439			let y: Option<f64> = x.checked_convert();
440			assert_eq!(y, None);
441		}
442
443		#[test]
444		fn test_saturating_convert_overflow() {
445			let x: i128 = i128::MAX;
446			let y: f64 = x.saturating_convert();
447			assert_eq!(y, (1i128 << 53) as f64);
448		}
449
450		#[test]
451		fn test_saturating_convert_underflow() {
452			let x: i128 = i128::MIN;
453			let y: f64 = x.saturating_convert();
454			assert_eq!(y, -(1i128 << 53) as f64);
455		}
456
457		#[test]
458		fn test_wrapping_convert_overflow() {
459			let x: i128 = i128::MAX;
460			let y: f64 = x.wrapping_convert();
461			assert!(y.is_finite());
462		}
463
464		#[test]
465		fn test_wrapping_convert_underflow() {
466			let x: i128 = i128::MIN;
467			let y: f64 = x.wrapping_convert();
468			assert!(y.is_finite());
469		}
470	}
471
472	mod decimal {
473		use super::*;
474		use crate::value::decimal::Decimal;
475
476		#[test]
477		fn test_checked_convert() {
478			let x: i128 = 42;
479			let y: Option<Decimal> = x.checked_convert();
480			assert!(y.is_some());
481			let decimal = y.unwrap();
482			assert_eq!(decimal.to_string(), "42");
483		}
484
485		#[test]
486		fn test_saturating_convert() {
487			let x: i128 = i128::MIN;
488			let y: Decimal = x.saturating_convert();
489			assert_eq!(y.to_string(), "-170141183460469231731687303715884105728");
490		}
491
492		#[test]
493		fn test_wrapping_convert() {
494			let x: i128 = i128::MAX;
495			let y: Decimal = x.wrapping_convert();
496			assert_eq!(y.to_string(), "170141183460469231731687303715884105727");
497		}
498	}
499
500	mod int {
501		use super::*;
502		use crate::value::int::Int;
503
504		#[test]
505		fn test_checked_convert() {
506			let x: i128 = i128::MIN;
507			let y: Option<Int> = x.checked_convert();
508			assert!(y.is_some());
509			assert_eq!(y.unwrap().to_string(), "-170141183460469231731687303715884105728");
510		}
511
512		#[test]
513		fn test_saturating_convert() {
514			let x: i128 = i128::MAX;
515			let y: Int = x.saturating_convert();
516			assert_eq!(y.to_string(), "170141183460469231731687303715884105727");
517		}
518
519		#[test]
520		fn test_wrapping_convert() {
521			let x: i128 = -1;
522			let y: Int = x.wrapping_convert();
523			assert_eq!(y.to_string(), "-1");
524		}
525	}
526
527	mod uint {
528		use super::*;
529		use crate::value::uint::Uint;
530
531		#[test]
532		fn test_checked_convert_positive() {
533			let x: i128 = 42;
534			let y: Option<Uint> = x.checked_convert();
535			assert!(y.is_some());
536			assert_eq!(y.unwrap().to_string(), "42");
537		}
538
539		#[test]
540		fn test_checked_convert_negative() {
541			let x: i128 = -1;
542			let y: Option<Uint> = x.checked_convert();
543			assert!(y.is_none());
544		}
545
546		#[test]
547		fn test_saturating_convert() {
548			let x: i128 = -1;
549			let y: Uint = x.saturating_convert();
550			assert_eq!(y.to_string(), "0");
551		}
552
553		#[test]
554		fn test_wrapping_convert() {
555			let x: i128 = -1;
556			let y: Uint = x.wrapping_convert();
557			assert_eq!(y.to_string(), "340282366920938463463374607431768211455");
558		}
559	}
560}