diff --git a/.travis.yml b/.travis.yml index baeb288..39c2911 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: go go: - - 1.14.x - 1.15.x + - 1.16.x before_install: - go get -t -v ./... diff --git a/cmd/generate/gen_x_seq.go.tmpl b/cmd/generate/gen_x_seq.go.tmpl index 20a6f41..13b266e 100644 --- a/cmd/generate/gen_x_seq.go.tmpl +++ b/cmd/generate/gen_x_seq.go.tmpl @@ -295,7 +295,14 @@ func (s *{{.Name}}Seq) Range(interval Interval) *{{.Name}}Seq { return new{{.Name}}Seq(slice) } -// Range returns a *{{.Name}}Seq without elements which make fn returns true +// Slice returns a sub *{{.Name}}Seq with specified index +func (s *{{.Name}}Seq) Slice(i, j int) *{{.Name}}Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return new{{.Name}}Seq(slice) +} + +// Trim returns a *{{.Name}}Seq without elements which make fn returns true func (s *{{.Name}}Seq) Trim(fn func(i int, v {{.Name}}) bool) *{{.Name}}Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/cmd/generate/gen_x_seq_test.go.tmpl b/cmd/generate/gen_x_seq_test.go.tmpl index 835c26e..74ab9af 100644 --- a/cmd/generate/gen_x_seq_test.go.tmpl +++ b/cmd/generate/gen_x_seq_test.go.tmpl @@ -46,6 +46,7 @@ func Test{{.Name}}Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v {{.Name}}) bool { return false }) @@ -675,6 +676,40 @@ func Test{{.Name}}Seq_Range(t *testing.T) { } } +func Test{{.Name}}Seq_Slice(t *testing.T) { + data := Random{{.Name}}s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data {{.Name}}s + args args + want {{.Name}}s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := new{{.Name}}Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).{{.Name}}s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func Test{{.Name}}Seq_Trim(t *testing.T) { data := Random{{.Name}}s(10) Sort(data) diff --git a/gen_float32_seq.go b/gen_float32_seq.go index 017ff18..9ae8dcb 100644 --- a/gen_float32_seq.go +++ b/gen_float32_seq.go @@ -297,7 +297,14 @@ func (s *Float32Seq) Range(interval Interval) *Float32Seq { return newFloat32Seq(slice) } -// Range returns a *Float32Seq without elements which make fn returns true +// Slice returns a sub *Float32Seq with specified index +func (s *Float32Seq) Slice(i, j int) *Float32Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newFloat32Seq(slice) +} + +// Trim returns a *Float32Seq without elements which make fn returns true func (s *Float32Seq) Trim(fn func(i int, v Float32) bool) *Float32Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_float32_seq_test.go b/gen_float32_seq_test.go index 8b12c6a..def97ac 100644 --- a/gen_float32_seq_test.go +++ b/gen_float32_seq_test.go @@ -48,6 +48,7 @@ func TestFloat32Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Float32) bool { return false }) @@ -677,6 +678,40 @@ func TestFloat32Seq_Range(t *testing.T) { } } +func TestFloat32Seq_Slice(t *testing.T) { + data := RandomFloat32s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Float32s + args args + want Float32s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newFloat32Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Float32s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestFloat32Seq_Trim(t *testing.T) { data := RandomFloat32s(10) Sort(data) diff --git a/gen_float64_seq.go b/gen_float64_seq.go index 5bee833..d4e623e 100644 --- a/gen_float64_seq.go +++ b/gen_float64_seq.go @@ -297,7 +297,14 @@ func (s *Float64Seq) Range(interval Interval) *Float64Seq { return newFloat64Seq(slice) } -// Range returns a *Float64Seq without elements which make fn returns true +// Slice returns a sub *Float64Seq with specified index +func (s *Float64Seq) Slice(i, j int) *Float64Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newFloat64Seq(slice) +} + +// Trim returns a *Float64Seq without elements which make fn returns true func (s *Float64Seq) Trim(fn func(i int, v Float64) bool) *Float64Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_float64_seq_test.go b/gen_float64_seq_test.go index 54a9354..185e444 100644 --- a/gen_float64_seq_test.go +++ b/gen_float64_seq_test.go @@ -48,6 +48,7 @@ func TestFloat64Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Float64) bool { return false }) @@ -677,6 +678,40 @@ func TestFloat64Seq_Range(t *testing.T) { } } +func TestFloat64Seq_Slice(t *testing.T) { + data := RandomFloat64s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Float64s + args args + want Float64s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newFloat64Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Float64s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestFloat64Seq_Trim(t *testing.T) { data := RandomFloat64s(10) Sort(data) diff --git a/gen_int16_seq.go b/gen_int16_seq.go index b7aef86..d645e5c 100644 --- a/gen_int16_seq.go +++ b/gen_int16_seq.go @@ -297,7 +297,14 @@ func (s *Int16Seq) Range(interval Interval) *Int16Seq { return newInt16Seq(slice) } -// Range returns a *Int16Seq without elements which make fn returns true +// Slice returns a sub *Int16Seq with specified index +func (s *Int16Seq) Slice(i, j int) *Int16Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newInt16Seq(slice) +} + +// Trim returns a *Int16Seq without elements which make fn returns true func (s *Int16Seq) Trim(fn func(i int, v Int16) bool) *Int16Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_int16_seq_test.go b/gen_int16_seq_test.go index d606f9f..891f2a3 100644 --- a/gen_int16_seq_test.go +++ b/gen_int16_seq_test.go @@ -48,6 +48,7 @@ func TestInt16Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Int16) bool { return false }) @@ -677,6 +678,40 @@ func TestInt16Seq_Range(t *testing.T) { } } +func TestInt16Seq_Slice(t *testing.T) { + data := RandomInt16s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Int16s + args args + want Int16s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newInt16Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Int16s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestInt16Seq_Trim(t *testing.T) { data := RandomInt16s(10) Sort(data) diff --git a/gen_int32_seq.go b/gen_int32_seq.go index 7e6f1c2..c0c7a81 100644 --- a/gen_int32_seq.go +++ b/gen_int32_seq.go @@ -297,7 +297,14 @@ func (s *Int32Seq) Range(interval Interval) *Int32Seq { return newInt32Seq(slice) } -// Range returns a *Int32Seq without elements which make fn returns true +// Slice returns a sub *Int32Seq with specified index +func (s *Int32Seq) Slice(i, j int) *Int32Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newInt32Seq(slice) +} + +// Trim returns a *Int32Seq without elements which make fn returns true func (s *Int32Seq) Trim(fn func(i int, v Int32) bool) *Int32Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_int32_seq_test.go b/gen_int32_seq_test.go index 7a55133..c545391 100644 --- a/gen_int32_seq_test.go +++ b/gen_int32_seq_test.go @@ -48,6 +48,7 @@ func TestInt32Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Int32) bool { return false }) @@ -677,6 +678,40 @@ func TestInt32Seq_Range(t *testing.T) { } } +func TestInt32Seq_Slice(t *testing.T) { + data := RandomInt32s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Int32s + args args + want Int32s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newInt32Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Int32s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestInt32Seq_Trim(t *testing.T) { data := RandomInt32s(10) Sort(data) diff --git a/gen_int64_seq.go b/gen_int64_seq.go index d3e2dd4..073c846 100644 --- a/gen_int64_seq.go +++ b/gen_int64_seq.go @@ -297,7 +297,14 @@ func (s *Int64Seq) Range(interval Interval) *Int64Seq { return newInt64Seq(slice) } -// Range returns a *Int64Seq without elements which make fn returns true +// Slice returns a sub *Int64Seq with specified index +func (s *Int64Seq) Slice(i, j int) *Int64Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newInt64Seq(slice) +} + +// Trim returns a *Int64Seq without elements which make fn returns true func (s *Int64Seq) Trim(fn func(i int, v Int64) bool) *Int64Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_int64_seq_test.go b/gen_int64_seq_test.go index 973a8a2..64b6158 100644 --- a/gen_int64_seq_test.go +++ b/gen_int64_seq_test.go @@ -48,6 +48,7 @@ func TestInt64Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Int64) bool { return false }) @@ -677,6 +678,40 @@ func TestInt64Seq_Range(t *testing.T) { } } +func TestInt64Seq_Slice(t *testing.T) { + data := RandomInt64s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Int64s + args args + want Int64s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newInt64Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Int64s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestInt64Seq_Trim(t *testing.T) { data := RandomInt64s(10) Sort(data) diff --git a/gen_int8_seq.go b/gen_int8_seq.go index edb52b0..6a461a4 100644 --- a/gen_int8_seq.go +++ b/gen_int8_seq.go @@ -297,7 +297,14 @@ func (s *Int8Seq) Range(interval Interval) *Int8Seq { return newInt8Seq(slice) } -// Range returns a *Int8Seq without elements which make fn returns true +// Slice returns a sub *Int8Seq with specified index +func (s *Int8Seq) Slice(i, j int) *Int8Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newInt8Seq(slice) +} + +// Trim returns a *Int8Seq without elements which make fn returns true func (s *Int8Seq) Trim(fn func(i int, v Int8) bool) *Int8Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_int8_seq_test.go b/gen_int8_seq_test.go index b34c296..4260f8f 100644 --- a/gen_int8_seq_test.go +++ b/gen_int8_seq_test.go @@ -48,6 +48,7 @@ func TestInt8Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Int8) bool { return false }) @@ -677,6 +678,40 @@ func TestInt8Seq_Range(t *testing.T) { } } +func TestInt8Seq_Slice(t *testing.T) { + data := RandomInt8s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Int8s + args args + want Int8s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newInt8Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Int8s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestInt8Seq_Trim(t *testing.T) { data := RandomInt8s(10) Sort(data) diff --git a/gen_int_seq.go b/gen_int_seq.go index 45e04b5..31a77c6 100644 --- a/gen_int_seq.go +++ b/gen_int_seq.go @@ -297,7 +297,14 @@ func (s *IntSeq) Range(interval Interval) *IntSeq { return newIntSeq(slice) } -// Range returns a *IntSeq without elements which make fn returns true +// Slice returns a sub *IntSeq with specified index +func (s *IntSeq) Slice(i, j int) *IntSeq { + sslice := s.getSlice() + slice := sslice[i:j] + return newIntSeq(slice) +} + +// Trim returns a *IntSeq without elements which make fn returns true func (s *IntSeq) Trim(fn func(i int, v Int) bool) *IntSeq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_int_seq_test.go b/gen_int_seq_test.go index 255165c..4b30c82 100644 --- a/gen_int_seq_test.go +++ b/gen_int_seq_test.go @@ -48,6 +48,7 @@ func TestIntSeq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Int) bool { return false }) @@ -677,6 +678,40 @@ func TestIntSeq_Range(t *testing.T) { } } +func TestIntSeq_Slice(t *testing.T) { + data := RandomInts(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Ints + args args + want Ints + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newIntSeq(data) + if got := s.Slice(tt.args.i, tt.args.j).Ints(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestIntSeq_Trim(t *testing.T) { data := RandomInts(10) Sort(data) diff --git a/gen_uint16_seq.go b/gen_uint16_seq.go index 40a59f7..308a6d5 100644 --- a/gen_uint16_seq.go +++ b/gen_uint16_seq.go @@ -297,7 +297,14 @@ func (s *Uint16Seq) Range(interval Interval) *Uint16Seq { return newUint16Seq(slice) } -// Range returns a *Uint16Seq without elements which make fn returns true +// Slice returns a sub *Uint16Seq with specified index +func (s *Uint16Seq) Slice(i, j int) *Uint16Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newUint16Seq(slice) +} + +// Trim returns a *Uint16Seq without elements which make fn returns true func (s *Uint16Seq) Trim(fn func(i int, v Uint16) bool) *Uint16Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_uint16_seq_test.go b/gen_uint16_seq_test.go index fde61c8..d9036ad 100644 --- a/gen_uint16_seq_test.go +++ b/gen_uint16_seq_test.go @@ -48,6 +48,7 @@ func TestUint16Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Uint16) bool { return false }) @@ -677,6 +678,40 @@ func TestUint16Seq_Range(t *testing.T) { } } +func TestUint16Seq_Slice(t *testing.T) { + data := RandomUint16s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Uint16s + args args + want Uint16s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newUint16Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Uint16s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestUint16Seq_Trim(t *testing.T) { data := RandomUint16s(10) Sort(data) diff --git a/gen_uint32_seq.go b/gen_uint32_seq.go index 046c175..ce85034 100644 --- a/gen_uint32_seq.go +++ b/gen_uint32_seq.go @@ -297,7 +297,14 @@ func (s *Uint32Seq) Range(interval Interval) *Uint32Seq { return newUint32Seq(slice) } -// Range returns a *Uint32Seq without elements which make fn returns true +// Slice returns a sub *Uint32Seq with specified index +func (s *Uint32Seq) Slice(i, j int) *Uint32Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newUint32Seq(slice) +} + +// Trim returns a *Uint32Seq without elements which make fn returns true func (s *Uint32Seq) Trim(fn func(i int, v Uint32) bool) *Uint32Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_uint32_seq_test.go b/gen_uint32_seq_test.go index 581a10c..a50ce01 100644 --- a/gen_uint32_seq_test.go +++ b/gen_uint32_seq_test.go @@ -48,6 +48,7 @@ func TestUint32Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Uint32) bool { return false }) @@ -677,6 +678,40 @@ func TestUint32Seq_Range(t *testing.T) { } } +func TestUint32Seq_Slice(t *testing.T) { + data := RandomUint32s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Uint32s + args args + want Uint32s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newUint32Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Uint32s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestUint32Seq_Trim(t *testing.T) { data := RandomUint32s(10) Sort(data) diff --git a/gen_uint64_seq.go b/gen_uint64_seq.go index 67857bc..557fd1e 100644 --- a/gen_uint64_seq.go +++ b/gen_uint64_seq.go @@ -297,7 +297,14 @@ func (s *Uint64Seq) Range(interval Interval) *Uint64Seq { return newUint64Seq(slice) } -// Range returns a *Uint64Seq without elements which make fn returns true +// Slice returns a sub *Uint64Seq with specified index +func (s *Uint64Seq) Slice(i, j int) *Uint64Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newUint64Seq(slice) +} + +// Trim returns a *Uint64Seq without elements which make fn returns true func (s *Uint64Seq) Trim(fn func(i int, v Uint64) bool) *Uint64Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_uint64_seq_test.go b/gen_uint64_seq_test.go index 290b2a8..a795220 100644 --- a/gen_uint64_seq_test.go +++ b/gen_uint64_seq_test.go @@ -48,6 +48,7 @@ func TestUint64Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Uint64) bool { return false }) @@ -677,6 +678,40 @@ func TestUint64Seq_Range(t *testing.T) { } } +func TestUint64Seq_Slice(t *testing.T) { + data := RandomUint64s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Uint64s + args args + want Uint64s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newUint64Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Uint64s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestUint64Seq_Trim(t *testing.T) { data := RandomUint64s(10) Sort(data) diff --git a/gen_uint8_seq.go b/gen_uint8_seq.go index 468c0ff..a70a78e 100644 --- a/gen_uint8_seq.go +++ b/gen_uint8_seq.go @@ -297,7 +297,14 @@ func (s *Uint8Seq) Range(interval Interval) *Uint8Seq { return newUint8Seq(slice) } -// Range returns a *Uint8Seq without elements which make fn returns true +// Slice returns a sub *Uint8Seq with specified index +func (s *Uint8Seq) Slice(i, j int) *Uint8Seq { + sslice := s.getSlice() + slice := sslice[i:j] + return newUint8Seq(slice) +} + +// Trim returns a *Uint8Seq without elements which make fn returns true func (s *Uint8Seq) Trim(fn func(i int, v Uint8) bool) *Uint8Seq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_uint8_seq_test.go b/gen_uint8_seq_test.go index 5ebc3f3..869c951 100644 --- a/gen_uint8_seq_test.go +++ b/gen_uint8_seq_test.go @@ -48,6 +48,7 @@ func TestUint8Seq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Uint8) bool { return false }) @@ -677,6 +678,40 @@ func TestUint8Seq_Range(t *testing.T) { } } +func TestUint8Seq_Slice(t *testing.T) { + data := RandomUint8s(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Uint8s + args args + want Uint8s + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newUint8Seq(data) + if got := s.Slice(tt.args.i, tt.args.j).Uint8s(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestUint8Seq_Trim(t *testing.T) { data := RandomUint8s(10) Sort(data) diff --git a/gen_uint_seq.go b/gen_uint_seq.go index 43b9671..f60eb6b 100644 --- a/gen_uint_seq.go +++ b/gen_uint_seq.go @@ -297,7 +297,14 @@ func (s *UintSeq) Range(interval Interval) *UintSeq { return newUintSeq(slice) } -// Range returns a *UintSeq without elements which make fn returns true +// Slice returns a sub *UintSeq with specified index +func (s *UintSeq) Slice(i, j int) *UintSeq { + sslice := s.getSlice() + slice := sslice[i:j] + return newUintSeq(slice) +} + +// Trim returns a *UintSeq without elements which make fn returns true func (s *UintSeq) Trim(fn func(i int, v Uint) bool) *UintSeq { sslice := s.getSlice() if fn == nil || len(sslice) == 0 { diff --git a/gen_uint_seq_test.go b/gen_uint_seq_test.go index ab0c10a..845af21 100644 --- a/gen_uint_seq_test.go +++ b/gen_uint_seq_test.go @@ -48,6 +48,7 @@ func TestUintSeq_NilSafe(t *testing.T) { seq.Last() seq.Percentile(0.5) seq.Range(Interval{}) + seq.Slice(0, 0) seq.Trim(func(i int, v Uint) bool { return false }) @@ -677,6 +678,40 @@ func TestUintSeq_Range(t *testing.T) { } } +func TestUintSeq_Slice(t *testing.T) { + data := RandomUints(100) + Sort(data) + + type args struct { + i int + j int + } + tests := []struct { + name string + data Uints + args args + want Uints + }{ + { + name: "regular", + data: data, + args: args{ + i: 1, + j: 3, + }, + want: data[1:3], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := newUintSeq(data) + if got := s.Slice(tt.args.i, tt.args.j).Uints(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Slice() = %v, want %v", got, tt.want) + } + }) + } +} + func TestUintSeq_Trim(t *testing.T) { data := RandomUints(10) Sort(data) diff --git a/go.mod b/go.mod index 8489142..8777b5a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/gochore/timeseq/v2 -go 1.15 +go 1.16