aboutsummaryrefslogtreecommitdiff
path: root/primrose/crates/primrose-library/src/treeset.rs
blob: 4e2b213f156a475095f524d3e846e0ba3dbec6c0 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*LIBSPEC-NAME*
rust-btreeset-spec std::collections::BTreeSet
*ENDLIBSPEC-NAME*/

use crate::traits::{Container, Indexable};
pub use std::collections::BTreeSet;

/*IMPL*
Container
*ENDIMPL*/
impl<T: Ord> Container<T> for BTreeSet<T> {
    /*LIBSPEC*
    /*OPNAME*
    len op-len pre-len post-len
    *ENDOPNAME*/
    (define (op-len xs) (cons xs (length xs)))
    (define (pre-len xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-len xs r) (equal? r (op-len xs)))
    *ENDLIBSPEC*/
    fn len(&mut self) -> usize {
        BTreeSet::len(self)
    }

    /*LIBSPEC*
    /*OPNAME*
    contains op-contains pre-contains post-contains
    *ENDOPNAME*/
    (define (op-contains xs x)
      (cond
        [(list? (member x xs)) (cons xs #t)]
        [else (cons xs #f)]))
    (define (pre-contains xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-contains xs x r) (equal? r (op-contains xs x)))
    *ENDLIBSPEC*/
    fn contains(&mut self, x: &T) -> bool {
        BTreeSet::contains(self, x)
    }

    /*LIBSPEC*
    /*OPNAME*
    is-empty op-is-empty pre-is-empty post-is-empty
    *ENDOPNAME*/
    (define (op-is-empty xs) (cons xs (null? xs)))
    (define (pre-is-empty xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-is-empty xs r) (equal? r (op-is-empty xs)))
    *ENDLIBSPEC*/
    fn is_empty(&mut self) -> bool {
        BTreeSet::is_empty(self)
    }

    /*LIBSPEC*
    /*OPNAME*
    clear op-clear pre-clear post-clear
    *ENDOPNAME*/
    (define (op-clear xs) null)
    (define (pre-clear xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-clear xs r) (equal? r (op-clear xs)))
    *ENDLIBSPEC*/
    fn clear(&mut self) {
        BTreeSet::clear(self);
    }

    /*LIBSPEC*
    /*OPNAME*
    insert op-insert pre-insert post-insert
    *ENDOPNAME*/
    (define (op-insert xs x) (remove-duplicates (sort (append xs (list x)) <)))
    (define (pre-insert xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-insert xs x ys) (equal? ys (op-insert xs x)))
    *ENDLIBSPEC*/
    fn insert(&mut self, elt: T) {
        BTreeSet::insert(self, elt);
    }

    /*LIBSPEC*
    /*OPNAME*
    remove op-remove pre-remove post-remove
    *ENDOPNAME*/
    (define (op-remove xs x)
      (cond
        [(list? (member x xs)) (cons (remove x xs) x)]
        [else (cons xs null)]))
    (define (pre-remove xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-remove xs r) (equal? r (op-remove xs)))
    *ENDLIBSPEC*/
    fn remove(&mut self, elt: T) -> Option<T> {
        match BTreeSet::remove(self, &elt) {
            true => Some(elt),
            false => None,
        }
    }
}

/*IMPL*
Indexable
*ENDIMPL*/
impl<T: Ord> Indexable<T> for BTreeSet<T> {
    /*LIBSPEC*
    /*OPNAME*
    first op-first pre-first post-first
    *ENDOPNAME*/
    (define (op-first xs)
      (cond
        [(null? xs) (cons xs null)]
        [else (cons xs (first xs))]))
    (define (pre-first xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-first xs r) (equal? r (op-first xs)))
    *ENDLIBSPEC*/
    fn first(&mut self) -> Option<&T> {
        BTreeSet::first(self)
    }

    /*LIBSPEC*
    /*OPNAME*
    last op-last pre-last post-last
    *ENDOPNAME*/
    (define (op-last xs)
      (cond
        [(null? xs) (cons xs null)]
        [else (cons xs (last xs))]))
    (define (pre-last xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-last xs r) (equal? r (op-last xs)))
    *ENDLIBSPEC*/
    fn last(&mut self) -> Option<&T> {
        BTreeSet::last(self)
    }

    /*LIBSPEC*
    /*OPNAME*
    nth op-nth pre-nth post-nth
    *ENDOPNAME*/
    (define (op-nth xs n)
      (cond
        [(>= n (length xs)) (cons xs null)]
        [(< n 0) (cons xs null)]
        [else (cons xs (list-ref xs n))]))
    (define (pre-nth xs) (equal? xs (remove-duplicates (sort xs <))))
    (define (post-nth n xs r) (equal? r (op-nth xs n)))
    *ENDLIBSPEC*/
    fn nth(&mut self, n: usize) -> Option<&T> {
        BTreeSet::iter(self).nth(n)
    }
}

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

    use crate::proptest::*;
    use im::conslist::ConsList;
    use proptest::{collection::btree_set, prelude::*};
    use std::iter::FromIterator;

    fn abstraction<T>(t: BTreeSet<T>) -> ConsList<T> {
        let list: ConsList<T> = ConsList::from_iter(t);
        list
    }

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: 100, .. ProptestConfig::default()
          })]
        #[test]
        fn test_btree_len(ref mut t in btree_set(".*", 0..100)) {
            let abs_list = abstraction(t.clone());
            // pre: our list model is a sorted and unique list
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            assert_eq!(Container::<String>::len(t), abs_list.len());
            assert_eq!(abstraction(t.clone()), abs_list);
        }

        #[test]
        fn test_btree_contains(ref mut t in btree_set(".*", 0..100), a in ".*") {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            assert_eq!(Container::<String>::contains(t, &a), contains(&abs_list, &a));
            assert_eq!(abstraction(t.clone()), abs_list);
        }

        #[test]
        fn test_btree_is_empty(ref mut t in btree_set(".*", 0..100)) {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            assert_eq!(Container::<String>::is_empty(t), abs_list.is_empty());
            assert_eq!(abstraction(t.clone()), abs_list);
        }

        #[test]
        fn test_btree_insert(ref mut t in btree_set(".*", 0..100), a in ".*") {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            let after_list = unique(&abs_list.append(conslist![a.clone()]).sort());
            Container::<String>::insert(t, a.clone());
            assert_eq!(abstraction(t.clone()), after_list);
        }

        #[test]
        fn test_btree_clear(ref mut t in btree_set(".*", 0..100)) {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            let after_list = clear(&abs_list);
            Container::<String>::clear(t);
            assert_eq!(abstraction(t.clone()), after_list);
        }

        #[test]
        fn test_btree_remove(ref mut t in btree_set(".*", 0..100), a in ".*") {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            let (after_list, abs_elem) = remove(&abs_list, a.clone());
            let elem = Container::<String>::remove(t, a.clone());
            assert_eq!(abstraction(t.clone()), after_list);
            assert_eq!(elem, abs_elem);
        }

        #[test]
        fn test_btree_first(ref mut t in btree_set(".*", 0..100)) {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            let elem = Indexable::<String>::first(t);
            let abs_first = first(&abs_list);
            assert_eq!(elem, abs_first);
            assert_eq!(abstraction(t.clone()), abs_list);
        }

        #[test]
        fn test_btree_last(ref mut t in btree_set(".*", 0..100)) {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            let elem = Indexable::<String>::last(t);
            let abs_last = last(&abs_list);
            assert_eq!(elem, abs_last);
            assert_eq!(abstraction(t.clone()), abs_list);
        }

        #[test]
        fn test_btree_nth(ref mut t in btree_set(".*", 0..100), n in 0usize..100) {
            let abs_list = abstraction(t.clone());
            //pre
            assert_eq!(abs_list, unique(&abs_list.sort()));
            //post
            let elem = Indexable::<String>::nth(t, n);
            let abs_nth = nth(&abs_list, n);
            assert_eq!(elem, abs_nth);
            assert_eq!(abstraction(t.clone()), abs_list);
        }
    }
}