MockMatcher Go ctor binding.

Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/a7aa7791
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/a7aa7791
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/a7aa7791

Branch: refs/heads/master
Commit: a7aa779141065a2964a70357d746522ae231d0aa
Parents: 2b8b632
Author: Marvin Humphrey <[email protected]>
Authored: Wed Aug 12 16:58:36 2015 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Wed Sep 9 18:25:23 2015 -0700

----------------------------------------------------------------------
 go/build.go            |  4 ++++
 go/lucy/search.go      | 25 +++++++++++++++++++++++++
 go/lucy/search_test.go | 19 +++++++++++++++++++
 3 files changed, 48 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/a7aa7791/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
index 828a79b..5c84645 100644
--- a/go/build.go
+++ b/go/build.go
@@ -171,6 +171,10 @@ func specClasses(parcel *cfc.Parcel) {
        bitVecBinding := cfc.NewGoClass(parcel, "Lucy::Object::BitVector")
        bitVecBinding.SpecMethod("To_Array", "ToArray() []bool")
        bitVecBinding.Register()
+
+       mockMatcherBinding := cfc.NewGoClass(parcel, 
"LucyX::Search::MockMatcher")
+       mockMatcherBinding.SetSuppressCtor(true)
+       mockMatcherBinding.Register()
 }
 
 func build() {

http://git-wip-us.apache.org/repos/asf/lucy/blob/a7aa7791/go/lucy/search.go
----------------------------------------------------------------------
diff --git a/go/lucy/search.go b/go/lucy/search.go
index 8d18ea9..0c8dbb2 100644
--- a/go/lucy/search.go
+++ b/go/lucy/search.go
@@ -25,8 +25,16 @@ package lucy
 #include "Lucy/Search/ANDQuery.h"
 #include "Lucy/Search/ORQuery.h"
 #include "Lucy/Document/HitDoc.h"
+#include "LucyX/Search/MockMatcher.h"
+#include "Clownfish/Blob.h"
 #include "Clownfish/Hash.h"
 #include "Clownfish/HashIterator.h"
+
+static inline void
+float32_set(float *floats, size_t i, float value) {
+       floats[i] = value;
+}
+
 */
 import "C"
 import "fmt"
@@ -183,3 +191,20 @@ func NewORQuery(children []Query) ORQuery {
        cfObj := C.lucy_ORQuery_new(childrenC)
        return WRAPORQuery(unsafe.Pointer(cfObj))
 }
+
+func newMockMatcher(docIDs []int32, scores []float32) MockMatcher {
+       docIDsconv := NewI32Array(docIDs)
+       docIDsCF := (*C.lucy_I32Array)(unsafe.Pointer(docIDsconv.TOPTR()))
+       var blob *C.cfish_Blob = nil
+       if scores != nil {
+               size := len(scores) * 4
+               floats := (*C.float)(C.malloc(C.size_t(size)))
+               for i := 0; i < len(scores); i++ {
+                       C.float32_set(floats, C.size_t(i), C.float(scores[i]))
+               }
+               blob = 
C.cfish_Blob_new_steal((*C.char)(unsafe.Pointer(floats)), C.size_t(size))
+               defer C.cfish_decref(unsafe.Pointer(blob))
+       }
+       matcher := C.lucy_MockMatcher_new(docIDsCF, blob)
+       return WRAPMockMatcher(unsafe.Pointer(matcher))
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/a7aa7791/go/lucy/search_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/search_test.go b/go/lucy/search_test.go
index d566553..c5e2cc2 100644
--- a/go/lucy/search_test.go
+++ b/go/lucy/search_test.go
@@ -249,3 +249,22 @@ func TestLeafQueryBasics(t *testing.T) {
        checkQueryEquals(t, query)
        checkQueryToStringHasFoo(t, query)
 }
+
+func TestMockMatcherBasics(t *testing.T) {
+       matcher := newMockMatcher([]int32{42, 43, 100}, []float32{1.5, 1.5, 
1.5})
+       if got := matcher.Next(); got != 42 {
+               t.Error("Next: %d", got)
+       }
+       if got := matcher.GetDocID(); got != 42 {
+               t.Error("GetDocID: %d", got)
+       }
+       if score := matcher.Score(); score != 1.5 {
+               t.Error("Score: %f", score)
+       }
+       if got := matcher.Advance(50); got != 100 {
+               t.Error("Advance: %d", got)
+       }
+       if got := matcher.Next(); got != 0 {
+               t.Error("Next (iteration finished): %d", got)
+       }
+}

Reply via email to