From e45562b2827d4a363c9b1209dc507a9d62430947 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sun, 14 Aug 2016 22:56:55 +0300 Subject: [PATCH] add misc tests --- misc.go | 13 ++++---- misc_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 misc_test.go diff --git a/misc.go b/misc.go index 85909dfc..1d4ef2fc 100644 --- a/misc.go +++ b/misc.go @@ -9,10 +9,13 @@ import ( "unicode/utf8" ) -func isRoot(name string) bool { - return path.Dir(name) == name -} +func isRoot(name string) bool { return path.Dir(name) == name } +// This function converts a size in bytes to a human readable form. For this +// purpose metric suffixes are used (e.g. 1K = 1000). For values less than 10 +// the first significant digit is shown, otherwise it is hidden. Numbers are +// always rounded down. For these reasons this function always show somewhat +// smaller values but it should be fine for most human beings. func humanize(size int64) string { if size < 1000 { return fmt.Sprintf("%d", size) @@ -32,7 +35,7 @@ func humanize(size int64) string { curr := float64(size) / 1000 for _, s := range suffix { if curr < 10 { - return fmt.Sprintf("%.1f%s", curr, s) + return fmt.Sprintf("%.1f%s", curr-0.0499, s) } else if curr < 1000 { return fmt.Sprintf("%d%s", int(curr), s) } @@ -98,7 +101,6 @@ func max(a, b int) int { return b } -// // We don't need no generic code // We don't need no thought control // No dark templates in compiler @@ -108,4 +110,3 @@ func max(a, b int) int { // All in all you're just another brick in the code // // -- Pink Trolled -- -// diff --git a/misc_test.go b/misc_test.go new file mode 100644 index 00000000..7995840e --- /dev/null +++ b/misc_test.go @@ -0,0 +1,85 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestIsRoot(t *testing.T) { + if !isRoot("/") { + t.Errorf(`"/" is root`) + } + + paths := []string{ + "", + "~", + "foo", + "foo/bar", + "foo/bar", + "/home", + "/home/user", + } + + for _, p := range paths { + if isRoot(p) { + t.Errorf("'%s' is not root", p) + } + } +} + +func TestHumanize(t *testing.T) { + nums := []struct { + i int64 + s string + }{ + {0, "0"}, + {9, "9"}, + {99, "99"}, + {999, "999"}, + {1000, "1.0K"}, + {1023, "1.0K"}, + {1025, "1.0K"}, + {1049, "1.0K"}, + {1050, "1.0K"}, + {1099, "1.0K"}, + {9999, "9.9K"}, + {10000, "10K"}, + {10100, "10K"}, + {10500, "10K"}, + {1000000, "1.0M"}, + } + + for _, num := range nums { + if h := humanize(num.i); h != num.s { + t.Errorf("at input '%d' expected '%s' but got '%s'", num.i, num.s, h) + } + } +} + +func TestExtractNums(t *testing.T) { + names := []struct { + s string + nums []int + rest []string + numFirst bool + }{ + {"foo123bar456", []int{123, 456}, []string{"foo", "bar"}, false}, + {"123foo456bar", []int{123, 456}, []string{"foo", "bar"}, true}, + {"a-1-1.txt", []int{1, 1}, []string{"a-", "-", ".txt"}, false}, + {"a-1-10.txt", []int{1, 10}, []string{"a-", "-", ".txt"}, false}, + {"a-10-1.txt", []int{10, 1}, []string{"a-", "-", ".txt"}, false}, + } + + for _, name := range names { + nums, rest, numFirst := extractNums(name.s) + if !reflect.DeepEqual(nums, name.nums) { + t.Errorf("at input '%s' expected '%v' but got '%v'", name.s, name.nums, nums) + } + if !reflect.DeepEqual(rest, name.rest) { + t.Errorf("at input '%s' expected '%v' but got '%v'", name.s, name.rest, rest) + } + if !numFirst == name.numFirst { + t.Errorf("at input '%s' expected '%t' but got '%t'", name.s, name.numFirst, numFirst) + } + } +}