@@ -32,13 +32,6 @@ class TestableEmbedder : public Embedder {
32
32
void computeEmbeddings () const override {}
33
33
void computeEmbeddings (const BasicBlock &BB) const override {}
34
34
using Embedder::lookupVocab;
35
- static void addVectors (Embedding &Dst, const Embedding &Src) {
36
- Embedder::addVectors (Dst, Src);
37
- }
38
- static void addScaledVector (Embedding &Dst, const Embedding &Src,
39
- float Factor) {
40
- Embedder::addScaledVector (Dst, Src, Factor);
41
- }
42
35
};
43
36
44
37
TEST (IR2VecTest, CreateSymbolicEmbedder) {
@@ -79,37 +72,83 @@ TEST(IR2VecTest, AddVectors) {
79
72
Embedding E1 = {1.0 , 2.0 , 3.0 };
80
73
Embedding E2 = {0.5 , 1.5 , -1.0 };
81
74
82
- TestableEmbedder::addVectors ( E1 , E2 ) ;
75
+ E1 += E2 ;
83
76
EXPECT_THAT (E1 , ElementsAre (1.5 , 3.5 , 2.0 ));
84
77
85
78
// Check that E2 is unchanged
86
79
EXPECT_THAT (E2 , ElementsAre (0.5 , 1.5 , -1.0 ));
87
80
}
88
81
82
+ TEST (IR2VecTest, SubtractVectors) {
83
+ Embedding E1 = {1.0 , 2.0 , 3.0 };
84
+ Embedding E2 = {0.5 , 1.5 , -1.0 };
85
+
86
+ E1 -= E2 ;
87
+ EXPECT_THAT (E1 , ElementsAre (0.5 , 0.5 , 4.0 ));
88
+
89
+ // Check that E2 is unchanged
90
+ EXPECT_THAT (E2 , ElementsAre (0.5 , 1.5 , -1.0 ));
91
+ }
92
+
89
93
TEST (IR2VecTest, AddScaledVector) {
90
94
Embedding E1 = {1.0 , 2.0 , 3.0 };
91
95
Embedding E2 = {2.0 , 0.5 , -1.0 };
92
96
93
- TestableEmbedder::addScaledVector ( E1 , E2 , 0 .5f );
97
+ E1 . scaleAndAdd ( E2 , 0 .5f );
94
98
EXPECT_THAT (E1 , ElementsAre (2.0 , 2.25 , 2.5 ));
95
99
96
100
// Check that E2 is unchanged
97
101
EXPECT_THAT (E2 , ElementsAre (2.0 , 0.5 , -1.0 ));
98
102
}
99
103
104
+ TEST (IR2VecTest, ApproximatelyEqual) {
105
+ Embedding E1 = {1.0 , 2.0 , 3.0 };
106
+ Embedding E2 = {1.0000001 , 2.0000001 , 3.0000001 };
107
+ EXPECT_TRUE (E1 .approximatelyEquals (E2 )); // Diff = 1e-7
108
+
109
+ Embedding E3 = {1.00002 , 2.00002 , 3.00002 }; // Diff = 2e-5
110
+ EXPECT_FALSE (E1 .approximatelyEquals (E3 ));
111
+ EXPECT_TRUE (E1 .approximatelyEquals (E3 , 3e-5 ));
112
+
113
+ Embedding E_clearly_within = {1.0000005 , 2.0000005 , 3.0000005 }; // Diff = 5e-7
114
+ EXPECT_TRUE (E1 .approximatelyEquals (E_clearly_within));
115
+
116
+ Embedding E_clearly_outside = {1.00001 , 2.00001 , 3.00001 }; // Diff = 1e-5
117
+ EXPECT_FALSE (E1 .approximatelyEquals (E_clearly_outside));
118
+
119
+ Embedding E4 = {1.0 , 2.0 , 3.5 }; // Large diff
120
+ EXPECT_FALSE (E1 .approximatelyEquals (E4 , 0.01 ));
121
+
122
+ Embedding E5 = {1.0 , 2.0 , 3.0 };
123
+ EXPECT_TRUE (E1 .approximatelyEquals (E5 , 0.0 ));
124
+ EXPECT_TRUE (E1 .approximatelyEquals (E5 ));
125
+ }
126
+
100
127
#if GTEST_HAS_DEATH_TEST
101
128
#ifndef NDEBUG
102
129
TEST (IR2VecTest, MismatchedDimensionsAddVectors) {
103
130
Embedding E1 = {1.0 , 2.0 };
104
131
Embedding E2 = {1.0 };
105
- EXPECT_DEATH (TestableEmbedder::addVectors (E1 , E2 ),
106
- " Vectors must have the same dimension" );
132
+ EXPECT_DEATH (E1 += E2 , " Vectors must have the same dimension" );
133
+ }
134
+
135
+ TEST (IR2VecTest, MismatchedDimensionsSubtractVectors) {
136
+ Embedding E1 = {1.0 , 2.0 };
137
+ Embedding E2 = {1.0 };
138
+ EXPECT_DEATH (E1 -= E2 , " Vectors must have the same dimension" );
107
139
}
108
140
109
141
TEST (IR2VecTest, MismatchedDimensionsAddScaledVector) {
110
142
Embedding E1 = {1.0 , 2.0 };
111
143
Embedding E2 = {1.0 };
112
- EXPECT_DEATH (TestableEmbedder::addScaledVector (E1 , E2 , 1 .0f ),
144
+ EXPECT_DEATH (E1 .scaleAndAdd (E2 , 1 .0f ),
145
+ " Vectors must have the same dimension" );
146
+ }
147
+
148
+ TEST (IR2VecTest, MismatchedDimensionsApproximatelyEqual) {
149
+ Embedding E1 = {1.0 , 2.0 };
150
+ Embedding E2 = {1.010 };
151
+ EXPECT_DEATH (E1 .approximatelyEquals (E2 ),
113
152
" Vectors must have the same dimension" );
114
153
}
115
154
#endif // NDEBUG
@@ -136,8 +175,9 @@ TEST(IR2VecTest, ZeroDimensionEmbedding) {
136
175
Embedding E1 ;
137
176
Embedding E2 ;
138
177
// Should be no-op, but not crash
139
- TestableEmbedder::addVectors (E1 , E2 );
140
- TestableEmbedder::addScaledVector (E1 , E2 , 1 .0f );
178
+ E1 += E2 ;
179
+ E1 -= E2 ;
180
+ E1 .scaleAndAdd (E2 , 1 .0f );
141
181
EXPECT_TRUE (E1 .empty ());
142
182
}
143
183
0 commit comments