github.com/primecitizens/pcz/std@v0.2.1/core/iter/slice_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package iter
     5  
     6  import "testing"
     7  
     8  var (
     9  	_ Interface[int, SliceIter[int]] = SliceIter[int]{}
    10  )
    11  
    12  func TestSliceIter(t *testing.T) {
    13  	iter := Slice([]string{"0", "1", "2", "3"})
    14  
    15  	if x := iter.Len(); x != 4 {
    16  		t.Errorf("want 4, got %d", x)
    17  	}
    18  
    19  	elem, ok := iter.Nth(-1)
    20  	if !ok || elem != "3" {
    21  		t.Errorf("want (3, true), got (%q, %v)", elem, ok)
    22  	}
    23  
    24  	elem, ok = iter.Nth(3)
    25  	if !ok || elem != "3" {
    26  		t.Errorf("want (3, true), got (%q, %v)", elem, ok)
    27  	}
    28  
    29  	elem, ok = iter.SliceFrom(-2).Nth(0)
    30  	if !ok || elem != "3" {
    31  		t.Errorf("want (3, true), got (%q, %v)", elem, ok)
    32  	}
    33  
    34  	elem, ok = iter.SliceFrom(2).Nth(1)
    35  	if !ok || elem != "3" {
    36  		t.Errorf("want (3, true), got (%q, %v)", elem, ok)
    37  	}
    38  }
    39  
    40  func TestSliceReverseIter(t *testing.T) {
    41  	iter := SliceReverse([]string{"0", "1", "2", "3"})
    42  	// iter: 3, 2, 1, 0
    43  
    44  	if x := iter.Len(); x != 4 {
    45  		t.Errorf("want 4, got %d", x)
    46  	}
    47  
    48  	elem, ok := iter.Nth(-1)
    49  	if !ok || elem != "0" {
    50  		t.Errorf("want (0, true), got (%q, %v)", elem, ok)
    51  	}
    52  
    53  	elem, ok = iter.Nth(3)
    54  	if !ok || elem != "0" {
    55  		t.Errorf("want (0, true), got (%q, %v)", elem, ok)
    56  	}
    57  
    58  	elem, ok = iter.SliceFrom(-2).Nth(0)
    59  	if !ok || elem != "2" {
    60  		t.Errorf("want (2, true), got (%q, %v)", elem, ok)
    61  	}
    62  
    63  	elem, ok = iter.SliceFrom(2).Nth(1)
    64  	if !ok || elem != "0" {
    65  		t.Errorf("want (0, true), got (%q, %v)", elem, ok)
    66  	}
    67  }