Skip to content

Commit

Permalink
minor refactor and tests change
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Koss committed Jan 27, 2024
1 parent d67b56c commit b2f66a6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 53 deletions.
17 changes: 15 additions & 2 deletions internal/internalpipe/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestAny(t *testing.T) {
}).Gen(len(a100k))
s := p.Any()
require.NotNil(t, s)
require.Greater(t, 90_000.0, pointer.From(s))
require.Greater(t, 90_000.0, pointer.Deref(s))
})

t.Run("Seven thread limit", func(t *testing.T) {
Expand All @@ -66,7 +66,7 @@ func TestAny(t *testing.T) {
}).Gen(len(a100k)).Parallel(7)
s := p.Any()
require.NotNil(t, s)
require.Greater(t, 90_000.0, pointer.From(s))
require.Greater(t, 90_000.0, pointer.Deref(s))
})

t.Run("Single thread NF limit", func(t *testing.T) {
Expand Down Expand Up @@ -131,4 +131,17 @@ func TestAny(t *testing.T) {
require.NotNil(t, s)
require.Equal(t, 90_001., *s)
})

t.Run("Ten thread bounded no limit filter", func(t *testing.T) {
p := Func(func(i int) (float64, bool) {
if i >= len(a100k) {
return 0., false
}
return a100k[i], a100k[i] > 90_000.0 && a100k[i] < 190_000.0
}).Filter(func(x *float64) bool { return int(*x)%2 == 0 }).Parallel(10)
s := p.Any()
require.NotNil(t, s)
require.Greater(t, *s, 90_000.)
require.Less(t, *s, 190_001.)
})
}
18 changes: 9 additions & 9 deletions internal/internalpipe/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_Filter(t *testing.T) {
}

res := p.Filter(func(x *int) bool {
return pointer.From(x)%2 == 0
return pointer.Deref(x)%2 == 0
}).Do()
j := 0
for i := 0; i < 100_000; i += 2 {
Expand All @@ -41,7 +41,7 @@ func Test_Filter(t *testing.T) {
}

res := p.Filter(func(x *int) bool {
return pointer.From(x)%2 == 0
return pointer.Deref(x)%2 == 0
}).Do()
j := 0
for i := 0; i < 100_000; i += 2 {
Expand All @@ -50,7 +50,7 @@ func Test_Filter(t *testing.T) {
}
})
t.Run("single thread even numbers empty res filter", func(t *testing.T) {
pts := pointer.To(7)
pts := pointer.Ref(7)
p := Pipe[int]{
Fn: func(i int) (*int, bool) {
return pts, false
Expand All @@ -61,12 +61,12 @@ func Test_Filter(t *testing.T) {
}

res := p.Filter(func(x *int) bool {
return pointer.From(x)%2 == 0
return pointer.Deref(x)%2 == 0
}).Do()
require.Equal(t, 0, len(res))
})
t.Run("seven thread even numbers empty res filter", func(t *testing.T) {
pts := pointer.To(7)
pts := pointer.Ref(7)
p := Pipe[int]{
Fn: func(i int) (*int, bool) {
return pts, false
Expand All @@ -77,12 +77,12 @@ func Test_Filter(t *testing.T) {
}

res := p.Filter(func(x *int) bool {
return pointer.From(x)%2 == 0
return pointer.Deref(x)%2 == 0
}).Do()
require.Equal(t, 0, len(res))
})
t.Run("seven thread even numbers empty res double filter", func(t *testing.T) {
pts := pointer.To(7)
pts := pointer.Ref(7)
p := Pipe[int]{
Fn: func(i int) (*int, bool) {
return pts, false
Expand All @@ -93,9 +93,9 @@ func Test_Filter(t *testing.T) {
}

res := p.Filter(func(x *int) bool {
return pointer.From(x)%2 == 0
return pointer.Deref(x)%2 == 0
}).Filter(func(x *int) bool {
return pointer.From(x)%2 == 0
return pointer.Deref(x)%2 == 0
}).Do()
require.Equal(t, 0, len(res))
})
Expand Down
8 changes: 4 additions & 4 deletions internal/internalpipe/sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_Sum(t *testing.T) {
t.Run("Single thread empty", func(t *testing.T) {
p := Pipe[float64]{
Fn: func(i int) (*float64, bool) {
return pointer.To(1.), false
return pointer.Ref(1.), false
},
Len: 0,
ValLim: -1,
Expand All @@ -66,7 +66,7 @@ func Test_Sum(t *testing.T) {
t.Run("Quadro thread empty", func(t *testing.T) {
p := Pipe[float64]{
Fn: func(i int) (*float64, bool) {
return pointer.To(1.), false
return pointer.Ref(1.), false
},
Len: 0,
ValLim: -1,
Expand All @@ -83,7 +83,7 @@ func Test_Sum(t *testing.T) {
t.Run("Single thread 1 elem", func(t *testing.T) {
p := Pipe[float64]{
Fn: func(i int) (*float64, bool) {
return pointer.To(100500.), i != 0
return pointer.Ref(100500.), i != 0
},
Len: 1,
ValLim: -1,
Expand All @@ -100,7 +100,7 @@ func Test_Sum(t *testing.T) {
t.Run("Quadro thread 1 elem", func(t *testing.T) {
p := Pipe[float64]{
Fn: func(i int) (*float64, bool) {
return pointer.To(100500.), i != 0
return pointer.Ref(100500.), i != 0
},
Len: 1,
ValLim: -1,
Expand Down
4 changes: 2 additions & 2 deletions internal/primitive/pointer/pointer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package pointer

func To[T any](x T) *T {
func Ref[T any](x T) *T {
return &x
}

func From[T any](x *T) (res T) {
func Deref[T any](x *T) (res T) {
if x == nil {
return
}
Expand Down
12 changes: 6 additions & 6 deletions internal/primitive/pointer/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ func Test_Pointer(t *testing.T) {
t.Parallel()

t.Run("To", func(t *testing.T) {
require.Equal(t, 10, *To(10))
require.Equal(t, 10., *To(10.))
require.Equal(t, 10, *Ref(10))
require.Equal(t, 10., *Ref(10.))
s := struct{ a, b int }{1, 2}
require.Equal(t, s, *To(s))
require.Equal(t, s, *Ref(s))
})

t.Run("From", func(t *testing.T) {
require.Equal(t, 10, From(To(10)))
require.Equal(t, 10., From(To(10.)))
require.Equal(t, 0, From[int](nil))
require.Equal(t, 10, Deref(Ref(10)))
require.Equal(t, 10., Deref(Ref(10.)))
require.Equal(t, 0, Deref[int](nil))
})
}
6 changes: 3 additions & 3 deletions pkg/pipe/pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,13 @@ func TestFilter(t *testing.T) {
if i%10 == 0 {
return nil, true
}
return pointer.To(float64(i)), true
return pointer.Ref(float64(i)), true
}

s := pipe.MapNL(
pipe.Func(genFunc).
Filter(pipies.NotNil[*float64]),
pointer.From[float64],
pointer.Deref[float64],
).Take(10_000).Sum(pipies.Sum[float64])
require.NotNil(t, s)

Expand All @@ -394,7 +394,7 @@ func TestFilter(t *testing.T) {
}
return nil
}).Filter(pipies.NotNil[*float64]),
pointer.From[float64],
pointer.Deref[float64],
).Sum(pipies.Sum[float64])
require.Equal(t, float64(sm), ss)
}
Expand Down
54 changes: 27 additions & 27 deletions pkg/pipies/pipies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Test_Predicates(t *testing.T) {

t.Run("NotNil", func(t *testing.T) {
t.Parallel()
require.True(t, NotNil(pointer.To(1)))
require.True(t, NotNil(pointer.Ref(1)))
require.False(t, NotNil[int](nil))
require.False(t, NotNil[int](nil))
require.False(t, NotNil[testT](nil))
Expand All @@ -29,7 +29,7 @@ func Test_Predicates(t *testing.T) {
})
t.Run("IsNil", func(t *testing.T) {
t.Parallel()
require.False(t, IsNil(pointer.To(1)))
require.False(t, IsNil(pointer.Ref(1)))
require.True(t, IsNil[int](nil))
require.True(t, IsNil[int](nil))
require.True(t, IsNil[testT](nil))
Expand All @@ -42,9 +42,9 @@ func Test_Predicates(t *testing.T) {
})
t.Run("NotZero", func(t *testing.T) {
t.Parallel()
require.True(t, NotZero(pointer.To(1)))
require.False(t, NotZero(pointer.To(0)))
require.False(t, NotZero(pointer.To[float32](0.0)))
require.True(t, NotZero(pointer.Ref(1)))
require.False(t, NotZero(pointer.Ref(0)))
require.False(t, NotZero(pointer.Ref[float32](0.0)))
require.False(t, NotZero(&struct{ a, b, c int }{}))
require.True(t, NotZero(&struct{ a, b, c int }{1, 0, 0}))
})
Expand All @@ -56,50 +56,50 @@ func Test_PredicateBuilders(t *testing.T) {
t.Run("Eq", func(t *testing.T) {
t.Parallel()
eq5 := Eq(5)
require.True(t, eq5(pointer.To(5)))
require.False(t, eq5(pointer.To(4)))
require.True(t, eq5(pointer.Ref(5)))
require.False(t, eq5(pointer.Ref(4)))
eqS := Eq("test")
require.True(t, eqS(pointer.To("test")))
require.False(t, eqS(pointer.To("sett")))
require.True(t, eqS(pointer.Ref("test")))
require.False(t, eqS(pointer.Ref("sett")))
})

t.Run("NotEq", func(t *testing.T) {
t.Parallel()
neq5 := NotEq(5)
require.False(t, neq5(pointer.To(5)))
require.True(t, neq5(pointer.To(4)))
require.False(t, neq5(pointer.Ref(5)))
require.True(t, neq5(pointer.Ref(4)))
neqS := NotEq("test")
require.False(t, neqS(pointer.To("test")))
require.True(t, neqS(pointer.To("sett")))
require.False(t, neqS(pointer.Ref("test")))
require.True(t, neqS(pointer.Ref("sett")))
})

t.Run("LessThan", func(t *testing.T) {
t.Parallel()
lt5 := LessThan(5)
require.True(t, lt5(pointer.To(4)))
require.False(t, lt5(pointer.To(5)))
require.False(t, lt5(pointer.To(6)))
require.True(t, lt5(pointer.Ref(4)))
require.False(t, lt5(pointer.Ref(5)))
require.False(t, lt5(pointer.Ref(6)))
ltf5 := LessThan(5.0)
require.True(t, ltf5(pointer.To(4.999)))
require.False(t, ltf5(pointer.To(float64(int(5)))))
require.False(t, ltf5(pointer.To(5.01)))
require.True(t, ltf5(pointer.Ref(4.999)))
require.False(t, ltf5(pointer.Ref(float64(int(5)))))
require.False(t, ltf5(pointer.Ref(5.01)))
})
}

func Test_Comparator(t *testing.T) {
t.Parallel()
require.True(t, Less(pointer.To(4), pointer.To(5)))
require.False(t, Less(pointer.To(5), pointer.To(5)))
require.False(t, Less(pointer.To(6), pointer.To(5)))
require.True(t, Less(pointer.To(4.999), pointer.To(5.0)))
require.False(t, Less(pointer.To(float64(int(5))), pointer.To(5.0)))
require.False(t, Less(pointer.To(5.01), pointer.To(5.0)))
require.True(t, Less(pointer.Ref(4), pointer.Ref(5)))
require.False(t, Less(pointer.Ref(5), pointer.Ref(5)))
require.False(t, Less(pointer.Ref(6), pointer.Ref(5)))
require.True(t, Less(pointer.Ref(4.999), pointer.Ref(5.0)))
require.False(t, Less(pointer.Ref(float64(int(5))), pointer.Ref(5.0)))
require.False(t, Less(pointer.Ref(5.01), pointer.Ref(5.0)))
}

func Test_Accum(t *testing.T) {
t.Parallel()
require.Equal(t, Sum(pointer.To(10), pointer.To(20)), 30)
require.Equal(t, Sum(pointer.To(10.0), pointer.To(20.0)), 30.0)
require.Equal(t, Sum(pointer.Ref(10), pointer.Ref(20)), 30)
require.Equal(t, Sum(pointer.Ref(10.0), pointer.Ref(20.0)), 30.0)
}

func Test_Not(t *testing.T) {
Expand Down

0 comments on commit b2f66a6

Please sign in to comment.