RingBufferTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: RingBufferTests.cpp,v $
00023  * Revision 1.3  2008/01/24 01:52:14  tsakamoto
00024  * *** empty log message ***
00025  *
00026  * Revision 1.2  2008/01/11 11:27:11  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.1  2007/12/20 07:50:17  arafune
00030  * *** empty log message ***
00031  *
00032  * Revision 1.2  2006/12/02 18:53:08  n-ando
00033  * Some tests were added.
00034  *
00035  * Revision 1.1  2006/11/27 08:37:03  n-ando
00036  * TestSuites are devided into each directory.
00037  *
00038  *
00039  */
00040 
00041 #ifndef RingBuffer_cpp
00042 #define RingBuffer_cpp
00043 
00044 #include <cppunit/ui/text/TestRunner.h>
00045 #include <cppunit/TextOutputter.h>
00046 #include <cppunit/extensions/TestFactoryRegistry.h>
00047 #include <cppunit/extensions/HelperMacros.h>
00048 #include <cppunit/TestAssert.h>
00049 
00050 #include <string>
00051 #include <sstream>
00052 #include <rtm/RingBuffer.h>
00053 
00054 //#define DEBUG
00055 
00056 static const int NLOOP = 0x00000fff;    // 読み書きのループ回数
00057 typedef ::RTC::BufferStatus::Enum ReturnCode;
00058 int isBlockTest = false;
00059 ReturnCode g_ret = RTC::BufferStatus::BUFFER_OK;
00060 
00061 struct thread_arg
00062 {
00063     thread_arg(RTC::RingBuffer<int>& buff, int loop=NLOOP)
00064         : m_buff(buff), m_n_loop(loop)
00065     {}
00066     RTC::RingBuffer<int>& m_buff;
00067     int m_n_loop;
00068 };
00069 // 読み込みスレッド
00070 void* reader(void* arg)
00071 {
00072   thread_arg* t_arg = (thread_arg*)arg;
00073   RTC::RingBuffer<int>* buff = (RTC::RingBuffer<int>*)&(t_arg->m_buff);
00074   
00075   ReturnCode ret;
00076   for (int i = 0; i < t_arg->m_n_loop; i++) {
00077         int output = -1;
00078         ret = (*buff).read(output);
00079 #if PRINTOUT
00080         std::cout << "read() : " << output << std::endl;
00081 #endif
00082         if (ret == RTC::BufferStatus::TIMEOUT) {
00083           g_ret = ret;
00084           break;
00085         }
00086         if (isBlockTest) {
00087           if (output != i) {
00088                 std::cout << "ERROR!!: output=" << output;
00089                 std::cout << ", correct data=" << i;
00090                 std::cout << ", return code=" << ret << std::endl;
00091                 abort();
00092           }
00093         }
00094   }
00095   return NULL;
00096 }
00097 
00098 // 書き込みスレッド
00099 void* writer(void* arg)
00100 {
00101   thread_arg* t_arg = (thread_arg*)arg;
00102   RTC::RingBuffer<int>* buff = (RTC::RingBuffer<int>*)&(t_arg->m_buff);
00103   
00104   
00105   for (int i = 0; i < t_arg->m_n_loop; i++) {
00106         int input = i;
00107         (*buff).write(input);
00108 #if PRINTOUT
00109         std::cout << "write(" << input << ")" << std::endl;
00110 #endif
00111   }
00112   return NULL;
00113 }
00114 
00115 
00116 namespace RingBuffer
00117 {
00122   class RingBufferTests
00123     : public CppUnit::TestFixture
00124   {
00125     CPPUNIT_TEST_SUITE(RingBufferTests);
00126 
00127     CPPUNIT_TEST(test_length);
00128     CPPUNIT_TEST(test_isFull);
00129     CPPUNIT_TEST(test_isEmpty);
00130     CPPUNIT_TEST(test_init);
00131     CPPUNIT_TEST(test_write_read);
00132     CPPUNIT_TEST(test_write_read_with_small_length);
00133     CPPUNIT_TEST(test_isNew);
00134     CPPUNIT_TEST(test_reset);
00135     CPPUNIT_TEST(test_wptr_put);
00136     CPPUNIT_TEST(test_advanceWptr);
00137     CPPUNIT_TEST(test_rptr_get);
00138     CPPUNIT_TEST(test_advanceRptr);
00139 
00140         CPPUNIT_TEST(test_owrite_rback); // full: overwrite, empty: readback, buff length 1,8
00141         CPPUNIT_TEST(test_owrite_dnothing); // full: overwrite, empty: do_nothing, buff length 1,8
00142         CPPUNIT_TEST(test_owrite_block); // full: overwrite, empty: block, buff length 1,8
00143 
00144         CPPUNIT_TEST(test_dnothing_rback); // full: do_nothing, empty: readback, buff length 1,8
00145         CPPUNIT_TEST(test_dnothing_dnothing); // full: do_nothing, empty: do_nothing, buff length 1,8
00146         CPPUNIT_TEST(test_dnothing_block); // full: do_nothing, empty: block, buff length 1,8
00147         CPPUNIT_TEST(test_block_rback); // full: block, empty: readback, buff length 1,8
00148         CPPUNIT_TEST(test_block_dnothing); // full: block, empty: do_nothing, buff length 1,8
00149         CPPUNIT_TEST(test_block_block_wr); // full: block, empty: block, buff length 1,8
00150 
00151         CPPUNIT_TEST(test_block_block_rw); // full: block, empty: block, read -> write
00152 
00153     CPPUNIT_TEST_SUITE_END();
00154                 
00155   private:
00156         void do_test(RTC::RingBuffer<int>& buff, int loop=NLOOP, bool read_first=false)
00157         {
00158           pthread_t tr, tw;
00159           thread_arg arg(buff,loop);
00160 
00161           if (read_first) {
00162                 // 読み込みスレッドの開始
00163                 if (pthread_create(&tr, NULL , reader, static_cast<void *>(&buff)) != 0) {
00164                   perror("pthread_create(r)");
00165                   return;
00166                 }
00167                 // 書き込みスレッドの開始
00168                 if (pthread_create(&tw, NULL , writer, static_cast<void *>(&buff)) != 0) {
00169                   perror("pthread_create(w)");
00170                   return;
00171                 }
00172           }
00173           else {
00174                 // 書き込みスレッドの開始
00175 //              if (pthread_create(&tw, NULL , writer, static_cast<void *>(&buff)) != 0) {
00176                 if (pthread_create(&tw, NULL , writer, &arg) != 0) {
00177                   perror("pthread_create(w)");
00178                   return;
00179                 }
00180                 // 読み込みスレッドの開始
00181 //              if (pthread_create(&tr, NULL , reader, static_cast<void *>(&buff)) != 0) {
00182                 if (pthread_create(&tr, NULL , reader, &arg) != 0) {
00183                   perror("pthread_create(r)");
00184                   return;
00185                 }
00186           }
00187           
00188           // スレッドの合流
00189           pthread_join(tw, NULL);
00190           pthread_join(tr, NULL);
00191         }
00192                 
00193   public:
00197     RingBufferTests()
00198     {
00199     }
00200                 
00204     ~RingBufferTests()
00205     {
00206     }
00207                 
00211     virtual void setUp()
00212     {
00213     }
00214                 
00218     virtual void tearDown()
00219     { 
00220     }
00221                 
00227     void test_length()
00228     {
00229       RTC::RingBuffer<int> buff(123);
00230       CPPUNIT_ASSERT(buff.length() == 123);
00231 
00232       size_t len(456);
00233       buff.length(len);
00234       CPPUNIT_ASSERT(buff.length() == len);
00235     }
00236 
00246     void test_isEmpty()
00247     {
00248       long int length = 10;
00249       RTC::RingBuffer<int> buff(length);
00250                         
00251       // (1) バッファ初期化直後、空ではないと判定されるか?
00252       int initialValue = 12345;
00253       for (unsigned int i(0); i < buff.length(); ++i)
00254         {
00255           buff.write(initialValue);
00256         }
00257       CPPUNIT_ASSERT_EQUAL(false, buff.empty());                        
00258                         
00259       // (2) 最後にデータが読み取られた後、新しいデータが書き込みされて
00260       // いない場合、空ではないと判定されるか?
00261       int readValue;
00262       CPPUNIT_ASSERT_EQUAL(::RTC::BufferStatus::BUFFER_OK, buff.read(readValue));
00263       CPPUNIT_ASSERT_EQUAL(false, buff.empty());
00264                         
00265       // (3) 最後にデータが読み取られた後、新しいデータが書き込みされた
00266       // 場合、空ではないと判定されるか?
00267       int writeValue = 98765;
00268       CPPUNIT_ASSERT_EQUAL(RTC::BufferBase<int>::BUFFER_OK, buff.write(writeValue));
00269       CPPUNIT_ASSERT_EQUAL(false, buff.empty());
00270     }
00271                 
00272                 
00280     void test_isFull()
00281     {
00282       // (1) バッファが空の場合、フル判定は偽となるか?
00283       int length1 = 10;
00284       RTC::RingBuffer<int> buff1(length1);
00285       CPPUNIT_ASSERT_EQUAL(false, buff1.full());
00286                         
00287       // (2) 全バッファにデータが書き込まれている状態で、フル判定は真
00288       // となるか?
00289       int length2 = 10;
00290       RTC::RingBuffer<int> buff2(length2);
00291       for (int i = 0; i < length2; i++) {
00292         buff2.write(i);
00293       }
00294       CPPUNIT_ASSERT_EQUAL(true, buff2.full());
00295                         
00296       // (3) バッファに幾分データが書き込まれている状態で、フル判定は偽
00297       // となるか?
00298       int length3 = 10;
00299       RTC::RingBuffer<int> buff3(length3);
00300       for (int i = 0; i < length3 / 2; i++) {
00301         buff3.write(i);
00302       }
00303       CPPUNIT_ASSERT_EQUAL(false, buff3.full());
00304     }
00305                                 
00311     void test_init()
00312     {
00313       // バッファを作成して、init()で初期化する
00314       long int length = 10;
00315       RTC::RingBuffer<int> buff(length);
00316                         
00317       int value = 12345;
00318       for (unsigned int i(0); i < buff.length(); ++i)
00319         {
00320           buff.write(value);
00321         }
00322                         
00323       // 設定したデータを正しく読み出せるか?
00324       int expected = 12345;
00325       for (long int i = 0; i < length; i++) {
00326         int actual;
00327         buff.read(actual);
00328         CPPUNIT_ASSERT_EQUAL(expected, actual);
00329       }
00330     }
00331 
00342     void test_write_read()
00343     {
00344       // (1) バッファ空状態で1データ書込・読出を行い、書き込んだデータ
00345       // を正しく読み出せるか?バッファ作成し、空のままにする
00346       long int length1 = 3;
00347       RTC::RingBuffer<int> buff1(length1);
00348       coil::Properties prop;
00349       prop["write.full_policy"] = "block";
00350       prop["write.timeout"] = "5.0";
00351       prop["read.empty_policy"] = "block";
00352       prop["read.timeout"] = "5.0";
00353 
00354       buff1.init(prop);
00355       // 1データ書込・読出を行う
00356       for (int writeValue = 0; writeValue < 100; writeValue++) {
00357         // 書込み
00358         if (buff1.full())
00359           {
00360             std::cout << "### FULL ###" << std::endl;
00361             int readValue;
00362 
00363             if (writeValue % 5 == 0)
00364               {
00365                 while (!buff1.empty())
00366                   {
00367                     std::cout << "read timeout: " << 5 << std::endl;
00368                     buff1.read(readValue, 5);
00369                     std::cout << "read: " << readValue << std::endl;
00370                   }
00371                 std::cout << "read timeout: " << 5 << std::endl;
00372                 std::cout << "waiting 5 sec" << std::endl;
00373                 std::cout << "read ret: " << RTC::BufferStatus::toString(buff1.read(readValue, 5)) << std::endl;
00374                 std::cout << "read: " << readValue << std::endl;
00375               }
00376             else
00377               {
00378                 buff1.read(readValue);
00379                 std::cout << "read: " << readValue << std::endl;
00380               }
00381 
00382             if (buff1.full())
00383               {
00384                 std::cout << "??? still full" << std::endl;
00385               }
00386             else
00387               {
00388                 std::cout << "buffer full was blown over." << std::endl;
00389               }
00390           }
00391         if (buff1.empty())
00392           {
00393             std::cout << "### EMPTY ###" << std::endl;
00394           }
00395 
00396         //        std::cout << "timeout-> " << writeValue << std::endl;
00397         std::cout << "write ret: " << RTC::BufferStatus::toString(buff1.write(writeValue, writeValue)) << std::endl;
00398                                 
00399         //        std::cout << "readable " << buff1.readable() << std::endl;
00400         //        std::cout << "writable " << buff1.writable() << std::endl;
00401 
00402         // 読出し
00403         int readValue;
00404         buff1.get(readValue);
00405                         
00406         std::cout << writeValue << " == " << readValue << std::endl;
00407 
00408         //        buff1.read(readValue)
00409         // 書き込んだデータを正しく読み出せたか?
00410         //      CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
00411         sleep(1);
00412       }
00413       return;
00414       // (2) 全バッファにデータが書き込まれている状態で1データ書込・読
00415       // 出を行い、書き込んだデータを正しく読み出せるか?バッファ作成し、
00416       // フル状態にする
00417       long int length2 = 10;
00418       RTC::RingBuffer<int> buff2(length2);
00419       for (int i = 0; i < length2; i++) {
00420         buff2.write(i + 123);
00421       }
00422                         
00423       // 1データ書込・読出を行う
00424       for (int writeValue = 0; writeValue < 100; writeValue++) {
00425         // 書込み
00426         buff2.write(writeValue);
00427                                 
00428         // 読出し
00429         int readValue;
00430         buff2.read(readValue);
00431                                 
00432         // 書き込んだデータを正しく読み出せたか?
00433         CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
00434       }
00435                         
00436       // (3) バッファに幾分データが書き込まれている状態で1データ書込・
00437       // 読出を行い、書き込んだデータを正しく読み出せるか?
00438       long int length3 = 10;
00439       RTC::RingBuffer<int> buff3(length3);
00440       for (int i = 0; i < length3 / 2; i++) {
00441         buff3.write(i + 123);
00442       }
00443                         
00444       // 1データ書込・読出を行う
00445       for (int writeValue = 0; writeValue < 100; writeValue++) {
00446         // 書込み
00447         buff3.write(writeValue);
00448                                 
00449         // 読出し
00450         int readValue;
00451         buff3.read(readValue);
00452                                 
00453         // 書き込んだデータを正しく読み出せたか?
00454         CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
00455       }
00456     }
00457                 
00468     void test_write_read_with_small_length()
00469     {
00470       // (1) バッファ空状態で1データ書込・読出を行い、書き込んだデータ
00471       // を正しく読み出せるか?バッファ作成し、空のままにする
00472       long int length1 = 2;
00473       RTC::RingBuffer<int> buff1(length1);
00474                         
00475       // 1データ書込・読出を行う
00476       for (int writeValue = 0; writeValue < 100; writeValue++) {
00477         // 書込み
00478         buff1.write(writeValue);
00479                                 
00480         // 読出し
00481         int readValue;
00482         buff1.read(readValue);
00483                                 
00484         // 書き込んだデータを正しく読み出せたか?
00485         CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
00486       }
00487                         
00488       // (2) 全バッファにデータが書き込まれている状態で1データ書込・読
00489       // 出を行い、書き込んだデータを正しく読み出せるか?バッファ作成し、
00490       // フル状態にする
00491       long int length2 = 2;
00492       RTC::RingBuffer<int> buff2(length2);
00493       coil::Properties prop;
00494       prop["write.full_policy"] = "overwrite";
00495       buff2.init(prop);
00496 
00497       for (int i = 0; i < length2; i++) {
00498         buff2.write(i + 123);
00499       }
00500                         
00501       // 1データ書込・読出を行う
00502 //      for (int writeValue = 0; writeValue < 100; writeValue++) {
00503 //      // 書込み
00504 //      buff2.write(writeValue);
00505 //                              
00506 //      // 読出し
00507 //      int readValue;
00508 //      buff2.read(readValue);
00509 //                              
00510 //      // 書き込んだデータを正しく読み出せたか?
00511 //      CPPUNIT_ASSERT_EQUAL(writeValue+1+123, readValue);
00512 //     }
00513 
00514         // 書込み
00515         /* policyがoverwriteでfull状態で書き込むと、古いデータを上書きし、読み込み側のポインタをインクリメントする。*/
00516         buff2.write(0);
00517                                 
00518         CPPUNIT_ASSERT_EQUAL(true, buff2.full());
00519         // 読出し
00520         int readValue;
00521         /* 読み出して読み込み側のポインタをインクメントするためfull状態ではなくなる */
00522         buff2.read(readValue);
00523 
00524         CPPUNIT_ASSERT_EQUAL(false, buff2.full());
00525         // 書き込んだデータを正しく読み出せたか?
00526         CPPUNIT_ASSERT_EQUAL(1+123, readValue);
00527                         
00528       // (3) バッファに幾分データが書き込まれている状態で1データ書込・読出を行い、書き込んだデータを正しく読み出せるか?
00529       long int length3 = 2;
00530       RTC::RingBuffer<int> buff3(length3);
00531       coil::Properties prop3;
00532       prop3["write.full_policy"] = "overwrite";
00533       buff3.init(prop3);
00534 
00535       for (int i = 0; i < 1; i++) {
00536         buff3.write(i + 123);
00537       }
00538       { 
00539         // 書込み
00540         buff3.write(-1);
00541                                 
00542         // 読出し
00543         int readValue;
00544         buff3.read(readValue);
00545                                 
00546         // 書き込んだデータを正しく読み出せたか?
00547         CPPUNIT_ASSERT_EQUAL(123, readValue);
00548       }
00549       // 1データ書込・読出を行う
00550       for (int writeValue = 0; writeValue < 100; writeValue++) {
00551         // 書込み
00552         buff3.write(writeValue);
00553                                 
00554         // 読出し
00555         int readValue;
00556         buff3.read(readValue);
00557                                 
00558         // 書き込んだデータを正しく読み出せたか?
00559         CPPUNIT_ASSERT_EQUAL(writeValue-1, readValue);
00560       }
00561     }
00562                 
00576     void test_isNew()
00577     {
00578       // (1) バッファが空の状態で、isNew判定が偽になるか?
00579       long int length1 = 10;
00580       RTC::RingBuffer<int> buff1(length1);
00581       CPPUNIT_ASSERT_EQUAL(true, buff1.empty());
00582                         
00583       // 全バッファにデータが書き込まれている状態で...
00584       long int length2 = 10;
00585       RTC::RingBuffer<int> buff2(length2);
00586                         
00587       for (int i = 0; i < length2; i++) {
00588         // (2) ...データ書込後のisNew判定が真になるか?
00589         int writeValue = i + 123;
00590         buff2.write(writeValue);
00591         CPPUNIT_ASSERT_EQUAL(false, buff2.empty());
00592                                 
00593         // (3) ...データ書込し、そのデータ読出を行った後のisNew判定が偽
00594         // になるか?
00595         int readValue;
00596         buff2.read(readValue);
00597         CPPUNIT_ASSERT_EQUAL(true, buff2.empty());
00598       }
00599                         
00600       // バッファに幾分データが書き込まれている状態で...
00601       long int length3 = 10;
00602       RTC::RingBuffer<int> buff3(length3);
00603       for (int i = 0; i < length3 / 2; i++) {
00604         buff3.write(i + 456);
00605       }
00606                         
00607       for (int i = 0; i < length3; i++) {
00608         // (4) ...データ書込後のisNew判定が真になるか?
00609         int writeValue = i + 123;
00610         buff3.write(writeValue);
00611         CPPUNIT_ASSERT_EQUAL(false, buff3.empty());
00612                                 
00613         // (5) ...データ書込し、そのデータ読出を行った後のisNew判定が偽
00614         // になるか?
00615         int readValue;
00616         buff3.read(readValue);
00617         CPPUNIT_ASSERT_EQUAL(false, buff3.empty());
00618       }
00619     }
00624     void test_reset()
00625     {
00626       int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
00627       RTC::RingBuffer<int> buff(10);
00628 
00629       for(int ic(0);ic<8;++ic)
00630       {
00631           buff.put(idata[ic]);
00632           buff.advanceWptr();
00633       }
00634       buff.advanceRptr(3);
00635       CPPUNIT_ASSERT_EQUAL(buff.get(), idata[3]);
00636       CPPUNIT_ASSERT_EQUAL(buff.readable(), (size_t)5);
00637 
00638       buff.reset();
00639       CPPUNIT_ASSERT(buff.empty());
00640       CPPUNIT_ASSERT_EQUAL(buff.get(), idata[0]);
00641       buff.put(idata[9]);
00642       CPPUNIT_ASSERT_EQUAL(buff.get(), idata[9]);
00643       CPPUNIT_ASSERT_EQUAL(buff.readable(), (size_t)0);
00644       
00645     }   
00650     void test_wptr_put()
00651     {
00652       int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
00653       RTC::RingBuffer<int> buff(10);
00654       for(int ic(0);ic<10;++ic)
00655       {
00656           buff.put(idata[ic]);
00657           buff.advanceWptr();
00658       }
00659       buff.reset();
00660       for(int ic(0);ic<10;++ic)
00661       {
00662           CPPUNIT_ASSERT_EQUAL(idata[ic],*buff.wptr(ic));
00663       }
00664       for(int ic(0);ic<10;++ic)
00665       {
00666           CPPUNIT_ASSERT_EQUAL(idata[(-ic+10)%10],*buff.wptr(-ic));
00667       }
00668       buff.advanceWptr(5);
00669       for(int ic(0);ic<10;++ic)
00670       {
00671           CPPUNIT_ASSERT_EQUAL(idata[(5+ic)%10],*buff.wptr(ic));
00672       }
00673       for(int ic(0);ic<10;++ic)
00674       {
00675           CPPUNIT_ASSERT_EQUAL(idata[(5-ic+10)%10],*buff.wptr(-ic));
00676       }
00677         
00678     }
00683     void test_advanceWptr()
00684     {
00685         int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
00686         RTC::RingBuffer<int> buff(10);
00687         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(-5),::RTC::BufferStatus::PRECONDITION_NOT_MET);
00688         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(5),::RTC::BufferStatus::BUFFER_OK);
00689         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(8),::RTC::BufferStatus::PRECONDITION_NOT_MET);
00690         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(-5),::RTC::BufferStatus::BUFFER_OK);
00691         buff.reset();
00692         for(int ic(0);ic<10;++ic)
00693         {
00694             buff.put(idata[ic]);
00695             buff.advanceWptr();
00696         }
00697         buff.reset();
00698         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(5),::RTC::BufferStatus::BUFFER_OK);
00699         buff.advanceRptr(5);
00700         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(-5),::RTC::BufferStatus::PRECONDITION_NOT_MET);
00701         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(8),::RTC::BufferStatus::BUFFER_OK);
00702         CPPUNIT_ASSERT_EQUAL(idata[3],*buff.wptr());
00703         CPPUNIT_ASSERT_EQUAL((size_t)8,buff.readable());
00704         CPPUNIT_ASSERT_EQUAL(buff.advanceWptr(-5),::RTC::BufferStatus::BUFFER_OK);
00705         CPPUNIT_ASSERT_EQUAL(idata[8],*buff.wptr());
00706         CPPUNIT_ASSERT_EQUAL((size_t)3,buff.readable());
00707     }
00712     void test_rptr_get()
00713     {
00714         int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
00715         RTC::RingBuffer<int> buff(10);
00716         for(int ic(0);ic<10;++ic)
00717         {  
00718             buff.put(idata[ic]);
00719             buff.advanceWptr();
00720         }
00721         buff.reset();
00722         for(int ic(0);ic<10;++ic)
00723         {
00724             CPPUNIT_ASSERT_EQUAL(idata[ic],*buff.rptr(ic));
00725         }
00726         for(int ic(0);ic<10;++ic)
00727         { 
00728             CPPUNIT_ASSERT_EQUAL(idata[(-ic+10)%10],*buff.rptr(-ic));
00729         }
00730         buff.advanceWptr(5);
00731         buff.advanceRptr(5);
00732         for(int ic(0);ic<10;++ic)
00733         {
00734             CPPUNIT_ASSERT_EQUAL(idata[(5+ic)%10],*buff.rptr(ic));
00735         }
00736         for(int ic(0);ic<10;++ic)
00737         {
00738             CPPUNIT_ASSERT_EQUAL(idata[(5-ic+10)%10],*buff.rptr(-ic));
00739         }
00740         buff.reset();
00741         buff.advanceWptr(10);
00742         for(int ic(0);ic<10;++ic)
00743         {
00744             CPPUNIT_ASSERT_EQUAL(idata[ic],buff.get());
00745             int ret;
00746             buff.get(ret);
00747             CPPUNIT_ASSERT_EQUAL(idata[ic],ret);
00748             buff.advanceRptr();
00749         }
00750     }
00755     void test_advanceRptr()
00756     {
00757         int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
00758         RTC::RingBuffer<int> buff(10);
00759         buff.advanceWptr(5);
00760         CPPUNIT_ASSERT_EQUAL(buff.advanceRptr(-6),::RTC::BufferStatus::PRECONDITION_NOT_MET);
00761         CPPUNIT_ASSERT_EQUAL(buff.advanceRptr(5),::RTC::BufferStatus::BUFFER_OK);
00762         CPPUNIT_ASSERT_EQUAL(buff.advanceRptr(8),::RTC::BufferStatus::PRECONDITION_NOT_MET);
00763         CPPUNIT_ASSERT_EQUAL(buff.advanceRptr(-5),::RTC::BufferStatus::BUFFER_OK);
00764         buff.reset();
00765         buff.advanceWptr(5);
00766         buff.advanceRptr(5);
00767         for(int ic(0);ic<10;++ic)
00768         {
00769             buff.put(idata[ic]);
00770             buff.advanceWptr();
00771         }
00772         CPPUNIT_ASSERT_EQUAL(buff.advanceRptr(-6),::RTC::BufferStatus::PRECONDITION_NOT_MET);
00773         CPPUNIT_ASSERT_EQUAL(buff.advanceRptr(8),::RTC::BufferStatus::BUFFER_OK);
00774         CPPUNIT_ASSERT_EQUAL(idata[8],*buff.rptr());
00775         CPPUNIT_ASSERT_EQUAL((size_t)8,buff.writable());
00776         CPPUNIT_ASSERT_EQUAL(buff.advanceRptr(-5),::RTC::BufferStatus::BUFFER_OK);
00777         CPPUNIT_ASSERT_EQUAL(idata[3],*buff.rptr());
00778         CPPUNIT_ASSERT_EQUAL((size_t)3,buff.writable());
00779     }
00780 
00781         /*
00782          * デッドロックの検証1
00783          *
00784          * バッファサイズ: 1, 8
00785          * write.full_policy: overwrite
00786          * read.empty_policy: readback
00787          *
00788          */
00789         void test_owrite_rback()
00790         {
00791           RTC::RingBuffer<int> buff(1);
00792 
00793           // デフォルト設定以外のバッファ
00794           coil::Properties prop;
00795           prop["write.full_policy"] = "overwrite";
00796           prop["read.empty_policy"] = "readback";
00797           buff.init(prop);
00798 
00799           g_ret = RTC::BufferStatus::BUFFER_OK;
00800           do_test(buff);
00801 
00802           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00803           g_ret = RTC::BufferStatus::BUFFER_OK;
00804 
00805           RTC::RingBuffer<int> buff2(8);
00806           buff2.init(prop);
00807 
00808           do_test(buff2);
00809           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00810           g_ret = RTC::BufferStatus::BUFFER_OK;
00811 
00812           std::cout << std::flush;
00813           return;
00814         }
00815 
00816 
00817         /*
00818          * デッドロックの検証2
00819          *
00820          * バッファサイズ: 1, 8
00821          * write.full_policy: overwrite
00822          * read.empty_policy: do_nothing
00823          *
00824          */
00825         void test_owrite_dnothing()
00826         {
00827           RTC::RingBuffer<int> buff(1);
00828 
00829           // デフォルト設定以外のバッファ
00830           coil::Properties prop;
00831           prop["write.full_policy"] = "overwrite";
00832           prop["read.empty_policy"] = "do_nothing";
00833           buff.init(prop);
00834 
00835           g_ret = RTC::BufferStatus::BUFFER_OK;
00836           do_test(buff);
00837           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00838           g_ret = RTC::BufferStatus::BUFFER_OK;
00839 
00840 
00841           RTC::RingBuffer<int> buff2(8);
00842           buff2.init(prop);
00843 
00844           do_test(buff2);
00845           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00846           g_ret = RTC::BufferStatus::BUFFER_OK;
00847 
00848           std::cout << std::flush;
00849           return;
00850         }
00851 
00852 
00853         /*
00854          * デッドロックの検証3
00855          *
00856          * バッファサイズ: 1, 8
00857          * write.full_policy: overwrite
00858          * read.empty_policy: block
00859          * read.timeout: 3.0
00860          *
00861          */
00862         void test_owrite_block()
00863         {
00864           RTC::RingBuffer<int> buff(1);
00865 
00866           // デフォルト設定以外のバッファ
00867           coil::Properties prop;
00868           prop["write.full_policy"] = "overwrite";
00869           prop["read.empty_policy"] = "block";
00870           prop["read.timeout"]      = "3.0";
00871           buff.init(prop);
00872 
00873           g_ret = RTC::BufferStatus::BUFFER_OK;
00874           do_test(buff);
00875           // CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00876           g_ret = RTC::BufferStatus::BUFFER_OK;
00877 
00878 
00879           RTC::RingBuffer<int> buff2(8);
00880           buff2.init(prop);
00881           do_test(buff2);
00882           // CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00883           g_ret = RTC::BufferStatus::BUFFER_OK;
00884 
00885 
00886           std::cout << std::flush;
00887           return;
00888         }
00889                 
00890 
00891         /*
00892          * デッドロックの検証4
00893          *
00894          * バッファサイズ: 1, 8
00895          * write.full_policy: do_nothing
00896          * read.empty_policy: readback
00897          *
00898          */
00899         void test_dnothing_rback()
00900         {
00901           RTC::RingBuffer<int> buff(1);
00902 
00903           // デフォルト設定以外のバッファ
00904           coil::Properties prop;
00905           prop["write.full_policy"] = "do_nothing";
00906           prop["read.empty_policy"] = "readback";
00907           buff.init(prop);
00908 
00909           g_ret = RTC::BufferStatus::BUFFER_OK;
00910           do_test(buff);
00911           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00912           g_ret = RTC::BufferStatus::BUFFER_OK;
00913 
00914 
00915           RTC::RingBuffer<int> buff2(8);
00916           buff2.init(prop);
00917           do_test(buff2);
00918           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00919           g_ret = RTC::BufferStatus::BUFFER_OK;
00920 
00921 
00922           std::cout << std::flush;
00923           return;
00924         }
00925 
00926 
00927         /*
00928          * デッドロックの検証5
00929          *
00930          * バッファサイズ: 1, 8
00931          * write.full_policy: do_nothing
00932          * read.empty_policy: do_nothing
00933          *
00934          */
00935         void test_dnothing_dnothing()
00936         {
00937           RTC::RingBuffer<int> buff(1);
00938 
00939           // デフォルト設定以外のバッファ
00940           coil::Properties prop;
00941           prop["write.full_policy"] = "do_nothing";
00942           prop["read.empty_policy"] = "do_nothing";
00943           buff.init(prop);
00944 
00945           g_ret = RTC::BufferStatus::BUFFER_OK;
00946           do_test(buff);
00947           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00948           g_ret = RTC::BufferStatus::BUFFER_OK;
00949 
00950 
00951           RTC::RingBuffer<int> buff2(8);
00952           buff2.init(prop);
00953           do_test(buff2);
00954           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00955           g_ret = RTC::BufferStatus::BUFFER_OK;
00956 
00957 
00958           std::cout << std::flush;
00959           return;
00960         }
00961 
00962 
00963         /*
00964          * デッドロックの検証6
00965          *
00966          * バッファサイズ: 1, 8
00967          * write.full_policy: do_nothing
00968          * read.empty_policy: block
00969          * read.timeout: 1.0
00970          *
00971          */
00972         void test_dnothing_block()
00973         {
00974           RTC::RingBuffer<int> buff(1);
00975 
00976           // デフォルト設定以外のバッファ
00977           coil::Properties prop;
00978           prop["write.full_policy"] = "do_nothing";
00979           prop["read.empty_policy"] = "block";
00980           prop["read.timeout"]      = "1.0";
00981           buff.init(prop);
00982 
00983           g_ret = RTC::BufferStatus::BUFFER_OK;
00984           do_test(buff);
00985           // CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00986           g_ret = RTC::BufferStatus::BUFFER_OK;
00987 
00988 
00989           RTC::RingBuffer<int> buff2(8);
00990           buff2.init(prop);
00991           do_test(buff2);
00992           // CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
00993           g_ret = RTC::BufferStatus::BUFFER_OK;
00994 
00995 
00996           std::cout << std::flush;
00997           return;
00998         }
00999 
01000 
01001         /*
01002          * デッドロックの検証7
01003          *
01004          * バッファサイズ: 1, 8
01005          * write.full_policy: block
01006          * read.empty_policy: readback
01007          * write.timeout: 1.0
01008          *
01009          */
01010         void test_block_rback()
01011         {
01012           RTC::RingBuffer<int> buff(1);
01013 
01014           // デフォルト設定以外のバッファ
01015           coil::Properties prop;
01016           prop["write.full_policy"] = "block";
01017           prop["read.empty_policy"] = "readback";
01018           prop["write.timeout"]      = "1.0";
01019           buff.init(prop);
01020 
01021           g_ret = RTC::BufferStatus::BUFFER_OK;
01022           do_test(buff,100);
01023           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01024           g_ret = RTC::BufferStatus::BUFFER_OK;
01025 
01026 
01027           RTC::RingBuffer<int> buff2(8);
01028           buff2.init(prop);
01029           do_test(buff2,100);
01030           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01031           g_ret = RTC::BufferStatus::BUFFER_OK;
01032 
01033 
01034           std::cout << std::flush;
01035           return;
01036         }
01037 
01038 
01039         /*
01040          * デッドロックの検証8
01041          *
01042          * バッファサイズ: 1, 8
01043          * write.full_policy: block
01044          * read.empty_policy: do_nothing
01045          * write.timeout: 1.0
01046          *
01047          */
01048         void test_block_dnothing()
01049         {
01050           RTC::RingBuffer<int> buff(1);
01051 
01052           // デフォルト設定以外のバッファ
01053           coil::Properties prop;
01054           prop["write.full_policy"] = "block";
01055           prop["read.empty_policy"] = "do_nothing";
01056           prop["write.timeout"]      = "1.0";
01057           buff.init(prop);
01058 
01059           g_ret = RTC::BufferStatus::BUFFER_OK;
01060           do_test(buff,100);
01061           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01062           g_ret = RTC::BufferStatus::BUFFER_OK;
01063 
01064 
01065           RTC::RingBuffer<int> buff2(8);
01066           buff2.init(prop);
01067           do_test(buff2,100);
01068           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01069           g_ret = RTC::BufferStatus::BUFFER_OK;
01070 
01071 
01072           std::cout << std::flush;
01073           return;
01074         }
01075 
01076 
01077         /*
01078          * デッドロックの検証9
01079          *
01080          * バッファサイズ: 1, 8
01081          * write.full_policy: block
01082          * read.empty_policy: block
01083          * write.timeout: 3.0
01084          * read.timeout: 3.0
01085          *
01086          */
01087         void test_block_block_wr()
01088         {
01089           RTC::RingBuffer<int> buff(1);
01090 
01091           // デフォルト設定以外のバッファ
01092           coil::Properties prop;
01093           prop["write.full_policy"] = "block";
01094           prop["read.empty_policy"] = "block";
01095           prop["write.timeout"]     = "3.0";
01096           prop["read.timeout"]      = "3.0";
01097           buff.init(prop);
01098 
01099           isBlockTest = true;
01100           g_ret = RTC::BufferStatus::BUFFER_OK;
01101           do_test(buff);
01102           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01103           g_ret = RTC::BufferStatus::BUFFER_OK;
01104 
01105 
01106           RTC::RingBuffer<int> buff2(8);
01107           buff2.init(prop);
01108           do_test(buff2);
01109           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01110           g_ret = RTC::BufferStatus::BUFFER_OK;
01111 
01112 
01113           isBlockTest = false;
01114           std::cout << std::flush;
01115           return;
01116         }
01117 
01118 
01119         /*
01120          * デッドロックの検証10 read -> write
01121          *
01122          * バッファサイズ: 1, 8
01123          * write.full_policy: block
01124          * read.empty_policy: block
01125          * write.timeout: 3.0
01126          * read.timeout: 3.0
01127          *
01128          */
01129         void test_block_block_rw()
01130         {
01131           RTC::RingBuffer<int> buff(1);
01132 
01133           // デフォルト設定以外のバッファ
01134           coil::Properties prop;
01135           prop["write.full_policy"] = "block";
01136           prop["read.empty_policy"] = "block";
01137           prop["write.timeout"]     = "3.0";
01138           prop["read.timeout"]      = "3.0";
01139           buff.init(prop);
01140 
01141           isBlockTest = true;
01142           g_ret = RTC::BufferStatus::BUFFER_OK;
01143           do_test(buff,true);
01144           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01145           g_ret = RTC::BufferStatus::BUFFER_OK;
01146 
01147 
01148           RTC::RingBuffer<int> buff2(8);
01149           buff2.init(prop);
01150           do_test(buff2,true);
01151           CPPUNIT_ASSERT(g_ret == RTC::BufferStatus::BUFFER_OK);
01152           g_ret = RTC::BufferStatus::BUFFER_OK;
01153 
01154 
01155           isBlockTest = false;
01156           std::cout << std::flush;
01157           return;
01158         }
01159 
01160 
01161 
01162   };
01163 }; // namespace RingBuffer
01164 
01165 /*
01166  * Register test suite
01167  */
01168 CPPUNIT_TEST_SUITE_REGISTRATION(RingBuffer::RingBufferTests);
01169 
01170 #ifdef LOCAL_MAIN
01171 int main(int argc, char* argv[])
01172 {
01173 
01174   FORMAT format = TEXT_OUT;
01175   int target = 0;
01176   std::string xsl;
01177   std::string ns;
01178   std::string fname;
01179   std::ofstream ofs;
01180 
01181   int i(1);
01182   while (i < argc)
01183     {
01184       std::string arg(argv[i]);
01185       std::string next_arg;
01186       if (i + 1 < argc) next_arg = argv[i + 1];
01187       else              next_arg = "";
01188 
01189       if (arg == "--text") { format = TEXT_OUT; break; }
01190       if (arg == "--xml")
01191         {
01192           if (next_arg == "")
01193             {
01194               fname = argv[0];
01195               fname += ".xml";
01196             }
01197           else
01198             {
01199               fname = next_arg;
01200             }
01201           format = XML_OUT;
01202           ofs.open(fname.c_str());
01203         }
01204       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
01205       if ( arg == "--cerr"      ) { target = 1; break; }
01206       if ( arg == "--xsl"       )
01207         {
01208           if (next_arg == "") xsl = "default.xsl"; 
01209           else                xsl = next_arg;
01210         }
01211       if ( arg == "--namespace" )
01212         {
01213           if (next_arg == "")
01214             {
01215               std::cerr << "no namespace specified" << std::endl;
01216               exit(1); 
01217             }
01218           else
01219             {
01220               xsl = next_arg;
01221             }
01222         }
01223       ++i;
01224     }
01225   CppUnit::TextUi::TestRunner runner;
01226   if ( ns.empty() )
01227     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
01228   else
01229     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
01230   CppUnit::Outputter* outputter = 0;
01231   std::ostream* stream = target ? &std::cerr : &std::cout;
01232   switch ( format )
01233     {
01234     case TEXT_OUT :
01235       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
01236       break;
01237     case XML_OUT :
01238       std::cout << "XML_OUT" << std::endl;
01239       outputter = new CppUnit::XmlOutputter(&runner.result(),
01240                                             ofs, "shift_jis");
01241       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
01242       break;
01243     case COMPILER_OUT :
01244       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
01245       break;
01246     }
01247   runner.setOutputter(outputter);
01248   runner.run();
01249   return 0; // runner.run() ? 0 : 1;
01250 }
01251 #endif // MAIN
01252 #endif // RingBuffer_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:17