func mergeProfileBlock(p *cover.Profile, pb cover.ProfileBlock, startIndex int) int { sortFunc := func(i int) bool { pi := p.Blocks[i+startIndex] return pi.StartLine >= pb.StartLine && (pi.StartLine != pb.StartLine || pi.StartCol >= pb.StartCol) } i := 0 if sortFunc(i) != true { i = sort.Search(len(p.Blocks)-startIndex, sortFunc) } i += startIndex if i < len(p.Blocks) && p.Blocks[i].StartLine == pb.StartLine && p.Blocks[i].StartCol == pb.StartCol { if p.Blocks[i].EndLine != pb.EndLine || p.Blocks[i].EndCol != pb.EndCol { log.Fatalf("OVERLAP MERGE: %v %v %v", p.FileName, p.Blocks[i], pb) } p.Blocks[i].Count |= pb.Count } else { if i > 0 { pa := p.Blocks[i-1] if pa.EndLine >= pb.EndLine && (pa.EndLine != pb.EndLine || pa.EndCol > pb.EndCol) { log.Fatalf("OVERLAP BEFORE: %v %v %v", p.FileName, pa, pb) } } if i < len(p.Blocks)-1 { pa := p.Blocks[i+1] if pa.StartLine <= pb.StartLine && (pa.StartLine != pb.StartLine || pa.StartCol < pb.StartCol) { log.Fatalf("OVERLAP AFTER: %v %v %v", p.FileName, pa, pb) } } p.Blocks = append(p.Blocks, cover.ProfileBlock{}) copy(p.Blocks[i+1:], p.Blocks[i:]) p.Blocks[i] = pb } return i + 1 }
func merge(p1, p2 *cover.Profile) *cover.Profile { output := cover.Profile{ FileName: p1.FileName, Mode: p1.Mode, } i, j := 0, 0 for i < len(p1.Blocks) && j < len(p2.Blocks) { bi, bj := p1.Blocks[i], p2.Blocks[j] if bi.StartLine == bj.StartLine && bi.StartCol == bj.StartCol { if bi.EndLine != bj.EndLine || bi.EndCol != bj.EndCol || bi.NumStmt != bj.NumStmt { panic("Not run on same source!") } output.Blocks = append(output.Blocks, cover.ProfileBlock{ StartLine: bi.StartLine, StartCol: bi.StartCol, EndLine: bi.EndLine, EndCol: bi.EndCol, NumStmt: bi.NumStmt, Count: bi.Count + bj.Count, }) i++ j++ } else if bi.StartLine < bj.StartLine || bi.StartLine == bj.StartLine && bi.StartCol < bj.StartCol { output.Blocks = append(output.Blocks, bi) i++ } else { output.Blocks = append(output.Blocks, bj) j++ } } for ; i < len(p1.Blocks); i++ { output.Blocks = append(output.Blocks, p1.Blocks[i]) } for ; j < len(p2.Blocks); j++ { output.Blocks = append(output.Blocks, p2.Blocks[j]) } return &output }