00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <boost/test/unit_test.hpp>
00021
00022 #include "FeatureVector.h"
00023
00024 using namespace Moses;
00025 using namespace std;
00026
00027 static const float TOL = 0.00001;
00028
00029 BOOST_AUTO_TEST_SUITE(fv)
00030
00031 BOOST_AUTO_TEST_CASE(vector_sum_diff)
00032 {
00033 FVector f1,f2,f3;
00034 FName n1("a");
00035 FName n2("b");
00036 FName n3("c");
00037 FName n4("d");
00038 f1[n1] = 1.2;
00039 f1[n2] = 1.4;
00040 f1[n3] = -0.1;
00041 f2[n1] = 0.01;
00042 f2[n3] = 5.6;
00043 f2[n4] = 0.6;
00044 f3[n1] =1.2;
00045 FVector sum = f1 + f2;
00046 FVector diff = f1 - f2;
00047 BOOST_CHECK_CLOSE((FValue)sum[n1], 1.21, TOL);
00048 BOOST_CHECK_CLOSE((FValue)sum[n2], 1.4, TOL);
00049 BOOST_CHECK_CLOSE((FValue)sum[n3], 5.5, TOL);
00050 BOOST_CHECK_CLOSE((FValue)sum[n4], 0.6, TOL);
00051 BOOST_CHECK_CLOSE((FValue)diff[n1], 1.19, TOL);
00052 BOOST_CHECK_CLOSE((FValue)diff[n2], 1.4, TOL);
00053 BOOST_CHECK_CLOSE((FValue)diff[n3], -5.7, TOL);
00054 BOOST_CHECK_CLOSE((FValue)diff[n4], -0.6, TOL);
00055 f1 -= f3;
00056 BOOST_CHECK_CLOSE((FValue)f1[n1],0,TOL);
00057 }
00058
00059
00060 BOOST_AUTO_TEST_CASE(scalar)
00061 {
00062 FVector f1,f2;
00063 FName n1("a");
00064 FName n2("b");
00065 FName n3("c");
00066 FName n4("d");
00067 f1[n1] = 0.2;
00068 f1[n2] = 9.178;
00069 f1[n3] = -0.1;
00070 f2[n1] = 0.01;
00071 f2[n3] = 5.6;
00072 f2[n4] = 0.6;
00073 FVector prod1 = f1 * 2;
00074 FVector prod2 = f1 * -0.1;
00075 FVector quot = f2 / 2;
00076 BOOST_CHECK_CLOSE((FValue)prod1[n1], 0.4, TOL);
00077 BOOST_CHECK_CLOSE((FValue)prod1[n2], 18.356, TOL);
00078 BOOST_CHECK_CLOSE((FValue)prod1[n3], -0.2, TOL);
00079
00080 BOOST_CHECK_CLOSE((FValue)prod2[n1], -0.02, TOL);
00081 BOOST_CHECK_CLOSE((FValue)prod2[n2], -0.9178, TOL);
00082 BOOST_CHECK_CLOSE((FValue)prod2[n3], 0.01, TOL);
00083
00084 BOOST_CHECK_CLOSE((FValue)quot[n1], 0.005, TOL);
00085 BOOST_CHECK_CLOSE((FValue)quot[n2], 0, TOL);
00086 BOOST_CHECK_CLOSE((FValue)quot[n3], 2.8, TOL);
00087 BOOST_CHECK_CLOSE((FValue)quot[n4], 0.3, TOL);
00088 }
00089
00090 BOOST_AUTO_TEST_CASE(inc)
00091 {
00092 FVector f1;
00093 FName n1("a");
00094 FName n2("b");
00095 f1[n1] = 2.3;
00096 f1[n2] = -0.4;
00097 f1[n1]+=2;
00098 BOOST_CHECK_CLOSE((FValue)f1[n1], 4.3, TOL);
00099 BOOST_CHECK_CLOSE((FValue)f1[n2], -0.4, TOL);
00100
00101 FValue res = ++f1[n2];
00102 BOOST_CHECK_CLOSE(res,0.6, TOL);
00103 BOOST_CHECK_CLOSE((FValue)f1[n1], 4.3, TOL);
00104 BOOST_CHECK_CLOSE((FValue)f1[n2], 0.6, TOL);
00105 }
00106
00107 BOOST_AUTO_TEST_CASE(vector_mult)
00108 {
00109 FVector f1,f2;
00110 FName n1("a");
00111 FName n2("b");
00112 FName n3("c");
00113 FName n4("d");
00114 f1[n1] = 0.2;
00115 f1[n2] = 9.178;
00116 f1[n3] = -0.1;
00117 f2[n1] = 0.01;
00118 f2[n2] = 5.6;
00119 f2[n3] = 1;
00120 f2[n4] = 0.6;
00121 FVector prod = f1 * f2;
00122 FVector quot = f1/f2;
00123 BOOST_CHECK_CLOSE((FValue)prod[n1], 0.002, TOL);
00124 BOOST_CHECK_CLOSE((FValue)prod[n2], 51.3968, TOL);
00125 BOOST_CHECK_CLOSE((FValue)prod[n3], -0.1, TOL);
00126 BOOST_CHECK_CLOSE((FValue)prod[n4], 0, TOL);
00127
00128 BOOST_CHECK_CLOSE((FValue)quot[n1], 20, TOL);
00129 BOOST_CHECK_CLOSE((FValue)quot[n2], 1.63892865, TOL);
00130 BOOST_CHECK_CLOSE((FValue)quot[n3], -0.1, TOL);
00131 BOOST_CHECK_CLOSE((FValue)quot[n4], 0, TOL);
00132 }
00133
00134 BOOST_AUTO_TEST_CASE(core)
00135 {
00136 FVector f1(2);
00137 f1[0] = 1.3;
00138 f1[1] = -1.9;
00139 BOOST_CHECK_CLOSE(f1[0],1.3,TOL);
00140 BOOST_CHECK_CLOSE(f1[1],-1.9,TOL);
00141 f1[1] = 0.1;
00142 BOOST_CHECK_CLOSE(f1[1],0.1,TOL);
00143
00144 BOOST_CHECK_EQUAL(f1.size(),2);
00145
00146 f1[FName("a")] = 1.2;
00147 BOOST_CHECK_EQUAL(f1.size(),3);
00148 }
00149
00150 BOOST_AUTO_TEST_CASE(core_arith)
00151 {
00152 FVector f1(2);
00153 FVector f2(2);
00154 FName n1("a");
00155 FName n2("b");
00156 f1[0] = 1.1;
00157 f1[1] = 0.25;
00158 f1[n1] = 3.6;
00159 f1[n2] = -1.5;
00160 f2[0] = 0.5;
00161 f2[1] = -0.1;
00162 f2[n1] = 1;
00163
00164
00165 FVector sum = f1+f2;
00166 FVector diff = f1-f2;
00167 FVector prod = f1*f2;
00168 FVector quot = f2/f1;
00169
00170 BOOST_CHECK_CLOSE((FValue)sum[0], 1.6 , TOL);
00171 BOOST_CHECK_CLOSE((FValue)sum[1], 0.15 , TOL);
00172 BOOST_CHECK_CLOSE((FValue)sum[n1], 4.6 , TOL);
00173 BOOST_CHECK_CLOSE((FValue)sum[n2], -1.5 , TOL);
00174
00175 BOOST_CHECK_CLOSE((FValue)diff[0], 0.6 , TOL);
00176 BOOST_CHECK_CLOSE((FValue)diff[1], 0.35 , TOL);
00177 BOOST_CHECK_CLOSE((FValue)diff[n1], 2.6 , TOL);
00178 BOOST_CHECK_CLOSE((FValue)diff[n2], -1.5 , TOL);
00179
00180 BOOST_CHECK_CLOSE((FValue)prod[0], 0.55 , TOL);
00181 BOOST_CHECK_CLOSE((FValue)prod[1], -0.025 , TOL);
00182 BOOST_CHECK_CLOSE((FValue)prod[n1], 3.6 , TOL);
00183 BOOST_CHECK_CLOSE((FValue)prod[n2], 0 , TOL);
00184
00185 BOOST_CHECK_CLOSE((FValue)quot[0], 0.4545454545 , TOL);
00186 BOOST_CHECK_CLOSE((FValue)quot[1], -0.4 , TOL);
00187 BOOST_CHECK_CLOSE((FValue)quot[n1], 0.277777777 , TOL);
00188 BOOST_CHECK_CLOSE((FValue)quot[n2], 0 , TOL);
00189
00190
00191 FVector f3(2);
00192 FVector f4(1);
00193 f3[0] = 2;
00194 f3[1] = -1;
00195 f4[0] = 5;
00196
00197 FVector sum1 = f3 + f4;
00198 FVector sum2 = f4 + f3;
00199 BOOST_CHECK_EQUAL(sum1,sum2);
00200 BOOST_CHECK_CLOSE(sum1[0], 7, TOL);
00201 BOOST_CHECK_CLOSE(sum1[1], -1, TOL);
00202
00203 FVector diff1 = f3 - f4;
00204 FVector diff2 = f4 - f3;
00205 BOOST_CHECK_CLOSE(diff1[0], -3, TOL);
00206 BOOST_CHECK_CLOSE(diff1[1], -1, TOL);
00207 BOOST_CHECK_CLOSE(diff2[0], 3, TOL);
00208 BOOST_CHECK_CLOSE(diff2[1], 1, TOL);
00209
00210 FVector prod1 = f3 * f4;
00211 FVector prod2 = f4 * f3;
00212 BOOST_CHECK_EQUAL(prod1,prod2);
00213 BOOST_CHECK_CLOSE(prod1[0], 10, TOL);
00214 BOOST_CHECK_CLOSE(prod1[1], 0, TOL);
00215
00216 FVector quot1 = f3 / f4;
00217 FVector quot2 = f4 / f3;
00218 BOOST_CHECK_CLOSE(quot1[0], 0.4, TOL);
00219 BOOST_CHECK_EQUAL(quot1[1], -numeric_limits<float>::infinity());
00220 BOOST_CHECK_CLOSE(quot2[0], 2.5, TOL);
00221 BOOST_CHECK_CLOSE(quot2[1], 0, TOL);
00222
00223 }
00224
00225 BOOST_AUTO_TEST_CASE(core_scalar)
00226 {
00227 FVector f1(3);
00228 FName n1("a");
00229 f1[0] = 1.5;
00230 f1[1] = 2.1;
00231 f1[2] = 4;
00232 f1[n1] = -0.5;
00233
00234 FVector prod = f1*2;
00235 FVector quot = f1/5;
00236
00237 BOOST_CHECK_CLOSE((FValue)prod[0], 3 , TOL);
00238 BOOST_CHECK_CLOSE((FValue)prod[1], 4.2 , TOL);
00239 BOOST_CHECK_CLOSE((FValue)prod[2], 8 , TOL);
00240 BOOST_CHECK_CLOSE((FValue)prod[n1],-1 , TOL);
00241
00242 BOOST_CHECK_CLOSE((FValue)quot[0], 0.3 , TOL);
00243 BOOST_CHECK_CLOSE((FValue)quot[1], 0.42 , TOL);
00244 BOOST_CHECK_CLOSE((FValue)quot[2], 0.8 , TOL);
00245 BOOST_CHECK_CLOSE((FValue)quot[n1],-0.1 , TOL);
00246
00247 }
00248
00249 BOOST_AUTO_TEST_CASE(l1norm)
00250 {
00251 FVector f1(3);
00252 FName n1("a");
00253 f1[0] = 1.5;
00254 f1[1] = 2.1;
00255 f1[2] = 4;
00256 f1[n1] = -0.5;
00257 FValue n = f1.l1norm();
00258 BOOST_CHECK_CLOSE((FValue)n, abs(1.5)+abs(2.1)+abs(4)+abs(-0.5), TOL);
00259 }
00260
00261
00262 BOOST_AUTO_TEST_CASE(sum)
00263 {
00264 FVector f1(3);
00265 FName n1("a");
00266 FName n2("b");
00267 f1[0] = 1.5;
00268 f1[1] = 2.1;
00269 f1[2] = 4;
00270 f1[n1] = -0.5;
00271 f1[n2] = 2.7;
00272 FValue n = f1.sum();
00273 BOOST_CHECK_CLOSE((FValue)n, 1.5+2.1+4-0.5+2.7, TOL);
00274 }
00275
00276 BOOST_AUTO_TEST_CASE(l2norm)
00277 {
00278 FVector f1(3);
00279 FName n1("a");
00280 f1[0] = 1.5;
00281 f1[1] = 2.1;
00282 f1[2] = 4;
00283 f1[n1] = -0.5;
00284 FValue n = f1.l2norm();
00285 BOOST_CHECK_CLOSE((FValue)n, sqrt((1.5*1.5)+(2.1*2.1)+(4*4)+(-0.5*-0.5)), TOL);
00286 }
00287
00288 BOOST_AUTO_TEST_CASE(ip)
00289 {
00290 FVector f1(2);
00291 FVector f2(2);
00292 FName n1("a");
00293 FName n2("b");
00294 FName n3("c");
00295 f1[0] = 1.1;
00296 f1[1] = -0.1; ;
00297 f1[n2] = -1.5;
00298 f1[n3] = 2.2;
00299 f2[0] = 0.5;
00300 f2[1] = 0.25;
00301 f2[n1] = 1;
00302 f2[n3] = 2.4;
00303 FValue p1 = inner_product(f1,f2);
00304 FValue p2 = inner_product(f2,f1);
00305 BOOST_CHECK_CLOSE(p1,p2,TOL);
00306 BOOST_CHECK_CLOSE((FValue)p1, 1.1*0.5 + -0.1*0.25 + 2.2*2.4, TOL);
00307 }
00308
00309
00310 BOOST_AUTO_TEST_SUITE_END()
00311