Skip to content

Commit

Permalink
Added goloop.g Numerable support
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed Jun 29, 2023
1 parent 5008faf commit 3e846a0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 27 deletions.
78 changes: 51 additions & 27 deletions fn.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package trit

import "reflect"
import (
"reflect"

// Logic is a special data type from which to determine the state of trit.
type Logic interface {
bool | int | int8 | int16 | int32 | int64 | Trit
"github.com/goloop/g"
)

// Logicable is a special data type from which to determine the state of trit.
type Logicable interface {
bool | g.Numerable | Trit
}

// The logicToTrit function converts any logic type to Trit object.
func logicToTrit[T Logic](v T) Trit {
// The logicToTrit function converts any logic type to Trit.
func logicToTrit[T Logicable](v T) Trit {
switch any(v).(type) {
case bool:
if any(v).(bool) {
Expand All @@ -19,22 +23,42 @@ func logicToTrit[T Logic](v T) Trit {
switch reflect.TypeOf(v).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
intValue := reflect.ValueOf(v).Int()
if intValue > 0 {
value := reflect.ValueOf(v).Int()
if value > 0 {
return True
} else if value < 0 {
return False
}

return Nil
}
case uint, uint8, uint16, uint32, uint64:
switch reflect.TypeOf(v).Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64:
value := reflect.ValueOf(v).Uint()
if value > 0 {
return True
}

// Can't be less than 0
return Nil
}
case float32, float64:
switch reflect.TypeOf(v).Kind() {
case reflect.Float32, reflect.Float64:
value := reflect.ValueOf(v).Float()
if value > 0 {
return True
} else if intValue < 0 {
} else if value < 0 {
return False
}

return Nil
}
case Trit:
return any(v).(Trit)
}

// The event will never fire because the function cannot work with
// types other than Logic. Therefore, this block cannot be tested.
return Nil
return any(v).(Trit)
}

// Default sets the default value for the trit-object
Expand All @@ -45,7 +69,7 @@ func logicToTrit[T Logic](v T) Trit {
// t := trit.Nil
// trit.Default(&t, trit.True)
// fmt.Println(t.String()) // Output: True
func Default[T Logic](t *Trit, v T) Trit {
func Default[T Logicable](t *Trit, v T) Trit {
// If the trit is not Nil, return the trit.
if t.Val() != Nil {
return *t
Expand All @@ -60,7 +84,7 @@ func Default[T Logic](t *Trit, v T) Trit {
// returns the result as Trit.
//
// See Trit.Not() for more information.
func Not[T Logic](t T) Trit {
func Not[T Logicable](t T) Trit {
trit := logicToTrit(t)
return trit.Not()
}
Expand All @@ -69,7 +93,7 @@ func Not[T Logic](t T) Trit {
// on a Trit-Like values and returns the result as Trit.
//
// See Trit.Ma() for more information.
func Ma[T Logic](t T) Trit {
func Ma[T Logicable](t T) Trit {
trit := logicToTrit(t)
return trit.Ma()
}
Expand All @@ -78,7 +102,7 @@ func Ma[T Logic](t T) Trit {
// value and returns the result as Trit.
//
// See Trit.La() for more information.
func La[T Logic](t T) Trit {
func La[T Logicable](t T) Trit {
trit := logicToTrit(t)
return trit.La()
}
Expand All @@ -87,7 +111,7 @@ func La[T Logic](t T) Trit {
// value and returns the result as Trit.
//
// See Trit.Ia() for more information.
func Ia[T Logic](t T) Trit {
func Ia[T Logicable](t T) Trit {
trit := logicToTrit(t)
return trit.Ia()
}
Expand All @@ -96,7 +120,7 @@ func Ia[T Logic](t T) Trit {
// and returns the result as Trit.
//
// See Trit.And() for more information.
func And[T, U Logic](a T, b U) Trit {
func And[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.And(tb)
Expand All @@ -106,7 +130,7 @@ func And[T, U Logic](a T, b U) Trit {
// and returns the result as Trit.
//
// See Trit.Or() for more information.
func Or[T, U Logic](a T, b U) Trit {
func Or[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Or(tb)
Expand All @@ -116,7 +140,7 @@ func Or[T, U Logic](a T, b U) Trit {
// and returns the result as Trit.
//
// See Trit.Xor() for more information.
func Xor[T, U Logic](a T, b U) Trit {
func Xor[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Xor(tb)
Expand All @@ -126,7 +150,7 @@ func Xor[T, U Logic](a T, b U) Trit {
// and returns the result as Trit.
//
// See Trit.Nand() for more information.
func Nand[T, U Logic](a T, b U) Trit {
func Nand[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Nand(tb)
Expand All @@ -136,7 +160,7 @@ func Nand[T, U Logic](a T, b U) Trit {
// and returns the result as Trit.
//
// See Trit.Nor() for more information.
func Nor[T, U Logic](a T, b U) Trit {
func Nor[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Nor(tb)
Expand All @@ -146,7 +170,7 @@ func Nor[T, U Logic](a T, b U) Trit {
// and returns the result as Trit.
//
// See Trit.Nxor() for more information.
func Nxor[T, U Logic](a T, b U) Trit {
func Nxor[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Nxor(tb)
Expand All @@ -156,7 +180,7 @@ func Nxor[T, U Logic](a T, b U) Trit {
// and returns the result as Trit.
//
// See Trit.Min() for more information.
func Min[T, U Logic](a T, b U) Trit {
func Min[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Min(tb)
Expand All @@ -166,7 +190,7 @@ func Min[T, U Logic](a T, b U) Trit {
// and returns the result as Trit.
//
// See Trit.Max() for more information.
func Max[T, U Logic](a T, b U) Trit {
func Max[T, U Logicable](a T, b U) Trit {
ta := logicToTrit(a)
tb := logicToTrit(b)
return ta.Max(tb)
Expand Down
42 changes: 42 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@ func TestLogicToTrit(t *testing.T) {
})
}

testsUint := []struct {
name string
in uint
out Trit
}{
{"1 should return True", 1, True},
{"0 should return Nil", 0, Nil},
{"1000000 should return True", 1000000, True},
}

for _, test := range testsUint {
t.Run(test.name, func(t *testing.T) {
result := logicToTrit(test.in)
if result != test.out {
t.Errorf("logicToTrit did not return %v for %v",
test.out, test.in)
}
})
}

testsFloat := []struct {
name string
in float64
out Trit
}{
{"0.3 should return True", 0.3, True},
{"-0.3 should return False", -0.3, False},
{"0.0 should return Nil", 0.0, Nil},
{"-77.5 should return False", -77.5, False},
{"1000000.5 should return True", 1000000.5, True},
}

for _, test := range testsFloat {
t.Run(test.name, func(t *testing.T) {
result := logicToTrit(test.in)
if result != test.out {
t.Errorf("logicToTrit did not return %v for %v",
test.out, test.in)
}
})
}

// Bool.
testsBool := []struct {
name string
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/goloop/trit

go 1.20

require github.com/goloop/g v1.7.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/goloop/g v1.7.1 h1:jfW8Lpd+8EbqDtp1g/fWnBY+U5HZKu6W1Ge8Gq1nFH4=
github.com/goloop/g v1.7.1/go.mod h1:qCL5T9TcYwUUm71EWiBNPxM/QKLyxpw8Gd8D6E8tOTU=

0 comments on commit 3e846a0

Please sign in to comment.