How to use addFunctionCoverage method of cover Package

Best Syzkaller code snippet using cover.addFunctionCoverage

html.go

Source:html.go Github

copy

Full Screen

...66 pos.Files = append(pos.Files, f)67 if file.coveredPCs == 0 {68 continue69 }70 addFunctionCoverage(file, d)71 contents := ""72 lines, err := parseFile(file.filename)73 if err == nil {74 contents = fileContents(file, lines, haveProgs)75 fileOpenErr = nil76 } else {77 // We ignore individual errors of opening/locating source files78 // because there is a number of reasons when/why it can happen.79 // We fail only if we can't open/locate any single source file.80 // syz-ci can mess state of source files (https://github.com/google/syzkaller/issues/1770),81 // or bazel lies about location of auto-generated files,82 // or a used can update source files with git pull/checkout.83 contents = html.EscapeString(err.Error())84 if fileOpenErr != nil {85 fileOpenErr = err86 }87 }88 d.Contents = append(d.Contents, template.HTML(contents))89 f.Index = len(d.Contents) - 190 }91 if fileOpenErr != nil {92 return fileOpenErr93 }94 for _, prog := range progs {95 d.Progs = append(d.Progs, template.HTML(html.EscapeString(prog.Data)))96 }97 processDir(d.Root)98 return coverTemplate.Execute(w, d)99}100type fileStats struct {101 Name string102 CoveredLines int103 TotalLines int104 CoveredPCs int105 TotalPCs int106 TotalFunctions int107 CoveredPCsInFunctions int108 TotalPCsInFunctions int109}110var csvFilesHeader = []string{111 "Filename",112 "CoveredLines",113 "TotalLines",114 "CoveredPCs",115 "TotalPCs",116 "TotalFunctions",117 "CoveredPCsInFunctions",118 "TotalPCsInFunctions",119}120func (rg *ReportGenerator) convertToStats(progs []Prog) ([]fileStats, error) {121 files, err := rg.prepareFileMap(progs)122 if err != nil {123 return nil, err124 }125 var data []fileStats126 for fname, file := range files {127 lines, err := parseFile(file.filename)128 if err != nil {129 fmt.Printf("failed to open/locate %s\n", file.filename)130 continue131 }132 totalFuncs := len(file.functions)133 var coveredInFunc int134 var pcsInFunc int135 for _, function := range file.functions {136 coveredInFunc += function.covered137 pcsInFunc += function.pcs138 }139 totalLines := len(lines)140 var coveredLines int141 for _, line := range file.lines {142 if len(line.progCount) != 0 {143 coveredLines++144 }145 }146 data = append(data, fileStats{147 Name: fname,148 CoveredLines: coveredLines,149 TotalLines: totalLines,150 CoveredPCs: file.coveredPCs,151 TotalPCs: file.totalPCs,152 TotalFunctions: totalFuncs,153 CoveredPCsInFunctions: coveredInFunc,154 TotalPCsInFunctions: pcsInFunc,155 })156 }157 return data, nil158}159func (rg *ReportGenerator) DoCSVFiles(w io.Writer, progs []Prog) error {160 data, err := rg.convertToStats(progs)161 if err != nil {162 return err163 }164 sort.SliceStable(data, func(i, j int) bool {165 return data[i].Name < data[j].Name166 })167 writer := csv.NewWriter(w)168 defer writer.Flush()169 if err := writer.Write(csvFilesHeader); err != nil {170 return err171 }172 var d [][]string173 for _, dt := range data {174 d = append(d, []string{175 dt.Name,176 strconv.Itoa(dt.CoveredLines),177 strconv.Itoa(dt.TotalLines),178 strconv.Itoa(dt.CoveredPCs),179 strconv.Itoa(dt.TotalPCs),180 strconv.Itoa(dt.TotalFunctions),181 strconv.Itoa(dt.CoveredPCsInFunctions),182 strconv.Itoa(dt.TotalPCsInFunctions),183 })184 }185 return writer.WriteAll(d)186}187func groupCoverByFilePrefixes(datas []fileStats, subsystems []Subsystem) map[string]map[string]string {188 d := make(map[string]map[string]string)189 for _, subsystem := range subsystems {190 var coveredLines int191 var totalLines int192 var coveredPCsInFile int193 var totalPCsInFile int194 var totalFuncs int195 var coveredPCsInFuncs int196 var pcsInFuncs int197 var percentLines float64198 var percentPCsInFile float64199 var percentPCsInFunc float64200 for _, path := range subsystem.Paths {201 for _, data := range datas {202 if !strings.HasPrefix(data.Name, path) {203 continue204 }205 coveredLines += data.CoveredLines206 totalLines += data.TotalLines207 coveredPCsInFile += data.CoveredPCs208 totalPCsInFile += data.TotalPCs209 totalFuncs += data.TotalFunctions210 coveredPCsInFuncs += data.CoveredPCsInFunctions211 pcsInFuncs += data.TotalPCsInFunctions212 }213 }214 if totalLines != 0 {215 percentLines = 100.0 * float64(coveredLines) / float64(totalLines)216 }217 if totalPCsInFile != 0 {218 percentPCsInFile = 100.0 * float64(coveredPCsInFile) / float64(totalPCsInFile)219 }220 if pcsInFuncs != 0 {221 percentPCsInFunc = 100.0 * float64(coveredPCsInFuncs) / float64(pcsInFuncs)222 }223 d[subsystem.Name] = map[string]string{224 "subsystem": subsystem.Name,225 "lines": fmt.Sprintf("%v / %v / %.2f%%", coveredLines, totalLines, percentLines),226 "PCsInFiles": fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFile, totalPCsInFile, percentPCsInFile),227 "totalFuncs": strconv.Itoa(totalFuncs),228 "PCsInFuncs": fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFuncs, pcsInFuncs, percentPCsInFunc),229 }230 }231 return d232}233func (rg *ReportGenerator) DoHTMLTable(w io.Writer, progs []Prog) error {234 data, err := rg.convertToStats(progs)235 if err != nil {236 return err237 }238 d := groupCoverByFilePrefixes(data, rg.subsystem)239 return coverTableTemplate.Execute(w, d)240}241var csvHeader = []string{242 "Filename",243 "Function",244 "Covered PCs",245 "Total PCs",246}247func (rg *ReportGenerator) DoCSV(w io.Writer, progs []Prog) error {248 files, err := rg.prepareFileMap(progs)249 if err != nil {250 return err251 }252 var data [][]string253 for fname, file := range files {254 for _, function := range file.functions {255 data = append(data, []string{256 fname,257 function.name,258 strconv.Itoa(function.covered),259 strconv.Itoa(function.pcs),260 })261 }262 }263 sort.Slice(data, func(i, j int) bool {264 if data[i][0] != data[j][0] {265 return data[i][0] < data[j][0]266 }267 return data[i][1] < data[j][1]268 })269 writer := csv.NewWriter(w)270 defer writer.Flush()271 if err := writer.Write(csvHeader); err != nil {272 return err273 }274 return writer.WriteAll(data)275}276func fileContents(file *file, lines [][]byte, haveProgs bool) string {277 var buf bytes.Buffer278 lineCover := perLineCoverage(file.covered, file.uncovered)279 htmlReplacer := strings.NewReplacer(">", "&gt;", "<", "&lt;", "&", "&amp;", "\t", " ")280 for i, ln := range lines {281 if haveProgs {282 prog, count := "", " "283 if line := file.lines[i+1]; len(line.progCount) != 0 {284 prog = fmt.Sprintf("onclick='onProgClick(%v)'", line.progIndex)285 count = fmt.Sprintf("% 5v", len(line.progCount))286 }287 buf.WriteString(fmt.Sprintf("<span class='count' %v>%v</span> ", prog, count))288 }289 start := 0290 cover := append(lineCover[i+1], lineCoverChunk{End: backend.LineEnd})291 for _, cov := range cover {292 end := cov.End - 1293 if end > len(ln) {294 end = len(ln)295 }296 if end == start {297 continue298 }299 chunk := htmlReplacer.Replace(string(ln[start:end]))300 start = end301 class := ""302 if cov.Covered && cov.Uncovered {303 class = "both"304 } else if cov.Covered {305 class = "covered"306 } else if cov.Uncovered {307 class = "uncovered"308 } else {309 buf.WriteString(chunk)310 continue311 }312 buf.WriteString(fmt.Sprintf("<span class='%v'>%v</span>", class, chunk))313 }314 buf.WriteByte('\n')315 }316 return buf.String()317}318type lineCoverChunk struct {319 End int320 Covered bool321 Uncovered bool322}323func perLineCoverage(covered, uncovered []backend.Range) map[int][]lineCoverChunk {324 lines := make(map[int][]lineCoverChunk)325 for _, r := range covered {326 mergeRange(lines, r, true)327 }328 for _, r := range uncovered {329 mergeRange(lines, r, false)330 }331 return lines332}333func mergeRange(lines map[int][]lineCoverChunk, r backend.Range, covered bool) {334 // Don't panic on broken debug info, it is frequently broken.335 if r.EndLine < r.StartLine {336 r.EndLine = r.StartLine337 }338 if r.EndLine == r.StartLine && r.EndCol <= r.StartCol {339 r.EndCol = backend.LineEnd340 }341 for line := r.StartLine; line <= r.EndLine; line++ {342 start := 0343 if line == r.StartLine {344 start = r.StartCol345 }346 end := backend.LineEnd347 if line == r.EndLine {348 end = r.EndCol349 }350 ln := lines[line]351 if ln == nil {352 ln = append(ln, lineCoverChunk{End: backend.LineEnd})353 }354 lines[line] = mergeLine(ln, start, end, covered)355 }356}357func mergeLine(chunks []lineCoverChunk, start, end int, covered bool) []lineCoverChunk {358 var res []lineCoverChunk359 chunkStart := 0360 for _, chunk := range chunks {361 if chunkStart >= end || chunk.End <= start {362 res = append(res, chunk)363 } else if covered && chunk.Covered || !covered && chunk.Uncovered {364 res = append(res, chunk)365 } else if chunkStart >= start && chunk.End <= end {366 if covered {367 chunk.Covered = true368 } else {369 chunk.Uncovered = true370 }371 res = append(res, chunk)372 } else {373 if chunkStart < start {374 res = append(res, lineCoverChunk{start, chunk.Covered, chunk.Uncovered})375 }376 mid := end377 if mid > chunk.End {378 mid = chunk.End379 }380 res = append(res, lineCoverChunk{mid, chunk.Covered || covered, chunk.Uncovered || !covered})381 if chunk.End > end {382 res = append(res, lineCoverChunk{chunk.End, chunk.Covered, chunk.Uncovered})383 }384 }385 chunkStart = chunk.End386 }387 return res388}389func addFunctionCoverage(file *file, data *templateData) {390 var buf bytes.Buffer391 for _, function := range file.functions {392 percentage := ""393 if function.covered > 0 {394 percentage = fmt.Sprintf("%v%%", percent(function.covered, function.pcs))395 } else {396 percentage = "---"397 }398 buf.WriteString(fmt.Sprintf("<span class='hover'>%v", function.name))399 buf.WriteString(fmt.Sprintf("<span class='cover hover'>%v", percentage))400 buf.WriteString(fmt.Sprintf("<span class='cover-right'>of %v", strconv.Itoa(function.pcs)))401 buf.WriteString("</span></span></span><br>\n")402 }403 data.Functions = append(data.Functions, template.HTML(buf.String()))...

Full Screen

Full Screen

addFunctionCoverage

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, playground")4}5import "fmt"6func main() {7 fmt.Println("Hello, playground")8}9import "fmt"10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import "fmt"18func main() {19 fmt.Println("Hello, playground")20}21import "fmt"22func main() {23 fmt.Println("Hello, playground")24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28}29import "fmt"30func main() {31 fmt.Println("Hello, playground")32}33import "fmt"34func main() {35 fmt.Println("Hello, playground")36}37import "fmt"38func main() {39 fmt.Println("Hello, playground")40}41import "fmt"42func main() {43 fmt.Println("Hello, playground")44}45import "fmt"46func main() {47 fmt.Println("Hello, playground")48}49import "fmt"50func main() {51 fmt.Println("Hello, playground")52}

Full Screen

Full Screen

addFunctionCoverage

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, playground")4}5import "fmt"6func main() {7 fmt.Println("Hello, playground")8}9import "fmt"10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import "fmt"18func main() {19 fmt.Println("Hello, playground")20}21import "fmt"22func main() {23 fmt.Println("Hello, playground")24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28}29import "fmt"30func main() {31 fmt.Println("Hello, playground")32}33import "fmt"34func main() {35 fmt.Println("Hello, playground")36}37import "fmt"38func main() {39 fmt.Println("Hello, playground")40}41import "fmt"42func main() {43 fmt.Println("Hello, playground")44}45import "fmt"46func main() {47 fmt.Println("Hello, playground")48}49import "fmt"50func main() {51 fmt.Println("Hello, playground")52}

Full Screen

Full Screen

addFunctionCoverage

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 trace.Start(os.Stderr)4 defer trace.Stop()5 time.Sleep(100 * time.Millisecond)6 c := testing.Coverage()7 fmt.Println(c.Counters[0].Name, c.Counters[0].Count)8}9import (10func main() {11 trace.Start(os.Stderr)12 defer trace.Stop()13 time.Sleep(100 * time.Millisecond)14 c := testing.Coverage()15 fmt.Println(c.Counters[0].Name, c.Counters[0].Count)16}17import (18func main() {19 trace.Start(os.Stderr)20 defer trace.Stop()21 time.Sleep(100 * time.Millisecond)22 c := testing.Coverage()23 fmt.Println(c.Counters[0].Name, c.Counters[0].Count)24}25import (26func main() {27 trace.Start(os.Stderr)28 defer trace.Stop()29 time.Sleep(100 * time.Millisecond)30 c := testing.Coverage()31 fmt.Println(c.Counters[0].Name, c.Counters[0].Count)32}33import (

Full Screen

Full Screen

addFunctionCoverage

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 trace.Start(os.Stdout)4 defer trace.Stop()5 fmt.Println("Hello, playground")6 func1()7 func2()8}9func func1() {10 fmt.Println("I am in func1")11}12func func2() {13 fmt.Println("I am in func2")14}15import (16func main() {17 trace.Start(os.Stdout)18 defer trace.Stop()19 fmt.Println("Hello, playground")20 func1()21 func2()22}23func func1() {24 fmt.Println("I am in func1")25}26func func2() {27 fmt.Println("I am in func2")28}29import (30func main() {31 trace.Start(os.Stdout)32 defer trace.Stop()33 fmt.Println("Hello, playground")34 func1()35 func2()36}37func func1() {38 fmt.Println("I am in func1")39}40func func2() {41 fmt.Println("I am in func2")42}43import (44func main() {45 trace.Start(os.Stdout)46 defer trace.Stop()47 fmt.Println("Hello, playground")48 func1()49 func2()50}51func func1() {52 fmt.Println("I am in func1")53}54func func2() {55 fmt.Println("I am in func2")56}57import (58func main() {59 trace.Start(os.Stdout)60 defer trace.Stop()61 fmt.Println("Hello, playground")62 func1()63 func2()64}65func func1() {66 fmt.Println("I am in func1")67}68func func2() {69 fmt.Println("I am in func2")70}71import (72func main() {

Full Screen

Full Screen

addFunctionCoverage

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, playground")4}5import "fmt"6func main() {7 fmt.Println("Hello, playground")8}9import "fmt"10func main() {11 fmt.Println("Hello, playground")12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import "fmt"18func main() {19 fmt.Println("Hello, playground")20}21import "fmt"22func main() {23 fmt.Println("Hello, playground")24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28}29import "fmt"30func main() {31 fmt.Println("Hello, playground")32}33import "fmt"34func main() {35 fmt.Println("Hello, playground")36}37import "fmt"38func main() {39 fmt.Println("Hello, playground")40}

Full Screen

Full Screen

addFunctionCoverage

Using AI Code Generation

copy

Full Screen

1import (2var count = flag.Int("count", 1, "number of times to run the test function")3func main() {4 flag.Parse()5 runtime.SetBlockProfileRate(1)6 runtime.SetMutexProfileFraction(1)7 f, err := os.Create("trace.out")8 if err != nil {9 panic(err)10 }11 defer f.Close()12 err = trace.Start(f)13 if err != nil {14 panic(err)15 }16 defer trace.Stop()17 for i := 0; i < *count; i++ {18 test()19 }20}21func test() {22 c = add(a, b)23 fmt.Printf("value of c in test() : %d24}25func add(a, b int) int {26}27import (28var count = flag.Int("count", 1, "number of times to run the test function")29func main() {30 flag.Parse()31 runtime.SetBlockProfileRate(1)32 runtime.SetMutexProfileFraction(1)33 f, err := os.Create("trace.out")34 if err != nil {35 panic(err)36 }37 defer f.Close()38 err = trace.Start(f)39 if err != nil {40 panic(err)41 }42 defer trace.Stop()43 for i := 0; i < *count; i++ {44 test()45 }46}47func test() {48 c = add(a, b)49 fmt.Printf("value of c in test() : %d50}51func add(a, b int) int {52}

Full Screen

Full Screen

addFunctionCoverage

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := cover.NewCover()4 f := func() {5 fmt.Println("Function is called")6 }7 c.AddFunctionCoverage(f)8 f()9 fmt.Println(c.Counters())10}11import (12func main() {13 c := cover.NewCover()14 c.AddFileCoverage("1.go")15 f()16 fmt.Println(c.Counters())17}18import (19func main() {20 c := cover.NewCover()21 c.AddDirCoverage(".")22 f()23 fmt.Println(c.Counters())24}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful