mod base;
mod fromstr;
mod integer;
mod num;
mod signed;
use std::cmp::Ordering;
use crate::public::traits::{CheckedCalc, CheckedType};
#[derive(Copy, Clone, Debug)]
pub struct Cell<U> {
error_tag: bool,
data: U,
}
impl<U> Cell<U> {
#[cfg(test)]
pub fn new(data: U) -> Self {
Cell {
error_tag: false,
data,
}
}
pub fn get_tag(&self) -> bool {
self.error_tag
}
pub fn get_data(&self) -> &U {
&self.data
}
}
impl<U: CheckedType + CheckedCalc> CheckedType for Cell<U> where
std::num::ParseIntError: std::convert::From<<U as ::num::Num>::FromStrRadixErr>
+ std::convert::From<<U as std::str::FromStr>::Err>
{
}
impl<U: PartialEq> PartialEq for Cell<U> {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}
impl<U: PartialEq> Eq for Cell<U> {}
impl<U: PartialOrd + Ord> Ord for Cell<U> {
fn cmp(&self, other: &Self) -> Ordering {
self.data.cmp(&other.data)
}
}
impl<U: PartialEq + Ord> PartialOrd for Cell<U> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(&other))
}
}
#[cfg(test)]
mod tests {
use super::Cell;
#[test]
fn overflow_test_1() {
let a = Cell::<i32>::new(std::i32::MAX);
let b = Cell::<i32>::new(1);
let c = a + b;
assert_eq!(c.error_tag, true);
assert_eq!(c, Cell::<i32>::new(1));
}
#[test]
fn overflow_test_2() {
let a = Cell::<i32>::new(std::i32::MIN);
let b = Cell::<i32>::new(1);
let c = a - b;
assert_eq!(c.error_tag, true);
assert_eq!(c, Cell::<i32>::new(1));
}
#[test]
fn overflow_test_3() {
let a = Cell::<i32>::new(std::i32::MAX);
let b = Cell::<i32>::new(2);
let c = a * b;
assert_eq!(c.error_tag, true);
assert_eq!(c, Cell::<i32>::new(1));
}
#[test]
fn overflow_test_4() {
let a = Cell::<i32>::new(std::i32::MIN);
let b = Cell::<i32>::new(-1);
let c = a / b;
assert_eq!(c.error_tag, true);
assert_eq!(c, Cell::<i32>::new(1));
}
}