-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutator_duplicate_test.go
86 lines (76 loc) · 2.05 KB
/
mutator_duplicate_test.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
// Archivo: mutator_duplicate_test.go
package mutator
import (
"testing"
"unicode"
)
// Helper function to count the number of letters in the result
func countLetters(runes []rune) map[rune]int {
counts := make(map[rune]int)
for _, r := range runes {
if unicode.IsLetter(r) {
counts[r]++
}
}
return counts
}
// Helper function to count the number of duplicated letters
func countDuplications(original []rune, changed []rune) int {
origCounts := countLetters(original)
changedCounts := countLetters(changed)
duplicationCount := 0
for r, origCount := range origCounts {
if changedCount, found := changedCounts[r]; found {
if changedCount > origCount {
duplicationCount += changedCount - origCount
}
}
}
return duplicationCount
}
// Test for duplicateChars
func TestDuplicateChars(t *testing.T) {
mutator := New(&Config{
MutationTypes: []MutationType{
NewDuplicateMutation(1, 1.0),
},
})
tests := []struct {
name string
input string
mutationType MutationType
expectedCount int // Expected number of duplications
}{
{
name: "Single duplicate",
input: "hello",
mutationType: NewDuplicateMutation(1, 1.0),
expectedCount: 1,
},
{
name: "Multiple duplicates",
input: "hello",
mutationType: NewDuplicateMutation(2, 1.0),
expectedCount: 2,
},
{
name: "More duplicates than characters",
input: "hello",
mutationType: NewDuplicateMutation(5, 1.0),
expectedCount: 5, // This might not be accurate due to randomness
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inputRunes := []rune(tt.input)
original := string(inputRunes)
result := mutator.duplicateChars(inputRunes, tt.mutationType)
resultStr := string(result)
// Count the number of duplications
duplicationCount := countDuplications([]rune(original), []rune(resultStr))
if duplicationCount != tt.expectedCount {
t.Errorf("duplicateChars() duplicated %d letters, want %d", duplicationCount, tt.expectedCount)
}
})
}
}