Skip to main content

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

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