Skip to content

Commit

Permalink
fix dimacs variable source
Browse files Browse the repository at this point in the history
Signed-off-by: Per Goncalves da Silva <[email protected]>
  • Loading branch information
Per Goncalves da Silva authored and perdasilva committed Aug 15, 2023
1 parent 6c3d457 commit f4b138b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/dimacs/dimacs_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (d *ConstraintGenerator) GetVariables(_ context.Context) ([]deppy.Variable,
for _, id := range d.dimacs.variables {
variable := input.NewSimpleVariable(deppy.IdentifierFromString(id))
variables = append(variables, variable)
varMap[variable.Identifier()] = variable
}

// create constraints out of the clauses
Expand Down
24 changes: 24 additions & 0 deletions cmd/dimacs/dimacs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package dimacs_test

import (
"bytes"
"context"
"testing"

"github.com/operator-framework/deppy/pkg/deppy"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -34,3 +37,24 @@ var _ = Describe("Dimacs", func() {
Expect(d.Clauses()).To(Equal([]string{"1 2 3"}))
})
})

var _ = Describe("Dimacs Variable Source", func() {
It("should create variables for a dimacs problem", func() {
problem := "p cnf 3 1\n1 2 3 0\n"
d, err := dimacs.NewDimacs(bytes.NewReader([]byte(problem)))
Expect(err).ToNot(HaveOccurred())
vs := dimacs.NewDimacsVariableSource(d)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
variables, err := vs.GetVariables(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(variables).To(HaveLen(3))

Expect(variables[0].Identifier()).To(Equal(deppy.Identifier("1")))
Expect(variables[0].Constraints()).To(HaveLen(1))
Expect(variables[1].Identifier()).To(Equal(deppy.Identifier("2")))
Expect(variables[1].Constraints()).To(HaveLen(1))
Expect(variables[2].Identifier()).To(Equal(deppy.Identifier("3")))
Expect(variables[2].Constraints()).To(BeEmpty())
})
})

0 comments on commit f4b138b

Please sign in to comment.