github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgTime/Period2_test.go (about)

     1  package kmgTime
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bronze1man/kmg/kmgTest"
     7  )
     8  
     9  //approach 2
    10  type ScratchPeriodList2 []ScratchPeriod2
    11  
    12  func (p ScratchPeriodList2) Len() int {
    13  	return len(p)
    14  }
    15  func (p ScratchPeriodList2) GetPeriodAtIndex(i int) Period {
    16  	return p[i].Period
    17  }
    18  func (p ScratchPeriodList2) Swap(i, j int) {
    19  	p[i], p[j] = p[j], p[i]
    20  }
    21  
    22  type ScratchPeriod2 struct {
    23  	Period
    24  	ItemList []int //a list of item user can get at this period
    25  }
    26  
    27  func TestPeriodListInterface(ot *testing.T) {
    28  	periodList := ScratchPeriodList2{
    29  		{
    30  			Period:   Period{Start: MustFromMysqlFormat("2001-01-00 23:30:00"), End: MustFromMysqlFormat("2001-01-01 23:30:00")},
    31  			ItemList: []int{1, 2},
    32  		},
    33  		{
    34  			Period:   Period{Start: MustFromMysqlFormat("2001-01-03 23:30:00"), End: MustFromMysqlFormat("2001-01-04 23:30:00")},
    35  			ItemList: []int{2, 3},
    36  		},
    37  		{
    38  			Period:   Period{Start: MustFromMysqlFormat("2001-01-02 23:30:00"), End: MustFromMysqlFormat("2001-01-03 23:30:00")},
    39  			ItemList: []int{3, 4},
    40  		},
    41  	}
    42  
    43  	PeriodListSort(periodList)
    44  	i, exist := SelectPeriodFromSortedPeriodList(MustFromMysqlFormat("2001-01-01 23:00:00"), periodList)
    45  	kmgTest.Equal(exist, true)
    46  	kmgTest.Equal(periodList[i].ItemList, []int{1, 2})
    47  	i, exist = SelectPeriodFromSortedPeriodList(MustFromMysqlFormat("2001-01-03 23:00:00"), periodList)
    48  	kmgTest.Equal(exist, true)
    49  	kmgTest.Equal(periodList[i].ItemList, []int{3, 4})
    50  }