-
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
Nov 25, 2024
1 parent
d57eef9
commit 13757e6
Showing
5 changed files
with
294 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package terngo | ||
|
||
import "github.com/yyle88/tern" | ||
|
||
func VV[T comparable](a T, b T) T { | ||
if a != tern.Zero[T]() { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func VF[T comparable](a T, b func() T) T { | ||
if a != tern.Zero[T]() { | ||
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,30 @@ | ||
package terngo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yyle88/tern/terngo" | ||
) | ||
|
||
func TestVV(t *testing.T) { | ||
require.Equal(t, 1, terngo.VV(0, 1)) | ||
require.Equal(t, 1, terngo.VV(1, 2)) | ||
require.Equal(t, "a", terngo.VV("", "a")) | ||
require.Equal(t, "a", terngo.VV("a", "b")) | ||
} | ||
|
||
func TestVF(t *testing.T) { | ||
require.Equal(t, 1, terngo.VF(0, func() int { | ||
return 1 | ||
})) | ||
require.Equal(t, 1, terngo.VF(1, func() int { | ||
return 2 | ||
})) | ||
require.Equal(t, "a", terngo.VF("", func() string { | ||
return "a" | ||
})) | ||
require.Equal(t, "a", terngo.VF("a", func() string { | ||
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,15 @@ | ||
package terngo | ||
|
||
import "github.com/yyle88/tern" | ||
|
||
func PV[T comparable](p *T, b T) { | ||
if *p == tern.Zero[T]() { | ||
*p = b | ||
} | ||
} | ||
|
||
func PF[T comparable](p *T, b func() T) { | ||
if *p == tern.Zero[T]() { | ||
*p = 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,52 @@ | ||
package terngo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yyle88/tern/terngo" | ||
) | ||
|
||
func TestPV(t *testing.T) { | ||
a := 0 | ||
terngo.PV(&a, 1) | ||
require.Equal(t, 1, a) | ||
|
||
b := 1 | ||
terngo.PV(&b, 2) | ||
require.Equal(t, 1, b) | ||
|
||
c := "" | ||
terngo.PV(&c, "a") | ||
require.Equal(t, "a", c) | ||
|
||
s := "a" | ||
terngo.PV(&s, "b") | ||
require.Equal(t, "a", s) | ||
} | ||
|
||
func TestPF(t *testing.T) { | ||
a := 0 | ||
terngo.PF(&a, func() int { | ||
return 1 | ||
}) | ||
require.Equal(t, 1, a) | ||
|
||
b := 1 | ||
terngo.PF(&b, func() int { | ||
return 2 | ||
}) | ||
require.Equal(t, 1, b) | ||
|
||
c := "" | ||
terngo.PF(&c, func() string { | ||
return "a" | ||
}) | ||
require.Equal(t, "a", c) | ||
|
||
s := "a" | ||
terngo.PF(&s, func() string { | ||
return "b" | ||
}) | ||
require.Equal(t, "a", s) | ||
} |