Skip to content

Commit

Permalink
Convert converts a list of values, and Defaine converts a single value
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed Jul 1, 2023
1 parent 31c3156 commit 315cbd5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
23 changes: 20 additions & 3 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,30 @@ func Set[T Logicable](t *Trit, v T) Trit {
return *t
}

// Convert converts the any Logicable type to Trit.
// Convert converts the any Logicable types to Trit.
//
// Example usage:
//
// t := trit.Convert(true)
// tuf := trit.Convert(true, 0, -1)
// fmt.Println(tuf[0].String()) // Output: True
// fmt.Println(tuf[1].String()) // Output: Unknown
// fmt.Println(tuf[2].String()) // Output: False
func Convert[T Logicable](v ...T) []Trit {
trit := make([]Trit, len(v))
for i, value := range v {
trit[i] = logicToTrit(value)
}

return trit
}

// Define converts the any Logicable type to Trit.
//
// Example usage:
//
// t := trit.Define(true)
// fmt.Println(t.String()) // Output: True
func Convert[T Logicable](v T) Trit {
func Define[T Logicable](v T) Trit {
trit := logicToTrit(v)
return trit
}
Expand Down
32 changes: 28 additions & 4 deletions fn_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package trit

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -141,8 +142,8 @@ func TestSet(t *testing.T) {
}
}

// TestConvert tests the Convert function.
func TestConvert(t *testing.T) {
// TestDefine tests the Define function.
func TestDefine(t *testing.T) {
tests := []struct {
name string
in float64
Expand All @@ -155,9 +156,32 @@ func TestConvert(t *testing.T) {

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

// TestConvert tests the Convert function.
func TestConvert(t *testing.T) {
tests := []struct {
name string
in []int
out []Trit
}{
{"-0.1 should return False", []int{-1, 1}, []Trit{False, True}},
{"7.7 should return True", []int{0, 1}, []Trit{Unknown, True}},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := Convert(test.in...)
// DeepEqual is used to compare slices.
if !reflect.DeepEqual(result, test.out) {
t.Errorf("Convert did not return %v for %v",
test.out, test.in)
}
})
Expand Down

0 comments on commit 315cbd5

Please sign in to comment.