summaryrefslogtreecommitdiff
path: root/learn_9/src/main.rs
blob: a9f6680acc5f5bda272ac30b720e896d970642ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
struct PairStore<T> {
    values: Vec<T>,
}

impl<T> PairStore<T> {
    pub fn new() -> Self {
        Self {
            values: Vec::new(),
        }
    }

    pub fn push(&mut self, value: T) {
        self.values.push(value)
    }

    pub fn get_pair_mut(&mut self, a: usize, b: usize) -> Option<(&mut T, &mut T)> {
        if a == b || a >= self.values.len() || b >= self.values.len() {
            return None
        }
        if a < b {
	    let (left, right) = self.values.split_at_mut(b);
  	    Some((&mut left[a], &mut right[0]))
	} else {
            let (left, right) = self.values.split_at_mut(a);
            Some((&mut right[0], &mut left[b]))
        }
    }
}

fn main() {
    ()
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn test_new() {
        assert_eq!(PairStore::<String>::new().values.len(), 0);
    }

    #[test]
    fn test_push() {
        let mut pairstore = PairStore::<String>::new();
        assert_eq!(pairstore.values.len(), 0);
        pairstore.push("abc".to_string());
        assert_eq!(pairstore.values.len(), 1);
    }

    #[test]
    fn test_get_pair_mut() {
        let mut pairstore = PairStore::<String>::new();
        assert!(pairstore.get_pair_mut(0,0).is_none());
        pairstore.push("a".to_string());
        pairstore.push("b".to_string());
        pairstore.push("c".to_string());
        pairstore.push("d".to_string());
        assert!(pairstore.get_pair_mut(4,0).is_none());
        assert!(pairstore.get_pair_mut(0,4).is_none());

        let Some((a,b)) = pairstore.get_pair_mut(0,1) else {
            panic!("expected Some");
        };
        assert_eq!(*a, "a".to_string());
        assert_eq!(*b, "b".to_string());
        
        *a = "A".to_string();
        *b = "B".to_string();
        let Some((A,B)) = pairstore.get_pair_mut(0,1) else {
            panic!("expected Some");
        };
        assert_eq!(*A, "A".to_string());
        assert_eq!(*B, "B".to_string());

        
        let Some((d,c)) = pairstore.get_pair_mut(3,2) else {
            panic!("expected Some");
        };
        assert_eq!(*c, "c".to_string());
        assert_eq!(*d, "d".to_string());
    }
 }