diff options
-rw-r--r-- | searchlib/src/tests/tensor/distance_functions/distance_functions_test.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/searchlib/src/tests/tensor/distance_functions/distance_functions_test.cpp b/searchlib/src/tests/tensor/distance_functions/distance_functions_test.cpp index 9d240bc688a..f11a28b8716 100644 --- a/searchlib/src/tests/tensor/distance_functions/distance_functions_test.cpp +++ b/searchlib/src/tests/tensor/distance_functions/distance_functions_test.cpp @@ -142,6 +142,46 @@ TEST(DistanceFunctionsTest, innerproduct_gives_expected_score) EXPECT_LT(i44, 0.000001); } +TEST(DistanceFunctionsTest, hamming_gives_expected_score) +{ + auto ct = vespalib::eval::ValueType::CellType::DOUBLE; + + auto hamming = make_distance_function(DistanceMetric::Hamming, ct); + + std::vector<std::vector<double>> + points{{0.0, 0.0, 0.0}, + {1.0, 0.0, 0.0}, + {0.0, 1.0, 1.0}, + {2.0, 2.0, 2.0}, + {0.5, 0.5, 0.5}, + {0.0,-1.0, 1.0}, + {1.0, 1.0, 1.0}}; + for (const auto & p : points) { + double h0 = hamming->calc(t(p), t(p)); + EXPECT_EQ(h0, 0.0); + EXPECT_EQ(hamming->to_rawscore(h0), 1.0); + } + double d12 = hamming->calc(t(points[1]), t(points[2])); + EXPECT_EQ(d12, 3.0); + EXPECT_DOUBLE_EQ(hamming->to_rawscore(d12), 1.0/(1.0 + 3.0)); + + double d16 = hamming->calc(t(points[1]), t(points[6])); + EXPECT_EQ(d16, 2.0); + EXPECT_DOUBLE_EQ(hamming->to_rawscore(d16), 1.0/(1.0 + 2.0)); + + double d23 = hamming->calc(t(points[2]), t(points[3])); + EXPECT_EQ(d23, 3.0); + EXPECT_DOUBLE_EQ(hamming->to_rawscore(d23), 1.0/(1.0 + 3.0)); + + double d24 = hamming->calc(t(points[2]), t(points[4])); + EXPECT_EQ(d24, 3.0); + EXPECT_DOUBLE_EQ(hamming->to_rawscore(d24), 1.0/(1.0 + 3.0)); + + double d25 = hamming->calc(t(points[2]), t(points[5])); + EXPECT_EQ(d25, 1.0); + EXPECT_DOUBLE_EQ(hamming->to_rawscore(d25), 1.0/(1.0 + 1.0)); +} + TEST(GeoDegreesTest, gives_expected_score) { auto ct = vespalib::eval::ValueType::CellType::DOUBLE; |