浏览代码

Fix the bug with deleting at 0 time

Vadim Markovtsev 8 年之前
父节点
当前提交
0bc941f975
共有 3 个文件被更改,包括 41 次插入7 次删除
  1. 5 6
      analyser.go
  2. 19 1
      file.go
  3. 17 0
      file_test.go

+ 5 - 6
analyser.go

@@ -135,19 +135,18 @@ func (analyser *Analyser) handleModification(
 		}
 	}
 
-	dump_before := ""
-	if analyser.Debug {
-		dump_before = file.Dump()
-	}
-
 	for _, edit := range diffs {
+		dump_before := ""
+		if analyser.Debug {
+			dump_before = file.Dump()
+		}
 		length := utf8.RuneCountInString(edit.Text)
 		func() {
 			defer func() {
 				r := recover()
 				if r != nil {
 					fmt.Fprintf(os.Stderr, "%s: internal diff error\n", change.To.Name)
-					fmt.Fprintf(os.Stderr, "Update(%d, %d, %d, %d)\n", day, position,
+					fmt.Fprintf(os.Stderr, "Update(%d, %d, %d (0), %d (0))\n", day, position,
 						length, utf8.RuneCountInString(pending.Text))
 					if dump_before != "" {
 						fmt.Fprintf(os.Stderr, "====TREE BEFORE====\n%s====END====\n", dump_before)

+ 19 - 1
file.go

@@ -59,6 +59,20 @@ func NewFile(time int, length int, status map[int]int64) *File {
 	return file
 }
 
+func NewFileFromTree(keys []int, vals []int, status map[int]int64) *File {
+	file := new(File)
+	file.status = status
+	file.tree = new(RBTree)
+	if len(keys) != len(vals) {
+		panic("keys and vals must be of equal length")
+	}
+	for i := 0; i < len(keys); i++ {
+		file.tree.Insert(Item{key: keys[i], value: vals[i]})
+	}
+	file.Validate()
+	return file
+}
+
 func (file *File) Len() int {
 	return file.tree.Max().Item().key
 }
@@ -161,11 +175,15 @@ func (file *File) Update(time int, pos int, ins_length int, del_length int) {
 			origin.key += delta
 		}
 	}
+
 	if ins_length > 0 {
 		if origin.value != time {
 			tree.Insert(Item{pos + ins_length, origin.value})
+		} else if pos == 0 {
+			// recover the beginning
+			tree.Insert(Item{pos, time})
 		}
-	} else if (pos > origin.key && previous.value != origin.value) || pos == origin.key {
+	} else if (pos > origin.key && previous.value != origin.value) || pos == origin.key || pos == 0 {
 		// continue the original interval
 		tree.Insert(Item{pos, origin.value})
 	}

+ 17 - 0
file_test.go

@@ -345,3 +345,20 @@ func TestBug4(t *testing.T) {
 	dump := file.Dump()
 	assert.Equal(t, "0 125\n2 215\n48 125\n50 215\n69 125\n73 215\n79 125\n80 0\n81 -1\n", dump)
 }
+
+func TestBug5(t *testing.T) {
+	status := map[int]int64{}
+	keys := []int{0, 2, 4, 7, 10}
+	vals := []int{24, 28, 24, 28, -1}
+	file := NewFileFromTree(keys, vals, status)
+	file.Update(28, 0, 1, 3)
+	dump := file.Dump()
+	assert.Equal(t, "0 28\n2 24\n5 28\n8 -1\n", dump)
+
+	keys = []int{0, 1, 16, 18,}
+	vals = []int{305, 0, 157, -1}
+	file = NewFileFromTree(keys, vals, status)
+	file.Update(310, 0, 0, 2)
+	dump = file.Dump()
+	assert.Equal(t, "0 0\n14 157\n16 -1\n", dump)
+}