-
Notifications
You must be signed in to change notification settings - Fork 8
/
floatincrement.go
49 lines (39 loc) · 951 Bytes
/
floatincrement.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
package floatincrement
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"reflect"
"strconv"
"github.com/gtramontina/ooze/viruses"
)
type FloatIncrement struct{}
// New returns a new FloatIncrement virus.
//
// It increments floating points by `1.0`.
func New() *FloatIncrement {
return &FloatIncrement{}
}
func (v *FloatIncrement) Incubate(node ast.Node, _ *types.Info) []*viruses.Infection {
literal, ok := node.(*ast.BasicLit)
if !ok || literal.Kind != token.FLOAT {
return nil
}
originalValue := literal.Value
var originalFloat float64
bitSize := reflect.TypeOf(originalFloat).Bits()
originalFloat, err := strconv.ParseFloat(originalValue, bitSize)
if err != nil {
return nil
}
originalFloat++
mutatedValue := fmt.Sprintf("%v", originalFloat)
return []*viruses.Infection{
viruses.NewInfection(
"Float Increment",
func() { literal.Value = mutatedValue },
func() { literal.Value = originalValue },
),
}
}