Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ec54a5

Browse files
committed
Correct sorting (now sorting subtrees)
1 parent 56b2d8f commit 2ec54a5

6 files changed

Lines changed: 96 additions & 47 deletions

File tree

core/file_walker.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,3 @@ func GetSubTree(path string, readDir ReadDir) File {
4141
}
4242
return File{name, folderSize, files}
4343
}
44-
45-
func PruneTree(tree *File, limit int64) {
46-
prunedFiles := []*File{}
47-
for _, file := range tree.Files {
48-
if file.Size >= limit {
49-
PruneTree(file, limit)
50-
prunedFiles = append(prunedFiles, file)
51-
}
52-
}
53-
tree.Files = prunedFiles
54-
55-
}

core/file_walker_test.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,3 @@ func TestGetSubTreeHandlesError(t *testing.T) {
8585
t.Error("GetSubTree didn't return emtpy file on ReadDir failure")
8686
}
8787
}
88-
89-
func TestPruneTree(t *testing.T) {
90-
testTree := &File{"b", 260, []*File{
91-
&File{"c", 100, []*File{}},
92-
&File{"d", 160, []*File{
93-
&File{"e", 50, []*File{}},
94-
&File{"f", 30, []*File{}},
95-
&File{"g", 80, []*File{
96-
&File{"i", 50, []*File{}},
97-
&File{"j", 30, []*File{}},
98-
}},
99-
}},
100-
}}
101-
expected := &File{"b", 260, []*File{
102-
&File{"c", 100, []*File{}},
103-
&File{"d", 160, []*File{
104-
&File{"g", 80, []*File{}},
105-
}},
106-
}}
107-
PruneTree(testTree, 60)
108-
if !reflect.DeepEqual(testTree, expected) {
109-
t.Errorf("tree not pruned correctly\ntree %v\nnot as expected %v", testTree, expected)
110-
}
111-
112-
}

core/tree_manipulation.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package core
2+
3+
import (
4+
"sort"
5+
)
6+
7+
type bySizeDesc []*File
8+
9+
func (f bySizeDesc) Len() int { return len(f) }
10+
func (f bySizeDesc) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
11+
func (f bySizeDesc) Less(i, j int) bool { return f[i].Size > f[j].Size }
12+
13+
func SortDesc(tree *File) {
14+
sort.Sort(bySizeDesc(tree.Files))
15+
for _, file := range tree.Files {
16+
SortDesc(file)
17+
}
18+
}
19+
20+
func PruneTree(tree *File, limit int64) {
21+
prunedFiles := []*File{}
22+
for _, file := range tree.Files {
23+
if file.Size >= limit {
24+
PruneTree(file, limit)
25+
prunedFiles = append(prunedFiles, file)
26+
}
27+
}
28+
tree.Files = prunedFiles
29+
30+
}

core/tree_manipulation_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package core
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSortTree(t *testing.T) {
9+
testTree := &File{"b", 260, []*File{
10+
&File{"c", 100, []*File{}},
11+
&File{"d", 160, []*File{
12+
&File{"e", 50, []*File{}},
13+
&File{"f", 30, []*File{}},
14+
&File{"g", 80, []*File{
15+
&File{"i", 30, []*File{}},
16+
&File{"j", 50, []*File{}},
17+
}},
18+
}},
19+
}}
20+
expected := &File{"b", 260, []*File{
21+
&File{"d", 160, []*File{
22+
&File{"g", 80, []*File{
23+
&File{"j", 50, []*File{}},
24+
&File{"i", 30, []*File{}},
25+
}},
26+
&File{"e", 50, []*File{}},
27+
&File{"f", 30, []*File{}},
28+
}},
29+
&File{"c", 100, []*File{}},
30+
}}
31+
SortDesc(testTree)
32+
if !reflect.DeepEqual(testTree, expected) {
33+
t.Errorf("tree not sorted correctly\ntree %v\nnot as expected %v", testTree, expected)
34+
}
35+
}
36+
37+
func TestPruneTree(t *testing.T) {
38+
testTree := &File{"b", 260, []*File{
39+
&File{"c", 100, []*File{}},
40+
&File{"d", 160, []*File{
41+
&File{"e", 50, []*File{}},
42+
&File{"f", 30, []*File{}},
43+
&File{"g", 80, []*File{
44+
&File{"i", 50, []*File{}},
45+
&File{"j", 30, []*File{}},
46+
}},
47+
}},
48+
}}
49+
expected := &File{"b", 260, []*File{
50+
&File{"c", 100, []*File{}},
51+
&File{"d", 160, []*File{
52+
&File{"g", 80, []*File{}},
53+
}},
54+
}}
55+
PruneTree(testTree, 60)
56+
if !reflect.DeepEqual(testTree, expected) {
57+
t.Errorf("tree not pruned correctly\ntree %v\nnot as expected %v", testTree, expected)
58+
}
59+
60+
}

interactive/interactive.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@ package interactive
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/viktomas/godu/core"
76
"io"
8-
"sort"
97
"strconv"
10-
)
11-
12-
type bySizeDesc []*core.File
138

14-
func (f bySizeDesc) Len() int { return len(f) }
15-
func (f bySizeDesc) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
16-
func (f bySizeDesc) Less(i, j int) bool { return f[i].Size > f[j].Size }
9+
"github.com/viktomas/godu/core"
10+
)
1711

1812
func InteractiveTree(tree *core.File, w io.Writer, r io.Reader, limit int64) {
1913
core.PruneTree(tree, limit)
20-
sort.Sort(bySizeDesc(tree.Files))
14+
core.SortDesc(tree)
2115
state := core.State{
2216
Folder: tree,
2317
}

interactive/interactive_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func TestDoesntGoPastRoot(t *testing.T) {
1313
testTree := &core.File{"b", 180, []*core.File{
1414
&core.File{"d", 100, []*core.File{
1515
&core.File{"e", 10, []*core.File{}},
16+
&core.File{"f", 30, []*core.File{}},
1617
}},
1718
}}
1819
input := "0\nb\nb\n"
@@ -21,7 +22,8 @@ func TestDoesntGoPastRoot(t *testing.T) {
2122
expected += "0)\t100B\td/\n"
2223
expected += "b/d/\n"
2324
expected += "---\n"
24-
expected += "0)\t10B\te\n"
25+
expected += "0)\t30B\tf\n"
26+
expected += "1)\t10B\te\n"
2527
expected += "b/\n"
2628
expected += "---\n"
2729
expected += "0)\t100B\td/\n"

0 commit comments

Comments
 (0)