Skip to main content

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

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