reifydb_core/encoded/
i16.rs1use std::ptr;
5
6use reifydb_type::value::r#type::Type;
7
8use crate::encoded::{encoded::EncodedValues, schema::Schema};
9
10impl Schema {
11 pub fn set_i16(&self, row: &mut EncodedValues, index: usize, value: impl Into<i16>) {
12 let field = &self.fields()[index];
13 debug_assert!(row.len() >= self.total_static_size());
14 debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int2);
15 row.set_valid(index, true);
16 unsafe {
17 ptr::write_unaligned(
18 row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut i16,
19 value.into(),
20 )
21 }
22 }
23
24 pub fn get_i16(&self, row: &EncodedValues, index: usize) -> i16 {
25 let field = &self.fields()[index];
26 debug_assert!(row.len() >= self.total_static_size());
27 debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int2);
28 unsafe { (row.as_ptr().add(field.offset as usize) as *const i16).read_unaligned() }
29 }
30
31 pub fn try_get_i16(&self, row: &EncodedValues, index: usize) -> Option<i16> {
32 if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Int2 {
33 Some(self.get_i16(row, index))
34 } else {
35 None
36 }
37 }
38}
39
40#[cfg(test)]
41pub mod tests {
42 use reifydb_type::value::r#type::Type;
43
44 use crate::encoded::schema::Schema;
45
46 #[test]
47 fn test_set_get_i16() {
48 let schema = Schema::testing(&[Type::Int2]);
49 let mut row = schema.allocate();
50 schema.set_i16(&mut row, 0, -1234i16);
51 assert_eq!(schema.get_i16(&row, 0), -1234i16);
52 }
53
54 #[test]
55 fn test_try_get_i16() {
56 let schema = Schema::testing(&[Type::Int2]);
57 let mut row = schema.allocate();
58
59 assert_eq!(schema.try_get_i16(&row, 0), None);
60
61 schema.set_i16(&mut row, 0, -1234i16);
62 assert_eq!(schema.try_get_i16(&row, 0), Some(-1234i16));
63 }
64
65 #[test]
66 fn test_extremes() {
67 let schema = Schema::testing(&[Type::Int2]);
68 let mut row = schema.allocate();
69
70 schema.set_i16(&mut row, 0, i16::MAX);
71 assert_eq!(schema.get_i16(&row, 0), i16::MAX);
72
73 let mut row2 = schema.allocate();
74 schema.set_i16(&mut row2, 0, i16::MIN);
75 assert_eq!(schema.get_i16(&row2, 0), i16::MIN);
76
77 let mut row3 = schema.allocate();
78 schema.set_i16(&mut row3, 0, 0i16);
79 assert_eq!(schema.get_i16(&row3, 0), 0i16);
80 }
81
82 #[test]
83 fn test_various_values() {
84 let schema = Schema::testing(&[Type::Int2]);
85
86 let test_values = [-32768i16, -10000i16, -1i16, 0i16, 1i16, 10000i16, 32767i16];
87
88 for value in test_values {
89 let mut row = schema.allocate();
90 schema.set_i16(&mut row, 0, value);
91 assert_eq!(schema.get_i16(&row, 0), value);
92 }
93 }
94
95 #[test]
96 fn test_mixed_with_other_types() {
97 let schema = Schema::testing(&[Type::Int2, Type::Int1, Type::Int2]);
98 let mut row = schema.allocate();
99
100 schema.set_i16(&mut row, 0, -30000i16);
101 schema.set_i8(&mut row, 1, 100i8);
102 schema.set_i16(&mut row, 2, 25000i16);
103
104 assert_eq!(schema.get_i16(&row, 0), -30000i16);
105 assert_eq!(schema.get_i8(&row, 1), 100i8);
106 assert_eq!(schema.get_i16(&row, 2), 25000i16);
107 }
108
109 #[test]
110 fn test_undefined_handling() {
111 let schema = Schema::testing(&[Type::Int2, Type::Int2]);
112 let mut row = schema.allocate();
113
114 schema.set_i16(&mut row, 0, 1234i16);
115
116 assert_eq!(schema.try_get_i16(&row, 0), Some(1234));
117 assert_eq!(schema.try_get_i16(&row, 1), None);
118
119 schema.set_undefined(&mut row, 0);
120 assert_eq!(schema.try_get_i16(&row, 0), None);
121 }
122
123 #[test]
124 fn test_try_get_i16_wrong_type() {
125 let schema = Schema::testing(&[Type::Boolean]);
126 let mut row = schema.allocate();
127
128 schema.set_bool(&mut row, 0, true);
129
130 assert_eq!(schema.try_get_i16(&row, 0), None);
131 }
132}