DfaTest.cpp

Go to the documentation of this file.
00001 /*==============================================================================
00002  * FILE:       DfaTest.cc
00003  * OVERVIEW:   Provides the implementation for the DfaTest class, which
00004  *              tests the data flow based type analysis code
00005  *============================================================================*/
00006 /*
00007  * $Revision: 1.4 $
00008  *
00009  * 19 Oct 04 - Mike: Created
00010  */
00011 
00012 #include "DfaTest.h"
00013 #include <iostream>     // For std::cerr
00014 #include <log.h>
00015 #include <boomerang.h>
00016 
00017 class ErrLogger : public Log {
00018 public:
00019     virtual Log &operator<<(const char *str) {
00020      std::cerr << str;
00021         return *this;
00022     }
00023     virtual ~ErrLogger() {};
00024 };
00025 /*==============================================================================
00026  * FUNCTION:        DfaTest::registerTests
00027  * OVERVIEW:        Register the test functions in the given suite
00028  * PARAMETERS:      Pointer to the test suite
00029  * RETURNS:         <nothing>
00030  *============================================================================*/
00031 #define MYTEST(name) \
00032 suite->addTest(new CppUnit::TestCaller<DfaTest> ("DfaTest", \
00033     &DfaTest::name, *this))
00034 
00035 void DfaTest::registerTests(CppUnit::TestSuite* suite) {
00036     MYTEST(testMeetInt);
00037     MYTEST(testMeetSize);
00038     MYTEST(testMeetPointer);
00039     MYTEST(testMeetUnion);
00040 }
00041 
00042 int DfaTest::countTestCases () const
00043 { return 2; }   // ? What's this for?
00044 
00045 /*==============================================================================
00046  * FUNCTION:        DfaTest::setUp
00047  * OVERVIEW:        Set up some expressions for use with all the tests
00048  * NOTE:            Called before any tests
00049  * PARAMETERS:      <none>
00050  * RETURNS:         <nothing>
00051  *============================================================================*/
00052 void DfaTest::setUp () {
00053 }
00054 
00055 /*==============================================================================
00056  * FUNCTION:        DfaTest::tearDown
00057  * OVERVIEW:        Delete expressions created in setUp
00058  * NOTE:            Called after all tests
00059  * PARAMETERS:      <none>
00060  * RETURNS:         <nothing>
00061  *============================================================================*/
00062 void DfaTest::tearDown () {
00063 }
00064 
00065 /*==============================================================================
00066  * FUNCTION:        DfaTest::testMeetInt
00067  * OVERVIEW:        Test meeting IntegerTypes with various other types
00068  *============================================================================*/
00069 void DfaTest::testMeetInt () {
00070     IntegerType i32(32, 1);
00071     IntegerType j32(32, 0);
00072     IntegerType u32(32, -1);
00073     IntegerType xint(0);
00074     IntegerType j16(16, 0);
00075     SizeType s32(32);
00076     SizeType s64(64);
00077     FloatType flt(32);
00078     PointerType pt(&flt);
00079     VoidType v;
00080     
00081     bool ch = false;
00082     i32.meetWith(&i32, ch, false);
00083     CPPUNIT_ASSERT(ch == false);
00084     std::ostringstream ost1;
00085     ost1<< &i32;
00086     std::string actual(ost1.str());
00087     std::string expected("i32");
00088     CPPUNIT_ASSERT_EQUAL(expected, actual);
00089 
00090     i32.meetWith(&j32, ch, false);
00091     CPPUNIT_ASSERT(ch == false);
00092     j32.meetWith(&i32, ch, false);
00093     CPPUNIT_ASSERT(ch == true);
00094     std::ostringstream ost2;
00095     ost2<< &i32;
00096     actual = ost2.str();
00097     expected = "i32";
00098     CPPUNIT_ASSERT_EQUAL(expected, actual);
00099 
00100     ch = false;
00101     j32.setSigned(0);
00102     j32.meetWith(&v, ch, false);
00103     CPPUNIT_ASSERT(ch == false);
00104     std::ostringstream ost2a;
00105     ost2a<< &j32;
00106     actual = ost2a.str();
00107     expected = "j32";
00108     CPPUNIT_ASSERT_EQUAL(expected, actual);
00109 
00110     ch = false;
00111     j32.meetWith(&u32, ch, false);
00112     CPPUNIT_ASSERT(ch == true);
00113     std::ostringstream ost3;
00114     ost3<< &j32;
00115     actual = ost3.str();
00116     expected = "u32";
00117     CPPUNIT_ASSERT_EQUAL(expected, actual);
00118 
00119     ch = false;
00120     u32.meetWith(&s32, ch, false);
00121     CPPUNIT_ASSERT(ch == false);
00122     std::ostringstream ost4;
00123     ost4<< &u32;
00124     actual = ost4.str();
00125     expected = "u32";
00126     CPPUNIT_ASSERT_EQUAL(expected, actual);
00127     
00128     u32.meetWith(&s64, ch, false);
00129     CPPUNIT_ASSERT(ch == true);
00130     std::ostringstream ost5;
00131     ost5<< &u32;
00132     actual = ost5.str();
00133     expected = "u64";
00134     CPPUNIT_ASSERT_EQUAL(expected, actual);
00135 
00136     ch = false;
00137     Type* res = i32.meetWith(&flt, ch, false);
00138     CPPUNIT_ASSERT(ch == true);
00139     std::ostringstream ost6;
00140     ost6<< res;
00141     actual = ost6.str();
00142     expected = "union";
00143     CPPUNIT_ASSERT_EQUAL(expected, actual);
00144 
00145     ch = false;
00146     res = i32.meetWith(&pt, ch, false);
00147     CPPUNIT_ASSERT(ch == true);
00148     std::ostringstream ost7;
00149     ost7<< res;
00150     actual = ost7.str();
00151     expected = "union";
00152     CPPUNIT_ASSERT_EQUAL(expected, actual);
00153 }
00154 
00155 /*==============================================================================
00156  * FUNCTION:        DfaTest::testMeetSize
00157  * OVERVIEW:        Test meeting IntegerTypes with various other types
00158  *============================================================================*/
00159 void DfaTest::testMeetSize () {
00160     IntegerType i32(32, 1);
00161     SizeType s32(32);
00162     SizeType s16(16);
00163     FloatType flt(32);
00164     VoidType v;
00165 
00166     bool ch = false;
00167     Type* res = s32.meetWith(&i32, ch, false);
00168     CPPUNIT_ASSERT(ch == true);
00169     std::ostringstream ost1;
00170     ost1 << res;
00171     std::string actual(ost1.str());
00172     std::string expected("i32");
00173     CPPUNIT_ASSERT_EQUAL(expected, actual);
00174 
00175     ch = false;
00176     res = s32.meetWith(&s16, ch, false);
00177     CPPUNIT_ASSERT(ch == false);
00178 
00179 #if 0
00180     // There is a known failure here; to show the warning, use ErrLogger
00181     Boomerang::get()->setLogger(new ErrLogger);
00182 
00183     res = s16.meetWith(&flt, ch, false);
00184     CPPUNIT_ASSERT(ch == true);
00185     std::ostringstream ost2;
00186     ost2 << res;
00187     actual = ost2.str();
00188     expected = "union";
00189     CPPUNIT_ASSERT_EQUAL(expected, actual);
00190 #endif
00191     
00192     ch = false;
00193     res = s16.meetWith(&v, ch, false);
00194     CPPUNIT_ASSERT(ch == false);
00195     std::ostringstream ost3;
00196     ost3 << res;
00197     actual = ost3.str();
00198     expected = "16";
00199     CPPUNIT_ASSERT_EQUAL(expected, actual);
00200     
00201 }
00202 
00203 /*==============================================================================
00204  * FUNCTION:        DfaTest::testMeetPointer
00205  * OVERVIEW:        Test meeting IntegerTypes with various other types
00206  *============================================================================*/
00207 void DfaTest::testMeetPointer() {
00208     IntegerType i32(32, 1);
00209     IntegerType u32(32, -1);
00210     PointerType pi32(&i32);
00211     PointerType pu32(&u32);
00212     VoidType v;
00213     
00214     std::ostringstream ost1;
00215     ost1 << pu32.getCtype();
00216     std::string actual(ost1.str());
00217     std::string expected("unsigned int *");
00218     CPPUNIT_ASSERT_EQUAL(expected, actual);
00219 
00220     bool ch = false;
00221     Type* res = pi32.meetWith(&pu32, ch, false);
00222     CPPUNIT_ASSERT(ch == true);
00223     std::ostringstream ost2;
00224     ost2 << res->getCtype();
00225     actual = ost2.str();
00226     expected = "/*signed?*/int *";
00227     CPPUNIT_ASSERT_EQUAL(expected, actual);
00228 
00229     ch = false;
00230     res = pi32.meetWith(&v, ch, false); 
00231     CPPUNIT_ASSERT(ch == false);
00232 
00233     res = pi32.meetWith(&i32, ch, false);
00234     std::ostringstream ost3;
00235     ost3 << res;
00236     actual = ost3.str();
00237     expected = "union";
00238     CPPUNIT_ASSERT_EQUAL(expected, actual);
00239 
00240 }
00241 
00242 /*==============================================================================
00243  * FUNCTION:        DfaTest::testMeetUnion
00244  * OVERVIEW:        Test meeting IntegerTypes with various other types
00245  *============================================================================*/
00246 void DfaTest::testMeetUnion() {
00247     UnionType u1;
00248     IntegerType i32(32, 1);
00249     IntegerType j32(32, 0);
00250     IntegerType u32(32, -1);
00251     FloatType flt(32);
00252     u1.addType(&i32, "bow");
00253     u1.addType(&flt, "wow");
00254 
00255     std::ostringstream ost1;
00256     ost1 << u1.getCtype();
00257     std::string actual(ost1.str());
00258     std::string expected("union { int bow; float wow; }");
00259     CPPUNIT_ASSERT_EQUAL(expected, actual);
00260 
00261     bool ch = false;
00262     Type* res = u1.meetWith(&j32, ch, false);
00263     CPPUNIT_ASSERT(ch == false);
00264     std::ostringstream ost2;
00265     ost2 << res->getCtype();
00266     actual = ost2.str();
00267     expected = "union { int bow; float wow; }";
00268     CPPUNIT_ASSERT_EQUAL(expected, actual);
00269     
00270     res = u1.meetWith(&j32, ch, false);
00271     CPPUNIT_ASSERT(ch == false);
00272     std::ostringstream ost3;
00273     ost3 << u1.getCtype();
00274     actual = ost3.str();
00275     expected = "union { int bow; float wow; }";
00276     CPPUNIT_ASSERT_EQUAL(expected, actual);
00277 
00278     // Note: this test relies on the int in the union having signedness 1
00279     res = u1.meetWith(&u32, ch, false);
00280     CPPUNIT_ASSERT(ch == true);
00281     std::ostringstream ost4;
00282     ost4 << u1.getCtype();
00283     actual = ost4.str();
00284     expected = "union { /*signed?*/int bow; float wow; }";
00285     CPPUNIT_ASSERT_EQUAL(expected, actual);
00286 }

Generated on Tue Sep 19 21:18:19 2006 for Boomerang by  doxygen 1.4.6