-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation.go
188 lines (157 loc) · 5.33 KB
/
mutation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2020-2021 C4 Project
//
// This file is part of c4t.
// Licenced under the MIT licence; see `LICENSE`.
package stat
import (
"encoding/csv"
"sort"
"strconv"
"github.com/c4-project/c4t/internal/id"
"github.com/c4-project/c4t/internal/timing"
"github.com/c4-project/c4t/internal/mutation"
"github.com/c4-project/c4t/internal/subject/status"
"github.com/c4-project/c4t/internal/helper/errhelp"
)
// Mutation holds statistics for each mutant in a mutation testing campaign.
type Mutation struct {
// ByIndex records statsets for each mutant index.
ByIndex map[mutation.Index]Mutant `json:"by_index"`
}
// Reset resets all of this statset's maps to empty, but non-nil.
func (m *Mutation) Reset() {
m.ByIndex = map[mutation.Index]Mutant{}
}
// AddAnalysis adds the information from mutation analysis a to this statset.
func (m *Mutation) AddAnalysis(a mutation.Analysis) {
m.ensure()
for mut, ma := range a {
ms := m.ByIndex[mut]
ms.addAnalysis(ma)
m.ByIndex[mut] = ms
}
}
func (m *Mutation) ensure() {
if m.ByIndex == nil {
m.ByIndex = map[mutation.Index]Mutant{}
}
}
// Mutants returns a sorted list of all mutant IDs seen in this statset.
func (m *Mutation) Mutants() []mutation.Mutant {
return m.MutantsWhere(FilterAllMutants)
}
// KilledMutants returns a sorted list of all mutant IDs killed in this statset.
func (m *Mutation) KilledMutants() []mutation.Mutant {
return m.MutantsWhere(FilterKilledMutants)
}
// MutantFilter is the type of mutant filtering predicates.
type MutantFilter func(m Mutant) bool
var (
// FilterAllMutants is a mutant filter that allows all mutants.
FilterAllMutants MutantFilter = func(mutant Mutant) bool { return true }
// FilterHitMutants is a mutant filter that allows hit mutants only.
FilterHitMutants MutantFilter = func(mutant Mutant) bool { return mutant.Hits.AtLeastOnce() }
// FilterKilledMutants is a mutant filter that allows killed mutants only.
FilterKilledMutants MutantFilter = func(mutant Mutant) bool { return mutant.Kills.AtLeastOnce() }
// FilterEscapedMutants is a mutant filter that allows only mutants that were hit but not killed.
FilterEscapedMutants MutantFilter = func(mutant Mutant) bool {
return mutant.Hits.AtLeastOnce() && !mutant.Kills.AtLeastOnce()
}
)
// MutantsWhere returns a sorted list of mutants satisfying pred.
// (It is a value receiver method to allow calling through templates.)
func (m Mutation) MutantsWhere(pred func(m Mutant) bool) []mutation.Mutant {
muts := make([]mutation.Mutant, 0, len(m.ByIndex))
for i, mstat := range m.ByIndex {
if pred(mstat) {
info := mstat.Info
info.SetIndexIfZero(i)
muts = append(muts, info)
}
}
sort.Slice(muts, func(i, j int) bool {
return muts[i].Index < muts[j].Index
})
return muts
}
// DumpCSV dumps into w a CSV representation of this mutation statistics set.
// Each line in the record has machine as a prefix.
// The writer is flushed at the end of this dump.
func (m *Mutation) DumpCSV(w *csv.Writer, machine id.ID) error {
var err error
for _, mut := range m.Mutants() {
if err = m.dumpMutant(w, machine, mut, m.ByIndex[mut.Index]); err != nil {
break
}
}
w.Flush()
return errhelp.FirstError(err, w.Error())
}
// Mutant gives statistics for a particular mutant.
type Mutant struct {
// Info contains the full mutant metadata set for the mutant.
Info mutation.Mutant `json:"info,omitempty"`
// Selections records the number of times this mutant has been selected.
Selections Hitset `json:"selections,omitempty"`
// Hits records the number of times this mutant has been hit (including kills).
Hits Hitset `json:"hits,omitempty"`
// Kills records the number of selections that resulted in kills.
Kills Hitset `json:"kills,omitempty"`
// Statuses records, for each status, the number of selections that resulted in that status.
Statuses map[status.Status]uint64 `json:"statuses,omitempty"`
}
// Hitset is a set of statistics relating to the way in which a mutant has been 'hit'.
type Hitset struct {
// Timespan records the first and most recent times this mutant was hit in this way.
Timespan timing.Span `json:"time_span,omitempty"`
// Count is the number of times this mutant was hit in this way.
Count uint64 `json:"count,omitempty"`
}
// AtLeastOnce gets whether the mutant was hit in a particular way at least once.
func (h *Hitset) AtLeastOnce() bool {
return 0 < h.Count
}
// Add records ntimes hits over timespan ts.
func (h *Hitset) Add(ntimes uint64, ts timing.Span) {
if ntimes == 0 {
return
}
h.Timespan.Union(ts)
h.Count += ntimes
}
func (m *Mutant) addAnalysis(ma mutation.MutantAnalysis) {
m.ensure()
m.Info = ma.Mutant
for _, h := range ma.Selections {
ts := h.Timespan
m.Selections.Add(1, ts)
m.Hits.Add(h.NumHits, ts)
if h.Killed() {
m.Kills.Add(1, ts)
}
m.Statuses[h.Status]++
}
}
func (m *Mutant) ensure() {
if m.Statuses == nil {
m.Statuses = map[status.Status]uint64{}
}
}
func (m *Mutation) dumpMutant(w *csv.Writer, machine id.ID, mut mutation.Mutant, mstats Mutant) error {
mstats.ensure()
cells := []string{
machine.String(),
fint(uint64(mut.Index)),
mut.Name.String(),
fint(mstats.Selections.Count),
fint(mstats.Hits.Count),
fint(mstats.Kills.Count),
}
for i := status.Ok; i <= status.Last; i++ {
cells = append(cells, fint(mstats.Statuses[i]))
}
return w.Write(cells)
}
func fint(i uint64) string {
return strconv.FormatUint(i, 10)
}