Skip to content

Instantly share code, notes, and snippets.

@WonderfulSoap
Last active March 22, 2022 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WonderfulSoap/a65747d4296af7ca09e6703ff6e9afbb to your computer and use it in GitHub Desktop.
Save WonderfulSoap/a65747d4296af7ca09e6703ff6e9afbb to your computer and use it in GitHub Desktop.
some generics function unit test examples
package main
import (
"reflect"
"testing"
)
type addAble interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float64 | ~float32 | ~string
}
// ge
func AddGeneric[T addAble](a, b) T {
return a + b
}
type addGenericTestCase[T canAdd] struct {
name string
a T
b T
want T
}
func runAddGenericTestCases[T canAdd](t *testing.T, cases []addGenericTestCase[T]) {
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if got := AddGeneric(tt.a, tt.b); got != tt.want {
t.Errorf("error info.......")
}
})
}
}
func TestAddGeneric(t *testing.T) {
intCases := []addGenericTestCase[int]{
{
name: "ok",
a: 1,
b: 1,
want: 2,
},
{
name: "ok2",
a: 10,
b: 10,
want: 20,
},
}
strCases := []addGenericTestCase[string]{
{
name: "ok",
a: "A",
b: "B",
want: "AB",
},
{
name: "ok2",
a: "Hello",
b: "World",
want: "HelloWorld",
},
}
float32Cases := []addGenericTestCase[float32]{
{
name: "ok",
a: 1.0,
b: 2.0,
want: 3.0,
},
}
runAddGenericTestCases(t, intCases)
runAddGenericTestCases(t, strCases)
runAddGenericTestCases(t, float32Cases)
}
func TestAddGeneric(t *testing.T) {
type testCase[T constraints.Ordered] struct {
name string
a T
b T
want T
}
var cases = []interface{}{
testCase[int]{
name: "int-ok",
a: 1,
b: 1,
want: 2,
},
testCase[int]{
name: "int-ok",
a: 10,
b: 10,
want: 20,
},
testCase[string]{
name: "str-ok",
a: "A",
b: "B",
want: "AB",
},
testCase[string]{
name: "str-ok2",
a: "Hello",
b: "World",
want: "HelloWorld",
},
}
for _, tt := range cases {
switch c := tt.(type) {
case testCase[int]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[uint]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[int8]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[uint8]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[int16]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[uint16]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[int32]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[uint32]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[int64]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[uint64]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[float32]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[float64]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
case testCase[string]:
t.Run(c.name, func(t *testing.T) {
if got := AddGeneric(c.a, c.b); !reflect.DeepEqual(got, c.want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment