Skip to main content

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

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