mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 09:01:35 +01:00
Optimize lib/interval tests' opKey look-up
This commit is contained in:
1 parent
6a9a3abd24
commit
f3c9c4005e
2 files changed
+115
-111
No files matched your search
+103
-99
@@ -22,6 +22,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
var fromNeg7ToPos7 = []*big.Int{
|
var fromNeg7ToPos7 = []*big.Int{
|
||||||
@@ -328,7 +329,7 @@ func shareBigIntPointers(x IntRange, y IntRange) bool {
|
|||||||
((x[1] == y[1]) && (x[1] != nil))
|
((x[1] == y[1]) && (x[1] != nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBruteForceAgrees(x IntRange, y IntRange, opKey string) error {
|
func testBruteForceAgrees(x IntRange, y IntRange, opKey rune) error {
|
||||||
brute, bruteOK := bruteForce(x, y, opKey)
|
brute, bruteOK := bruteForce(x, y, opKey)
|
||||||
got, gotOK := intOperators[opKey](x, y)
|
got, gotOK := intOperators[opKey](x, y)
|
||||||
if shareBigIntPointers(got, x) || shareBigIntPointers(got, y) {
|
if shareBigIntPointers(got, x) || shareBigIntPointers(got, y) {
|
||||||
@@ -351,7 +352,7 @@ func TestBruteForceAgreesSystematically(tt *testing.T) {
|
|||||||
y := IntRange{y0, y1}
|
y := IntRange{y0, y1}
|
||||||
|
|
||||||
if err := testBruteForceAgrees(x, y, opKey); err != nil {
|
if err := testBruteForceAgrees(x, y, opKey); err != nil {
|
||||||
tt.Fatalf("%v %s %v: %v", x, opKey, y, err)
|
tt.Fatalf("%v %c %v: %v", x, opKey, y, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -375,7 +376,7 @@ func TestBruteForceAgreesRandomly(tt *testing.T) {
|
|||||||
x := IntRange{gen(rng), gen(rng)}
|
x := IntRange{gen(rng), gen(rng)}
|
||||||
y := IntRange{gen(rng), gen(rng)}
|
y := IntRange{gen(rng), gen(rng)}
|
||||||
if err := testBruteForceAgrees(x, y, opKey); err != nil {
|
if err := testBruteForceAgrees(x, y, opKey); err != nil {
|
||||||
tt.Fatalf("%v %s %v: %v", x, opKey, y, err)
|
tt.Fatalf("%v %c %v: %v", x, opKey, y, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -399,7 +400,8 @@ func testOp1(s string) error {
|
|||||||
i := 0
|
i := 0
|
||||||
for ; i < len(s) && s[i] != ' '; i++ {
|
for ; i < len(s) && s[i] != ' '; i++ {
|
||||||
}
|
}
|
||||||
opKey, s := s[:i], s[i:]
|
opKey, _ := utf8.DecodeRuneInString(s[:i])
|
||||||
|
s = s[i:]
|
||||||
|
|
||||||
y, s, err := parseInterval(s)
|
y, s, err := parseInterval(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -437,26 +439,28 @@ func testOp1(s string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var intOperators = map[string]func(IntRange, IntRange) (IntRange, bool){
|
var intOperators = map[rune]func(IntRange, IntRange) (IntRange, bool){
|
||||||
"∪": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Unite(y), true },
|
'∪': func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Unite(y), true },
|
||||||
"∩": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Intersect(y), true },
|
'∩': func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Intersect(y), true },
|
||||||
"+": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Add(y), true },
|
'+': func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Add(y), true },
|
||||||
"-": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Sub(y), true },
|
'-': func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Sub(y), true },
|
||||||
"*": func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Mul(y), true },
|
'*': func(x IntRange, y IntRange) (z IntRange, ok bool) { return x.Mul(y), true },
|
||||||
"/": IntRange.Quo,
|
'/': IntRange.Quo,
|
||||||
"<<": IntRange.Lsh,
|
'«': IntRange.Lsh,
|
||||||
">>": IntRange.Rsh,
|
'»': IntRange.Rsh,
|
||||||
"&": IntRange.And,
|
'&': IntRange.And,
|
||||||
"|": IntRange.Or,
|
'|': IntRange.Or,
|
||||||
}
|
}
|
||||||
|
|
||||||
var intOperatorsKeys []string
|
var intOperatorsKeys []rune
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
for k := range intOperators {
|
for k := range intOperators {
|
||||||
intOperatorsKeys = append(intOperatorsKeys, k)
|
intOperatorsKeys = append(intOperatorsKeys, k)
|
||||||
}
|
}
|
||||||
sort.Strings(intOperatorsKeys)
|
sort.Slice(intOperatorsKeys, func(i, j int) bool {
|
||||||
|
return intOperatorsKeys[i] < intOperatorsKeys[j]
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpUnite(tt *testing.T) {
|
func TestOpUnite(tt *testing.T) {
|
||||||
@@ -663,103 +667,103 @@ func TestOpQuo(tt *testing.T) {
|
|||||||
|
|
||||||
func TestOpLsh(tt *testing.T) {
|
func TestOpLsh(tt *testing.T) {
|
||||||
testOp(tt,
|
testOp(tt,
|
||||||
"[ 3, 3] << [ -5, -5] == invalid",
|
"[ 3, 3] « [ -5, -5] == invalid",
|
||||||
"[ 3, 3] << [ 0, 0] == [ 3, 3]",
|
"[ 3, 3] « [ 0, 0] == [ 3, 3]",
|
||||||
"[ 0, 0] << [ -7, 7] == invalid",
|
"[ 0, 0] « [ -7, 7] == invalid",
|
||||||
"[ 0, 2] << [ 0, 5] == [ 0, 64]",
|
"[ 0, 2] « [ 0, 5] == [ 0, 64]",
|
||||||
"[ 3, 6] << [ 10, 15] == [3072, 196608]",
|
"[ 3, 6] « [ 10, 15] == [3072, 196608]",
|
||||||
"[ 3, +∞) << [ -4, -2] == invalid",
|
"[ 3, +∞) « [ -4, -2] == invalid",
|
||||||
"[ 3, +∞) << [ 10, 15] == [3072, +∞)",
|
"[ 3, +∞) « [ 10, 15] == [3072, +∞)",
|
||||||
"[ 3, +∞) << ( -∞, 15] == invalid",
|
"[ 3, +∞) « ( -∞, 15] == invalid",
|
||||||
"[ 3, 6] << ( -∞, 15] == invalid",
|
"[ 3, 6] « ( -∞, 15] == invalid",
|
||||||
"[ 3, 6] << ( -∞, +∞) == invalid",
|
"[ 3, 6] « ( -∞, +∞) == invalid",
|
||||||
"( -∞, +∞) << ( -∞, +∞) == invalid",
|
"( -∞, +∞) « ( -∞, +∞) == invalid",
|
||||||
"( -∞, +∞) << [ 1, 2] == ( -∞, +∞)",
|
"( -∞, +∞) « [ 1, 2] == ( -∞, +∞)",
|
||||||
"( -∞, +∞) << [ 0, 0] == ( -∞, +∞)",
|
"( -∞, +∞) « [ 0, 0] == ( -∞, +∞)",
|
||||||
"[ 3, 6] << [...empty..] == [...empty..]",
|
"[ 3, 6] « [...empty..] == [...empty..]",
|
||||||
"[...empty..] << [ 10, 15] == [...empty..]",
|
"[...empty..] « [ 10, 15] == [...empty..]",
|
||||||
"[...empty..] << [...empty..] == [...empty..]",
|
"[...empty..] « [...empty..] == [...empty..]",
|
||||||
"( -∞, +∞) << [...empty..] == [...empty..]",
|
"( -∞, +∞) « [...empty..] == [...empty..]",
|
||||||
|
|
||||||
"[ 1, 4] << [ -11, -10] == invalid",
|
"[ 1, 4] « [ -11, -10] == invalid",
|
||||||
|
|
||||||
"[ 1, 4] << [ -6, 2] == invalid",
|
"[ 1, 4] « [ -6, 2] == invalid",
|
||||||
|
|
||||||
"[ -3, -1] << [ 0, 3] == [ -24, -1]",
|
"[ -3, -1] « [ 0, 3] == [ -24, -1]",
|
||||||
"[ -3, 0] << [ 0, 3] == [ -24, 0]",
|
"[ -3, 0] « [ 0, 3] == [ -24, 0]",
|
||||||
"[ -3, 1] << [ 0, 3] == [ -24, 8]",
|
"[ -3, 1] « [ 0, 3] == [ -24, 8]",
|
||||||
"[ -3, 4] << [ 0, 3] == [ -24, 32]",
|
"[ -3, 4] « [ 0, 3] == [ -24, 32]",
|
||||||
"[ -1, 4] << [ 0, 3] == [ -8, 32]",
|
"[ -1, 4] « [ 0, 3] == [ -8, 32]",
|
||||||
"[ 0, 4] << [ 0, 3] == [ 0, 32]",
|
"[ 0, 4] « [ 0, 3] == [ 0, 32]",
|
||||||
"[ 1, 4] << [ 0, 3] == [ 1, 32]",
|
"[ 1, 4] « [ 0, 3] == [ 1, 32]",
|
||||||
|
|
||||||
"[ -3, -1] << [ 2, 3] == [ -24, -4]",
|
"[ -3, -1] « [ 2, 3] == [ -24, -4]",
|
||||||
"[ -3, 0] << [ 2, 3] == [ -24, 0]",
|
"[ -3, 0] « [ 2, 3] == [ -24, 0]",
|
||||||
"[ -3, 1] << [ 2, 3] == [ -24, 8]",
|
"[ -3, 1] « [ 2, 3] == [ -24, 8]",
|
||||||
"[ -3, 4] << [ 2, 3] == [ -24, 32]",
|
"[ -3, 4] « [ 2, 3] == [ -24, 32]",
|
||||||
"[ -1, 4] << [ 2, 3] == [ -8, 32]",
|
"[ -1, 4] « [ 2, 3] == [ -8, 32]",
|
||||||
"[ 0, 4] << [ 2, 3] == [ 0, 32]",
|
"[ 0, 4] « [ 2, 3] == [ 0, 32]",
|
||||||
"[ 1, 4] << [ 2, 3] == [ 4, 32]",
|
"[ 1, 4] « [ 2, 3] == [ 4, 32]",
|
||||||
|
|
||||||
"[ -9, +∞) << [ 2, +∞) == ( -∞, +∞)",
|
"[ -9, +∞) « [ 2, +∞) == ( -∞, +∞)",
|
||||||
"[ -1, +∞) << [ 2, +∞) == ( -∞, +∞)",
|
"[ -1, +∞) « [ 2, +∞) == ( -∞, +∞)",
|
||||||
"[ 0, +∞) << [ 2, +∞) == [ 0, +∞)",
|
"[ 0, +∞) « [ 2, +∞) == [ 0, +∞)",
|
||||||
"[ 1, +∞) << [ 2, +∞) == [ 4, +∞)",
|
"[ 1, +∞) « [ 2, +∞) == [ 4, +∞)",
|
||||||
"[ 7, +∞) << [ 2, +∞) == [ 28, +∞)",
|
"[ 7, +∞) « [ 2, +∞) == [ 28, +∞)",
|
||||||
"[ -1, 1] << ( -∞, +∞) == invalid",
|
"[ -1, 1] « ( -∞, +∞) == invalid",
|
||||||
"[ 0, 0] << ( -∞, +∞) == invalid",
|
"[ 0, 0] « ( -∞, +∞) == invalid",
|
||||||
"[ 1, 1] << ( -∞, +∞) == invalid",
|
"[ 1, 1] « ( -∞, +∞) == invalid",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpRsh(tt *testing.T) {
|
func TestOpRsh(tt *testing.T) {
|
||||||
testOp(tt,
|
testOp(tt,
|
||||||
"[ 3, 3] >> [ -5, -5] == invalid",
|
"[ 3, 3] » [ -5, -5] == invalid",
|
||||||
"[ 3, 3] >> [ 0, 0] == [ 3, 3]",
|
"[ 3, 3] » [ 0, 0] == [ 3, 3]",
|
||||||
"[ 0, 0] >> [ -7, 7] == invalid",
|
"[ 0, 0] » [ -7, 7] == invalid",
|
||||||
"[ 0, 2] >> [ 0, 5] == [ 0, 2]",
|
"[ 0, 2] » [ 0, 5] == [ 0, 2]",
|
||||||
"[ 3, 6] >> [ 10, 15] == [ 0, 0]",
|
"[ 3, 6] » [ 10, 15] == [ 0, 0]",
|
||||||
"[ 3, +∞) >> [ -4, -2] == invalid",
|
"[ 3, +∞) » [ -4, -2] == invalid",
|
||||||
"[ 3, +∞) >> [ 10, 15] == [ 0, +∞)",
|
"[ 3, +∞) » [ 10, 15] == [ 0, +∞)",
|
||||||
"[ 3, +∞) >> ( -∞, 15] == invalid",
|
"[ 3, +∞) » ( -∞, 15] == invalid",
|
||||||
"[ 3, 6] >> ( -∞, 15] == invalid",
|
"[ 3, 6] » ( -∞, 15] == invalid",
|
||||||
"[ 3, 6] >> ( -∞, +∞) == invalid",
|
"[ 3, 6] » ( -∞, +∞) == invalid",
|
||||||
"( -∞, +∞) >> ( -∞, +∞) == invalid",
|
"( -∞, +∞) » ( -∞, +∞) == invalid",
|
||||||
"( -∞, +∞) >> [ 1, 2] == ( -∞, +∞)",
|
"( -∞, +∞) » [ 1, 2] == ( -∞, +∞)",
|
||||||
"( -∞, +∞) >> [ 0, 0] == ( -∞, +∞)",
|
"( -∞, +∞) » [ 0, 0] == ( -∞, +∞)",
|
||||||
"[ 3, 6] >> [...empty..] == [...empty..]",
|
"[ 3, 6] » [...empty..] == [...empty..]",
|
||||||
"[...empty..] >> [ 10, 15] == [...empty..]",
|
"[...empty..] » [ 10, 15] == [...empty..]",
|
||||||
"[...empty..] >> [...empty..] == [...empty..]",
|
"[...empty..] » [...empty..] == [...empty..]",
|
||||||
"( -∞, +∞) >> [...empty..] == [...empty..]",
|
"( -∞, +∞) » [...empty..] == [...empty..]",
|
||||||
|
|
||||||
"[ 1, 4] >> [ -11, -10] == invalid",
|
"[ 1, 4] » [ -11, -10] == invalid",
|
||||||
|
|
||||||
"[ 1, 4] >> [ -6, 2] == invalid",
|
"[ 1, 4] » [ -6, 2] == invalid",
|
||||||
|
|
||||||
"[ -3, -1] >> [ 0, 3] == [ -3, -1]",
|
"[ -3, -1] » [ 0, 3] == [ -3, -1]",
|
||||||
"[ -3, 0] >> [ 0, 3] == [ -3, 0]",
|
"[ -3, 0] » [ 0, 3] == [ -3, 0]",
|
||||||
"[ -3, 1] >> [ 0, 3] == [ -3, 1]",
|
"[ -3, 1] » [ 0, 3] == [ -3, 1]",
|
||||||
"[ -3, 4] >> [ 0, 3] == [ -3, 4]",
|
"[ -3, 4] » [ 0, 3] == [ -3, 4]",
|
||||||
"[ -1, 4] >> [ 0, 3] == [ -1, 4]",
|
"[ -1, 4] » [ 0, 3] == [ -1, 4]",
|
||||||
"[ 0, 4] >> [ 0, 3] == [ 0, 4]",
|
"[ 0, 4] » [ 0, 3] == [ 0, 4]",
|
||||||
"[ 1, 4] >> [ 0, 3] == [ 0, 4]",
|
"[ 1, 4] » [ 0, 3] == [ 0, 4]",
|
||||||
|
|
||||||
"[ -3, -1] >> [ 1, 3] == [ -2, -1]",
|
"[ -3, -1] » [ 1, 3] == [ -2, -1]",
|
||||||
"[ -3, 0] >> [ 1, 3] == [ -2, 0]",
|
"[ -3, 0] » [ 1, 3] == [ -2, 0]",
|
||||||
"[ -3, 1] >> [ 1, 3] == [ -2, 0]",
|
"[ -3, 1] » [ 1, 3] == [ -2, 0]",
|
||||||
"[ -3, 4] >> [ 1, 3] == [ -2, 2]",
|
"[ -3, 4] » [ 1, 3] == [ -2, 2]",
|
||||||
"[ -1, 4] >> [ 1, 3] == [ -1, 2]",
|
"[ -1, 4] » [ 1, 3] == [ -1, 2]",
|
||||||
"[ 0, 4] >> [ 1, 3] == [ 0, 2]",
|
"[ 0, 4] » [ 1, 3] == [ 0, 2]",
|
||||||
"[ 1, 4] >> [ 1, 3] == [ 0, 2]",
|
"[ 1, 4] » [ 1, 3] == [ 0, 2]",
|
||||||
|
|
||||||
"[ -9, +∞) >> [ 2, +∞) == [ -3, +∞)",
|
"[ -9, +∞) » [ 2, +∞) == [ -3, +∞)",
|
||||||
"[ -1, +∞) >> [ 2, +∞) == [ -1, +∞)",
|
"[ -1, +∞) » [ 2, +∞) == [ -1, +∞)",
|
||||||
"[ 0, +∞) >> [ 2, +∞) == [ 0, +∞)",
|
"[ 0, +∞) » [ 2, +∞) == [ 0, +∞)",
|
||||||
"[ 1, +∞) >> [ 2, +∞) == [ 0, +∞)",
|
"[ 1, +∞) » [ 2, +∞) == [ 0, +∞)",
|
||||||
"[ 7, +∞) >> [ 2, +∞) == [ 0, +∞)",
|
"[ 7, +∞) » [ 2, +∞) == [ 0, +∞)",
|
||||||
"[ -1, 1] >> ( -∞, +∞) == invalid",
|
"[ -1, 1] » ( -∞, +∞) == invalid",
|
||||||
"[ 0, 0] >> ( -∞, +∞) == invalid",
|
"[ 0, 0] » ( -∞, +∞) == invalid",
|
||||||
"[ 1, 1] >> ( -∞, +∞) == invalid",
|
"[ 1, 1] » ( -∞, +∞) == invalid",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-12
@@ -440,18 +440,18 @@ func (x radialInput) Or(y radialInput) radialOutPair {
|
|||||||
return radialOutPair{ox | oy, ox | oy}
|
return radialOutPair{ox | oy, ox | oy}
|
||||||
}
|
}
|
||||||
|
|
||||||
var riOperators = map[string]func(radialInput, radialInput) radialOutPair{
|
var riOperators = map[rune]func(radialInput, radialInput) radialOutPair{
|
||||||
"+": radialInput.Add,
|
'+': radialInput.Add,
|
||||||
"-": radialInput.Sub,
|
'-': radialInput.Sub,
|
||||||
"*": radialInput.Mul,
|
'*': radialInput.Mul,
|
||||||
"/": radialInput.Quo,
|
'/': radialInput.Quo,
|
||||||
"<<": radialInput.Lsh,
|
'«': radialInput.Lsh,
|
||||||
">>": radialInput.Rsh,
|
'»': radialInput.Rsh,
|
||||||
"&": radialInput.And,
|
'&': radialInput.And,
|
||||||
"|": radialInput.Or,
|
'|': radialInput.Or,
|
||||||
}
|
}
|
||||||
|
|
||||||
func bruteForce(x IntRange, y IntRange, opKey string) (z IntRange, ok bool) {
|
func bruteForce(x IntRange, y IntRange, opKey rune) (z IntRange, ok bool) {
|
||||||
op := riOperators[opKey]
|
op := riOperators[opKey]
|
||||||
iMin, iMax := enumerate(x)
|
iMin, iMax := enumerate(x)
|
||||||
jMin, jMax := enumerate(y)
|
jMin, jMax := enumerate(y)
|
||||||
@@ -472,7 +472,7 @@ func bruteForce(x IntRange, y IntRange, opKey string) (z IntRange, ok bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch opKey {
|
switch opKey {
|
||||||
case "∪":
|
case '∪':
|
||||||
if iMinC, iMaxC := iMin.canonicalize(), iMax.canonicalize(); iMinC <= iMaxC {
|
if iMinC, iMaxC := iMin.canonicalize(), iMax.canonicalize(); iMinC <= iMaxC {
|
||||||
merge(radialOutPair{iMinC, iMaxC})
|
merge(radialOutPair{iMinC, iMaxC})
|
||||||
}
|
}
|
||||||
@@ -486,7 +486,7 @@ func bruteForce(x IntRange, y IntRange, opKey string) (z IntRange, ok bool) {
|
|||||||
result[1] = +roRadius + 1
|
result[1] = +roRadius + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
case "∩":
|
case '∩':
|
||||||
iMinC, iMaxC := iMin.canonicalize(), iMax.canonicalize()
|
iMinC, iMaxC := iMin.canonicalize(), iMax.canonicalize()
|
||||||
for j := jMin; j <= jMax; j++ {
|
for j := jMin; j <= jMax; j++ {
|
||||||
jC := j.canonicalize()
|
jC := j.canonicalize()
|
||||||
|
|||||||
Reference in new issue
Block a user