📄 test_matrix_fixed.cxx
字号:
(d5.get(0,0)==4.0 && d5.get(0,1)==4.0 && d5.get(1,0)==4.0 && d5.get(1,1)==4.0)), true);
TEST("d0+=d2",
((d0+=d2),
(d0.get(0,0)==4.0 && d0.get(0,1)==4.0 && d0.get(1,0)==4.0 && d0.get(1,1)==4.0)), true);
// test multiplications and divisions
d2(0,0) = 1; d2(0,1) = 2; d2(1,0) = 3;
TEST("d0=d2*5.0f",
((d0=d2*5.0f),
(d0.get(0,0)==5 && d0.get(0,1)==10 && d0.get(1,0)==15)), true);
TEST("d0=5.0f*d2",
((d0=5.0f*d2),
(d0.get(0,0)==5 && d0.get(0,1)==10 && d0.get(1,0)==15)), true);
TEST("d2*=5.0f",((d2*=5.0f), (d2== d0)), true);
TEST("d0=d2/5.0f",
((d0=d2/5.0f),
(d0.get(0,0)==1 && d0.get(0,1)==2 && d0.get(1,0)==3)), true);
TEST("d2/=5.0f", ((d2/=5.0f), (d2==d0)), true);
float d6values [] = {1.0f,2.0f,
3.0f,4.0f};
vnl_float_2x2 d6(d6values);
TEST("vnl_float_2x2 d6({1.0,2.0,3.0,4.0})", d6.get(1,1), 4.0);
float d7values [] = {5.0,6.0,
7.0,8.0};
vnl_float_2x2 d7(d7values);
TEST("vnl_float_2x2 d7({5.0,6.0,7.0,8.0})", d7.get(1,1), 8.0);
TEST("d5=d6*d7", ((d5=d6*d7),
(d5.get(0,0)==19.0 && d5.get(0,1)==22.0 && d5.get(1,0)==43.0 && d5.get(1,1)==50.0)), true);
TEST("d6*=d7", ((d6*=d7),
(d6.get(0,0)==19.0 && d6.get(0,1)==22.0 && d6.get(1,0)==43.0 && d6.get(1,1)==50.0)), true);
// additional tests
vnl_float_2x2 m1, m2;
float mvalues [] = {0,-2,2,0};
vnl_float_2x2 m(mvalues);
m1 = m; m2 = m;
vnl_matrix<float> m3;
TEST("m(i,j)",
(m(0,0)==0 && m(0,1)==-2 && m(1,0)==2 && m(1,1)==0), true);
TEST("m.transpose()",
((m1 = m.transpose()),
(m1(0,0)==0 && m1(0,1)==2 && m1(1,0)==-2 && m1(1,1)==0)), true);
TEST("element_product(m,m)",
((m1 = element_product(m,m)),
(m1(0,0)==0 && m1(0,1)==4 && m1(1,0)==4 && m1(1,1)==0)), true);
TEST("element_quotient(m,[2])",
((m2 = 2),
(m1 = element_quotient(m,m2)),
(m1(0,0)==0 && m1(0,1)==-1 && m1(1,0)==1 && m1(1,1)==0)), true);
TEST("m.extract(1,1,1,1)",
((m3 = m.extract(1,1,1,1)),
(m3.rows()==1 && m3.columns()==1 && m3(0,0)==m(1,1))), true);
TEST("m.update([4],1,1)",
((m3=4),
(m.update(m3,1,1)),
(m(0,0)==0 && m(0,1)==-2 && m(1,0)==2 && m(1,1)==4)), true);
}
static
void test_double()
{
vnl_matrix_fixed<double,2,2> d0;
TEST("vnl_matrix_fixed<double,2,2> d0", (d0.rows()==2 && d0.columns()==2), true);
vnl_matrix_fixed<double,3,4> d1;
TEST("vnl_matrix_fixed<double,3,4> d1", (d1.rows()==3 && d1.columns()==4), true);
vnl_double_2x2 d2(2.0);
TEST("vnl_double_2x2 d2(2.0)",
(d2.get(0,0)==2.0 && d2.get(0,1)==2.0 && d2.get(1,0)==2.0 && d2.get(1,1)==2.0), true);
TEST("d0=2.0", (d0=2.0,
(d0.get(0,0)==2.0 && d0.get(0,1)==2.0 && d0.get(1,0)==2.0 && d0.get(1,1)==2.0)), true);
TEST("d0 == d2", (d0 == d2), true);
TEST("(d0 == d2)", (d0==d2), true);
TEST("d2.put(1,1,3.0)", (d2.put(1,1,3.0),d2.get(1,1)), 3.0);
TEST("d2.get(1,1)", d2.get(1,1), 3.0);
TEST("d0 == d2", (d0 == d2), false);
TEST("d0 != d2", (d0 != d2), true);
TEST("(d0 == d2)", (d0==d2), false);
TEST("d1.fill(3.0)",
(d1.fill(3.0),
(d1.get(0,0)==3.0 && d1.get(1,1)==3.0 && d1.get(2,2)==3.0 && d1.get(2,3)==3.0)), true);
TEST("d2.fill(3.0)",
(d2.fill(2.0),
(d2.get(0,0)==2.0 && d2.get(0,1)==2.0 && d2.get(1,0)==2.0 && d2.get(1,1)==2.0)), true);
TEST("d0=d2", (d0=d2, (d0==d2)), true);
// test additions and subtractions
TEST("d0=d2+3.0",
((d0=d2+3.0),
(d0.get(0,0)==5.0 && d0.get(0,1)==5.0 && d0.get(1,0)==5.0 && d0.get(1,1)==5.0)), true);
TEST("d0+=(-3.0)",
(d0+=(-3.0),
(d0.get(0,0)==2.0 && d0.get(0,1)==2.0 && d0.get(1,0)==2.0 && d0.get(1,1)==2.0)), true);
vnl_double_2x2 d5;
TEST("d5=d0+d2",
((d5=d0+d2),
(d5.get(0,0)==4.0 && d5.get(0,1)==4.0 && d5.get(1,0)==4.0 && d5.get(1,1)==4.0)), true);
TEST("d0+=d2",
((d0+=d2),
(d0.get(0,0)==4.0 && d0.get(0,1)==4.0 && d0.get(1,0)==4.0 && d0.get(1,1)==4.0)), true);
// test multiplications and divisions
d2(0,0) = 1; d2(0,1) = 2; d2(1,0) = 3;
TEST("d0=d2*5.0",
((d0=d2*5.0),
(d0.get(0,0)==5 && d0.get(0,1)==10 && d0.get(1,0)==15)), true);
TEST("d0=5.0*d2",
((d0=5.0*d2),
(d0.get(0,0)==5 && d0.get(0,1)==10 && d0.get(1,0)==15)), true);
TEST("d2*=5.0",((d2*=5.0), (d2== d0)), true);
TEST("d0=d2/5.0",
((d0=d2/5.0),
(d0.get(0,0)==1 && d0.get(0,1)==2 && d0.get(1,0)==3)), true);
TEST("d2/=5.0", ((d2/=5.0), (d2==d0)), true);
double d6values [] = {1.0,2.0,
3.0,4.0};
vnl_double_2x2 d6(d6values);
TEST("vnl_double_2x2 d6({1.0,2.0,3.0,4.0})", d6.get(1,1), 4.0);
double d7values [] = {5.0,6.0,
7.0,8.0};
vnl_double_2x2 d7(d7values);
TEST("vnl_double_2x2 d7({5.0,6.0,7.0,8.0})", d7.get(1,1), 8.0);
TEST("d5=d6*d7", ((d5=d6*d7),
(d5.get(0,0)==19.0 && d5.get(0,1)==22.0 && d5.get(1,0)==43.0 && d5.get(1,1)==50.0)), true);
TEST("d6*=d7", ((d6*=d7),
(d6.get(0,0)==19.0 && d6.get(0,1)==22.0 && d6.get(1,0)==43.0 && d6.get(1,1)==50.0)), true);
// apply sqrt to every element
double d8values [] = {0.0, 1.0, 9.0, 16.0};
vnl_double_2x2 d8(d8values);
d8 = d8.apply(vcl_sqrt);
TEST("apply(sqrt)", d8[0][0]==0 && d8[0][1]==1 && d8[1][0]==3 && d8[1][1]==4, true);
// normalizations
d8.normalize_rows();
TEST("normalize_rows()", d8[0][0]==0 && d8[0][1]==1, true);
TEST_NEAR("normalize_rows()", d8[1][0], 0.6, 1e-12);
TEST_NEAR("normalize_rows()", d8[1][1], 0.8, 1e-12);
d8.normalize_columns();
TEST("normalize_columns()", d8[0][0]==0 && d8[1][0]==1, true);
}
void test_matrix_fixed()
{
verbose_malloc = true;
double datablock[9] = {
11, 12, 13,
21, 22, 23,
31, 32, 33,
};
vcl_printf("Calling ctor -- should be no mallocs\n");
reset_count;
vnl_double_3x3 X(datablock);
check_count;
vcl_printf("X = [ %g %g %g\n %g %g %g\n %g %g %g ]\n",
X(0,0),X(0,1),X(0,2),X(1,0),X(1,1),X(1,2),X(2,0),X(2,1),X(2,2));
reset_count;
vnl_double_3 v(10,11,12);
check_count;
vcl_printf("v = [ %g %g %g ]\n", v(0), v(1), v(2));
reset_count;
vnl_double_3 splork = X * (v + v);
check_count;
vcl_printf("splork = [ %g %g %g ]\n", splork(0), splork(1), splork(2));
// This shouldn't compile...
#if 0
vnl_matrix<double>* base = new vnl_double_3x3(datablock);
#endif
vcl_printf("Now watch the mallocs\n");
vnl_matrix_ref<double> CX = X;
vnl_vector_ref<double> cv = v;
vnl_vector<double> Xv = CX * (cv + cv);
vcl_printf("X v = [ %g %g %g ]\n", Xv[0], Xv[1], Xv[2]);
verbose_malloc = false;
// test that vnl_double_3x3's can be multiplied
vnl_double_3x3 A(datablock);
vnl_double_3x3 B = A * A;
// test that vnl_double_3x3's can be added and subtracted
B = A + B;
B -= A;
B.fill(1.0);
TEST("fill(1)", B(0,0)==1 && B(0,1)==1 && B(1,2)==1 && B(2,2)==1, true);
B.fill_diagonal(4.0);
TEST("fill_diagonal(4)", B(0,0)==4 && B(0,1)==1 && B(1,2)==1 && B(2,2)==4, true);
// apply sqrt to every element
B = B.apply(vcl_sqrt);
TEST("apply(sqrt)", B(1,1)==2 && B(0,2)==1 && B(2,1)==1 && B(2,2)==2, true);
test_multiply();
test_size();
test_int();
test_float();
test_double();
}
#ifdef TEST_MALLOC
// BAD-BAD-BAD these operators new/delete are picked up by *all* tests!!!
// The problem is that they don't provide new[] and delete[]. - PVr
// with gcc 3.0, formatted stream output uses operator
// new so printing to cout here causes stack overflow.
void* operator new(vcl_size_t s)
// [18.4.1] lib.new.delete
#if defined(VCL_SUNPRO_CC_5) || defined(GNU_LIBSTDCXX_V3) || defined(VCL_KAI)
throw(std::bad_alloc)
#endif
{
void *r = vcl_malloc(s);
++malloc_count;
if (verbose_malloc)
vcl_printf("malloc: %08lX for %d\n", (unsigned long)r, int(s));
return r;
}
void operator delete(void* s)
#if defined(GNU_LIBSTDCXX_V3) || defined(VCL_SUNPRO_CC_5)
throw()
#endif
{
if (verbose_malloc)
vcl_printf("delete: %08lX\n", (unsigned long)s);
vcl_free(s);
}
#endif // TEST_MALLOC
TESTMAIN(test_matrix_fixed);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -