summaryrefslogtreecommitdiff
path: root/extractor/matching_comparator_test.cc
blob: b8f898cfc9c144aa2bd99a63fdb98613729e4d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <gtest/gtest.h>

#include "matching.h"
#include "matching_comparator.h"

using namespace ::testing;

namespace {

class MatchingComparatorTest : public Test {
 protected:
  virtual void SetUp() {
    comparator = make_shared<MatchingComparator>(1, 20);
  }

  shared_ptr<MatchingComparator> comparator;
};

TEST_F(MatchingComparatorTest, SmallerSentenceId) {
  vector<int> left_locations{1};
  Matching left(left_locations.begin(), 1, 1);
  vector<int> right_locations{100};
  Matching right(right_locations.begin(), 1, 5);
  EXPECT_EQ(-1, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, GreaterSentenceId) {
  vector<int> left_locations{100};
  Matching left(left_locations.begin(), 1, 5);
  vector<int> right_locations{1};
  Matching right(right_locations.begin(), 1, 1);
  EXPECT_EQ(1, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, SmalleraXb) {
  vector<int> left_locations{1};
  Matching left(left_locations.begin(), 1, 1);
  vector<int> right_locations{21};
  Matching right(right_locations.begin(), 1, 1);
  // The matching exceeds the max rule span.
  EXPECT_EQ(-1, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, EqualaXb) {
  vector<int> left_locations{1};
  Matching left(left_locations.begin(), 1, 1);
  vector<int> lower_right_locations{3};
  Matching right(lower_right_locations.begin(), 1, 1);
  EXPECT_EQ(0, comparator->Compare(left, right, 1, true));

  vector<int> higher_right_locations{20};
  right = Matching(higher_right_locations.begin(), 1, 1);
  EXPECT_EQ(0, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, GreateraXb) {
  vector<int> left_locations{1};
  Matching left(left_locations.begin(), 1, 1);
  vector<int> right_locations{2};
  Matching right(right_locations.begin(), 1, 1);
  // The gap between the prefix and the suffix is of size 0, less than the
  // min gap size.
  EXPECT_EQ(1, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, SmalleraXbXc) {
  vector<int> left_locations{1, 3};
  Matching left(left_locations.begin(), 2, 1);
  vector<int> right_locations{4, 6};
  // The common part doesn't match.
  Matching right(right_locations.begin(), 2, 1);
  EXPECT_EQ(-1, comparator->Compare(left, right, 1, true));

  // The common part matches, but the rule exceeds the max span.
  vector<int> other_right_locations{3, 21};
  right = Matching(other_right_locations.begin(), 2, 1);
  EXPECT_EQ(-1, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, EqualaXbXc) {
  vector<int> left_locations{1, 3};
  Matching left(left_locations.begin(), 2, 1);
  vector<int> right_locations{3, 5};
  // The leftmost possible match.
  Matching right(right_locations.begin(), 2, 1);
  EXPECT_EQ(0, comparator->Compare(left, right, 1, true));

  // The rightmost possible match.
  vector<int> other_right_locations{3, 20};
  right = Matching(other_right_locations.begin(), 2, 1);
  EXPECT_EQ(0, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, GreateraXbXc) {
  vector<int> left_locations{1, 4};
  Matching left(left_locations.begin(), 2, 1);
  vector<int> right_locations{3, 5};
  // The common part doesn't match.
  Matching right(right_locations.begin(), 2, 1);
  EXPECT_EQ(1, comparator->Compare(left, right, 1, true));
}

TEST_F(MatchingComparatorTest, SmallerabXcXd) {
  vector<int> left_locations{9, 13};
  Matching left(left_locations.begin(), 2, 1);
  // The suffix doesn't start on the next position.
  vector<int> right_locations{11, 13, 15};
  Matching right(right_locations.begin(), 3, 1);
  EXPECT_EQ(-1, comparator->Compare(left, right, 1, false));

  // The common part doesn't match.
  vector<int> other_right_locations{10, 16, 18};
  right = Matching(other_right_locations.begin(), 3, 1);
  EXPECT_EQ(-1, comparator->Compare(left, right, 1, false));
}

TEST_F(MatchingComparatorTest, EqualabXcXd) {
  vector<int> left_locations{10, 13};
  Matching left(left_locations.begin(), 2, 1);
  vector<int> right_locations{11, 13, 15};
  Matching right(right_locations.begin(), 3, 1);
  EXPECT_EQ(0, comparator->Compare(left, right, 1, false));
}

TEST_F(MatchingComparatorTest, GreaterabXcXd) {
  vector<int> left_locations{9, 15};
  Matching left(left_locations.begin(), 2, 1);
  // The suffix doesn't start on the next position.
  vector<int> right_locations{7, 15, 17};
  Matching right(right_locations.begin(), 3, 1);
  EXPECT_EQ(1, comparator->Compare(left, right, 1, false));

  // The common part doesn't match.
  vector<int> other_right_locations{10, 13, 16};
  right = Matching(other_right_locations.begin(), 3, 1);
  EXPECT_EQ(1, comparator->Compare(left, right, 1, false));
}

}  // namespace