-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangyile
committed
Dec 17, 2024
1 parent
078ff96
commit 03b9b82
Showing
9 changed files
with
121 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package nulltern | ||
|
||
func PP[T any](a *T, b *T) *T { | ||
if a != nil { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func PF[T any](a *T, b func() *T) *T { | ||
if a != nil { | ||
return a | ||
} | ||
return b() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package nulltern_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yyle88/tern/nulltern" | ||
) | ||
|
||
type Example[T comparable] struct{ a T } | ||
|
||
func newExample[T comparable](a T) *Example[T] { | ||
return &Example[T]{a: a} | ||
} | ||
|
||
func compare[T comparable](t *testing.T, value T, example *Example[T]) { | ||
require.Equal(t, value, example.a) | ||
} | ||
|
||
func TestPP(t *testing.T) { | ||
compare(t, 1, nulltern.PP(nil, newExample(1))) | ||
compare(t, 1, nulltern.PP(newExample(1), newExample(2))) | ||
compare(t, "a", nulltern.PP(nil, newExample("a"))) | ||
compare(t, "a", nulltern.PP(newExample("a"), newExample("b"))) | ||
} | ||
|
||
func TestPF(t *testing.T) { | ||
compare(t, 1, nulltern.PF(nil, func() *Example[int] { | ||
return newExample(1) | ||
})) | ||
compare(t, 1, nulltern.PF(newExample(1), func() *Example[int] { | ||
return newExample(2) | ||
})) | ||
compare(t, "a", nulltern.PF(nil, func() *Example[string] { | ||
return newExample("a") | ||
})) | ||
compare(t, "a", nulltern.PF(newExample("a"), func() *Example[string] { | ||
return newExample("b") | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package tern | ||
package tern_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yyle88/tern" | ||
) | ||
|
||
func TestZero(t *testing.T) { | ||
res := Zero[int]() | ||
t.Log(res) | ||
require.Equal(t, 0, tern.Zero[int]()) | ||
require.Equal(t, "", tern.Zero[string]()) | ||
require.Equal(t, 0.0, tern.Zero[float64]()) | ||
require.Equal(t, rune(0), tern.Zero[rune]()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.