github.com/tawesoft/golib/v2@v2.10.0/legacy/operator/bool_test.go (about) 1 package operator 2 3 import ( 4 "testing" 5 ) 6 7 const T = true 8 const F = false 9 10 func TestBooleanUnary(t *testing.T) { 11 type test struct { 12 p bool 13 f func(bool) bool 14 expected bool 15 } 16 17 var tests = []test { 18 {T, Bool.Unary.True, T}, // test 0 19 {F, Bool.Unary.True, T}, // test 1 20 {T, Bool.Unary.False, F}, // test 2 21 {F, Bool.Unary.False, F}, // test 3 22 {T, Bool.Unary.Not, F}, // test 4 23 {F, Bool.Unary.Not, T}, // test 5 24 {T, Bool.Unary.Identity, T}, // test 6 25 {F, Bool.Unary.Identity, F}, // test 7 26 } 27 28 for idx, i := range tests { 29 var result = i.f(i.p) 30 if result != i.expected { 31 t.Errorf("test %d: got %t, but expected %t", idx, result, i.expected) 32 } 33 } 34 } 35 36 func TestBooleanBinaru(t *testing.T) { 37 var f = Bool.Binary 38 39 var testfns = []func(bool, bool)bool{ 40 f.True, // test 0 41 f.False, // test 1 42 f.P, // test 2 43 f.Q, // test 3 44 f.NotP, // test 4 45 f.NotQ, // test 5 46 f.Eq, // test 6 47 f.Neq, // test 7 48 f.And, // test 8 49 f.Nand, // test 9 50 f.Or, // test 10 51 f.Nor, // test 11 52 f.Xor, // test 12 53 f.Xnor, // test 13 54 f.Implies, // test 14 55 f.NonImplies, // test 15 56 f.ConImplies, // test 16 57 f.ConNonImplies, // test 17 58 } 59 60 var truthTable = []bool{ 61 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 62 // True, False, P, Q, NotP, NotQ, Eq, Neq, And, Nand, Or, Nor, Xor, Xnor, =>, =/=>, <=, <=/= 63 64 // p: T, q: T (case 0) 65 T, F, T, T, F, F, T, F, T, F, T, F, F, T, T, F, T, F, 66 67 // p: T, q: F (case 1) 68 T, F, T, F, F, T, F, T, F, T, T, F, T, F, F, T, T, F, 69 70 // p: F, q: T (case 2) 71 T, F, F, T, T, F, F, T, F, T, T, F, T, F, T, F, F, T, 72 73 // p: F, q: F (case 3) 74 T, F, F, F, T, T, T, F, F, T, F, T, F, T, T, F, T, F, 75 } 76 77 for idx, i := range testfns { 78 var results = []bool{i(T, T), i(T, F), i(F, T), i(F, F)} 79 var expected = []bool{ 80 truthTable[0 * len(testfns) + idx], 81 truthTable[1 * len(testfns) + idx], 82 truthTable[2 * len(testfns) + idx], 83 truthTable[3 * len(testfns) + idx], 84 } 85 86 for n := 0; n < 4; n++ { 87 if results[n] == expected[n] { continue } 88 t.Errorf("test %d case %d: got %t but expected %t", idx, n, results[n], expected[n]) 89 } 90 } 91 } 92 93 func TestBooleanNary(t *testing.T) { 94 type test struct { 95 f func(...bool) bool 96 p []bool 97 expected bool 98 } 99 100 var tests = []test { 101 {Bool.Nary.True, []bool{}, T}, // test 0 102 {Bool.Nary.True, []bool{T}, T}, // test 1 103 {Bool.Nary.True, []bool{F}, T}, // test 2 104 {Bool.Nary.True, []bool{T, F, T, F}, T}, // test 3 105 106 {Bool.Nary.False, []bool{}, F}, // test 4 107 {Bool.Nary.False, []bool{T}, F}, // test 5 108 {Bool.Nary.False, []bool{F}, F}, // test 6 109 {Bool.Nary.False, []bool{T, F, T, F}, F}, // test 7 110 111 {Bool.Nary.All, []bool{}, T}, // test 8 112 {Bool.Nary.All, []bool{T}, T}, // test 9 113 {Bool.Nary.All, []bool{F}, F}, // test 10 114 {Bool.Nary.All, []bool{T, T, T, T}, T}, // test 11 115 {Bool.Nary.All, []bool{T, T, F, T}, F}, // test 12 116 117 {Bool.Nary.Any, []bool{}, F}, // test 13 118 {Bool.Nary.Any, []bool{T}, T}, // test 14 119 {Bool.Nary.Any, []bool{F}, F}, // test 15 120 {Bool.Nary.Any, []bool{T, T, T, T}, T}, // test 16 121 {Bool.Nary.Any, []bool{T, T, F, T}, T}, // test 17 122 {Bool.Nary.Any, []bool{F, F, F, F}, F}, // test 18 123 124 {Bool.Nary.None, []bool{}, T}, // test 19 125 {Bool.Nary.None, []bool{T}, F}, // test 20 126 {Bool.Nary.None, []bool{F}, T}, // test 21 127 {Bool.Nary.None, []bool{T, T, T, T}, F}, // test 22 128 {Bool.Nary.None, []bool{T, T, F, T}, F}, // test 23 129 {Bool.Nary.None, []bool{F, F, F, F}, T}, // test 24 130 131 {boolNaryAll1, []bool{}, T}, // test 25 132 {boolNaryAll1, []bool{T}, T}, // test 26 133 {boolNaryAll1, []bool{F}, F}, // test 27 134 {boolNaryAll1, []bool{T, T, T, T}, T}, // test 28 135 {boolNaryAll1, []bool{T, T, F, T}, F}, // test 29 136 137 {boolNaryAll2, []bool{}, T}, // test 30 138 {boolNaryAll2, []bool{T}, T}, // test 31 139 {boolNaryAll2, []bool{F}, F}, // test 32 140 {boolNaryAll2, []bool{T, T, T, T}, T}, // test 33 141 {boolNaryAll2, []bool{T, T, F, T}, F}, // test 34 142 143 {boolNaryAny1, []bool{}, F}, // test 35 144 {boolNaryAny1, []bool{T}, T}, // test 36 145 {boolNaryAny1, []bool{F}, F}, // test 37 146 {boolNaryAny1, []bool{T, T, T, T}, T}, // test 38 147 {boolNaryAny1, []bool{T, T, F, T}, T}, // test 39 148 {boolNaryAny1, []bool{F, F, F, F}, F}, // test 40 149 150 {boolNaryAny2, []bool{}, F}, // test 41 151 {boolNaryAny2, []bool{T}, T}, // test 42 152 {boolNaryAny2, []bool{F}, F}, // test 43 153 {boolNaryAny2, []bool{T, T, T, T}, T}, // test 44 154 {boolNaryAny2, []bool{T, T, F, T}, T}, // test 45 155 {boolNaryAny2, []bool{F, F, F, F}, F}, // test 46 156 157 {boolNaryNone1, []bool{}, T}, // test 47 158 {boolNaryNone1, []bool{T}, F}, // test 48 159 {boolNaryNone1, []bool{F}, T}, // test 49 160 {boolNaryNone1, []bool{T, T, T, T}, F}, // test 50 161 {boolNaryNone1, []bool{T, T, F, T}, F}, // test 51 162 {boolNaryNone1, []bool{F, F, F, F}, T}, // test 52 163 164 {boolNaryNone2, []bool{}, T}, // test 53 165 {boolNaryNone2, []bool{T}, F}, // test 54 166 {boolNaryNone2, []bool{F}, T}, // test 55 167 {boolNaryNone2, []bool{T, T, T, T}, F}, // test 56 168 {boolNaryNone2, []bool{T, T, F, T}, F}, // test 57 169 {boolNaryNone2, []bool{F, F, F, F}, T}, // test 58 170 } 171 172 for idx, i := range tests { 173 var result = i.f(i.p...) 174 if result != i.expected { 175 t.Errorf("test %d: got %t, but expected %t", idx, result, i.expected) 176 } 177 } 178 }