Skip to content

Commit

Permalink
Add new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed Jun 29, 2023
1 parent 8c5c42d commit 2cda0d0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,24 @@ func Neq[T, U Logicable](a T, b U) Trit {
tb := logicToTrit(b)
return ta.Neq(tb)
}

// Confidence returns boolean true if all Trit-Like values has definiteness,
// i.e. is either True or False.
//
// Example usage:
//
// a := trit.Confidence(trit.True, trit.False, trit.Unknown)
// fmt.Println(a.String()) // Output: False
//
// b := trit.Confidence(trit.True, trit.True, trit.False)
// fmt.Println(b.String()) // Output: True
func Confidence[T Logicable](ts ...T) bool {
for _, t := range ts {
trit := logicToTrit(t)
if trit == Unknown {
return false
}
}

return true
}
45 changes: 45 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,48 @@ func TestNeq(t *testing.T) {
})
}
}

// TestConfidence tests the Confidence function.
func TestConfidence(t *testing.T) {
tests := []struct {
name string
in []Trit
out bool
}{
{
name: "Confidence should return true for (True, True, True)",
in: []Trit{True, True, True},
out: true,
},
{
name: "Confidence should return true for (True, True, False)",
in: []Trit{True, True, False},
out: true,
},
{
name: "Confidence should return true for (False, False, False)",
in: []Trit{False, False, False},
out: true,
},
{
name: "Confidence should return false for (False, Unknown, True)",
in: []Trit{False, Unknown, True},
out: false,
},
{
name: "Confidence should return false for (Unknown, Unknown)",
in: []Trit{Unknown, Unknown},
out: false,
},
}

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

0 comments on commit 2cda0d0

Please sign in to comment.