github.com/m3db/m3@v1.5.0/src/x/ident/iterator_test.go (about)

     1  // Copyright (c) 2018 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 ident
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func stringIDs() []string {
    30  	return []string{
    31  		"foo",
    32  		"bar",
    33  		"baz",
    34  	}
    35  }
    36  
    37  func testIDs() []ID {
    38  	raw := stringIDs()
    39  	ids := make([]ID, 0, len(raw))
    40  	for _, id := range raw {
    41  		ids = append(ids, StringID(id))
    42  	}
    43  	return ids
    44  }
    45  
    46  func TestSliceIteratorEmpty(t *testing.T) {
    47  	iter := NewIDSliceIterator([]ID{})
    48  	require.Equal(t, 0, iter.Remaining())
    49  	require.False(t, iter.Next())
    50  }
    51  
    52  func TestSliceIterator(t *testing.T) {
    53  	expected := map[string]struct{}{
    54  		"foo": struct{}{},
    55  		"bar": struct{}{},
    56  		"baz": struct{}{},
    57  	}
    58  	iter := NewIDSliceIterator(testIDs())
    59  	require.Equal(t, len(expected), iter.Remaining())
    60  	for iter.Next() {
    61  		c := iter.Current()
    62  		if _, ok := expected[c.String()]; ok {
    63  			delete(expected, c.String())
    64  			continue
    65  		}
    66  		require.Equal(t, len(expected), iter.Remaining())
    67  		require.Fail(t, "unknown id", c.String())
    68  	}
    69  	require.Empty(t, expected)
    70  	// repeated close should be idempotent
    71  	iter.Close()
    72  	iter.Close()
    73  }
    74  
    75  func TestSliceIteratorDuplicate(t *testing.T) {
    76  	expected := map[string]struct{}{
    77  		"foo": struct{}{},
    78  		"bar": struct{}{},
    79  		"baz": struct{}{},
    80  	}
    81  	iter := NewIDSliceIterator(testIDs())
    82  	clone := iter.Duplicate()
    83  	expectedLen := len(expected)
    84  	require.Equal(t, expectedLen, iter.Remaining())
    85  	require.Equal(t, expectedLen, clone.Remaining())
    86  	defer iter.Close()
    87  	for iter.Next() {
    88  		c := iter.Current()
    89  		if _, ok := expected[c.String()]; ok {
    90  			delete(expected, c.String())
    91  			continue
    92  		}
    93  		require.Equal(t, len(expected), iter.Remaining())
    94  		require.Fail(t, "unknown id", c.String())
    95  	}
    96  	require.Empty(t, expected)
    97  	require.Equal(t, expectedLen, clone.Remaining())
    98  }
    99  
   100  func TestStringSliceIterator(t *testing.T) {
   101  	expected := map[string]struct{}{
   102  		"foo": struct{}{},
   103  		"bar": struct{}{},
   104  		"baz": struct{}{},
   105  	}
   106  	iter := NewStringIDsSliceIterator(stringIDs())
   107  	require.Equal(t, len(expected), iter.Remaining())
   108  	for iter.Next() {
   109  		c := iter.Current()
   110  		if _, ok := expected[c.String()]; ok {
   111  			delete(expected, c.String())
   112  			continue
   113  		}
   114  		require.Equal(t, len(expected), iter.Remaining())
   115  		require.Fail(t, "unknown id", c.String())
   116  	}
   117  	require.Empty(t, expected)
   118  }
   119  func TestStringSliceIteratorDuplicate(t *testing.T) {
   120  	expected := map[string]struct{}{
   121  		"foo": struct{}{},
   122  		"bar": struct{}{},
   123  		"baz": struct{}{},
   124  	}
   125  	iter := NewStringIDsSliceIterator(stringIDs())
   126  	clone := iter.Duplicate()
   127  	expectedLen := len(expected)
   128  	require.Equal(t, expectedLen, iter.Remaining())
   129  	require.Equal(t, expectedLen, clone.Remaining())
   130  	for iter.Next() {
   131  		c := iter.Current()
   132  		if _, ok := expected[c.String()]; ok {
   133  			delete(expected, c.String())
   134  			continue
   135  		}
   136  		require.Equal(t, len(expected), iter.Remaining())
   137  		require.Fail(t, "unknown id", c.String())
   138  	}
   139  	require.Empty(t, expected)
   140  	require.Equal(t, expectedLen, clone.Remaining())
   141  }