Browse Source

Merge pull request #93 from vmarkovtsev/master

Fixes in File.Update
Vadim Markovtsev 6 years ago
parent
commit
f87d7f43f9
3 changed files with 76 additions and 6 deletions
  1. 2 2
      Makefile
  2. 7 3
      internal/burndown/file.go
  3. 67 1
      internal/burndown/file_test.go

+ 2 - 2
Makefile

@@ -32,10 +32,10 @@ internal/pb/pb_pb2.py: internal/pb/pb.proto
 cmd/hercules/plugin_template_source.go: cmd/hercules/plugin.template
 	cd cmd/hercules && go generate
 
-${GOPATH}/src/gopkg.in/bblfsh/client-go.v2:
+${GOPATH}/src/gopkg.in/bblfsh/client-go.v2/README.md:
 	go get -d -v gopkg.in/bblfsh/client-go.v2/...
 
-${GOPATH}/pkg/$(PKG)/gopkg.in/bblfsh/client-go.v2: ${GOPATH}/src/gopkg.in/bblfsh/client-go.v2
+${GOPATH}/pkg/$(PKG)/gopkg.in/bblfsh/client-go.v2: ${GOPATH}/src/gopkg.in/bblfsh/client-go.v2/README.md
 	cd ${GOPATH}/src/gopkg.in/bblfsh/client-go.v2 && \
 	make dependencies
 

+ 7 - 3
internal/burndown/file.go

@@ -144,10 +144,12 @@ func (file *File) Update(time int, pos int, insLength int, delLength int) {
 	}
 	iter := tree.FindLE(pos)
 	origin := *iter.Item()
-	file.updateTime(time, time, insLength)
+	if insLength > 0 {
+		file.updateTime(time, time, insLength)
+	}
 	if delLength == 0 {
 		// simple case with insertions only
-		if origin.Key < pos || (origin.Value == time && pos == 0) {
+		if origin.Key < pos || (origin.Value == time && (pos == 0 || pos == origin.Key)) {
 			iter = iter.Next()
 		}
 		for ; !iter.Limit(); iter = iter.Next() {
@@ -188,7 +190,7 @@ func (file *File) Update(time int, pos int, insLength int, delLength int) {
 	var previous *rbtree.Item
 	if insLength > 0 && (origin.Value != time || origin.Key == pos) {
 		// insert our new interval
-		if iter.Item().Value == time {
+		if iter.Item().Value == time && iter.Item().Key - delLength == pos {
 			prev := iter.Prev()
 			if prev.Item().Value != time {
 				iter.Item().Key = pos
@@ -249,6 +251,8 @@ func (file *File) Merge(day int, others... *File) {
 			if ol & TreeMergeMark == TreeMergeMark {
 				continue
 			}
+			// the following should happen rarely:
+			// l & TreeMergeMark != TreeMergeMark && l != ol
 			if l & TreeMergeMark == TreeMergeMark || l > ol {
 				// 1 - the line is merged in myself and exists in other
 				// 2 - the same line introduced in different branches,

+ 67 - 1
internal/burndown/file_test.go

@@ -426,29 +426,61 @@ func TestBug4File(t *testing.T) {
 	file := NewFile(0, 10, func(a, b, c int) {
 		updateStatusFile(status, a, b, c)
 	})
+	// 0 0 | 10 -1
 	file.Update(125, 0, 20, 9)
+	// 0 125 | 20 0 | 21 -1
 	file.Update(125, 0, 20, 20)
+	// 0 125 | 20 0 | 21 -1
 	file.Update(166, 12, 1, 1)
+	// 0 125 | 12 166 | 13 125 | 20 0 | 21 -1
 	file.Update(214, 2, 1, 1)
+	// 0 125 | 2 214 | 3 125 | 12 166 | 13 125 | 20 0 | 21 -1
 	file.Update(214, 4, 9, 0)
+	// 0 125 | 2 214 | 3 125 | 4 214 | 13 125 | 21 166 | 22 125 | 29 0 | 30 -1
 	file.Update(214, 27, 1, 1)
+	// 0 125 | 2 214 | 3 125 | 4 214 | 13 125 | 21 166 | 22 125 | 27 214 | 28 125 | 29 0 | 30 -1
 	file.Update(215, 3, 1, 1)
+	// 0 125 | 2 214 | 3 215 | 4 214 | 13 125 | 21 166 | 22 125 | 27 214 | 28 125 | 29 0 | 30 -1
 	file.Update(215, 13, 1, 1)
+	// 0 125 | 2 214 | 3 215 | 4 214 | 13 215 | 14 125 | 21 166 | 22 125 | 27 214 | 28 125 | 29 0 | 30 -1
 	file.Update(215, 17, 1, 1)
+	// 0 125 | 2 214 | 3 215 | 4 214 | 13 215 | 14 125 | 17 215 | 18 125 | 21 166 | 22 125 | 27 214 | 28 125 | 29 0 | 30 -1
 	file.Update(215, 19, 5, 0)
+	// 0 125 | 2 214 | 3 215 | 4 214 | 13 215 | 14 125 | 17 215 | 18 125 | 19 215 | 24 125 | 26 166 | 27 125 | 32 214 | 33 125 | 34 0 | 35 -1
 	file.Update(215, 25, 0, 1)
+	// 0 125 | 2 214 | 3 215 | 4 214 | 13 215 | 14 125 | 17 215 | 18 125 | 19 215 | 24 125 | 25 166 | 26 125 | 31 214 | 32 125 | 33 0 | 34 -1
 	file.Update(215, 31, 6, 1)
+	// 0 125 | 2 214 | 3 215 | 4 214 | 13 215 | 14 125 | 17 215 | 18 125 | 19 215 | 24 125 | 25 166 | 26 125 | 31 215 | 37 125 | 38 0 | 39 -1
 	file.Update(215, 27, 15, 0)
+	// 0 125 | 2 214 | 3 215 | 4 214 | 13 215 | 14 125 | 17 215 | 18 125 | 19 215 | 24 125 | 25 166 | 26 125 | 27 215 | 42 125 | 46 215 | 52 125 | 53 0 | 54 -1
 	file.Update(215, 2, 25, 4)
+	// 0 125 | 2 215 | 27 214 | 34 215 | 35 125 | 38 215 | 39 125 | 40 215 | 45 125 | 46 166 | 47 125 | 48 215 | 63 125 | 67 215 | 73 125 | 74 0 | 75 -1
 	file.Update(215, 28, 1, 1)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 34 215 | 35 125 | 38 215 | 39 125 | 40 215 | 45 125 | 46 166 | 47 125 | 48 215 | 63 125 | 67 215 | 73 125 | 74 0 | 75 -1
 	file.Update(215, 30, 7, 2)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 30 215 | 37 214 | 39 215 | 40 125 | 43 215 | 44 125 | 45 215 | 50 125 | 51 166 | 52 125 | 53 215 | 68 125 | 72 215 | 78 125 | 79 0 | 80 -1
 	file.Update(215, 38, 1, 0)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 30 215 | 37 214 | 38 215 | 39 214 | 40 215 | 41 125 | 44 215 | 45 125 | 46 215 | 51 125 | 52 166 | 53 125 | 54 215 | 69 125 | 73 215 | 79 125 | 80 0 | 81 -1
 	file.Update(215, 40, 4, 2)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 30 215 | 37 214 | 38 215 | 39 214 | 40 215 | 44 125 | 46 215 | 47 125 | 48 215 | 53 125 | 54 166 | 55 125 | 56 215 | 71 125 | 75 215 | 81 125 | 82 0 | 83 -1
 	file.Update(215, 46, 1, 0)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 30 215 | 37 214 | 38 215 | 39 214 | 40 215 | 44 125 | 46 215 | 48 125 | 49 215 | 54 125 | 55 166 | 56 125 | 57 215 | 72 125 | 76 215 | 82 125 | 83 0 | 84 -1
 	file.Update(215, 49, 1, 0)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 30 215 | 37 214 | 38 215 | 39 214 | 40 215 | 44 125 | 46 215 | 48 125 | 49 215 | 55 125 | 56 166 | 57 125 | 58 215 | 73 125 | 77 215 | 83 125 | 84 0 | 85 -1
 	file.Update(215, 52, 2, 6)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 30 215 | 37 214 | 38 215 | 39 214 | 40 215 | 44 125 | 46 215 | 48 125 | 49 215 | 69 125 | 73 215 | 79 125 | 80 0 | 81 -1
 	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)
+	assert.Equal(t, "0 125\n2 215\n27 214\n28 215\n29 214\n30 215\n37 214\n38 215\n39 214\n40 215\n44 125\n46 215\n48 125\n49 215\n69 125\n73 215\n79 125\n80 0\n81 -1\n", dump)
+
+	file.Update(214, 38, 1, 1)
+	// 0 125 | 2 215 | 27 214 | 28 215 | 29 214 | 30 215 | 37 214 | 40 215 | 44 125 | 46 215 | 48 125 | 49 215 | 69 125 | 73 215 | 79 125 | 80 0 | 81 -1
+	dump = file.Dump()
+	assert.Equal(t, "0 125\n2 215\n27 214\n28 215\n29 214\n30 215\n37 214\n40 215\n44 125\n46 215\n48 125\n49 215\n69 125\n73 215\n79 125\n80 0\n81 -1\n", dump)
+
+	file.Update(300, 28, 1, 1)
+	// 0 125 | 2 215 | 27 214 | 28 300 | 29 214 | 30 215 | 37 214 | 40 215 | 44 125 | 46 215 | 48 125 | 49 215 | 69 125 | 73 215 | 79 125 | 80 0 | 81 -1
+	dump = file.Dump()
+	assert.Equal(t, "0 125\n2 215\n27 214\n28 300\n29 214\n30 215\n37 214\n40 215\n44 125\n46 215\n48 125\n49 215\n69 125\n73 215\n79 125\n80 0\n81 -1\n", dump)
 }
 
 func TestBug5File(t *testing.T) {
@@ -619,4 +651,38 @@ func TestFileMergeNil(t *testing.T) {
 	assert.Panics(t, func() {
 		file.Merge(1, nil)
 	})
+}
+
+func TestBug6File(t *testing.T) {
+	status := map[int]int64{}
+	keys := []int{0, 113, 153, 154}
+	vals := []int{7, 10, 7, -1}
+	file := NewFileFromTree(keys, vals, func(a, b, c int) {
+		updateStatusFile(status, a, b, c)
+	})
+	// 0 7 | 113 10 | 153 7 | 154 -1
+	file.Update(10, 99, 1, 1)
+	// 0 7 | 99 10 | 100 7 | 113 10 | 153 7 | 154 -1
+	file.Update(10, 104, 1, 1)
+	// 0 7 | 99 10 | 100 7 | 104 10 | 105 7 | 113 10 | 153 7 | 154 -1
+	file.Update(10, 106, 1, 1)
+	// 0 7 | 99 10 | 100 7 | 104 10 | 105 7 | 106 10 | 107 7 | 113 10 | 153 7 | 154 -1
+	file.Update(10, 108, 1, 1)
+	// 0 7 | 99 10 | 100 7 | 104 10 | 105 7 | 106 10 | 107 7 | 108 10 | 109 7 | 113 10 | 153 7 | 154 -1
+	file.Update(10, 115, 2, 0)
+	// 0 7 | 99 10 | 100 7 | 104 10 | 105 7 | 106 10 | 107 7 | 108 10 | 109 7 | 113 10 | 155 7 | 156 -1
+	file.Update(10, 125, 4, 2)
+	// 0 7 | 99 10 | 100 7 | 104 10 | 105 7 | 106 10 | 107 7 | 108 10 | 109 7 | 113 10 | 157 7 | 158 -1
+
+	dump := file.Dump()
+	assert.Equal(t, "0 7\n99 10\n100 7\n104 10\n105 7\n106 10\n107 7\n108 10\n109 7\n113 10\n157 7\n158 -1\n", dump)
+
+	file = NewFileFromTree(keys, vals, func(a, b, c int) {
+		updateStatusFile(status, a, b, c)
+	})
+	// 0 7 | 113 10 | 153 7 | 154 -1
+	file.Update(10, 112, 1, 1)
+	// 0 7 | 112 10 | 153 7 | 154 -1
+	dump = file.Dump()
+	assert.Equal(t, "0 7\n112 10\n153 7\n154 -1\n", dump)
 }