From 73844ae02215ca5a100c2458917005c739e4c294 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 27 Nov 2012 22:04:04 +0000 Subject: [PATCH] Factor mtime functions info github.com/ncw/swift --- swiftsync.go | 54 ----------------------- swiftsync_test.go | 106 +--------------------------------------------- 2 files changed, 2 insertions(+), 158 deletions(-) diff --git a/swiftsync.go b/swiftsync.go index dbd6744fc..a359cf81d 100644 --- a/swiftsync.go +++ b/swiftsync.go @@ -11,8 +11,6 @@ import ( "os" "path/filepath" "runtime/pprof" - "strconv" - "strings" ) // Globals @@ -101,58 +99,6 @@ func walk(root string) FsObjects { return files } -// Turns a number of ns into a floating point string in seconds -// -// Trims trailing zeros and guaranteed to be perfectly accurate -func nsToFloatString(ns int64) string { - if ns < 0 { - return "-" + nsToFloatString(-ns) - } - result := fmt.Sprintf("%010d", ns) - split := len(result) - 9 - result, decimals := result[:split], result[split:] - decimals = strings.TrimRight(decimals, "0") - if decimals != "" { - result += "." - result += decimals - } - return result -} - -// Turns a floating point string in seconds into a ns integer -// -// Guaranteed to be perfectly accurate -func floatStringToNs(s string) (ns int64, err error) { - if s != "" && s[0] == '-' { - ns, err = floatStringToNs(s[1:]) - return -ns, err - } - point := strings.IndexRune(s, '.') - if point >= 0 { - tail := s[point+1:] - if len(tail) > 0 { - if len(tail) > 9 { - tail = tail[:9] - } - uns, err := strconv.ParseUint(tail, 10, 64) - if err != nil { - return 0, err - } - ns = int64(uns) - for i := 9 - len(tail); i > 0; i-- { - ns *= 10 - } - } - s = s[:point] - } - secs, err := strconv.ParseInt(s, 10, 64) - if err != nil { - return 0, err - } - ns += int64(1000000000) * secs - return ns, nil -} - // syntaxError prints the syntax func syntaxError() { fmt.Fprintf(os.Stderr, `Sync files and directores to and from swift diff --git a/swiftsync_test.go b/swiftsync_test.go index a453b5f10..347cf605b 100644 --- a/swiftsync_test.go +++ b/swiftsync_test.go @@ -5,108 +5,6 @@ import ( "testing" ) -func TestNsToFloatString(t *testing.T) { - for _, d := range []struct { - ns int64 - fs string - }{ - {0, "0"}, - {1, "0.000000001"}, - {1000, "0.000001"}, - {1000000, "0.001"}, - {100000000, "0.1"}, - {1000000000, "1"}, - {10000000000, "10"}, - {12345678912, "12.345678912"}, - {12345678910, "12.34567891"}, - {12345678900, "12.3456789"}, - {12345678000, "12.345678"}, - {12345670000, "12.34567"}, - {12345600000, "12.3456"}, - {12345000000, "12.345"}, - {12340000000, "12.34"}, - {12300000000, "12.3"}, - {12000000000, "12"}, - {10000000000, "10"}, - {1347717491123123123, "1347717491.123123123"}, - } { - if nsToFloatString(d.ns) != d.fs { - t.Error("Failed", d.ns, "!=", d.fs) - } - if d.ns > 0 && nsToFloatString(-d.ns) != "-"+d.fs { - t.Error("Failed on negative", d.ns, "!=", d.fs) - } - } -} - -func TestFloatStringToNs(t *testing.T) { - for _, d := range []struct { - ns int64 - fs string - }{ - {0, "0"}, - {0, "0."}, - {0, "0.0"}, - {0, "0.0000000001"}, - {1, "0.000000001"}, - {1000, "0.000001"}, - {1000000, "0.001"}, - {100000000, "0.1"}, - {100000000, "0.10"}, - {100000000, "0.1000000001"}, - {1000000000, "1"}, - {1000000000, "1."}, - {1000000000, "1.0"}, - {10000000000, "10"}, - {12345678912, "12.345678912"}, - {12345678912, "12.3456789129"}, - {12345678912, "12.34567891299"}, - {12345678910, "12.34567891"}, - {12345678900, "12.3456789"}, - {12345678000, "12.345678"}, - {12345670000, "12.34567"}, - {12345600000, "12.3456"}, - {12345000000, "12.345"}, - {12340000000, "12.34"}, - {12300000000, "12.3"}, - {12000000000, "12"}, - {10000000000, "10"}, - // This is a typical value which has more bits in than a float64 - {1347717491123123123, "1347717491.123123123"}, - } { - ns, err := floatStringToNs(d.fs) - if err != nil { - t.Error("Failed conversion", err) - } - if ns != d.ns { - t.Error("Failed", d.fs, "!=", d.ns, "was", ns) - } - if d.ns > 0 { - ns, err := floatStringToNs("-" + d.fs) - if err != nil { - t.Error("Failed conversion", err) - } - if ns != -d.ns { - t.Error("Failed on negative", -d.ns, "!=", "-"+d.fs) - } - } - } - - // These are expected to produce errors - for _, fs := range []string{ - "", - ".0", - " 1", - "- 1", - "- 1", - "1.-1", - "1.0.0", - "1x0", - } { - ns, err := floatStringToNs(fs) - if err == nil { - t.Error("Didn't produce expected error", fs, ns) - } - } - +func TestFIXME(t *testing.T) { + // FIXME test something! }