00001
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
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);
00141 CPPUNIT_TEST(test_owrite_dnothing);
00142 CPPUNIT_TEST(test_owrite_block);
00143
00144 CPPUNIT_TEST(test_dnothing_rback);
00145 CPPUNIT_TEST(test_dnothing_dnothing);
00146 CPPUNIT_TEST(test_dnothing_block);
00147 CPPUNIT_TEST(test_block_rback);
00148 CPPUNIT_TEST(test_block_dnothing);
00149 CPPUNIT_TEST(test_block_block_wr);
00150
00151 CPPUNIT_TEST(test_block_block_rw);
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
00176 if (pthread_create(&tw, NULL , writer, &arg) != 0) {
00177 perror("pthread_create(w)");
00178 return;
00179 }
00180
00181
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
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
00260
00261 int readValue;
00262 CPPUNIT_ASSERT_EQUAL(::RTC::BufferStatus::BUFFER_OK, buff.read(readValue));
00263 CPPUNIT_ASSERT_EQUAL(false, buff.empty());
00264
00265
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
00283 int length1 = 10;
00284 RTC::RingBuffer<int> buff1(length1);
00285 CPPUNIT_ASSERT_EQUAL(false, buff1.full());
00286
00287
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
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
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
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
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
00397 std::cout << "write ret: " << RTC::BufferStatus::toString(buff1.write(writeValue, writeValue)) << std::endl;
00398
00399
00400
00401
00402
00403 int readValue;
00404 buff1.get(readValue);
00405
00406 std::cout << writeValue << " == " << readValue << std::endl;
00407
00408
00409
00410
00411 sleep(1);
00412 }
00413 return;
00414
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
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
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
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
00471
00472 long int length1 = 2;
00473 RTC::RingBuffer<int> buff1(length1);
00474
00475
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
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
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 buff2.write(0);
00517
00518 CPPUNIT_ASSERT_EQUAL(true, buff2.full());
00519
00520 int readValue;
00521
00522 buff2.read(readValue);
00523
00524 CPPUNIT_ASSERT_EQUAL(false, buff2.full());
00525
00526 CPPUNIT_ASSERT_EQUAL(1+123, readValue);
00527
00528
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
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
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
00589 int writeValue = i + 123;
00590 buff2.write(writeValue);
00591 CPPUNIT_ASSERT_EQUAL(false, buff2.empty());
00592
00593
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
00609 int writeValue = i + 123;
00610 buff3.write(writeValue);
00611 CPPUNIT_ASSERT_EQUAL(false, buff3.empty());
00612
00613
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
00783
00784
00785
00786
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
00819
00820
00821
00822
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
00855
00856
00857
00858
00859
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
00876 g_ret = RTC::BufferStatus::BUFFER_OK;
00877
00878
00879 RTC::RingBuffer<int> buff2(8);
00880 buff2.init(prop);
00881 do_test(buff2);
00882
00883 g_ret = RTC::BufferStatus::BUFFER_OK;
00884
00885
00886 std::cout << std::flush;
00887 return;
00888 }
00889
00890
00891
00892
00893
00894
00895
00896
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
00929
00930
00931
00932
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
00965
00966
00967
00968
00969
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
00986 g_ret = RTC::BufferStatus::BUFFER_OK;
00987
00988
00989 RTC::RingBuffer<int> buff2(8);
00990 buff2.init(prop);
00991 do_test(buff2);
00992
00993 g_ret = RTC::BufferStatus::BUFFER_OK;
00994
00995
00996 std::cout << std::flush;
00997 return;
00998 }
00999
01000
01001
01002
01003
01004
01005
01006
01007
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
01041
01042
01043
01044
01045
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
01079
01080
01081
01082
01083
01084
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
01121
01122
01123
01124
01125
01126
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 };
01164
01165
01166
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;
01250 }
01251 #endif // MAIN
01252 #endif // RingBuffer_cpp