From 1650839e497d3cf7a35a228917aa894913aa9b9b Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Sat, 8 Dec 2018 12:54:58 -0500 Subject: [PATCH] handle scratch images (closes #76) --- filetree/efficiency.go | 7 ++++++- filetree/efficiency_test.go | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/filetree/efficiency.go b/filetree/efficiency.go index b521bff5..68ed320a 100644 --- a/filetree/efficiency.go +++ b/filetree/efficiency.go @@ -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) diff --git a/filetree/efficiency_test.go b/filetree/efficiency_test.go index 3ec779f0..e4612011 100644 --- a/filetree/efficiency_test.go +++ b/filetree/efficiency_test.go @@ -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() @@ -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 { @@ -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)) + } + +}