瀏覽代碼

Add internal integrity check

Vadim Markovtsev 8 年之前
父節點
當前提交
05937ecfd6
共有 3 個文件被更改,包括 24 次插入2 次删除
  1. 13 1
      analyser.go
  2. 6 1
      file.go
  3. 5 0
      file_test.go

+ 13 - 1
analyser.go

@@ -117,6 +117,10 @@ func (analyser *Analyser) handleModification(
 	}
 	dmp := diffmatchpatch.New()
 	src, dst, _ := dmp.DiffLinesToRunes(str_from, str_to)
+	if file.Len() != len(src) {
+		panic(fmt.Sprintf("%s: internal integrity error src %d != %d",
+			change.To.Name, len(src), file.Len()))
+	}
 	diffs := dmp.DiffMainRunes(src, dst, false)
 	// we do not call RunesToDiffLines so the number of lines equals
 	// to the rune count
@@ -128,6 +132,11 @@ func (analyser *Analyser) handleModification(
 				r := recover()
 				if r != nil {
 					fmt.Fprintf(os.Stderr, "%s: internal diff error\n", change.To.Name)
+					fmt.Fprint(os.Stderr, "====BEFORE====\n")
+					fmt.Fprint(os.Stderr, str_from)
+					fmt.Fprint(os.Stderr, "====AFTER====\n")
+					fmt.Fprint(os.Stderr, str_to)
+					fmt.Fprint(os.Stderr, "====END====\n")
 					panic(r)
 				}
 			}()
@@ -139,12 +148,15 @@ func (analyser *Analyser) handleModification(
 				position += length
 			case diffmatchpatch.DiffDelete:
 				file.Update(day, position, 0, length)
-				break
 			default:
 				panic(fmt.Sprintf("diff operation is not supported: %d", edit.Type))
 			}
 		}()
 	}
+	if file.Len() != len(dst) {
+		panic(fmt.Sprintf("%s: internal integrity error dst %d != %d",
+			change.To.Name, len(dst), file.Len()))
+	}
 }
 
 func (analyser *Analyser) handleRename(from, to string, files map[string]*File) {

+ 6 - 1
file.go

@@ -35,6 +35,10 @@ func NewFile(time int, length int, status map[int]int64) *File {
 	return file
 }
 
+func (file *File) Len() int {
+	return file.tree.Max().Item().key
+}
+
 func (file *File) Update(time int, pos int, ins_length int, del_length int) {
 	if time < 0 {
 		panic("time may not be negative")
@@ -50,7 +54,8 @@ func (file *File) Update(time int, pos int, ins_length int, del_length int) {
 	}
 	tree := file.tree
 	if pos > tree.Max().Item().key {
-		panic("attempt to insert after the end of the file")
+		panic(fmt.Sprintf("attempt to insert after the end of the file: %d < %d",
+		                  tree.Max().Item().key, pos))
 	}
 	status := file.status
 	iter := tree.FindLE(pos)

+ 5 - 0
file_test.go

@@ -48,6 +48,11 @@ func TestBullshit(t *testing.T) {
 	assert.Equal(t, int64(0), status[1])
 }
 
+func TestLen(t *testing.T) {
+	file, _ := fixture()
+	assert.Equal(t, 100, file.Len())
+}
+
 func TestInsert(t *testing.T) {
 	file, status := fixture()
 	file.Update(1, 10, 10, 0)