github.com/btcsuite/btcd@v0.24.0/blockchain/timesorter_test.go (about)

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