github.com/m3db/m3@v1.5.0/src/x/time/range_iter_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package time 22 23 import ( 24 "container/list" 25 "testing" 26 "time" 27 28 "github.com/stretchr/testify/require" 29 ) 30 31 var ( 32 testTimeRanges = []Range{ 33 {Start: testStart, End: testStart.Add(time.Second)}, 34 {Start: testStart.Add(2 * time.Second), End: testStart.Add(10 * time.Second)}, 35 {Start: testStart.Add(20 * time.Second), End: testStart.Add(25 * time.Second)}, 36 } 37 ) 38 39 func getTestList() *list.List { 40 l := list.New() 41 for _, r := range testTimeRanges { 42 l.PushBack(r) 43 } 44 return l 45 } 46 47 func TestRangeIter(t *testing.T) { 48 it := newRangeIter(nil) 49 require.False(t, it.Next()) 50 51 it = newRangeIter(getTestList()) 52 for i := 0; i < len(testTimeRanges); i++ { 53 require.True(t, it.Next()) 54 require.Equal(t, testTimeRanges[i], it.Value()) 55 } 56 require.False(t, it.Next()) 57 58 } 59 60 func TestRangeIterEOF(t *testing.T) { 61 it := newRangeIter(nil) 62 require.False(t, it.Next()) 63 64 it = newRangeIter(getTestList()) 65 var i int 66 for it.Next() { 67 require.Equal(t, testTimeRanges[i], it.Value()) 68 i++ 69 } 70 // Next() always returns falsy after hitting eof. 71 require.False(t, it.Next()) 72 }