github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/ppln/v2/common_test.go (about)

     1  package ppln
     2  
     3  import (
     4  	"slices"
     5  	"testing"
     6  )
     7  
     8  func TestRangeInput(t *testing.T) {
     9  	tests := []struct {
    10  		from, to int
    11  		want     []int
    12  	}{
    13  		{0, 5, []int{0, 1, 2, 3, 4}},
    14  		{-2, 3, []int{-2, -1, 0, 1, 2}},
    15  		{0, 0, nil},
    16  		{1, 1, nil},
    17  	}
    18  	for _, test := range tests {
    19  		var got []int
    20  		for i, err := range RangeInput(test.from, test.to) {
    21  			if err != nil {
    22  				t.Fatalf("RangeInput(%d,%d) returned error: %v",
    23  					test.from, test.to, err)
    24  			}
    25  			got = append(got, i)
    26  		}
    27  		if !slices.Equal(got, test.want) {
    28  			t.Fatalf("RangeInput(%d,%d)=%v, want %v",
    29  				test.from, test.to, got, test.want)
    30  		}
    31  	}
    32  }
    33  
    34  func TestSliceInput(t *testing.T) {
    35  	want := []int{3, 5, 1, 7}
    36  	var got []int
    37  	for i, err := range SliceInput(slices.Clone(want)) {
    38  		if err != nil {
    39  			t.Fatalf("SliceInput(%v) returned error: %v",
    40  				want, err)
    41  		}
    42  		got = append(got, i)
    43  	}
    44  	if !slices.Equal(got, want) {
    45  		t.Fatalf("SliceInput(%v)=%v, want %v",
    46  			want, got, want)
    47  	}
    48  }