Skip to main content

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

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