root/OpenSceneGraph/trunk/examples/osgunittests/osgunittests.cpp @ 2222

Revision 2222, 6.3 kB (checked in by robert, 11 years ago)

Added test of sizeof(types) - run osgunittests sizeof.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/UnitTestFramework>
2#include <osg/ArgumentParser>
3#include <osg/ApplicationUsage>
4
5#include <osg/Matrix>
6
7
8void testFrustum(double left,double right,double bottom,double top,double zNear,double zFar)
9{
10    osg::Matrix f;
11    f.makeFrustum(left,right,bottom,top,zNear,zFar);
12
13   
14    double c_zNear = f(3,2) / (f(2,2)-1.0f);
15    double c_zFar = f(3,2) / (1.0f+f(2,2));
16   
17    double c_left = c_zNear * (f(2,0)-1.0f) / f(0,0);
18    double c_right = c_zNear * (1.0f+f(2,0)) / f(0,0);
19
20    double c_top = c_zNear * (1+f(2,1)) / f(1,1);
21    double c_bottom = c_zNear * (f(2,1)-1.0f) / f(1,1);
22   
23    f.getFrustum(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar);
24
25    std::cout << "testFrustum"<<std::endl;
26    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
27    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
28
29    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
30    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
31
32    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
33    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
34   
35    std::cout << std::endl;
36}
37
38void testOrtho(double left,double right,double bottom,double top,double zNear,double zFar)
39{
40    osg::Matrix f;
41    f.makeOrtho(left,right,bottom,top,zNear,zFar);
42
43    double c_zNear = (f(3,2)+1.0f) / f(2,2);
44    double c_zFar = (f(3,2)-1.0f) / f(2,2);
45   
46    double c_left = -(1.0f+f(3,0)) / f(0,0);
47    double c_right = (1.0f-f(3,0)) / f(0,0);
48
49    double c_bottom = -(1.0f+f(3,1)) / f(1,1);
50    double c_top = (1.0f-f(3,1)) / f(1,1);
51   
52    f.getOrtho(c_left,c_right,c_bottom,c_top,c_zNear,c_zFar);
53
54
55    std::cout << "testOrtho"<<std::endl;
56    std::cout << "  left = "<<left<<" compute "<<c_left<<std::endl;
57    std::cout << "  right = "<<right<<" compute "<<c_right<<std::endl;
58
59    std::cout << "  bottom = "<<bottom<<" compute "<<c_bottom<<std::endl;
60    std::cout << "  top = "<<top<<" compute "<<c_top<<std::endl;
61
62    std::cout << "  zNear = "<<zNear<<" compute "<<c_zNear<<std::endl;
63    std::cout << "  zFar = "<<zFar<<" compute "<<c_zFar<<std::endl;
64   
65    std::cout << std::endl;
66}
67
68void testLookAt(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)
69{
70    osg::Matrix mv;
71    mv.makeLookAt(eye,center,up);
72   
73    osg::Vec3 c_eye,c_center,c_up;
74    mv.getLookAt(c_eye,c_center,c_up);
75   
76    std::cout << "testLookAt"<<std::endl;
77    std::cout << "  eye "<<eye<< " compute "<<c_eye<<std::endl;
78    std::cout << "  eye "<<center<< " compute "<<c_center<<std::endl;
79    std::cout << "  eye "<<up<< " compute "<<c_up<<std::endl;
80   
81    std::cout << std::endl;
82   
83}
84
85void sizeOfTest()
86{
87  std::cout<<"sizeof(bool)=="<<sizeof(bool)<<std::endl;
88  std::cout<<"sizeof(char)=="<<sizeof(char)<<std::endl;
89  std::cout<<"sizeof(short)=="<<sizeof(short)<<std::endl;
90  std::cout<<"sizeof(int)=="<<sizeof(int)<<std::endl;
91  std::cout<<"sizeof(long)=="<<sizeof(long)<<std::endl;
92  std::cout<<"sizeof(long int)=="<<sizeof(long int)<<std::endl;
93
94#if defined(_MSC_VER)
95  // long long isn't supported on VS6.0...
96  std::cout<<"sizeof(__int64)=="<<sizeof(__int64)<<std::endl;
97#else
98  std::cout<<"sizeof(long long)=="<<sizeof(long long)<<std::endl;
99#endif
100  std::cout<<"sizeof(float)=="<<sizeof(float)<<std::endl;
101  std::cout<<"sizeof(double)=="<<sizeof(double)<<std::endl;
102}
103
104int main( int argc, char** argv )
105{
106    osg::ArgumentParser arguments(&argc,argv);
107
108    // set up the usage document, in case we need to print out how to use this program.
109    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which runs units tests.");
110    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options]");
111    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
112    arguments.getApplicationUsage()->addCommandLineOption("qt","Display qualified tests.");
113    arguments.getApplicationUsage()->addCommandLineOption("sizeof","Display sizeof tests.");
114    arguments.getApplicationUsage()->addCommandLineOption("matrix","Display qualified tests.");
115 
116
117    if (arguments.argc()<=1)
118    {
119        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
120        return 1;
121    }
122
123    bool printQualifiedTest = false;
124    while (arguments.read("qt")) printQualifiedTest = true;
125
126    bool printMatrixTest = false;
127    while (arguments.read("matrix")) printMatrixTest = true;
128
129    bool printSizeOfTest = false;
130    while (arguments.read("sizeof")) printSizeOfTest = true;
131
132    // if user request help write it out to cout.
133    if (arguments.read("-h") || arguments.read("--help"))
134    {
135        std::cout<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl;
136        arguments.getApplicationUsage()->write(std::cout,arguments.getApplicationUsage()->getCommandLineOptions());
137        return 1;
138    }
139
140    // any option left unread are converted into errors to write out later.
141    arguments.reportRemainingOptionsAsUnrecognized();
142
143    // report any errors if they have occured when parsing the program aguments.
144    if (arguments.errors())
145    {
146        arguments.writeErrorMessages(std::cout);
147        return 1;
148    }
149   
150
151    if (printMatrixTest)
152    {
153        std::cout<<"******   Running matrix tests   ******"<<std::endl;
154
155        testFrustum(-1,1,-1,1,1,1000);
156        testFrustum(0,1,1,2,2.5,100000);
157
158        testOrtho(0,1,1,2,2.1,1000);
159        testOrtho(-1,10,1,20,2.5,100000);
160
161        testLookAt(osg::Vec3(10.0,4.0,2.0),osg::Vec3(10.0,4.0,2.0)+osg::Vec3(0.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0));
162        testLookAt(osg::Vec3(10.0,4.0,2.0),osg::Vec3(10.0,4.0,2.0)+osg::Vec3(1.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0));
163
164    }
165   
166    if (printSizeOfTest)
167    {
168        std::cout<<"**** sizeof() tests  ******"<<std::endl;
169       
170        sizeOfTest();
171
172        std::cout<<std::endl;
173    }
174
175
176    if (printQualifiedTest)
177    {
178         std::cout<<"*****   Qualified Tests  ******"<<std::endl;
179
180         osgUtx::QualifiedTestPrinter printer;
181         osgUtx::TestGraph::instance().root()->accept( printer );   
182         std::cout<<std::endl;
183    }
184
185    std::cout<<"******   Running tests   ******"<<std::endl;
186
187    // Global Data or Context
188    osgUtx::TestContext ctx;
189    osgUtx::TestRunner runner( ctx );
190    runner.specify("root");
191
192    osgUtx::TestGraph::instance().root()->accept( runner );
193
194    return 0;
195}
Note: See TracBrowser for help on using the browser.