pub mod s_d_u8_i32 {
use std::convert::TryInto;
pub fn exceeding_max_i32_threshold(_num: u64) -> bool {
let max: u64 = i32::max_value().try_into().unwrap();
if _num > max {
true
} else {
false
}
}
pub fn count_vec_items_left(_vec: &Vec<u8>) -> u64 {
let items_left: u64 = _vec.len().try_into().unwrap();
items_left
}
pub fn flush_value_to_zero(_value: u64, _position: u64, _size: u64) -> u64 {
let new_value: u64 = _value
- ((_value % (10_u64.pow(_position.try_into().unwrap())))
- (_value % (10_u64.pow((_position - _size).try_into().unwrap()))));
new_value
}
pub fn insert_value_at_position(
_value: u64,
_single_value: u64,
_position: u64,
_size: u64,
) -> u64 {
let mut string_single_value = _single_value.to_string();
while string_single_value.len() < (_size as u64).try_into().unwrap() {
string_single_value = "0".to_owned() + &string_single_value;
}
let new_single_value: u64 = string_single_value.parse::<u64>().unwrap();
let zeroed_value: u64 = flush_value_to_zero(_value, _position, _size);
let new_value: u64 =
zeroed_value + new_single_value * (10_u64.pow((_position - _size).try_into().unwrap()));
new_value
}
pub fn access_value(_value: u64, _position: u64, _size: u64) -> u64 {
let _mode: u64 = ((_value % (10_u64.pow(_position.try_into().unwrap())))
- (_value % (10_u64.pow((_position - _size).try_into().unwrap()))))
/ (10_u64.pow((_position - _size).try_into().unwrap()));
_mode
}
pub fn serialize_u8_to_i32(u8_data: Vec<u8>) -> Vec<i32> {
let mut vec_of_i32s: Vec<i32> = Vec::new();
if exceeding_max_i32_threshold(count_vec_items_left(&u8_data).into()) == false {
let items_left: u64 = count_vec_items_left(&u8_data).try_into().unwrap();
let batches_left: u64 = items_left / 3;
let last_batch_count: u64 = items_left % 3;
if batches_left >= 1 {
for i in 1..=batches_left {
let mut single_value_for_i32_vec: u64 = 1000000000;
let mut n: u64 = 0;
if i == 2 {
n = 3;
} else if i >= 3 {
n = i + (i - 1) + (i - 2);
}
let one: u64 = (*u8_data.get(n as usize).unwrap()).into();
let two: u64 = (*u8_data.get(n as usize + 1).unwrap()).into();
let three: u64 = (*u8_data.get(n as usize + 2).unwrap()).into();
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, one as u64, 9, 3);
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, two as u64, 6, 3);
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, three as u64, 3, 3);
vec_of_i32s.push(single_value_for_i32_vec.try_into().unwrap());
}
}
let mut index: usize = 0;
if batches_left == 0 {
index = 0;
} else if batches_left == 1 {
index = 3;
} else if batches_left >= 2 {
index = (batches_left as usize + 3) + (batches_left as usize - 1) + (batches_left as usize - 2);
}
if last_batch_count == 1 {
let mut single_value_for_i32_vec: u64 = 1000000000;
let one: u64 = (*u8_data.get(index).unwrap()).into();
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, one as u64, 3, 3);
single_value_for_i32_vec = flush_value_to_zero(single_value_for_i32_vec, 10, 1);
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, 0, 10, 1);
vec_of_i32s.push(single_value_for_i32_vec.try_into().unwrap());
}
if last_batch_count == 2 {
let mut single_value_for_i32_vec: u64 = 1000000000;
let one: u64 = (*u8_data.get(index).unwrap()).into();
let two: u64 = (*u8_data.get(index + 1).unwrap()).into();
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, one as u64, 6, 3);
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, two as u64, 3, 3);
single_value_for_i32_vec = flush_value_to_zero(single_value_for_i32_vec, 10, 1);
single_value_for_i32_vec =
insert_value_at_position(single_value_for_i32_vec, 2, 10, 1);
vec_of_i32s.push(single_value_for_i32_vec.try_into().unwrap());
}
}
vec_of_i32s
}
pub fn deserialize_i32_to_u8(_i32_data: Vec<i32>) -> Vec<u8> {
let mut vec_of_u8s: Vec<u8> = Vec::new();
for single_i32_from_vec in _i32_data {
let mode: u64 = access_value(single_i32_from_vec as u64, 10, 1);
if mode == 1 {
vec_of_u8s.push(
access_value(single_i32_from_vec as u64, 9, 3)
.try_into()
.unwrap(),
);
vec_of_u8s.push(
access_value(single_i32_from_vec as u64, 6, 3)
.try_into()
.unwrap(),
);
vec_of_u8s.push(
access_value(single_i32_from_vec as u64, 3, 3)
.try_into()
.unwrap(),
);
}
if mode == 2 {
vec_of_u8s.push(
access_value(single_i32_from_vec as u64, 6, 3)
.try_into()
.unwrap(),
);
vec_of_u8s.push(
access_value(single_i32_from_vec as u64, 3, 3)
.try_into()
.unwrap(),
);
}
if mode == 0 || single_i32_from_vec <= 255 {
vec_of_u8s.push(
access_value(single_i32_from_vec as u64, 3, 3)
.try_into()
.unwrap(),
);
}
}
vec_of_u8s
}
}
#[cfg(test)]
mod tests {
use super::s_d_u8_i32;
#[test]
fn test_flush_3_3_000() {
let _test_single_value_for_i32_vec_000: u64 = 1000000000;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_000, 3, 3);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_3_3_123() {
let _test_single_value_for_i32_vec_123: u64 = 1000000123;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_123, 3, 3);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_3_3_999() {
let _test_single_value_for_i32_vec_999: u64 = 1000000999;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_999, 3, 3);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_6_6_000() {
let _test_single_value_for_i32_vec_000: u64 = 1000000000;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_000, 6, 6);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_6_6_123() {
let _test_single_value_for_i32_vec_123: u64 = 1000123123;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_123, 6, 6);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_6_6_999() {
let _test_single_value_for_i32_vec_999: u64 = 1000999999;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_999, 6, 6);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_9_9_000() {
let _test_single_value_for_i32_vec_000: u64 = 1000000000;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_000, 9, 9);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_6_3_999() {
let _test_single_value_for_i32_vec_999: u64 = 1999999999;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_999, 6, 3);
assert_eq!(v, 1999000999);
}
#[test]
fn test_flush_9_3_999() {
let _test_single_value_for_i32_vec_000: u64 = 1999000000;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_000, 9, 3);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_9_9_123() {
let _test_single_value_for_i32_vec_123: u64 = 1123123123;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_123, 9, 9);
assert_eq!(v, 1000000000);
}
#[test]
fn test_flush_9_9_999() {
let _test_single_value_for_i32_vec_999: u64 = 1999999999;
let v = s_d_u8_i32::flush_value_to_zero(_test_single_value_for_i32_vec_999, 9, 9);
assert_eq!(v, 1000000000);
}
#[test]
fn test_access_3_3_1() {
let _test_single_value_for_i32_vec_000: u64 = 1009010011;
let v = s_d_u8_i32::access_value(_test_single_value_for_i32_vec_000, 10, 1);
assert_eq!(v, 1);
}
#[test]
fn test_access_3_3_123() {
let _test_single_value_for_i32_vec_123: u64 = 1123123123;
let v = s_d_u8_i32::access_value(_test_single_value_for_i32_vec_123, 3, 3);
assert_eq!(v, 123);
}
#[test]
fn test_access_3_3_999() {
let _test_single_value_for_i32_vec_999: u64 = 1999999999;
let v = s_d_u8_i32::access_value(_test_single_value_for_i32_vec_999, 3, 3);
assert_eq!(v, 999);
}
#[test]
fn test_insert_3_3_000() {
let _test_single_value_for_i32_vec_000: u64 = 1000000000;
let _single_val: u64 = 000;
let v = s_d_u8_i32::insert_value_at_position(
_test_single_value_for_i32_vec_000,
_single_val,
3,
3,
);
assert_eq!(v, 1000000000);
}
#[test]
fn test_insert_3_3_123() {
let _test_single_value_for_i32_vec_123: u64 = 1123123000;
let _single_val: u64 = 123;
let v = s_d_u8_i32::insert_value_at_position(
_test_single_value_for_i32_vec_123,
_single_val,
3,
3,
);
assert_eq!(v, 1123123123);
}
#[test]
fn test_insert_3_3_999() {
let _test_single_value_for_i32_vec_999: u64 = 1999999009;
let _single_val: u64 = 999;
let v = s_d_u8_i32::insert_value_at_position(
_test_single_value_for_i32_vec_999,
_single_val,
3,
3,
);
assert_eq!(v, 1999999999);
}
#[test]
fn test_insert_9_9_111() {
let _test_single_value_for_i32_vec_999: u64 = 1999999999;
let _single_val: u64 = 111;
let v = s_d_u8_i32::insert_value_at_position(
_test_single_value_for_i32_vec_999,
_single_val,
9,
3,
);
assert_eq!(v, 1111999999);
}
#[test]
fn test_i32_threshold_over() {
let number: u64 = 2147483648;
let b = s_d_u8_i32::exceeding_max_i32_threshold(number);
assert_eq!(b, true);
}
#[test]
fn test_i32_threshold_under() {
let number: u64 = 2147483647;
let b = s_d_u8_i32::exceeding_max_i32_threshold(number);
assert_eq!(b, false);
}
#[test]
fn test_count_vec_items_left() {
let mut vec = Vec::with_capacity(10);
for i in 0..10 {
vec.push(i);
}
let items_left: u64 = s_d_u8_i32::count_vec_items_left(&vec);
assert_eq!(items_left, 10);
}
#[test]
fn test_serialize_u8_to_i32_one() {
let mut vec: Vec<u8> = Vec::new();
for i in 1..=3 {
vec.push(i);
}
let mut a: Vec<i32> = Vec::new();
a.push(1001002003);
let v: Vec<i32> = s_d_u8_i32::serialize_u8_to_i32(vec);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
assert_eq!(matching, 1);
}
#[test]
fn test_serialize_u8_to_i32_two() {
let mut vec: Vec<u8> = Vec::new();
for i in 1..=6 {
vec.push(i);
}
let mut a: Vec<i32> = Vec::new();
a.push(1001002003);
a.push(1004005006);
let v: Vec<i32> = s_d_u8_i32::serialize_u8_to_i32(vec);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 2);
}
#[test]
fn test_serialize_u8_to_i32_three() {
let mut vec: Vec<u8> = Vec::new();
for i in 99..=105 {
vec.push(i);
}
let mut a: Vec<i32> = Vec::new();
a.push(1099100101);
a.push(1102103104);
a.push(0000000105);
let v: Vec<i32> = s_d_u8_i32::serialize_u8_to_i32(vec);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 3);
}
#[test]
fn test_serialize_u8_to_i32_four() {
let mut vec: Vec<u8> = Vec::new();
for i in 99..=106 {
vec.push(i);
}
let mut a: Vec<i32> = Vec::new();
a.push(1099100101);
a.push(1102103104);
a.push(2000105106);
let v: Vec<i32> = s_d_u8_i32::serialize_u8_to_i32(vec);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 3);
}
#[test]
fn test_serialize_u8_to_i32_five() {
let mut vec: Vec<u8> = Vec::new();
for i in 9..=16 {
vec.push(i);
}
let mut a: Vec<i32> = Vec::new();
a.push(1009010011);
a.push(1012013014);
a.push(2000015016);
let v: Vec<i32> = s_d_u8_i32::serialize_u8_to_i32(vec);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 3);
}
#[test]
fn test_serialize_u8_to_i32_six() {
let mut vec: Vec<u8> = Vec::new();
vec.push(1);
let mut a: Vec<i32> = Vec::new();
a.push(0000000001);
let v: Vec<i32> = s_d_u8_i32::serialize_u8_to_i32(vec);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 1);
}
#[test]
fn test_deserialize_i32_to_u8_one() {
let mut vec: Vec<i32> = Vec::new();
vec.push(1009010011);
vec.push(1012013014);
vec.push(2000015016);
println!("vec: {:?}", vec);
let mut a: Vec<u8> = Vec::new();
a.push(9);
a.push(10);
a.push(11);
a.push(12);
a.push(13);
a.push(14);
a.push(15);
a.push(16);
println!("a: {:?}", a);
let v: Vec<u8> = s_d_u8_i32::deserialize_i32_to_u8( vec);
println!("v: {:?}", v);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 8);
}
#[test]
fn test_deserialize_i32_to_u8_two() {
let mut vec: Vec<i32> = Vec::new();
vec.push(0000000001);
let mut a: Vec<u8> = Vec::new();
a.push(1);
println!("a: {:?}", a);
let v: Vec<u8> = s_d_u8_i32::deserialize_i32_to_u8( vec);
println!("v: {:?}", v);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 1);
}
#[test]
fn test_deserialize_i32_to_u8_three() {
let mut vec: Vec<i32> = Vec::new();
vec.push(1009010011);
vec.push(1012013014);
println!("vec: {:?}", vec);
let mut a: Vec<u8> = Vec::new();
a.push(9);
a.push(10);
a.push(11);
a.push(12);
a.push(13);
a.push(14);
println!("a: {:?}", a);
let v: Vec<u8> = s_d_u8_i32::deserialize_i32_to_u8(vec);
println!("v: {:?}", v);
let matching = a.iter().zip(&v).filter(|&(a, v)| a == v).count();
println!("{:?} vs {:?}", a, v);
assert_eq!(matching, 6);
}
}