浏览代码

Merge #689

689: fix boundary case of assembler::remove_contig_at r=Dirbaio a=hikaricai

Found a bug when learning source code of smoltcp.

Co-authored-by: caiyuanhao <caiyuanhao@hiakriMacBook-Pro.local>
bors[bot] 2 年之前
父节点
当前提交
a79413395e
共有 1 个文件被更改,包括 15 次插入2 次删除
  1. 15 2
      src/storage/assembler.rs

+ 15 - 2
src/storage/assembler.rs

@@ -171,8 +171,9 @@ impl Assembler {
         }
 
         // Removing the last one.
-        self.contigs[at] = Contig::empty();
-        &mut self.contigs[at]
+        let p = &mut self.contigs[self.contigs.len() - 1];
+        *p = Contig::empty();
+        p
     }
 
     /// Add a contig at the given index, and return a pointer to it.
@@ -476,6 +477,18 @@ mod test {
         assert_eq!(assr, contigs![(4, 4), (4, 0)]);
     }
 
+    #[test]
+    fn test_boundary_case_remove_front() {
+        let mut vec = vec![(1, 1); CONTIG_COUNT];
+        vec[0] = (0, 2);
+        let mut assr = Assembler::from(vec);
+        assert_eq!(assr.remove_front(), Some(2));
+        let mut vec = vec![(1, 1); CONTIG_COUNT];
+        vec[CONTIG_COUNT - 1] = (2, 0);
+        let exp_assr = Assembler::from(vec);
+        assert_eq!(assr, exp_assr);
+    }
+
     #[test]
     fn test_iter_empty() {
         let assr = Assembler::new(16);