From beb7312d07265cfe0044319997437ad1f3f81284 Mon Sep 17 00:00:00 2001 From: Per Goncalves da Silva Date: Thu, 3 Aug 2023 11:19:06 +0200 Subject: [PATCH] fix dimacs variable source Signed-off-by: Per Goncalves da Silva --- cmd/dimacs/dimacs_constraints.go | 1 + cmd/dimacs/dimacs_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/cmd/dimacs/dimacs_constraints.go b/cmd/dimacs/dimacs_constraints.go index d28f6c3..6741495 100644 --- a/cmd/dimacs/dimacs_constraints.go +++ b/cmd/dimacs/dimacs_constraints.go @@ -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 diff --git a/cmd/dimacs/dimacs_test.go b/cmd/dimacs/dimacs_test.go index d18b65a..e221ea9 100644 --- a/cmd/dimacs/dimacs_test.go +++ b/cmd/dimacs/dimacs_test.go @@ -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" @@ -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()) + }) +})