Skip to content

Commit

Permalink
#88 insert() tested and fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 27, 2023
1 parent afe29b4 commit fd59929
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ impl<K: PartialEq + Clone, V: Clone, const N: usize> Map<K, V, N> {
#[inline]
pub fn insert(&mut self, k: K, v: V) {
let mut target = self.next;
for i in 0..self.next {
let mut i = 0;
loop {
if i == self.next {
assert!(i < N, "No more keys available in the map");
self.next += 1;
break;
}
let p = unsafe { self.pairs[i].assume_init_ref() };
if let Some((bk, _bv)) = &p {
if *bk == k {
Expand All @@ -112,10 +118,9 @@ impl<K: PartialEq + Clone, V: Clone, const N: usize> Map<K, V, N> {
if !p.is_some() {
target = i;
}
i += 1;
}
assert!(target < N, "No more keys available in the map");
self.pairs[target].write(Some((k, v)));
self.next += 1;
}

/// Get a reference to a single value.
Expand Down Expand Up @@ -190,6 +195,14 @@ fn insert_and_check_length() {
assert_eq!(2, m.len());
}

#[test]
fn overwrites_keys() {
let mut m: Map<i32, i32, 1> = Map::new();
m.insert(1, 42);
m.insert(1, 42);
assert_eq!(1, m.len());
}

#[test]
fn empty_length() {
let m: Map<u32, u32, 10> = Map::new();
Expand Down

0 comments on commit fd59929

Please sign in to comment.