master > master: codego unit tests aktualisiert

This commit is contained in:
RD 2021-05-12 18:46:51 +02:00
parent c10f194ce4
commit 1e37fd2ea9
3 changed files with 167 additions and 107 deletions

View File

@ -20,59 +20,59 @@ import (
func TestRekursivEvalLiteral(test *testing.T) {
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var I []string
var val int
tree, _ = schema.ParseExpr("A0")
tree = schema.ParseExpr("A0")
I = []string{"A0"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 1)
tree, _ = schema.ParseExpr("A0")
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 1)
tree = schema.ParseExpr("A0")
I = []string{}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 0)
tree, _ = schema.ParseExpr("! A0")
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 0)
tree = schema.ParseExpr("! A0")
I = []string{"A0"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 0)
tree, _ = schema.ParseExpr("! A0")
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 0)
tree = schema.ParseExpr("! A0")
I = []string{}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 1)
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 1)
}
func TestRekursivEvalComplex1(test *testing.T) {
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var I []string
var val int
tree, _ = schema.ParseExpr("( ! A0 || (( A0 && A3 ) || A2 ))")
tree = schema.ParseExpr("( ! A0 || (( A0 && A3 ) || A2 ))")
I = []string{"A0", "A2"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 1)
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 1)
I = []string{"A0", "A3"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 1)
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 1)
I = []string{"A0"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 0)
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 0)
I = []string{"A4", "A8"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 1)
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 1)
}
func TestRekursivEvalComplex2(test *testing.T) {
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var I []string
var val int
tree, _ = schema.ParseExpr("( ! A0 || (( A0 && A3 ) || ! A2 ))")
tree = schema.ParseExpr("( ! A0 || (( A0 && A3 ) || ! A2 ))")
I = []string{"A0", "A2"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 0)
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 0)
I = []string{"A0", "A3"}
val = rekursion.RekursivEval(tree, I)
assert.Equal(val, 1)
go rekursion.RekursivEval(ch, tree, I)
assert.Equal(<-ch, 1)
}
/* ---------------------------------------------------------------- *
@ -82,10 +82,12 @@ func TestRekursivEvalComplex2(test *testing.T) {
func TestRekursivAtomsNoduplicates(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan []string)
var tree syntaxbaum.SyntaxBaum
var val []string
tree, _ = schema.ParseExpr("( A4 && ( A4 || A4 ))")
val = rekursion.RekursivAtoms(tree)
tree = schema.ParseExpr("( A4 && ( A4 || A4 ))")
go rekursion.RekursivAtoms(ch, tree)
val = <-ch
var n int = len(utils.FilterStrings(&val, func(x string) bool { return x == "A4" }))
assert.Equal(n, 1, "Atome dürfen nicht mehrfach vorkommen!")
}
@ -94,10 +96,12 @@ func TestRekursivAtomsNononatoms(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
test.Skip("Syntax for generic expressions in ANTLR4 g4 needs to be implemented.")
var assert = assert.New(test)
var ch = make(chan []string)
var tree syntaxbaum.SyntaxBaum
var val []string
tree, _ = schema.ParseExpr("( {F} || A3 )")
val = rekursion.RekursivAtoms(tree)
tree = schema.ParseExpr("( {F} || A3 )")
go rekursion.RekursivAtoms(ch, tree)
val = <-ch
utils.SortStrings(&val)
assert.NotContains(val, "F", "Nichtatomare Formeln dürfen nicht vorkommen!")
}
@ -105,10 +109,12 @@ func TestRekursivAtomsNononatoms(test *testing.T) {
func TestRekursivAtomsCalc1(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan []string)
var tree syntaxbaum.SyntaxBaum
var val []string
tree, _ = schema.ParseExpr("A0")
val = rekursion.RekursivAtoms(tree)
tree = schema.ParseExpr("A0")
go rekursion.RekursivAtoms(ch, tree)
val = <-ch
utils.SortStrings(&val)
assert.Equal(val, []string{"A0"})
}
@ -116,10 +122,12 @@ func TestRekursivAtomsCalc1(test *testing.T) {
func TestRekursivAtomsCalc2(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan []string)
var tree syntaxbaum.SyntaxBaum
var val []string
tree, _ = schema.ParseExpr("((( ! A8 && A3 ) || A4 ) && A0 )")
val = rekursion.RekursivAtoms(tree)
tree = schema.ParseExpr("((( ! A8 && A3 ) || A4 ) && A0 )")
go rekursion.RekursivAtoms(ch, tree)
val = <-ch
utils.SortStrings(&val)
assert.Equal(val, []string{"A0", "A3", "A4", "A8"})
}
@ -131,51 +139,51 @@ func TestRekursivAtomsCalc2(test *testing.T) {
func TestRekursivDepthCalc1(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("A0")
val = rekursion.RekursivDepth(tree)
assert.Equal(val, 0)
tree = schema.ParseExpr("A0")
go rekursion.RekursivDepth(ch, tree)
assert.Equal(<-ch, 0)
}
func TestRekursivDepthCalc2(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("!! A8")
val = rekursion.RekursivDepth(tree)
assert.Equal(val, 2)
tree = schema.ParseExpr("!! A8")
go rekursion.RekursivDepth(ch, tree)
assert.Equal(<-ch, 2)
}
func TestRekursivDepthCalc3(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("( ! A0 && A3 )")
val = rekursion.RekursivDepth(tree)
assert.Equal(val, 2)
tree = schema.ParseExpr("( ! A0 && A3 )")
go rekursion.RekursivDepth(ch, tree)
assert.Equal(<-ch, 2)
}
func TestRekursivDepthCalc4(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("((( ! A0 && A3 ) || A4 ) && A8 )")
val = rekursion.RekursivDepth(tree)
assert.Equal(val, 4)
tree = schema.ParseExpr("((( ! A0 && A3 ) || A4 ) && A8 )")
go rekursion.RekursivDepth(ch, tree)
assert.Equal(<-ch, 4)
}
func TestRekursivDepthCalc5(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("! ((( ! A0 && A3 ) || A4 ) && A8 )")
val = rekursion.RekursivDepth(tree)
assert.Equal(val, 5)
tree = schema.ParseExpr("! ((( ! A0 && A3 ) || A4 ) && A8 )")
go rekursion.RekursivDepth(ch, tree)
assert.Equal(<-ch, 5)
}
/* ---------------------------------------------------------------- *
@ -185,51 +193,51 @@ func TestRekursivDepthCalc5(test *testing.T) {
func TestRekursivLengthCalc1(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("A0")
val = rekursion.RekursivLength(tree)
assert.Equal(val, 1)
tree = schema.ParseExpr("A0")
go rekursion.RekursivLength(ch, tree)
assert.Equal(<-ch, 1)
}
func TestRekursivLengthCalc2(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("!! A8")
val = rekursion.RekursivLength(tree)
assert.Equal(val, 3)
tree = schema.ParseExpr("!! A8")
go rekursion.RekursivLength(ch, tree)
assert.Equal(<-ch, 3)
}
func TestRekursivLengthCalc3(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("( ! A0 && A3 )")
val = rekursion.RekursivLength(tree)
assert.Equal(val, 4)
tree = schema.ParseExpr("( ! A0 && A3 )")
go rekursion.RekursivLength(ch, tree)
assert.Equal(<-ch, 4)
}
func TestRekursivLengthCalc4(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("((( ! A0 && A3 ) || A4 ) && A8 )")
val = rekursion.RekursivLength(tree)
assert.Equal(val, 8)
tree = schema.ParseExpr("((( ! A0 && A3 ) || A4 ) && A8 )")
go rekursion.RekursivLength(ch, tree)
assert.Equal(<-ch, 8)
}
func TestRekursivLengthCalc5(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("! ((( ! A0 && A3 ) || A4 ) && A8 )")
val = rekursion.RekursivLength(tree)
assert.Equal(val, 9)
tree = schema.ParseExpr("! ((( ! A0 && A3 ) || A4 ) && A8 )")
go rekursion.RekursivLength(ch, tree)
assert.Equal(<-ch, 9)
}
/* ---------------------------------------------------------------- *
@ -239,49 +247,49 @@ func TestRekursivLengthCalc5(test *testing.T) {
func TestRekursivParenthesesCalc1(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("A0")
val = rekursion.RekursivParentheses(tree)
assert.Equal(val, 0)
tree = schema.ParseExpr("A0")
go rekursion.RekursivParentheses(ch, tree)
assert.Equal(<-ch, 0)
}
func TestRekursivParenthesesCalc2(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("!! A8")
val = rekursion.RekursivParentheses(tree)
assert.Equal(val, 0)
tree = schema.ParseExpr("!! A8")
go rekursion.RekursivParentheses(ch, tree)
assert.Equal(<-ch, 0)
}
func TestRekursivParenthesesCalc3(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("( ! A0 && A3 )")
val = rekursion.RekursivParentheses(tree)
assert.Equal(val, 2)
tree = schema.ParseExpr("( ! A0 && A3 )")
go rekursion.RekursivParentheses(ch, tree)
assert.Equal(<-ch, 2)
}
func TestRekursivParenthesesCalc4(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("((( ! A0 && A3 ) || A4 ) && A8 )")
val = rekursion.RekursivParentheses(tree)
assert.Equal(val, 6)
tree = schema.ParseExpr("((( ! A0 && A3 ) || A4 ) && A8 )")
go rekursion.RekursivParentheses(ch, tree)
assert.Equal(<-ch, 6)
}
func TestRekursivParenthesesCalc5(test *testing.T) {
test.Skip("Methode noch nicht implementiert")
var assert = assert.New(test)
var ch = make(chan int)
var tree syntaxbaum.SyntaxBaum
var val int
tree, _ = schema.ParseExpr("! ((( ! A0 && A3 ) || A4 ) && A8 )")
val = rekursion.RekursivParentheses(tree)
assert.Equal(val, 6)
tree = schema.ParseExpr("! ((( ! A0 && A3 ) || A4 ) && A8 )")
go rekursion.RekursivParentheses(ch, tree)
assert.Equal(<-ch, 6)
}

View File

@ -6,6 +6,7 @@ package schema_test
import (
"logik/aussagenlogik/schema"
"logik/aussagenlogik/syntaxbaum"
"testing"
"github.com/stretchr/testify/assert"
@ -17,6 +18,25 @@ import (
func TestParseExpr(test *testing.T) {
var assert = assert.New(test)
assert.Equal(0, 0)
schema.ParseExpr("A0")
var tree syntaxbaum.SyntaxBaum
tree = schema.ParseExpr("A8712")
assert.Equal("A8712", tree.GetExpr())
assert.Equal("atom", tree.GetKind())
assert.Equal(0, len(tree.GetChildren()))
tree = schema.ParseExpr(" ! A5 ")
assert.Equal("! A5", tree.GetExpr())
assert.Equal("not", tree.GetKind())
assert.Equal(1, len(tree.GetChildren()))
tree = schema.ParseExpr("A0 -> A1")
assert.Equal("(A0 -> A1)", tree.GetExpr())
assert.Equal("implies", tree.GetKind())
assert.Equal(2, len(tree.GetChildren()))
tree = schema.ParseExpr("( A0 && A1) || A2")
assert.Equal("((A0 && A1) || A2)", tree.GetExpr())
assert.Equal("or2", tree.GetKind())
assert.Equal(2, len(tree.GetChildren()))
}

View File

@ -12,6 +12,28 @@ import (
"github.com/stretchr/testify/assert"
)
/* ---------------------------------------------------------------- *
* TESTCASE Min2, Max2, MinList, MaxList
* ---------------------------------------------------------------- */
func TestMin(test *testing.T) {
var assert = assert.New(test)
assert.Equal(3, utils.Min2(3, 8))
assert.Equal(8, utils.Min2(100, 8))
assert.Equal(50, utils.MinList([]int{50}))
assert.Equal(1, utils.MinList([]int{50, 1}))
assert.Equal(2, utils.MinList([]int{50, 34, 10, 2, 8, 89}))
}
func TestMax(test *testing.T) {
var assert = assert.New(test)
assert.Equal(8, utils.Max2(3, 8))
assert.Equal(100, utils.Max2(100, 8))
assert.Equal(50, utils.MaxList([]int{50}))
assert.Equal(50, utils.MaxList([]int{50, 1}))
assert.Equal(89, utils.MaxList([]int{50, 34, 10, 2, 8, 89}))
}
/* ---------------------------------------------------------------- *
* TESTCASE SortStrings
* ---------------------------------------------------------------- */
@ -20,7 +42,7 @@ func TestSortStrings(test *testing.T) {
var assert = assert.New(test)
var list = []string{"katze", "Hund", "baby", "Pluto", "Saturn", "Mond"}
utils.SortStrings(&list)
assert.Equal(list, []string{"baby", "Hund", "katze", "Mond", "Pluto", "Saturn"})
assert.Equal([]string{"baby", "Hund", "katze", "Mond", "Pluto", "Saturn"}, list)
}
/* ---------------------------------------------------------------- *
@ -31,11 +53,11 @@ func TestFilterStrings(test *testing.T) {
var assert = assert.New(test)
var list = []string{"abram", "aaron", "aardvark", "aarhus", "alaska", "eel", "aal"}
var list2 = utils.FilterStrings(&list, func(x string) bool { return strings.HasPrefix(x, "aa") })
assert.Equal(list2, []string{"aaron", "aardvark", "aarhus", "aal"})
assert.Equal([]string{"aaron", "aardvark", "aarhus", "aal"}, list2)
}
/* ---------------------------------------------------------------- *
* TESTCASE UnionStrings2, UnionStringsTo
* TESTCASE UnionStrings2, UnionStringsTo, UnionStringsList
* ---------------------------------------------------------------- */
func TestUnionStrings2(test *testing.T) {
@ -44,14 +66,24 @@ func TestUnionStrings2(test *testing.T) {
var list2 = []string{"yellow", "orange", "lila", "red"}
var list = utils.UnionStrings2(list1, list2)
utils.SortStrings(&list)
assert.Equal(list, []string{"black", "blue", "green", "grey", "lila", "orange", "red", "yellow"})
assert.Equal([]string{"black", "blue", "green", "grey", "lila", "orange", "red", "yellow"}, list)
}
func UnionStringsTo(test *testing.T) {
func TestUnionStringsTo(test *testing.T) {
var assert = assert.New(test)
var list1 = []string{"red", "blue", "blue", "green"}
var list2 = []string{"yellow", "red", "black"}
var list1 = []string{"red", "blue", "green"}
var list2 = []string{"yellow", "red", "blue", "red", "black"}
utils.UnionStringsTo(&list1, list2)
utils.SortStrings(&list1)
assert.Equal(list1, []string{"black", "blue", "green", "red", "yellow"})
assert.Equal([]string{"black", "blue", "green", "red", "yellow"}, list1)
}
func TestUnionStringsList(test *testing.T) {
var assert = assert.New(test)
var list []string
var lists [][]string
lists = [][]string{{"red", "blue", "blue", "green"}, {"yellow", "red", "black"}}
list = utils.UnionStringsList(lists)
utils.SortStrings(&list)
assert.Equal([]string{"black", "blue", "green", "red", "yellow"}, list)
}