github.com/decred/dcrd/blockchain@v1.2.1/timesorter_test.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2015-2018 The Decred developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package blockchain
     7  
     8  import (
     9  	"reflect"
    10  	"sort"
    11  	"testing"
    12  )
    13  
    14  // TestTimeSorter tests the timeSorter implementation.
    15  func TestTimeSorter(t *testing.T) {
    16  	tests := []struct {
    17  		in   []int64
    18  		want []int64
    19  	}{
    20  		{
    21  			in: []int64{
    22  				1351228575, // Fri Oct 26 05:16:15 UTC 2012 (Block #205000)
    23  				1348310759, // Sat Sep 22 10:45:59 UTC 2012 (Block #200000)
    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  				1349492104, // Sat Oct  6 02:55:04 UTC 2012 (Block #202000)
    27  			},
    28  			want: []int64{
    29  				1305758502, // Wed May 18 22:41:42 UTC 2011 (Block #125000)
    30  				1347777156, // Sun Sep 16 06:32:36 UTC 2012 (Block #199000)
    31  				1348310759, // Sat Sep 22 10:45:59 UTC 2012 (Block #200000)
    32  				1349492104, // Sat Oct  6 02:55:04 UTC 2012 (Block #202000)
    33  				1351228575, // Fri Oct 26 05:16:15 UTC 2012 (Block #205000)
    34  			},
    35  		},
    36  	}
    37  
    38  	for i, test := range tests {
    39  		result := make([]int64, len(test.in))
    40  		copy(result, test.in)
    41  		sort.Sort(timeSorter(result))
    42  		if !reflect.DeepEqual(result, test.want) {
    43  			t.Errorf("timeSorter #%d got %v want %v", i, result,
    44  				test.want)
    45  			continue
    46  		}
    47  	}
    48  }