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
|
/*LIBSPEC-NAME*
rust-hashset-spec std::collections::HashSet
*ENDLIBSPEC-NAME*/
use crate::traits::Container;
pub use std::collections::HashSet;
use std::hash::Hash;
/*IMPL*
Container
*ENDIMPL*/
impl<T: Ord + Hash> Container<T> for HashSet<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(&self) -> usize {
HashSet::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(&self, x: &T) -> bool {
HashSet::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(&self) -> bool {
HashSet::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) {
HashSet::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) {
HashSet::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 HashSet::remove(self, &elt) {
true => Some(elt),
false => None,
}
}
fn iter<'a>(&'a self) -> impl Iterator<Item = &'a T>
where
T: 'a,
{
(&self).into_iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::proptest::*;
use im::conslist::ConsList;
use proptest::{collection::hash_set, prelude::*};
use std::iter::FromIterator;
fn abstraction<T: Ord>(h: HashSet<T>) -> ConsList<T> {
let list: ConsList<T> = ConsList::from_iter(h);
list.sort()
}
proptest! {
#![proptest_config(ProptestConfig {
cases: 100, .. ProptestConfig::default()
})]
#[test]
fn test_hashset_len(ref mut h in hash_set(".*", 0..100)) {
let abs_list = abstraction(h.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(h), abs_list.len());
assert_eq!(abstraction(h.clone()), abs_list);
}
#[test]
fn test_hashset_contains(ref mut h in hash_set(".*", 0..100), a in ".*") {
let abs_list = abstraction(h.clone());
//pre
assert_eq!(abs_list, unique(&abs_list.sort()));
//post
assert_eq!(Container::<String>::contains(h, &a), contains(&abs_list, &a));
assert_eq!(abstraction(h.clone()), abs_list);
}
#[test]
fn test_hashset_is_empty(ref mut h in hash_set(".*", 0..100)) {
let abs_list = abstraction(h.clone());
//pre
assert_eq!(abs_list, unique(&abs_list.sort()));
//post
assert_eq!(Container::<String>::is_empty(h), abs_list.is_empty());
assert_eq!(abstraction(h.clone()), abs_list);
}
#[test]
fn test_hashset_insert(ref mut h in hash_set(".*", 0..100), a in ".*") {
let abs_list = abstraction(h.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(h, a.clone());
assert_eq!(abstraction(h.clone()), after_list);
}
#[test]
fn test_hash_clear(ref mut h in hash_set(".*", 0..100)) {
let abs_list = abstraction(h.clone());
//pre
assert_eq!(abs_list, unique(&abs_list.sort()));
//post
let after_list = clear(&abs_list);
Container::<String>::clear(h);
assert_eq!(abstraction(h.clone()), after_list);
}
#[test]
fn test_hashset_remove(ref mut h in hash_set(".*", 0..100), a in ".*") {
let abs_list = abstraction(h.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(h, a.clone());
assert_eq!(abstraction(h.clone()), after_list);
assert_eq!(elem, abs_elem);
}
}
}
|