func TestTautology(t *testing.T) {
	a, b, nb := literal.ConstructTestLiterals()

	clause := Clause{}
	clause.Append(a)
	clause.Append(b)

	clause2 := clause.Copy()
	clause2.Append(nb)

	cases := []struct {
		clause Clause
		want   bool
	}{
		{clause, false},
		{clause2, true},
	}

	for _, c := range cases {
		got := c.clause.Tautology()
		if got != c.want {
			t.Errorf("%q.Tautology() == %t, want %t", c.clause, got, c.want)
		}
	}
}
Beispiel #2
0
//ConstructMoreTestClauses used by the tests
func ConstructMoreTestClauses() (Clause, Clause, Clause, Clause) {
	a, b, nb := literal.ConstructTestLiterals()
	na := a.Negation()
	c, d, _ := literal.ConstructMoreTestLiterals()

	one := Clause{}
	one.Append(a)

	two := Clause{}
	two.Append(a)
	two.Append(b)

	three := Clause{}
	three.Append(na)
	three.Append(b)
	three.Append(c)

	four := Clause{}
	four.Append(d)
	four.Append(b)
	four.Append(nb)

	return one, two, three, four

}
Beispiel #3
0
func TestPrint(t *testing.T) {
	a, b, nb := literal.ConstructTestLiterals()

	//case1 -- empty clause
	c1 := Clause{}
	got := c1.String()
	want := "{}"
	if got != want {
		t.Errorf("String() == %q, want %q", got, want)
	}

	//case2 -- 1 thing
	c2 := Clause{}
	c2.Append(a)
	got = c2.String()
	want = "{A}"
	if got != want {
		t.Errorf("String() == %q, want %q", got, want)
	}

	//case 3 -- 2 things
	c3 := Clause{}
	c3.Append(b)
	c3.Append(a)
	got = c3.String()
	want = "{A, B}"
	if got != want {
		t.Errorf("String() == %q, want %q", got, want)
	}

	//case 4 -- 3 things
	c4 := Clause{}
	c4.Append(nb)
	c4.Append(b)
	c4.Append(a)
	got = c4.String()
	want = "{A, B, ~B}"
	if got != want {
		t.Errorf("String() == %q, want %q", got, want)
	}

}
Beispiel #4
0
//ConstructTestClauses used by the tests
func ConstructTestClauses() (Clause, Clause, Clause, Clause) {
	a, b, nb := literal.ConstructTestLiterals()

	empty := Clause{}

	one := Clause{}
	one.Append(a)

	two := Clause{}
	two.Append(a)
	two.Append(b)

	three := Clause{}
	three.Append(a)
	three.Append(b)
	three.Append(nb)

	return empty, one, two, three

}
func TestReduce(t *testing.T) {

	a, b, _ := literal.ConstructTestLiterals()
	c := literal.Literal{Name: "C", Negated: false}
	one, two, three, four := clause.ConstructMoreTestClauses()
	clauseSet := ClauseSet{}
	clauseSet.Append(one)
	clauseSet.Append(two)
	clauseSet.Append(three)
	clauseSet.Append(four)

	cases := []struct {
		lit  literal.Literal
		want string
	}{
		{a, "{{B, C}}"},
		{b, "{{A}}"},
		{a.Negation(), "{{}, {B}}"},
		{c, "{{A}, {A, B}}"},
	}

	for _, c := range cases {
		cs := clauseSet.Copy()
		before := cs.String() //{{A}, {~A}, {A, B}, {A, ~B}}
		new := cs.Reduce(c.lit)
		post := cs.String()
		got := new.String()

		if got != c.want {
			t.Errorf("%q.Reduce(%q) == %q, want %q", before, c.lit, got, c.want)
		}
		if before != post {
			t.Errorf("before (%q) != post (%q); cs changed", before, post)
		}
	}
}
Beispiel #6
0
func TestCopy(t *testing.T) {
	a, b, _ := literal.ConstructTestLiterals()

	//case0 -- empty Clause, don't change
	c := Clause{}
	copy := c.Copy()
	if !Equals(c, copy) {
		t.Errorf("%q.Copy() == %q, want %q", c, copy, c)
	}

	//case1 -- empty Clause, change copy
	c = Clause{}
	copy = c.Copy()
	copy.Append(a)
	if Equals(c, copy) {
		t.Errorf("copy (%q) == original (%q)", copy, c)
	}

	//case2 -- 1 thing in Clause, don't change
	c = Clause{}
	c.Append(a)
	copy = c.Copy()
	if !Equals(c, copy) {
		t.Errorf("%q.Copy() == %q, want %q", c, copy, c)
	}

	//case3 -- 1 thing in Clause, change copy
	c = Clause{}
	c.Append(a)
	copy = c.Copy()
	copy.Append(b)
	if Equals(c, copy) {
		t.Errorf("copy (%q) == original (%q)", copy, c)
	}

}
func TestAppend(t *testing.T) {
	empty, one, two, _ := clause.ConstructTestClauses()

	//case0 -- 1 clause
	cs := ClauseSet{}
	cs.Append(empty)
	got := cs.String()
	want := "{{}}"
	if got != want {
		t.Errorf("Append(%q) == %q, want %q", empty, got, want)
	}

	//case1 -- 2 things in ClauseSet (added in sorted order)
	cs = ClauseSet{}
	cs.Append(empty)
	cs.Append(one)
	got = cs.String()
	want = "{{}, {A}}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", empty, one, got, want)
	}

	//case2 -- 2 things in ClauseSet (added in non-sorted order)
	cs = ClauseSet{}
	cs.Append(one)
	cs.Append(empty)
	got = cs.String()
	want = "{{}, {A}}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", one, empty, got, want)
	}

	//case3 -- 3 things in ClauseSet (added in sorted order)
	cs = ClauseSet{}
	cs.Append(empty)
	cs.Append(one)
	cs.Append(two)
	got = cs.String()
	want = "{{}, {A}, {A, B}}"
	if got != want {
		t.Errorf("Append(%q, %q, %q) == %q, want %q", empty, one, two, got, want)
	}

	//case4 -- 3 things in ClauseSet (added in non-sorted order)
	cs = ClauseSet{}
	cs.Append(two)
	cs.Append(empty)
	cs.Append(one)
	got = cs.String()
	want = "{{}, {A}, {A, B}}"
	if got != want {
		t.Errorf("Append(%q, %q, %q) == %q, want %q", two, empty, one, got, want)
	}

	a, _, nb := literal.ConstructTestLiterals()
	nOne := clause.Clause{}
	nOne.Append(a.Negation())

	nTwo := clause.Clause{}
	nTwo.Append(a)
	nTwo.Append(nb)

	//case5 -- 2 things in ClauseSet -- same length, but one less than other (added in sorted order)
	cs = ClauseSet{}
	cs.Append(one)
	cs.Append(nOne)
	got = cs.String()
	want = "{{A}, {~A}}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", one, nOne, got, want)
	}

	//case6 -- 2 things in ClauseSet -- same length, but one less than other (added in non-sorted order)
	cs = ClauseSet{}
	cs.Append(nOne)
	cs.Append(one)
	got = cs.String()
	want = "{{A}, {~A}}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", nOne, one, got, want)
	}

	//case7 -- 2 things in ClauseSet -- same length, but one less than other (added in sorted order)
	cs = ClauseSet{}
	cs.Append(two)
	cs.Append(nTwo)
	got = cs.String()
	want = "{{A, B}, {A, ~B}}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", two, nTwo, got, want)
	}

	//case7 -- 2 things in ClauseSet -- same length, but one less than other (added in non-sorted order)
	cs = ClauseSet{}
	cs.Append(nTwo)
	cs.Append(two)
	got = cs.String()
	want = "{{A, B}, {A, ~B}}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", nTwo, two, got, want)
	}

}
func TestRemoveIndex(t *testing.T) {
	a, b, nb := literal.ConstructTestLiterals()

	//case0 -- remove from empty clause
	clause := Clause{}
	before := clause.String()
	clause.Remove(a)
	got := clause.String()
	want := "{}"
	if got != want {
		t.Errorf("%q.Remove(%q) == %q, want %q", before, a, got, want)
	}

	//case1 -- remove only literal from the clause
	clause = Clause{}
	clause.Append(a)
	before = clause.String()
	clause.Remove(a)
	got = clause.String()
	want = "{}"
	if got != want {
		t.Errorf("%q.Remove(%q) == %q, want %q", before, a, got, want)
	}

	//case2 -- 2 different literals, remove first
	clause = Clause{}
	clause.Append(a)
	clause.Append(b)
	before = clause.String()
	clause.Remove(a)
	got = clause.String()
	want = "{B}"
	if got != want {
		t.Errorf("%q.Remove(%q) == %q, want %q", before, a, got, want)
	}

	//case3 -- 2 different literals, remove second
	clause = Clause{}
	clause.Append(a)
	clause.Append(b)
	before = clause.String()
	clause.Remove(b)
	got = clause.String()
	want = "{A}"
	if got != want {
		t.Errorf("%q.Remove(%q) == %q, want %q", before, b, got, want)
	}

	//case4 -- 3 different literals, remove first
	clause = Clause{}
	clause.Append(a)
	clause.Append(b)
	clause.Append(nb)
	before = clause.String()
	clause.Remove(a)
	got = clause.String()
	want = "{B, ~B}"
	if got != want {
		t.Errorf("%q.Remove(%q) == %q, want %q", before, a, got, want)
	}

	//case5 -- 3 different literals, remove second
	clause = Clause{}
	clause.Append(a)
	clause.Append(b)
	clause.Append(nb)
	before = clause.String()
	clause.Remove(b)
	got = clause.String()
	want = "{A, ~B}"
	if got != want {
		t.Errorf("%q.Remove(%q) == %q, want %q", before, b, got, want)
	}

	//case6 -- 3 different literals, remove third
	clause = Clause{}
	clause.Append(a)
	clause.Append(b)
	clause.Append(nb)
	before = clause.String()
	clause.Remove(nb)
	got = clause.String()
	want = "{A, B}"
	if got != want {
		t.Errorf("%q.Remove(%q) == %q, want %q", before, nb, got, want)
	}
}
func TestAppend(t *testing.T) {
	a, b, nb := literal.ConstructTestLiterals()

	//case0 -- 1 literal
	c0 := Clause{}
	c0.Append(a)
	got := c0.String()
	want := "{A}"
	if got != want {
		t.Errorf("Append(%q) == %q, want %q", a, got, want)
	}

	//case1 -- 2 of the same literal
	c1 := Clause{}
	c1.Append(a)
	c1.Append(a)
	got = c1.String()
	want = "{A}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", a, a, got, want)
	}

	//case2 -- 2 different literals (added in sorted order)
	c2 := Clause{}
	c2.Append(a)
	c2.Append(b)
	got = c2.String()
	want = "{A, B}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", a, b, got, want)
	}

	//case3 -- 2 different literals (added in non-sorted order)
	c3 := Clause{}
	c3.Append(a)
	c3.Append(b)
	got = c3.String()
	want = "{A, B}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", b, a, got, want)
	}

	//case4 -- 2 opposite literals (added in sorted order)
	c4 := Clause{}
	c4.Append(b)
	c4.Append(nb)
	got = c4.String()
	want = "{B, ~B}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", b, nb, got, want)
	}

	//case5 -- 2 opposite literals (added in non-sorted order)
	c5 := Clause{}
	c5.Append(nb)
	c5.Append(b)
	got = c5.String()
	want = "{B, ~B}"
	if got != want {
		t.Errorf("Append(%q, %q) == %q, want %q", nb, b, got, want)
	}

	//case6 -- 3  literals (added in sorted order)
	c6 := Clause{}
	c6.Append(a)
	c6.Append(b)
	c6.Append(nb)
	got = c6.String()
	want = "{A, B, ~B}"
	if got != want {
		t.Errorf("Append(%q, %q, %q) == %q, want %q", a, b, nb, got, want)
	}

	//case7 -- 3  literals (added in non-sorted order)
	c7 := Clause{}
	c7.Append(nb)
	c7.Append(b)
	c7.Append(a)
	got = c7.String()
	want = "{A, B, ~B}"
	if got != want {
		t.Errorf("Append(%q, %q, %q) == %q, want %q", nb, b, a, got, want)
	}

}
func TestEquality(t *testing.T) {
	a, b, nb := literal.ConstructTestLiterals()

	//case0 -- empty clauses
	c0 := Clause{}
	c1 := Clause{}

	got := Equals(c0, c1)
	want := true
	if got != want {
		t.Errorf("Equals(%q, %q) == %t, want %t", c0, c1, got, want)
	}

	//case1 -- one thing in each (true)
	c0 = Clause{}
	c0.Append(a)
	c1 = Clause{}
	c1.Append(a)

	got = Equals(c0, c1)
	want = true
	if got != want {
		t.Errorf("Equals(%q, %q) == %t, want %t", c0, c1, got, want)
	}

	//case2 -- one thing in each (false)
	c0 = Clause{}
	c0.Append(a)
	c1 = Clause{}
	c1.Append(b)

	got = Equals(c0, c1)
	want = false
	if got != want {
		t.Errorf("Equals(%q, %q) == %t, want %t", c0, c1, got, want)
	}

	//case3 -- two things in each (true)
	c0 = Clause{}
	c0.Append(a)
	c0.Append(b)
	c1 = Clause{}
	c1.Append(a)
	c1.Append(b)

	got = Equals(c0, c1)
	want = true
	if got != want {
		t.Errorf("Equals(%q, %q) == %t, want %t", c0, c1, got, want)
	}

	//case4 -- two things in each (false)
	c0 = Clause{}
	c0.Append(a)
	c0.Append(b)
	c1 = Clause{}
	c1.Append(a)
	c1.Append(nb)

	got = Equals(c0, c1)
	want = false
	if got != want {
		t.Errorf("Equals(%q, %q) == %t, want %t", c0, c1, got, want)
	}

}