diff --git a/fn.go b/fn.go index 962efc8..6c5d6f8 100644 --- a/fn.go +++ b/fn.go @@ -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 } diff --git a/fn_test.go b/fn_test.go index d737a03..53ff241 100644 --- a/fn_test.go +++ b/fn_test.go @@ -1,6 +1,7 @@ package trit import ( + "reflect" "testing" ) @@ -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 @@ -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) } })