-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
83 lines (69 loc) · 2.24 KB
/
main_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
package main
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewOrganismSize(t *testing.T) {
size := 13
organism := NewOrganism([]byte("HELLO WORLD !"), size)
assert.Equal(t, size, len(organism.DNA))
}
func TestNewPopulationSize(t *testing.T) {
blockSize := 13
cypher := addPadding([]byte("test test test"), blockSize)
population := NewPopulation(cypher, 10, blockSize)
assert.Equal(t, 10, len(population))
for _, organism := range population {
assert.Equal(t, blockSize, len(organism.DNA))
}
}
func TestPopulationBestIndividual(t *testing.T) {
population := NewPopulation([]byte("DH OE!ROLW LL"), 10, 13)
best := population.GetBest()
for _, organism := range population {
assert.True(t, best.Fitness >= organism.Fitness)
}
assert.Greater(t, best.Fitness, float64(0.0))
}
func TestPermute(t *testing.T) {
o := Organism{
DNA: []int{10, 0, 11, 7, 1, 12, 8, 4, 3, 6, 5, 2, 9},
}
cyphertext := o.GetSolution([]byte("HELLO WORLD !"))
assert.Equal(t, "DH OE!ROLW LL", cyphertext)
o = Organism{
DNA: []int{1, 4, 11, 12, 3, 2, 9, 7, 6, 8, 0, 10, 5},
}
plain := o.GetSolution([]byte(cyphertext))
assert.Equal(t, "HELLO WORLD !", plain)
}
func TestFit(t *testing.T) {
badOrganism := Organism{DNA: []int{10, 0, 11, 7, 1, 12, 8, 4, 3, 6, 5, 2, 9}}
badOrganism.calcFitness([]byte("HELLO WORLD !"))
goodOrganism := Organism{DNA: []int{1, 4, 11, 12, 3, 2, 9, 7, 6, 8, 0, 10, 5}}
goodOrganism.calcFitness([]byte("DH OE!ROLW LL"))
assert.Greater(t, goodOrganism.Fitness, badOrganism.Fitness)
}
func TestIntegration(t *testing.T) {
blockSize := 13
cypher, _ := os.ReadFile("cipher.txt")
cypher = addPadding(cypher, blockSize)
assert.Equal(t, 0, len(cypher)%blockSize)
popSize := 200
population := NewPopulation(cypher, popSize, blockSize)
assert.Equal(t, popSize, len(population))
for _, organism := range population {
assert.Equal(t, blockSize, len(organism.DNA))
}
}
func TestCrossover(t *testing.T) {
blockSize := 13
parent1 := Organism{DNA: []int{1, 4, 11, 12, 3, 2, 9, 7, 6, 8, 0, 10, 5}}
parent2 := Organism{DNA: []int{10, 0, 11, 7, 1, 12, 8, 4, 3, 6, 5, 2, 9}}
child := crossover(parent1, parent2)
assert.Equal(t, blockSize, len(child.DNA))
for i := 0; i < blockSize; i++ {
assert.Contains(t, child.DNA, i)
}
}