Skip to content

Commit

Permalink
handle scratch images (closes #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed Dec 8, 2018
1 parent d78b6cd commit 1650839
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion filetree/efficiency.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func Efficiency(trees []*FileTree) (float64, EfficiencySlice) {
minimumPathSizes += value.minDiscoveredSize
discoveredPathSizes += value.CumulativeSize
}
score := float64(minimumPathSizes) / float64(discoveredPathSizes)
var score float64
if discoveredPathSizes == 0 {
score = 1.0
} else {
score = float64(minimumPathSizes) / float64(discoveredPathSizes)
}

sort.Sort(inefficientMatches)

Expand Down
26 changes: 24 additions & 2 deletions filetree/efficiency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

func TestEfficencyMap(t *testing.T) {
func TestEfficency(t *testing.T) {
trees := make([]*FileTree, 3)
for idx := range trees {
trees[idx] = NewFileTree()
Expand Down Expand Up @@ -32,7 +32,7 @@ func TestEfficencyMap(t *testing.T) {
for _, match := range actualMatches {
t.Logf(" match: %+v", match)
}
t.Fatalf("Expected to find %d inefficient path, but found %d", len(expectedMatches), len(actualMatches))
t.Fatalf("Expected to find %d inefficient paths, but found %d", len(expectedMatches), len(actualMatches))
}

if expectedMatches[0].Path != actualMatches[0].Path {
Expand All @@ -43,3 +43,25 @@ func TestEfficencyMap(t *testing.T) {
t.Errorf("Expected cumulative size of %v but go %v", expectedMatches[0].CumulativeSize, actualMatches[0].CumulativeSize)
}
}

func TestEfficency_ScratchImage(t *testing.T) {
trees := make([]*FileTree, 3)
for idx := range trees {
trees[idx] = NewFileTree()
}

trees[0].AddPath("/nothing", FileInfo{Size: 0})

var expectedScore = 1.0
var expectedMatches = EfficiencySlice{}
actualScore, actualMatches := Efficiency(trees)

if expectedScore != actualScore {
t.Errorf("Expected score of %v but go %v", expectedScore, actualScore)
}

if len(actualMatches) > 0 {
t.Fatalf("Expected to find %d inefficient paths, but found %d", len(expectedMatches), len(actualMatches))
}

}

0 comments on commit 1650839

Please sign in to comment.