@@ -32,13 +32,6 @@ class TestableEmbedder : public Embedder {
3232 void computeEmbeddings () const override {}
3333 void computeEmbeddings (const BasicBlock &BB) const override {}
3434 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- }
4235};
4336
4437TEST (IR2VecTest, CreateSymbolicEmbedder) {
@@ -79,37 +72,83 @@ TEST(IR2VecTest, AddVectors) {
7972 Embedding E1 = {1.0 , 2.0 , 3.0 };
8073 Embedding E2 = {0.5 , 1.5 , -1.0 };
8174
82- TestableEmbedder::addVectors ( E1 , E2 ) ;
75+ E1 += E2 ;
8376 EXPECT_THAT (E1 , ElementsAre (1.5 , 3.5 , 2.0 ));
8477
8578 // Check that E2 is unchanged
8679 EXPECT_THAT (E2 , ElementsAre (0.5 , 1.5 , -1.0 ));
8780}
8881
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+
8993TEST (IR2VecTest, AddScaledVector) {
9094 Embedding E1 = {1.0 , 2.0 , 3.0 };
9195 Embedding E2 = {2.0 , 0.5 , -1.0 };
9296
93- TestableEmbedder::addScaledVector ( E1 , E2 , 0 .5f );
97+ E1 . scaleAndAdd ( E2 , 0 .5f );
9498 EXPECT_THAT (E1 , ElementsAre (2.0 , 2.25 , 2.5 ));
9599
96100 // Check that E2 is unchanged
97101 EXPECT_THAT (E2 , ElementsAre (2.0 , 0.5 , -1.0 ));
98102}
99103
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+
100127#if GTEST_HAS_DEATH_TEST
101128#ifndef NDEBUG
102129TEST (IR2VecTest, MismatchedDimensionsAddVectors) {
103130 Embedding E1 = {1.0 , 2.0 };
104131 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" );
107139}
108140
109141TEST (IR2VecTest, MismatchedDimensionsAddScaledVector) {
110142 Embedding E1 = {1.0 , 2.0 };
111143 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 ),
113152 " Vectors must have the same dimension" );
114153}
115154#endif // NDEBUG
@@ -136,8 +175,9 @@ TEST(IR2VecTest, ZeroDimensionEmbedding) {
136175 Embedding E1 ;
137176 Embedding E2 ;
138177 // 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 );
141181 EXPECT_TRUE (E1 .empty ());
142182}
143183
0 commit comments