...

Source file src/edge-infra.dev/third_party/gopherage/pkg/cov/aggregate.go

Documentation: edge-infra.dev/third_party/gopherage/pkg/cov

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cov
    18  
    19  import (
    20  	"golang.org/x/tools/cover"
    21  )
    22  
    23  // AggregateProfiles takes multiple coverage profiles and produces a new
    24  // coverage profile that counts the number of profiles that hit a block at least
    25  // once.
    26  func AggregateProfiles(profiles [][]*cover.Profile) ([]*cover.Profile, error) {
    27  	setProfiles := make([][]*cover.Profile, 0, len(profiles))
    28  	for _, p := range profiles {
    29  		c := countToBoolean(p)
    30  		setProfiles = append(setProfiles, c)
    31  	}
    32  	aggregateProfiles, err := MergeMultipleProfiles(setProfiles)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return aggregateProfiles, nil
    37  }
    38  
    39  // countToBoolean converts a profile containing hit counts to instead contain
    40  // only 1s or 0s.
    41  func countToBoolean(profile []*cover.Profile) []*cover.Profile {
    42  	setProfile := make([]*cover.Profile, 0, len(profile))
    43  	for _, p := range profile {
    44  		pc := deepCopyProfile(*p)
    45  		for i := range pc.Blocks {
    46  			if pc.Blocks[i].Count > 0 {
    47  				pc.Blocks[i].Count = 1
    48  			}
    49  		}
    50  		setProfile = append(setProfile, &pc)
    51  	}
    52  	return setProfile
    53  }
    54  

View as plain text