Skip to main content

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

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 ReifyDB
3
4use super::*;
5
6impl_safe_convert_unsigned_demote!(u16 => u8);
7impl_safe_convert_promote!(u16 => u32, u64, u128);
8
9impl_safe_unsigned_convert!(u16 => i8, i16, i32, i64, i128);
10
11impl_safe_convert_unsigned_to_float!(24; u16 => f32);
12impl_safe_convert_unsigned_to_float!(53; u16 => f64);
13
14impl_safe_convert_to_int!(u16);
15impl_safe_convert_unsigned_to_uint!(u16);
16
17impl_safe_convert_to_decimal_from_int!(u16);
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: u16 = 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: u16 = 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: u16 = 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: u16 = 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: u16 = 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: u16 = 40000;
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: u16 = 40000;
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: u16 = 40000;
82			let y: i16 = x.wrapping_convert();
83			assert_eq!(y, -25536i16);
84		}
85	}
86
87	mod i32 {
88		use super::*;
89
90		#[test]
91		fn test_checked_convert_happy() {
92			let x: u16 = 42;
93			let y: Option<i32> = x.checked_convert();
94			assert_eq!(y, Some(42i32));
95		}
96
97		#[test]
98		fn test_checked_convert_max() {
99			let x: u16 = u16::MAX;
100			let y: Option<i32> = x.checked_convert();
101			assert_eq!(y, Some(65535i32));
102		}
103
104		#[test]
105		fn test_saturating_convert() {
106			let x: u16 = u16::MAX;
107			let y: i32 = x.saturating_convert();
108			assert_eq!(y, 65535i32);
109		}
110
111		#[test]
112		fn test_wrapping_convert() {
113			let x: u16 = u16::MAX;
114			let y: i32 = x.wrapping_convert();
115			assert_eq!(y, 65535i32);
116		}
117	}
118
119	mod i64 {
120		use super::*;
121
122		#[test]
123		fn test_checked_convert_happy() {
124			let x: u16 = 42;
125			let y: Option<i64> = x.checked_convert();
126			assert_eq!(y, Some(42i64));
127		}
128
129		#[test]
130		fn test_checked_convert_max() {
131			let x: u16 = u16::MAX;
132			let y: Option<i64> = x.checked_convert();
133			assert_eq!(y, Some(65535i64));
134		}
135
136		#[test]
137		fn test_saturating_convert() {
138			let x: u16 = u16::MAX;
139			let y: i64 = x.saturating_convert();
140			assert_eq!(y, 65535i64);
141		}
142
143		#[test]
144		fn test_wrapping_convert() {
145			let x: u16 = u16::MAX;
146			let y: i64 = x.wrapping_convert();
147			assert_eq!(y, 65535i64);
148		}
149	}
150
151	mod i128 {
152		use super::*;
153
154		#[test]
155		fn test_checked_convert_happy() {
156			let x: u16 = 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: u16 = u16::MAX;
164			let y: Option<i128> = x.checked_convert();
165			assert_eq!(y, Some(65535i128));
166		}
167
168		#[test]
169		fn test_saturating_convert() {
170			let x: u16 = u16::MAX;
171			let y: i128 = x.saturating_convert();
172			assert_eq!(y, 65535i128);
173		}
174
175		#[test]
176		fn test_wrapping_convert() {
177			let x: u16 = u16::MAX;
178			let y: i128 = x.wrapping_convert();
179			assert_eq!(y, 65535i128);
180		}
181	}
182
183	mod f32 {
184		use super::*;
185
186		#[test]
187		fn test_checked_convert() {
188			let x: u16 = 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: u16 = 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: u16 = u16::MAX;
203			let y: f32 = x.wrapping_convert();
204			assert_eq!(y, 65535.0f32);
205		}
206	}
207
208	mod f64 {
209		use super::*;
210
211		#[test]
212		fn test_checked_convert() {
213			let x: u16 = 42;
214			let y: Option<f64> = x.checked_convert();
215			assert_eq!(y, Some(42.0f64));
216		}
217
218		#[test]
219		fn test_saturating_convert() {
220			let x: u16 = 100;
221			let y: f64 = x.saturating_convert();
222			assert_eq!(y, 100.0f64);
223		}
224
225		#[test]
226		fn test_wrapping_convert() {
227			let x: u16 = u16::MAX;
228			let y: f64 = x.wrapping_convert();
229			assert_eq!(y, 65535.0f64);
230		}
231	}
232
233	mod u8 {
234		use super::*;
235
236		#[test]
237		fn test_checked_convert_happy() {
238			let x: u16 = 255;
239			let y: Option<u8> = x.checked_convert();
240			assert_eq!(y, Some(255u8));
241		}
242
243		#[test]
244		fn test_checked_convert_unhappy() {
245			let x: u16 = 256;
246			let y: Option<u8> = x.checked_convert();
247			assert_eq!(y, None);
248		}
249
250		#[test]
251		fn test_saturating_convert() {
252			let x: u16 = 1000;
253			let y: u8 = x.saturating_convert();
254			assert_eq!(y, u8::MAX);
255		}
256
257		#[test]
258		fn test_wrapping_convert() {
259			let x: u16 = 256;
260			let y: u8 = x.wrapping_convert();
261			assert_eq!(y, 0u8);
262		}
263	}
264
265	mod u32 {
266		use super::*;
267
268		#[test]
269		fn test_checked_convert() {
270			let x: u16 = u16::MAX;
271			let y: Option<u32> = x.checked_convert();
272			assert_eq!(y, Some(65535u32));
273		}
274
275		#[test]
276		fn test_saturating_convert() {
277			let x: u16 = u16::MAX;
278			let y: u32 = x.saturating_convert();
279			assert_eq!(y, 65535u32);
280		}
281
282		#[test]
283		fn test_wrapping_convert() {
284			let x: u16 = 42;
285			let y: u32 = x.wrapping_convert();
286			assert_eq!(y, 42u32);
287		}
288	}
289
290	mod u64 {
291		use super::*;
292
293		#[test]
294		fn test_checked_convert() {
295			let x: u16 = u16::MAX;
296			let y: Option<u64> = x.checked_convert();
297			assert_eq!(y, Some(65535u64));
298		}
299
300		#[test]
301		fn test_saturating_convert() {
302			let x: u16 = u16::MAX;
303			let y: u64 = x.saturating_convert();
304			assert_eq!(y, 65535u64);
305		}
306
307		#[test]
308		fn test_wrapping_convert() {
309			let x: u16 = 42;
310			let y: u64 = x.wrapping_convert();
311			assert_eq!(y, 42u64);
312		}
313	}
314
315	mod u128 {
316		use super::*;
317
318		#[test]
319		fn test_checked_convert() {
320			let x: u16 = u16::MAX;
321			let y: Option<u128> = x.checked_convert();
322			assert_eq!(y, Some(65535u128));
323		}
324
325		#[test]
326		fn test_saturating_convert() {
327			let x: u16 = u16::MAX;
328			let y: u128 = x.saturating_convert();
329			assert_eq!(y, 65535u128);
330		}
331
332		#[test]
333		fn test_wrapping_convert() {
334			let x: u16 = 42;
335			let y: u128 = x.wrapping_convert();
336			assert_eq!(y, 42u128);
337		}
338	}
339
340	mod decimal {
341		use super::*;
342		use crate::value::decimal::Decimal;
343
344		#[test]
345		fn test_checked_convert() {
346			let x: u16 = 42;
347			let y: Option<Decimal> = x.checked_convert();
348			assert!(y.is_some());
349			let decimal = y.unwrap();
350			assert_eq!(decimal.to_string(), "42");
351		}
352
353		#[test]
354		fn test_saturating_convert() {
355			let x: u16 = u16::MAX;
356			let y: Decimal = x.saturating_convert();
357			assert_eq!(y.to_string(), "65535");
358		}
359
360		#[test]
361		fn test_wrapping_convert() {
362			let x: u16 = 1000;
363			let y: Decimal = x.wrapping_convert();
364			assert_eq!(y.to_string(), "1000");
365		}
366	}
367
368	mod int {
369		use super::*;
370		use crate::value::int::Int;
371
372		#[test]
373		fn test_checked_convert() {
374			let x: u16 = u16::MAX;
375			let y: Option<Int> = x.checked_convert();
376			assert!(y.is_some());
377			assert_eq!(y.unwrap().to_string(), "65535");
378		}
379
380		#[test]
381		fn test_saturating_convert() {
382			let x: u16 = 32767;
383			let y: Int = x.saturating_convert();
384			assert_eq!(y.to_string(), "32767");
385		}
386
387		#[test]
388		fn test_wrapping_convert() {
389			let x: u16 = 0;
390			let y: Int = x.wrapping_convert();
391			assert_eq!(y.to_string(), "0");
392		}
393	}
394
395	mod uint {
396		use super::*;
397		use crate::value::uint::Uint;
398
399		#[test]
400		fn test_checked_convert() {
401			let x: u16 = 42;
402			let y: Option<Uint> = x.checked_convert();
403			assert!(y.is_some());
404			assert_eq!(y.unwrap().to_string(), "42");
405		}
406
407		#[test]
408		fn test_saturating_convert() {
409			let x: u16 = u16::MAX;
410			let y: Uint = x.saturating_convert();
411			assert_eq!(y.to_string(), "65535");
412		}
413
414		#[test]
415		fn test_wrapping_convert() {
416			let x: u16 = 1234;
417			let y: Uint = x.wrapping_convert();
418			assert_eq!(y.to_string(), "1234");
419		}
420	}
421}