You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2012/04/06 18:22:54 UTC

[lucy-commits] svn commit: r1310447 - in /lucy/trunk: core/Lucy/Test/Highlight/ perl/buildlib/Lucy/Build/Binding/ perl/t/ perl/t/binding/ perl/t/core/

Author: nwellnhof
Date: Fri Apr  6 16:22:53 2012
New Revision: 1310447

URL: http://svn.apache.org/viewvc?rev=1310447&view=rev
Log:
LUCY-149 Convert t/310-heat_map.t to C

Added:
    lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.c
    lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.cfh
    lucy/trunk/perl/t/binding/310-heat_map.t
    lucy/trunk/perl/t/core/310-heat_map.t
Removed:
    lucy/trunk/perl/t/310-heat_map.t
Modified:
    lucy/trunk/core/Lucy/Test/Highlight/TestHighlighter.c
    lucy/trunk/perl/buildlib/Lucy/Build/Binding/Misc.pm

Added: lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.c
URL: http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.c?rev=1310447&view=auto
==============================================================================
--- lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.c (added)
+++ lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.c Fri Apr  6 16:22:53 2012
@@ -0,0 +1,174 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define C_LUCY_TESTHIGHLIGHTER
+#include "Lucy/Util/ToolSet.h"
+
+#include "Lucy/Test.h"
+#include "Lucy/Test/Highlight/TestHeatMap.h"
+#include "Lucy/Highlight/HeatMap.h"
+
+#include "Lucy/Search/Span.h"
+
+static void
+test_calc_proximity_boost(TestBatch *batch) {
+    VArray  *spans    = VA_new(0);
+    HeatMap *heat_map = HeatMap_new(spans, 133);
+    Span    *span1    = Span_new(  0, 10, 1.0f);
+    Span    *span2    = Span_new( 10, 10, 1.0f);
+    Span    *span3    = Span_new(  5,  4, 1.0f);
+    Span    *span4    = Span_new(100, 10, 1.0f);
+    Span    *span5    = Span_new(150, 10, 1.0f);
+
+    float big_boost     = HeatMap_Calc_Proximity_Boost(heat_map, span1, span2);
+    float eq_big_boost  = HeatMap_Calc_Proximity_Boost(heat_map, span1, span3);
+    float smaller_boost = HeatMap_Calc_Proximity_Boost(heat_map, span1, span4);
+    float zero_boost    = HeatMap_Calc_Proximity_Boost(heat_map, span1, span5);
+
+    TEST_TRUE(batch, big_boost == eq_big_boost,
+              "overlapping and abutting produce the same proximity boost");
+    TEST_TRUE(batch, big_boost > smaller_boost, "closer is better");
+    TEST_TRUE(batch, zero_boost == 0.0,
+              "distance outside of window yields no prox boost");
+
+    DECREF(span1);
+    DECREF(span2);
+    DECREF(span3);
+    DECREF(span4);
+    DECREF(span5);
+    DECREF(heat_map);
+    DECREF(spans);
+}
+
+static void
+test_flatten_spans(TestBatch *batch) {
+    VArray  *spans    = VA_new(8);
+    VArray  *wanted   = VA_new(8);
+    HeatMap *heat_map = HeatMap_new(spans, 133);
+
+    VArray *flattened, *boosts;
+
+    VA_Push(spans, (Obj*)Span_new(10, 10, 1.0f));
+    VA_Push(spans, (Obj*)Span_new(16, 14, 2.0f));
+    flattened = HeatMap_Flatten_Spans(heat_map, spans);
+    VA_Push(wanted, (Obj*)Span_new(10,  6, 1.0f));
+    VA_Push(wanted, (Obj*)Span_new(16,  4, 3.0f));
+    VA_Push(wanted, (Obj*)Span_new(20, 10, 2.0f));
+    TEST_TRUE(batch, VA_Equals(flattened, (Obj*)wanted),
+              "flatten two overlapping spans");
+    VA_Clear(wanted);
+    boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans);
+    VA_Push(wanted, (Obj*)Span_new(10, 20, 3.0f));
+    TEST_TRUE(batch, VA_Equals(boosts, (Obj*)wanted),
+              "prox boosts for overlap");
+    VA_Clear(wanted);
+    VA_Clear(spans);
+    DECREF(boosts);
+    DECREF(flattened);
+
+    VA_Push(spans, (Obj*)Span_new(10, 10, 1.0f));
+    VA_Push(spans, (Obj*)Span_new(16, 14, 2.0f));
+    VA_Push(spans, (Obj*)Span_new(50,  1, 1.0f));
+    flattened = HeatMap_Flatten_Spans(heat_map, spans);
+    VA_Push(wanted, (Obj*)Span_new(10,  6, 1.0f));
+    VA_Push(wanted, (Obj*)Span_new(16,  4, 3.0f));
+    VA_Push(wanted, (Obj*)Span_new(20, 10, 2.0f));
+    VA_Push(wanted, (Obj*)Span_new(50,  1, 1.0f));
+    TEST_TRUE(batch, VA_Equals(flattened, (Obj*)wanted),
+              "flatten two overlapping spans, leave hole, then third span");
+    VA_Clear(wanted);
+    boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans);
+    TEST_TRUE(batch, VA_Get_Size(boosts) == 2 + 1,
+              "boosts generated for each unique pair, since all were in range");
+    VA_Clear(spans);
+    DECREF(boosts);
+    DECREF(flattened);
+
+    VA_Push(spans, (Obj*)Span_new(10, 10, 1.0f));
+    VA_Push(spans, (Obj*)Span_new(14,  4, 4.0f));
+    VA_Push(spans, (Obj*)Span_new(16, 14, 2.0f));
+    flattened = HeatMap_Flatten_Spans(heat_map, spans);
+    VA_Push(wanted, (Obj*)Span_new(10,  4, 1.0f));
+    VA_Push(wanted, (Obj*)Span_new(14,  2, 5.0f));
+    VA_Push(wanted, (Obj*)Span_new(16,  2, 7.0f));
+    VA_Push(wanted, (Obj*)Span_new(18,  2, 3.0f));
+    VA_Push(wanted, (Obj*)Span_new(20, 10, 2.0f));
+    TEST_TRUE(batch, VA_Equals(flattened, (Obj*)wanted),
+              "flatten three overlapping spans");
+    VA_Clear(wanted);
+    boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans);
+    TEST_TRUE(batch, VA_Get_Size(boosts) == 2 + 1,
+              "boosts generated for each unique pair, since all were in range");
+    VA_Clear(spans);
+    DECREF(boosts);
+    DECREF(flattened);
+
+    VA_Push(spans, (Obj*)Span_new(10, 10,  1.0f));
+    VA_Push(spans, (Obj*)Span_new(16, 14,  4.0f));
+    VA_Push(spans, (Obj*)Span_new(16, 14,  2.0f));
+    VA_Push(spans, (Obj*)Span_new(30, 10, 10.0f));
+    flattened = HeatMap_Flatten_Spans(heat_map, spans);
+    VA_Push(wanted, (Obj*)Span_new(10,  6,  1.0f));
+    VA_Push(wanted, (Obj*)Span_new(16,  4,  7.0f));
+    VA_Push(wanted, (Obj*)Span_new(20, 10,  6.0f));
+    VA_Push(wanted, (Obj*)Span_new(30, 10, 10.0f));
+    TEST_TRUE(batch, VA_Equals(flattened, (Obj*)wanted),
+              "flatten 4 spans, middle two have identical range");
+    VA_Clear(wanted);
+    boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans);
+    TEST_TRUE(batch, VA_Get_Size(boosts) == 3 + 2 + 1,
+              "boosts generated for each unique pair, since all were in range");
+    VA_Clear(spans);
+    DECREF(boosts);
+    DECREF(flattened);
+
+    VA_Push(spans, (Obj*)Span_new( 10, 10,  1.0f));
+    VA_Push(spans, (Obj*)Span_new( 16,  4,  4.0f));
+    VA_Push(spans, (Obj*)Span_new( 16, 14,  2.0f));
+    VA_Push(spans, (Obj*)Span_new(230, 10, 10.0f));
+    flattened = HeatMap_Flatten_Spans(heat_map, spans);
+    VA_Push(wanted, (Obj*)Span_new( 10,  6,  1.0f));
+    VA_Push(wanted, (Obj*)Span_new( 16,  4,  7.0f));
+    VA_Push(wanted, (Obj*)Span_new( 20, 10,  2.0f));
+    VA_Push(wanted, (Obj*)Span_new(230, 10, 10.0f));
+    TEST_TRUE(batch, VA_Equals(flattened, (Obj*)wanted),
+              "flatten 4 spans, middle two have identical starts but different ends");
+    VA_Clear(wanted);
+    boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans);
+    TEST_TRUE(batch, VA_Get_Size(boosts) == 2 + 1,
+              "boosts not generated for out of range span");
+    VA_Clear(spans);
+    DECREF(boosts);
+    DECREF(flattened);
+
+    DECREF(heat_map);
+    DECREF(wanted);
+    DECREF(spans);
+}
+
+void
+TestHeatMap_run_tests() {
+    TestBatch *batch = TestBatch_new(13);
+
+    TestBatch_Plan(batch);
+
+    test_calc_proximity_boost(batch);
+    test_flatten_spans(batch);
+
+    DECREF(batch);
+}
+
+

Added: lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.cfh
URL: http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.cfh?rev=1310447&view=auto
==============================================================================
--- lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.cfh (added)
+++ lucy/trunk/core/Lucy/Test/Highlight/TestHeatMap.cfh Fri Apr  6 16:22:53 2012
@@ -0,0 +1,24 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel Lucy;
+
+inert class Lucy::Test::Highlight::TestHeatMap {
+    inert void
+    run_tests();
+}
+
+

Modified: lucy/trunk/core/Lucy/Test/Highlight/TestHighlighter.c
URL: http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Test/Highlight/TestHighlighter.c?rev=1310447&r1=1310446&r2=1310447&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Test/Highlight/TestHighlighter.c (original)
+++ lucy/trunk/core/Lucy/Test/Highlight/TestHighlighter.c Fri Apr  6 16:22:53 2012
@@ -414,8 +414,8 @@ test_Find_Sentences(TestBatch *batch, Se
 
     VArray *got = Highlighter_Find_Sentences(highlighter, text, 101, 50);
     VArray *wanted = VA_new(2);
-    VA_push(wanted, (Obj*)Span_new(120, 19, 0.0f));
-    VA_push(wanted, (Obj*)Span_new(140, 19, 0.0f));
+    VA_Push(wanted, (Obj*)Span_new(120, 19, 0.0f));
+    VA_Push(wanted, (Obj*)Span_new(140, 19, 0.0f));
     TEST_TRUE(batch,
               VA_Equals(got, (Obj*)wanted),
               "find_sentences with explicit args");
@@ -431,7 +431,7 @@ test_Find_Sentences(TestBatch *batch, Se
     got = Highlighter_Find_Sentences(highlighter, text, 0, 0);
     wanted = VA_new(15);
     for (int i = 0; i < 15; ++i) {
-        VA_push(wanted, (Obj*)Span_new(i * 20, 19, 0.0f));
+        VA_Push(wanted, (Obj*)Span_new(i * 20, 19, 0.0f));
     }
     TEST_TRUE(batch,
               VA_Equals(got, (Obj*)wanted),
@@ -442,7 +442,7 @@ test_Find_Sentences(TestBatch *batch, Se
     text = (CharBuf*)ZCB_WRAP_STR(" Foo", 4);
     got = Highlighter_Find_Sentences(highlighter, text, 0, 0);
     wanted = VA_new(1);
-    VA_push(wanted, (Obj*)Span_new(1, 3, 0.0f));
+    VA_Push(wanted, (Obj*)Span_new(1, 3, 0.0f));
     TEST_TRUE(batch,
               VA_Equals(got, (Obj*)wanted),
               "Skip leading whitespace but get first sentence");

Modified: lucy/trunk/perl/buildlib/Lucy/Build/Binding/Misc.pm
URL: http://svn.apache.org/viewvc/lucy/trunk/perl/buildlib/Lucy/Build/Binding/Misc.pm?rev=1310447&r1=1310446&r2=1310447&view=diff
==============================================================================
--- lucy/trunk/perl/buildlib/Lucy/Build/Binding/Misc.pm (original)
+++ lucy/trunk/perl/buildlib/Lucy/Build/Binding/Misc.pm Fri Apr  6 16:22:53 2012
@@ -291,6 +291,9 @@ PPCODE:
         lucy_TestVArray_run_tests();
     }
     // Lucy::Highlight
+    else if (strEQ(package, "TestHeatMap")) {
+        lucy_TestHeatMap_run_tests();
+    }
     else if (strEQ(package, "TestHighlighter")) {
         lucy_TestHighlighter_run_tests();
     }

Added: lucy/trunk/perl/t/binding/310-heat_map.t
URL: http://svn.apache.org/viewvc/lucy/trunk/perl/t/binding/310-heat_map.t?rev=1310447&view=auto
==============================================================================
--- lucy/trunk/perl/t/binding/310-heat_map.t (added)
+++ lucy/trunk/perl/t/binding/310-heat_map.t Fri Apr  6 16:22:53 2012
@@ -0,0 +1,51 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+use Lucy::Test;
+
+my $heat_map = Lucy::Highlight::HeatMap->new( spans => [], );
+
+my $boost = $heat_map->calc_proximity_boost(
+    span1 => make_span( 0,  10, 1.0 ),
+    span2 => make_span( 10, 10, 1.0 )
+);
+cmp_ok( $boost, '>', 0, "calc_proximity_boost" );
+
+my $spans = make_spans( [ 10, 10, 1.0 ], [ 16, 14, 2.0 ] );
+my $flattened = $heat_map->flatten_spans($spans);
+is( scalar @$flattened, 3, "flatten_spans" );
+my $boosts = $heat_map->generate_proximity_boosts($spans);
+is( scalar @$boosts, 1, "generate_proximity_boosts" );
+
+sub make_span {
+    return Lucy::Search::Span->new(
+        offset => $_[0],
+        length => $_[1],
+        weight => $_[2],
+    );
+}
+
+sub make_spans {
+    my @spans;
+    for my $arg_ref (@_) {
+        push @spans, make_span( @{$arg_ref}[ 0 .. 2 ] );
+    }
+    return \@spans;
+}
+

Added: lucy/trunk/perl/t/core/310-heat_map.t
URL: http://svn.apache.org/viewvc/lucy/trunk/perl/t/core/310-heat_map.t?rev=1310447&view=auto
==============================================================================
--- lucy/trunk/perl/t/core/310-heat_map.t (added)
+++ lucy/trunk/perl/t/core/310-heat_map.t Fri Apr  6 16:22:53 2012
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+use strict;
+use warnings;
+
+use Lucy::Test;
+Lucy::Test::run_tests("TestHeatMap");
+