diff options
| author | Andrey Konovalov <andreyknvl@google.com> | 2017-08-01 15:28:12 +0200 |
|---|---|---|
| committer | Andrey Konovalov <andreyknvl@google.com> | 2017-08-01 15:57:03 +0200 |
| commit | 493773c70dcb7c21f94b2325e0970c606eb50f34 (patch) | |
| tree | cbba57b7fb38eb6983da9d9c5a00993b54ce0ba8 /prog | |
| parent | 032fb6f70ae1f26d2febedb6c566e341aaa5e039 (diff) | |
prog: properly remove calls when splicing progs
Use removeCall() to update use references.
Also add a test and speed up other ones.
Diffstat (limited to 'prog')
| -rw-r--r-- | prog/mutation.go | 4 | ||||
| -rw-r--r-- | prog/mutation_test.go | 18 | ||||
| -rw-r--r-- | prog/size_test.go | 16 |
3 files changed, 25 insertions, 13 deletions
diff --git a/prog/mutation.go b/prog/mutation.go index d6a9380ae..9486b70a8 100644 --- a/prog/mutation.go +++ b/prog/mutation.go @@ -29,8 +29,8 @@ func (p *Prog) Mutate(rs rand.Source, ncalls int, ct *ChoiceTable, corpus []*Pro p0c := p0.Clone() idx := r.Intn(len(p.Calls)) p.Calls = append(p.Calls[:idx], append(p0c.Calls, p.Calls[idx:]...)...) - if len(p.Calls) > ncalls { - p.Calls = p.Calls[:ncalls] + for i := len(p.Calls) - 1; i >= ncalls; i-- { + p.removeCall(i) } case r.nOutOf(20, 31): // Insert a new call. diff --git a/prog/mutation_test.go b/prog/mutation_test.go index 57b049376..44a6eabc3 100644 --- a/prog/mutation_test.go +++ b/prog/mutation_test.go @@ -46,6 +46,19 @@ next: } } +func TestMutateCorpus(t *testing.T) { + rs, iters := initTest(t) + var corpus []*Prog + for i := 0; i < 100; i++ { + p := Generate(rs, 10, nil) + corpus = append(corpus, p) + } + for i := 0; i < iters; i++ { + p1 := Generate(rs, 10, nil) + p1.Mutate(rs, 10, nil, corpus) + } +} + func TestMutateTable(t *testing.T) { tests := [][2]string{ // Insert calls. @@ -276,8 +289,9 @@ func TestMinimize(t *testing.T) { func TestMinimizeRandom(t *testing.T) { rs, iters := initTest(t) + iters /= 10 // Long test. for i := 0; i < iters; i++ { - p := Generate(rs, 10, nil) + p := Generate(rs, 5, nil) Minimize(p, len(p.Calls)-1, func(p1 *Prog, callIndex int) bool { if err := p1.validate(); err != nil { t.Fatalf("invalid program: %v", err) @@ -292,7 +306,7 @@ func TestMinimizeRandom(t *testing.T) { }, true) } for i := 0; i < iters; i++ { - p := Generate(rs, 10, nil) + p := Generate(rs, 5, nil) Minimize(p, len(p.Calls)-1, func(p1 *Prog, callIndex int) bool { if err := p1.validate(); err != nil { t.Fatalf("invalid program: %v", err) diff --git a/prog/size_test.go b/prog/size_test.go index 0f51edc2b..d206a0faa 100644 --- a/prog/size_test.go +++ b/prog/size_test.go @@ -20,15 +20,13 @@ func TestAssignSizeRandom(t *testing.T) { if data1 := p.Serialize(); !bytes.Equal(data0, data1) { t.Fatalf("different lens assigned, initial: %v, new: %v", data0, data1) } - for try := 0; try <= 10; try++ { - p.Mutate(rs, 10, nil, nil) - data0 := p.Serialize() - for _, call := range p.Calls { - assignSizesCall(call) - } - if data1 := p.Serialize(); !bytes.Equal(data0, data1) { - t.Fatalf("different lens assigned, initial: %v, new: %v", data0, data1) - } + p.Mutate(rs, 10, nil, nil) + data0 = p.Serialize() + for _, call := range p.Calls { + assignSizesCall(call) + } + if data1 := p.Serialize(); !bytes.Equal(data0, data1) { + t.Fatalf("different lens assigned, initial: %v, new: %v", data0, data1) } } } |
