-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfloatdecrement.go
49 lines (39 loc) · 951 Bytes
/
floatdecrement.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 floatdecrement
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"reflect"
"strconv"
"github.com/gtramontina/ooze/viruses"
)
type FloatDecrement struct{}
// New returns a new FloatDecrement virus.
//
// It decrements floating points by `1.0`.
func New() *FloatDecrement {
return &FloatDecrement{}
}
func (v *FloatDecrement) 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 Decrement",
func() { literal.Value = mutatedValue },
func() { literal.Value = originalValue },
),
}
}