From 20f7c2ecd07d5d7ed7ebe5aa47b979627362530f Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 18 Jul 2013 10:06:35 -0500 Subject: [PATCH] Add tests for TimeSorter. --- internal_test.go | 7 +++++++ timesorter_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 timesorter_test.go diff --git a/internal_test.go b/internal_test.go index c5c9a4040..d82fdb033 100644 --- a/internal_test.go +++ b/internal_test.go @@ -13,6 +13,7 @@ package btcchain import ( "github.com/conformal/btcutil" + "time" ) // TstCheckBlockSanity makes the internal checkBlockSanity function available to @@ -26,3 +27,9 @@ func TstCheckBlockSanity(block *btcutil.Block) error { func TstSetCoinbaseMaturity(maturity int64) { coinbaseMaturity = maturity } + +// TstTimeSorter makes the internal timeSorter type available to the test +// package. +func TstTimeSorter(times []time.Time) timeSorter { + return timeSorter(times) +} diff --git a/timesorter_test.go b/timesorter_test.go new file mode 100644 index 000000000..397feae01 --- /dev/null +++ b/timesorter_test.go @@ -0,0 +1,51 @@ +// Copyright (c) 2013 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package btcchain_test + +import ( + "github.com/conformal/btcchain" + "reflect" + "sort" + "testing" + "time" +) + +// TestTimeSorter tests the timeSorter implementation. +func TestTimeSorter(t *testing.T) { + tests := []struct { + in []time.Time + want []time.Time + }{ + { + in: []time.Time{ + time.Unix(1351228575, 0), // Fri Oct 26 05:16:15 UTC 2012 (Block #205000) + time.Unix(1351228575, 1), // Fri Oct 26 05:16:15 UTC 2012 (+1 nanosecond) + time.Unix(1348310759, 0), // Sat Sep 22 10:45:59 UTC 2012 (Block #200000) + time.Unix(1305758502, 0), // Wed May 18 22:41:42 UTC 2011 (Block #125000) + time.Unix(1347777156, 0), // Sun Sep 16 06:32:36 UTC 2012 (Block #199000) + time.Unix(1349492104, 0), // Sat Oct 6 02:55:04 UTC 2012 (Block #202000) + }, + want: []time.Time{ + time.Unix(1305758502, 0), // Wed May 18 22:41:42 UTC 2011 (Block #125000) + time.Unix(1347777156, 0), // Sun Sep 16 06:32:36 UTC 2012 (Block #199000) + time.Unix(1348310759, 0), // Sat Sep 22 10:45:59 UTC 2012 (Block #200000) + time.Unix(1349492104, 0), // Sat Oct 6 02:55:04 UTC 2012 (Block #202000) + time.Unix(1351228575, 0), // Fri Oct 26 05:16:15 UTC 2012 (Block #205000) + time.Unix(1351228575, 1), // Fri Oct 26 05:16:15 UTC 2012 (+1 nanosecond) + }, + }, + } + + for i, test := range tests { + result := make([]time.Time, len(test.in)) + copy(result, test.in) + sort.Sort(btcchain.TstTimeSorter(result)) + if !reflect.DeepEqual(result, test.want) { + t.Errorf("timeSorter #%d got %v want %v", i, result, + test.want) + continue + } + } +}