Skip to content

Commit

Permalink
such hashmap coverage like wow
Browse files Browse the repository at this point in the history
  • Loading branch information
yaleman committed Jun 26, 2024
1 parent afd64c8 commit dd7bdab
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/hashmap/asynch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ mod tests {
#[tokio::test]
async fn test_hashmap_basic_write() {
let hmap: HashMap<usize, usize> = HashMap::new();
let mut hmap_write = hmap.write().await;
let mut hmap_write = hmap.try_write().unwrap();

hmap_write.insert(10, 10);
hmap_write.insert(15, 15);
Expand Down
61 changes: 59 additions & 2 deletions src/hashmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ mod tests {
let mut hmap_write = hmap.write();

hmap_write.insert(10, 10);
hmap_write.insert(15, 15);
hmap_write.extend(vec![(15, 15)]);

assert!(hmap_write.contains_key(&10));
assert!(hmap_write.contains_key(&15));
Expand All @@ -201,6 +201,9 @@ mod tests {
assert!(hmap_write.contains_key(&15));

assert!(hmap_write.remove(&30).is_none());
assert!(!hmap_write.is_empty());

assert_eq!(hmap_write.keys().count(), 1);

hmap_write.clear();
assert!(!hmap_write.contains_key(&10));
Expand All @@ -223,6 +226,9 @@ mod tests {

let mut hmap_w2 = hmap.write();
hmap_w2.insert(20, 20);
assert!(!hmap_w2.is_empty());
assert_eq!(hmap_w2.keys().count(), 3);
assert_eq!(hmap_w2.len(), 3);
hmap_w2.commit();

assert!(hmap_r1.contains_key(&10));
Expand All @@ -233,11 +239,14 @@ mod tests {
assert!(hmap_r2.contains_key(&10));
assert!(hmap_r2.contains_key(&15));
assert!(hmap_r2.contains_key(&20));
assert!(!hmap_r2.is_empty());
assert_eq!(hmap_r2.len(), 3);
assert_eq!(hmap_r2.keys().count(), 3);
}

#[test]
fn test_hashmap_basic_read_snapshot() {
let hmap: HashMap<usize, usize> = HashMap::new();
let hmap: HashMap<usize, usize> = HashMap::default();
let mut hmap_w1 = hmap.write();
hmap_w1.insert(10, 10);
hmap_w1.insert(15, 15);
Expand All @@ -246,6 +255,18 @@ mod tests {
assert!(snap.contains_key(&10));
assert!(snap.contains_key(&15));
assert!(!snap.contains_key(&20));
hmap_w1.commit();

let hmap_read = hmap.read();
let snap = hmap_read.to_snapshot();
assert!(snap.contains_key(&10));
assert!(snap.contains_key(&15));
assert!(!snap.contains_key(&20));
assert_eq!(snap.len(), 2);
assert!(!snap.is_empty());
assert!(snap.iter().find(|(_k, v)| **v == 10).is_some());
assert_eq!(snap.values().count(), 2);
assert_eq!(snap.keys().count(), 2);
}

#[test]
Expand All @@ -258,6 +279,9 @@ mod tests {
hmap_w1.insert(15, 15);

assert!(hmap_w1.iter().count() == 2);

let hmap_r1 = hmap.read();
assert!(hmap_r1.iter().count() == 0);
}

#[test]
Expand All @@ -282,4 +306,37 @@ mod tests {
vec.sort_unstable();
assert_eq!(vec, [(10, 11), (15, 16), (20, 21)]);
}

#[test]
fn test_hashmap_keys() {
let hmap: HashMap<usize, usize> = vec![(10, 10), (15, 15), (20, 20)].into_iter().collect();
let hmap_read = hmap.read();
assert!(hmap_read.keys().find(|&&x| x == 10).is_some());
let hmap_write = hmap.write();
assert!(hmap_write.keys().find(|&&x| x == 10).is_some());
}
#[test]
fn test_hashmap_values() {
let hmap: HashMap<usize, usize> = vec![(10, 11), (15, 15), (20, 20)].into_iter().collect();
let hmap_read = hmap.read();
assert!(hmap_read.values().find(|&&x| x == 11).is_some());
let hmap_write = hmap.write();
assert!(hmap_write.values().find(|&&x| x == 11).is_some());
}

#[test]
fn test_write_snapshot_bits() {
let hmap: HashMap<usize, usize> = vec![(10, 11), (15, 15), (20, 20)].into_iter().collect();
let hmap_write = hmap.write();
let hmap_write_snapshot = hmap_write.to_snapshot();
assert!(!hmap_write_snapshot.is_empty());
assert_eq!(hmap_write_snapshot.len(), 3);
assert!(hmap_write_snapshot.contains_key(&10));
assert!(hmap_write_snapshot.values().find(|&&x| x == 11).is_some());
assert!(hmap_write_snapshot.values().find(|&&x| x == 10).is_none());
assert!(hmap_write_snapshot.keys().find(|&&x| x == 10).is_some());
assert!(hmap_write_snapshot.keys().find(|&&x| x == 11).is_none());
assert!(hmap_write_snapshot.keys().find(|&&x| x == 10).is_some());
assert!(hmap_write_snapshot.iter().count() == 3);
}
}

0 comments on commit dd7bdab

Please sign in to comment.