Skip to content

Commit

Permalink
Removes return error from NewDeppySolver
Browse files Browse the repository at this point in the history
It never returns an error

Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Jun 1, 2023
1 parent 9a7655c commit e2ea021
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 31 deletions.
7 changes: 2 additions & 5 deletions cmd/dimacs/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewDimacsCommand() *cobra.Command {
Long: `Solves a sat problem given in dimacs format. For instance:
c
c this is a comment
c header: p cnf <number of variable> <number of clauses>
c header: p cnf <number of variable> <number of clauses>
p cnf 2 2
c clauses end in zero, negative means 'not'
c 0 (zero) is not a valid literal
Expand Down Expand Up @@ -53,10 +53,7 @@ func solve(path string) error {
}

// build solver
so, err := solver.NewDeppySolver(NewDimacsEntitySource(dimacs), NewDimacsVariableSource(dimacs))
if err != nil {
return err
}
so := solver.NewDeppySolver(NewDimacsEntitySource(dimacs), NewDimacsVariableSource(dimacs))

// get solution
solution, err := so.Solve(context.Background(), solver.AddAllVariablesToSolution())
Expand Down
5 changes: 1 addition & 4 deletions cmd/sudoku/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ func NewSudokuCommand() *cobra.Command {
func solve() error {
// build solver
sudoku := NewSudoku()
so, err := solver.NewDeppySolver(sudoku, sudoku)
if err != nil {
return err
}
so := solver.NewDeppySolver(sudoku, sudoku)

// get solution
solution, err := so.Solve(context.Background())
Expand Down
4 changes: 2 additions & 2 deletions pkg/deppy/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ type DeppySolver struct {
variableSource input.VariableSource
}

func NewDeppySolver(entitySource input.EntitySource, variableSource input.VariableSource) (*DeppySolver, error) {
func NewDeppySolver(entitySource input.EntitySource, variableSource input.VariableSource) *DeppySolver {
return &DeppySolver{
entitySource: entitySource,
variableSource: variableSource,
}, nil
}
}

func (d DeppySolver) Solve(ctx context.Context, options ...Option) (*Solution, error) {
Expand Down
30 changes: 10 additions & 20 deletions pkg/deppy/solver/solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("2"),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.SelectedVariables()).To(MatchAllKeys(Keys{
Expand All @@ -67,8 +66,7 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("2", constraint.Mandatory()),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.SelectedVariables()).To(MatchAllKeys(Keys{
Expand All @@ -85,8 +83,7 @@ var _ = Describe("Entity", func() {
}
s := NewEntitySource(variables)

so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.SelectedVariables()).To(MatchAllKeys(Keys{
Expand All @@ -102,17 +99,15 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("3"),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.Error()).To(HaveOccurred())
})

It("should return peripheral errors", func() {
s := NewEntitySource(nil)
so, err := solver.NewDeppySolver(s, FailingVariableSource{})
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, FailingVariableSource{})
solution, err := so.Solve(context.Background())
Expect(err).To(HaveOccurred())
Expect(solution).To(BeNil())
Expand All @@ -125,8 +120,7 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("3", constraint.Prohibited()),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.SelectedVariables()).To(MatchAllKeys(Keys{
Expand All @@ -142,8 +136,7 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("3", constraint.Prohibited()),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background(), solver.AddAllVariablesToSolution())
Expect(err).ToNot(HaveOccurred())
Expect(solution.AllVariables()).To(Equal([]deppy.Variable{
Expand All @@ -161,8 +154,7 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("4"),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.SelectedVariables()).To(MatchAllKeys(Keys{
Expand All @@ -179,8 +171,7 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("4"),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.SelectedVariables()).To(MatchAllKeys(Keys{
Expand All @@ -199,8 +190,7 @@ var _ = Describe("Entity", func() {
input.NewSimpleVariable("6"),
}
s := NewEntitySource(variables)
so, err := solver.NewDeppySolver(s, s)
Expect(err).ToNot(HaveOccurred())
so := solver.NewDeppySolver(s, s)
solution, err := so.Solve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution.SelectedVariables()).To(MatchAllKeys(Keys{
Expand Down

0 comments on commit e2ea021

Please sign in to comment.