示例#1
0
文件: test.cpp 项目: NEXUS1000/class
int main()
{
	Complex z1(4,3);
	z1.Show();
	std::cout << "z1="<< z1 <<std::endl;
	Complex z2(4,-3);
	z2.Show();
	std::cout << "z2="<< z2 <<std::endl;
	Complex z3(0,3);
	z3.Show();
	std::cout << "z3="<< z3 <<std::endl;
	Complex z4(1,0);
	z4.Show();
	std::cout << "z4="<< z4 <<std::endl;
	Complex z5(0,5);
	z5.Show();
	std::cout << "z5="<< z5 <<std::endl;
	Complex z6(0,0);
	z6.Show();
	std::cout << "z6="<< z6 <<std::endl;
	
	(z1 + z2).Show();
	(z1 - z2).Show();
	(z1 * z1).Show();
	(z1 * z2).Show();
	std::cout << "|z1| = " 
			  << z1.GetLenth() << std::endl;
	z1.Conjugate().Show();
	z2.Conjugate().Show();
	
	return 0;
}
示例#2
0
void ComplexTestSuite::IO()
{
	gsl::complex z1(1,2);
	std::string s1("1 + 2");
	s1 += IMAG_SYMB;
	std::stringstream ss1;
	ss1 << z1;
	CPPUNIT_ASSERT(s1 == ss1.str());
	
	gsl::complex z2(1,-2);
	std::string s2("1 - 2");
	s2 += IMAG_SYMB;
	std::stringstream ss2;
	ss2 << z2;
	CPPUNIT_ASSERT(s2 == ss2.str());
	
	gsl::complex z3(0,2);
	std::string s3("2");
	s3 += IMAG_SYMB;
	std::stringstream ss3;
	ss3 << z3;
	CPPUNIT_ASSERT(s3 == ss3.str());
	
	gsl::complex z4(0,-3);
	std::string s4("-3");
	s4 += IMAG_SYMB;
	std::stringstream ss4;
	ss4 << z4;
	CPPUNIT_ASSERT(s4 == ss4.str());
	
	gsl::complex z5(1,0);
	std::string s5("1");
	std::stringstream ss5;
	ss5 << z5;
	CPPUNIT_ASSERT(s5 == ss5.str());
	
	gsl::complex z6(0,0);
	std::string s6("0");
	std::stringstream ss6;
	ss6 << z6;
	CPPUNIT_ASSERT(s6 == ss6.str());	
}
int main()
{
    // std::cout << " ------ test 1, direct init from rvalue ------- " << std::endl;
// #if defined(__GNUC__) && 0 // GCC having trouble parsing the extra parens
    // X z2((0, X() ));
// #else
    X z2((X()));
// #endif 

    // std::cout << " ------ test 2, copy init from rvalue ------- " << std::endl;
    X z4 = X();

    // std::cout << " ------ test 3, copy init from lvalue ------- " << std::endl;
    X z5 = z4;

    // std::cout << " ------ test 4, direct init from lvalue ------- " << std::endl;
    X z6(z4);

    // std::cout << " ------ test 5, construct const ------- " << std::endl;
    X const z7;

    // std::cout << " ------ test 6, copy init from lvalue ------- " << std::endl;
    X z8 = z7;

    // std::cout << " ------ test 7, direct init from lvalue ------- " << std::endl;
    X z9(z7);

    // std::cout << " ------ test 8, pass rvalue by-value ------- " << std::endl;
    sink(source());

    // std::cout << " ------ test 9, pass const rvalue by-value ------- " << std::endl;
    sink(csource());

    // std::cout << " ------ test 10, pass rvalue by overloaded reference ------- " << std::endl;
    // This one fails in Comeau's strict mode due to 8.5.3/5.  GCC 3.3.1 passes it.
    sink2(source());

    // std::cout << " ------ test 11, pass const rvalue by overloaded reference ------- " << std::endl;
    sink2(csource());

#if 0    // These two correctly fail to compile, just as desired
    std::cout << " ------ test 12, pass rvalue by non-const reference ------- " << std::endl;
    sink3(source());

    std::cout << " ------ test 13, pass const rvalue by non-const reference ------- " << std::endl;
    sink3(csource());
#endif 

    // std::cout << " ------ test 14, pass lvalue by-value ------- " << std::endl;
    sink(z5);

    // std::cout << " ------ test 15, pass const lvalue by-value ------- " << std::endl;
    sink(z7);

    // std::cout << " ------ test 16, pass lvalue by-reference ------- " << std::endl;
    sink2(z4);

    // std::cout << " ------ test 17, pass const lvalue by const reference ------- " << std::endl;
    sink2(z7);

    // std::cout << " ------ test 18, pass const lvalue by-reference ------- " << std::endl;
#if 0   // correctly fails to compile, just as desired
    sink3(z7);
#endif 

    // std::cout << " ------ test 19, pass rvalue by value to template param ------- " << std::endl;
    tsink(source());

    // std::cout << " ------ test 20, direct initialize a const A with an A ------- " << std::endl;
    typedef X const XC;
    sink2(XC(X()));

    // std::cout << " ------ test 21, assign from lvalue ------- " << std::endl;
    z4 = z5;

    // std::cout << " ------ test 22, assign from rvalue ------- " << std::endl;
    z4 = source();
}