Skip to main content

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

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