timesorter_test.go raw

   1  package blockchain
   2  
   3  import (
   4  	"reflect"
   5  	"sort"
   6  	"testing"
   7  )
   8  
   9  // TestTimeSorter tests the timeSorter implementation.
  10  func TestTimeSorter(t *testing.T) {
  11  	tests := []struct {
  12  		in   []int64
  13  		want []int64
  14  	}{
  15  		{
  16  			in: []int64{
  17  				1351228575, // Fri Oct 26 05:16:15 UTC 2012 (Block #205000)
  18  				1348310759, // Sat Sep 22 10:45:59 UTC 2012 (Block #200000)
  19  				1305758502, // Wed May 18 22:41:42 UTC 2011 (Block #125000)
  20  				1347777156, // Sun Sep 16 06:32:36 UTC 2012 (Block #199000)
  21  				1349492104, // Sat Oct  6 02:55:04 UTC 2012 (Block #202000)
  22  			},
  23  			want: []int64{
  24  				1305758502, // Wed May 18 22:41:42 UTC 2011 (Block #125000)
  25  				1347777156, // Sun Sep 16 06:32:36 UTC 2012 (Block #199000)
  26  				1348310759, // Sat Sep 22 10:45:59 UTC 2012 (Block #200000)
  27  				1349492104, // Sat Oct  6 02:55:04 UTC 2012 (Block #202000)
  28  				1351228575, // Fri Oct 26 05:16:15 UTC 2012 (Block #205000)
  29  			},
  30  		},
  31  	}
  32  	for i, test := range tests {
  33  		result := make([]int64, len(test.in))
  34  		copy(result, test.in)
  35  		sort.Sort(timeSorter(result))
  36  		if !reflect.DeepEqual(result, test.want) {
  37  			t.Errorf("timeSorter #%d got %v want %v", i, result,
  38  				test.want,
  39  			)
  40  			continue
  41  		}
  42  	}
  43  }
  44