gitee.com/quant1x/gox@v1.21.2/api/slices_limit_test.go (about)

     1  // Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
     2  
     3  package api
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  type tcase struct {
    10  	Start *int
    11  	End   *int
    12  	ExpN  int
    13  	ExpS  int
    14  	ExpE  int
    15  }
    16  
    17  func TestRange(t *testing.T) {
    18  
    19  	vals := []int{0, 1, 2, 3}
    20  
    21  	N := len(vals)
    22  
    23  	i := func(i int) *int {
    24  		return &i
    25  	}
    26  
    27  	tests := []tcase{
    28  		{
    29  			Start: nil,
    30  			End:   nil,
    31  			ExpN:  4,
    32  			ExpS:  0,
    33  			ExpE:  3,
    34  		},
    35  		{
    36  			Start: i(1),
    37  			End:   i(3),
    38  			ExpN:  3,
    39  			ExpS:  1,
    40  			ExpE:  3,
    41  		},
    42  		{
    43  			Start: nil,
    44  			End:   i(-1),
    45  			ExpN:  4,
    46  			ExpS:  0,
    47  			ExpE:  3,
    48  		},
    49  		{
    50  			Start: nil,
    51  			End:   i(-2),
    52  			ExpN:  3,
    53  			ExpS:  0,
    54  			ExpE:  2,
    55  		},
    56  		{
    57  			Start: i(-3),
    58  			End:   i(-2),
    59  			ExpN:  2,
    60  			ExpS:  1,
    61  			ExpE:  2,
    62  		},
    63  	}
    64  
    65  	for i, tc := range tests {
    66  
    67  		rng := &ScopeLimit{Start: tc.Start, End: tc.End}
    68  
    69  		nrows, err := rng.NRows(N)
    70  		if err != nil {
    71  			panic(err)
    72  		}
    73  		if nrows != tc.ExpN {
    74  			t.Errorf("%d: |got: %v |expected: %v", i, nrows, tc.ExpN)
    75  		}
    76  
    77  		s, e, err := rng.Limits(N)
    78  		if err != nil {
    79  			panic(err)
    80  		}
    81  		if s != tc.ExpS || e != tc.ExpE {
    82  			t.Errorf("%d: |got: %v,%v |expected: %v,%v", i, s, e, tc.ExpS, tc.ExpE)
    83  		}
    84  	}
    85  
    86  }