Skip to content

Commit

Permalink
Fix #7: Rename pigosat.NewPigosat to pigosat.New
Browse files Browse the repository at this point in the history
This reduces stutter
  • Loading branch information
wkschwartz committed Feb 13, 2015
1 parent 8b67367 commit 6df4339
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
17 changes: 8 additions & 9 deletions pigosat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Designing your model is beyond the scope of this document, but Googling
// "satisfiability problem", "conjunctive normal form", and "DIMACS" are good
// places to start. Once you have your model, create a Pigosat instance p with
// pigosat.NewPigosat, add the model to the instance with p.AddClauses, and
// solve with p.Solve.
// pigosat.New, add the model to the instance with p.AddClauses, and solve with
// p.Solve.
package pigosat

// #cgo CFLAGS: -DNDEBUG -O3
Expand Down Expand Up @@ -38,8 +38,8 @@ const (
Unsatisfiable int = C.PICOSAT_UNSATISFIABLE
)

// Struct Pigosat must be created with NewPigosat and stores the state of the
// solver. Once initialized by NewPigosat, it is safe for concurrent use.
// Struct Pigosat must be created with New and stores the state of the
// solver. It is safe for concurrent use.
//
// You must not use runtime.SetFinalizer with Pigosat objects. Attempting to
// call a method on an uninitialized Pigosat object panics.
Expand Down Expand Up @@ -93,10 +93,9 @@ func cfdopen(file *os.File, mode string) (*C.FILE, error) {
return cfile, nil
}

// NewPigosat returns a new Pigosat instance, ready to have literals added to
// it. The error return value need only be checked if the OutputFile option is
// non-nil.
func NewPigosat(options *Options) (*Pigosat, error) {
// New returns a new Pigosat instance, ready to have literals added to it. The
// error return value need only be checked if the OutputFile option is non-nil.
func New(options *Options) (*Pigosat, error) {
// PicoSAT * picosat_init (void);
p := C.picosat_init()
if options != nil {
Expand Down Expand Up @@ -131,7 +130,7 @@ func NewPigosat(options *Options) (*Pigosat, error) {
}

// delete frees memory associated with p's PicoSAT object. It only needs to be
// called from the runtime.SetFinalizer set in NewPigosat.
// called from the runtime.SetFinalizer set in New.
func (p *Pigosat) delete() {
// For some reason, SetFinalizer needs delete to be idempotent/reentrant.
// That said, since finalizers are only run when there are no more
Expand Down
26 changes: 13 additions & 13 deletions pigosat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func wasExpected(t *testing.T, i int, p *Pigosat, ft *formulaTest, status int,

func TestFormulas(t *testing.T) {
for i, ft := range formulaTests {
p, _ := NewPigosat(nil)
p, _ := New(nil)
p.AddClauses(ft.formula)
status, solution := p.Solve()
wasExpected(t, i, p, &ft, status, solution)
Expand All @@ -230,7 +230,7 @@ func TestIterSolve(t *testing.T) {
var status int
var this, last []bool // solutions
for i, ft := range formulaTests {
p, _ := NewPigosat(nil)
p, _ := New(nil)
p.AddClauses(ft.formula)
count := 0
for status, this = p.Solve(); status == Satisfiable; status, this = p.Solve() {
Expand All @@ -253,7 +253,7 @@ func TestIterSolve(t *testing.T) {
func TestBlockSolution(t *testing.T) {
var status int
for i, ft := range formulaTests {
p, _ := NewPigosat(nil)
p, _ := New(nil)

// Test bad inputs: one too short (remember sol[0] is always blank)
solution := make([]bool, p.Variables())
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestPropLimit(t *testing.T) {
}
seenUn, seenSat := false, false
for limit := uint64(1); limit < 20; limit++ {
p, _ := NewPigosat(&Options{PropagationLimit: limit})
p, _ := New(&Options{PropagationLimit: limit})
p.AddClauses(ft.formula)
status, solution := p.Solve()
if status == Unknown {
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestOutput(t *testing.T) {
}
}()
prefix := fmt.Sprintf("asdf%x ", i)
p, err := NewPigosat(&Options{Verbosity: 1, OutputFile: tmp, Prefix: prefix})
p, err := New(&Options{Verbosity: 1, OutputFile: tmp, Prefix: prefix})
if err != nil {
t.Fatal(err)
}
Expand All @@ -358,13 +358,13 @@ func TestOutput(t *testing.T) {
// Without MeasureAllCalls, AddClasuses is not measured. With it, it is.
func TestMeasureAllCalls(t *testing.T) {
for i, ft := range formulaTests {
p, _ := NewPigosat(nil)
p, _ := New(nil)
p.AddClauses(ft.formula)
if p.Seconds() != 0 {
t.Errorf("Test %d: Seconds without MeasureAllCalls should not "+
"measure AddClauses, but p.Seconds() == %v", i, p.Seconds())
}
p, _ = NewPigosat(&Options{MeasureAllCalls: true})
p, _ = New(&Options{MeasureAllCalls: true})
p.AddClauses(ft.formula)
if p.Seconds() == 0 {
t.Errorf("Test %d: Seconds with MeasureAllCalls should measure "+
Expand All @@ -388,7 +388,7 @@ func assertPanics(t *testing.T, test, method string, f func()) {
// Test that method calls on uninitialized or deleted objects panic
func TestUninitializedOrDeleted(t *testing.T) {
var a, b *Pigosat
b, _ = NewPigosat(nil)
b, _ = New(nil)
b.delete()
for name, p := range map[string]*Pigosat{"uninit": a, "deleted": b} {
assertPanics(t, name, "AddClauses", func() {
Expand Down Expand Up @@ -419,7 +419,7 @@ func TestPrint(t *testing.T) {
t.Error(err)
}
}()
p, err := NewPigosat(nil)
p, err := New(nil)
p.AddClauses(ft.formula)
p.Print(tmp)
// Now we make sure the file was written.
Expand All @@ -436,7 +436,7 @@ func TestPrint(t *testing.T) {

// This is the example from the README.
func Example_readme() {
p, _ := NewPigosat(nil)
p, _ := New(nil)
p.AddClauses([][]int32{{1, 2}, {1}, {-2}})
fmt.Printf("# variables == %d\n", p.Variables())
fmt.Printf("# clauses == %d\n", p.AddedOriginalClauses())
Expand Down Expand Up @@ -466,7 +466,7 @@ func BenchmarkSolve(b *testing.B) {
formula := formulaTests[benchTest].formula
b.ResetTimer()
for i := 0; i < b.N; i++ {
p, _ := NewPigosat(nil)
p, _ := New(nil)
p.AddClauses(formula)
_, _ = p.Solve()
}
Expand All @@ -477,7 +477,7 @@ func BenchmarkSolve(b *testing.B) {
func BenchmarkCreate(b *testing.B) {
var p *Pigosat
for i := 0; i < b.N; i++ {
p, _ = NewPigosat(nil)
p, _ = New(nil)
}
// Shut the compiler up about not using p.
b.StopTimer()
Expand All @@ -491,7 +491,7 @@ func BenchmarkAddClauses(b *testing.B) {
formula := formulaTests[benchTest].formula
b.ResetTimer()
for i := 0; i < b.N; i++ {
p, _ := NewPigosat(nil)
p, _ := New(nil)
b.StartTimer()
p.AddClauses(formula)
b.StopTimer()
Expand Down

0 comments on commit 6df4339

Please sign in to comment.