calc_rational

CLI calculator for rational numbers.
git clone https://git.philomathiclife.com/repos/calc_rational
Log | Files | Refs | README

tests.rs (3573B)


      1 use super::Cache;
      2 #[test]
      3 fn len() {
      4     let mut c = Cache::<bool, 1>::new();
      5     assert_eq!(0, c.len());
      6     c.push(false);
      7     assert_eq!(1, c.len());
      8     c.push(false);
      9     assert_eq!(1, c.len());
     10 }
     11 #[test]
     12 fn is_empty() {
     13     let mut c = Cache::<bool, 1>::new();
     14     assert!(c.is_empty());
     15     c.push(false);
     16     assert!(!c.is_empty());
     17 }
     18 #[expect(clippy::cognitive_complexity, reason = "a lot to test")]
     19 #[test]
     20 fn get() {
     21     let mut c = Cache::<bool, 4>::new();
     22     assert_eq!(c.get(0), None);
     23     assert_eq!(c.get(1), None);
     24     assert_eq!(c.get(2), None);
     25     assert_eq!(c.get(3), None);
     26     assert_eq!(c.get(4), None);
     27     assert_eq!(c.get(5), None);
     28     assert_eq!(c.get(usize::MAX), None);
     29     c.push(true);
     30     assert_eq!(c.get(0), Some(&true));
     31     assert_eq!(c.get(1), None);
     32     assert_eq!(c.get(2), None);
     33     assert_eq!(c.get(3), None);
     34     assert_eq!(c.get(4), None);
     35     assert_eq!(c.get(5), None);
     36     assert_eq!(c.get(usize::MAX), None);
     37     c.push(false);
     38     assert_eq!(c.get(0), Some(&false));
     39     assert_eq!(c.get(1), Some(&true));
     40     assert_eq!(c.get(2), None);
     41     assert_eq!(c.get(3), None);
     42     assert_eq!(c.get(4), None);
     43     assert_eq!(c.get(5), None);
     44     assert_eq!(c.get(usize::MAX), None);
     45     c.push(false);
     46     assert_eq!(c.get(0), Some(&false));
     47     assert_eq!(c.get(1), Some(&false));
     48     assert_eq!(c.get(2), Some(&true));
     49     assert_eq!(c.get(3), None);
     50     assert_eq!(c.get(4), None);
     51     assert_eq!(c.get(5), None);
     52     assert_eq!(c.get(usize::MAX), None);
     53     c.push(true);
     54     assert_eq!(c.get(0), Some(&true));
     55     assert_eq!(c.get(1), Some(&false));
     56     assert_eq!(c.get(2), Some(&false));
     57     assert_eq!(c.get(3), Some(&true));
     58     assert_eq!(c.get(4), None);
     59     assert_eq!(c.get(5), None);
     60     assert_eq!(c.get(usize::MAX), None);
     61     c.push(true);
     62     assert_eq!(c.get(0), Some(&true));
     63     assert_eq!(c.get(1), Some(&true));
     64     assert_eq!(c.get(2), Some(&false));
     65     assert_eq!(c.get(3), Some(&false));
     66     assert_eq!(c.get(4), None);
     67     assert_eq!(c.get(5), None);
     68     assert_eq!(c.get(usize::MAX), None);
     69 }
     70 #[test]
     71 fn get_unsafe() {
     72     let mut c = Cache::<bool, 4>::new();
     73     assert!(!c.get_unchecked(0));
     74     assert!(!c.get_unchecked(1));
     75     assert!(!c.get_unchecked(2));
     76     assert!(!c.get_unchecked(3));
     77     assert!(!c.get_unchecked(4));
     78     c.push(true);
     79     assert!(c.get_unchecked(0));
     80     assert!(!c.get_unchecked(1));
     81     assert!(!c.get_unchecked(2));
     82     assert!(!c.get_unchecked(3));
     83     assert!(c.get_unchecked(4));
     84 }
     85 #[expect(clippy::indexing_slicing, reason = "comment justifies correctness")]
     86 #[test]
     87 fn index() {
     88     let mut c = Cache::<bool, 4>::new();
     89     c.push(true);
     90     // `c.len() > 0`.
     91     assert!(c[0]);
     92 }
     93 #[expect(clippy::indexing_slicing, reason = "comment justifies correctness")]
     94 #[test]
     95 #[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
     96 fn index_panic() {
     97     let c = Cache::<bool, 4>::new();
     98     // `c.len() > 0`.
     99     assert!(c[0]);
    100 }
    101 #[expect(clippy::indexing_slicing, reason = "comments justify correctness")]
    102 #[test]
    103 fn push() {
    104     let mut c = Cache::<bool, 4>::new();
    105     c.push(true);
    106     // `c.len() > 0`.
    107     assert!(c[0]);
    108     c.push(true);
    109     // `c.len() > 0`.
    110     assert!(c[0]);
    111     c.push(false);
    112     // `c.len() > 0`.
    113     assert!(!c[0]);
    114     c.push(true);
    115     // `c.len() > 0`.
    116     assert!(c[0]);
    117     c.push(false);
    118     // `c.len() > 0`.
    119     assert!(!c[0]);
    120     c.push(false);
    121     // `c.len() > 0`.
    122     assert!(!c[0]);
    123 }
    124 #[test]
    125 fn new() {
    126     _ = Cache::<bool, 0>::new();
    127     _ = Cache::<bool, 32>::new();
    128     _ = Cache::<bool, 31>::new();
    129 }