41 #ifndef RingBuffer_cpp 42 #define RingBuffer_cpp 44 #include <cppunit/ui/text/TestRunner.h> 45 #include <cppunit/TextOutputter.h> 46 #include <cppunit/extensions/TestFactoryRegistry.h> 47 #include <cppunit/extensions/HelperMacros.h> 48 #include <cppunit/TestAssert.h> 56 static const int NLOOP = 0x00000fff;
76 for (
int i = 0; i < t_arg->
m_n_loop; i++) {
78 ret = (*buff).read(output);
80 std::cout <<
"read() : " << output << std::endl;
88 std::cout <<
"ERROR!!: output=" << output;
89 std::cout <<
", correct data=" << i;
90 std::cout <<
", return code=" << ret << std::endl;
105 for (
int i = 0; i < t_arg->
m_n_loop; i++) {
107 (*buff).
write(input);
109 std::cout <<
"write(" << input <<
")" << std::endl;
123 :
public CppUnit::TestFixture
127 CPPUNIT_TEST(test_length);
128 CPPUNIT_TEST(test_isFull);
129 CPPUNIT_TEST(test_isEmpty);
130 CPPUNIT_TEST(test_init);
131 CPPUNIT_TEST(test_write_read);
132 CPPUNIT_TEST(test_write_read_with_small_length);
133 CPPUNIT_TEST(test_isNew);
134 CPPUNIT_TEST(test_reset);
135 CPPUNIT_TEST(test_wptr_put);
136 CPPUNIT_TEST(test_advanceWptr);
137 CPPUNIT_TEST(test_rptr_get);
138 CPPUNIT_TEST(test_advanceRptr);
140 CPPUNIT_TEST(test_owrite_rback);
141 CPPUNIT_TEST(test_owrite_dnothing);
142 CPPUNIT_TEST(test_owrite_block);
144 CPPUNIT_TEST(test_dnothing_rback);
145 CPPUNIT_TEST(test_dnothing_dnothing);
146 CPPUNIT_TEST(test_dnothing_block);
147 CPPUNIT_TEST(test_block_rback);
148 CPPUNIT_TEST(test_block_dnothing);
149 CPPUNIT_TEST(test_block_block_wr);
151 CPPUNIT_TEST(test_block_block_rw);
153 CPPUNIT_TEST_SUITE_END();
163 if (pthread_create(&tr, NULL ,
reader, static_cast<void *>(&buff)) != 0) {
164 perror(
"pthread_create(r)");
168 if (pthread_create(&tw, NULL ,
writer, static_cast<void *>(&buff)) != 0) {
169 perror(
"pthread_create(w)");
176 if (pthread_create(&tw, NULL ,
writer, &arg) != 0) {
177 perror(
"pthread_create(w)");
182 if (pthread_create(&tr, NULL ,
reader, &arg) != 0) {
183 perror(
"pthread_create(r)");
189 pthread_join(tw, NULL);
190 pthread_join(tr, NULL);
230 CPPUNIT_ASSERT(buff.
length() == 123);
234 CPPUNIT_ASSERT(buff.
length() == len);
248 long int length = 10;
252 int initialValue = 12345;
253 for (
unsigned int i(0); i < buff.
length(); ++i)
255 buff.
write(initialValue);
257 CPPUNIT_ASSERT_EQUAL(
false, buff.
empty());
263 CPPUNIT_ASSERT_EQUAL(
false, buff.
empty());
267 int writeValue = 98765;
269 CPPUNIT_ASSERT_EQUAL(
false, buff.
empty());
285 CPPUNIT_ASSERT_EQUAL(
false, buff1.
full());
291 for (
int i = 0; i < length2; i++) {
294 CPPUNIT_ASSERT_EQUAL(
true, buff2.
full());
300 for (
int i = 0; i < length3 / 2; i++) {
303 CPPUNIT_ASSERT_EQUAL(
false, buff3.
full());
314 long int length = 10;
318 for (
unsigned int i(0); i < buff.
length(); ++i)
324 int expected = 12345;
325 for (
long int i = 0; i < length; i++) {
328 CPPUNIT_ASSERT_EQUAL(expected, actual);
346 long int length1 = 3;
349 prop[
"write.full_policy"] =
"block";
350 prop[
"write.timeout"] =
"5.0";
351 prop[
"read.empty_policy"] =
"block";
352 prop[
"read.timeout"] =
"5.0";
356 for (
int writeValue = 0; writeValue < 100; writeValue++) {
360 std::cout <<
"### FULL ###" << std::endl;
363 if (writeValue % 5 == 0)
365 while (!buff1.
empty())
367 std::cout <<
"read timeout: " << 5 << std::endl;
368 buff1.
read(readValue, 5);
369 std::cout <<
"read: " << readValue << std::endl;
371 std::cout <<
"read timeout: " << 5 << std::endl;
372 std::cout <<
"waiting 5 sec" << std::endl;
374 std::cout <<
"read: " << readValue << std::endl;
378 buff1.
read(readValue);
379 std::cout <<
"read: " << readValue << std::endl;
384 std::cout <<
"??? still full" << std::endl;
388 std::cout <<
"buffer full was blown over." << std::endl;
393 std::cout <<
"### EMPTY ###" << std::endl;
404 buff1.
get(readValue);
406 std::cout << writeValue <<
" == " << readValue << std::endl;
417 long int length2 = 10;
419 for (
int i = 0; i < length2; i++) {
420 buff2.
write(i + 123);
424 for (
int writeValue = 0; writeValue < 100; writeValue++) {
426 buff2.
write(writeValue);
430 buff2.
read(readValue);
433 CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
438 long int length3 = 10;
440 for (
int i = 0; i < length3 / 2; i++) {
441 buff3.
write(i + 123);
445 for (
int writeValue = 0; writeValue < 100; writeValue++) {
447 buff3.
write(writeValue);
451 buff3.
read(readValue);
454 CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
472 long int length1 = 2;
476 for (
int writeValue = 0; writeValue < 100; writeValue++) {
478 buff1.
write(writeValue);
482 buff1.
read(readValue);
485 CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
491 long int length2 = 2;
494 prop[
"write.full_policy"] =
"overwrite";
497 for (
int i = 0; i < length2; i++) {
498 buff2.
write(i + 123);
518 CPPUNIT_ASSERT_EQUAL(
true, buff2.
full());
522 buff2.
read(readValue);
524 CPPUNIT_ASSERT_EQUAL(
false, buff2.
full());
526 CPPUNIT_ASSERT_EQUAL(1+123, readValue);
529 long int length3 = 2;
532 prop3[
"write.full_policy"] =
"overwrite";
535 for (
int i = 0; i < 1; i++) {
536 buff3.
write(i + 123);
544 buff3.
read(readValue);
547 CPPUNIT_ASSERT_EQUAL(123, readValue);
550 for (
int writeValue = 0; writeValue < 100; writeValue++) {
552 buff3.
write(writeValue);
556 buff3.
read(readValue);
559 CPPUNIT_ASSERT_EQUAL(writeValue-1, readValue);
579 long int length1 = 10;
581 CPPUNIT_ASSERT_EQUAL(
true, buff1.
empty());
584 long int length2 = 10;
587 for (
int i = 0; i < length2; i++) {
589 int writeValue = i + 123;
590 buff2.
write(writeValue);
591 CPPUNIT_ASSERT_EQUAL(
false, buff2.
empty());
596 buff2.
read(readValue);
597 CPPUNIT_ASSERT_EQUAL(
true, buff2.
empty());
601 long int length3 = 10;
603 for (
int i = 0; i < length3 / 2; i++) {
604 buff3.
write(i + 456);
607 for (
int i = 0; i < length3; i++) {
609 int writeValue = i + 123;
610 buff3.
write(writeValue);
611 CPPUNIT_ASSERT_EQUAL(
false, buff3.
empty());
616 buff3.
read(readValue);
617 CPPUNIT_ASSERT_EQUAL(
false, buff3.
empty());
626 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
629 for(
int ic(0);ic<8;++ic)
635 CPPUNIT_ASSERT_EQUAL(buff.
get(), idata[3]);
636 CPPUNIT_ASSERT_EQUAL(buff.
readable(), (size_t)5);
639 CPPUNIT_ASSERT(buff.
empty());
640 CPPUNIT_ASSERT_EQUAL(buff.
get(), idata[0]);
642 CPPUNIT_ASSERT_EQUAL(buff.
get(), idata[9]);
643 CPPUNIT_ASSERT_EQUAL(buff.
readable(), (size_t)0);
652 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
654 for(
int ic(0);ic<10;++ic)
660 for(
int ic(0);ic<10;++ic)
662 CPPUNIT_ASSERT_EQUAL(idata[ic],*buff.
wptr(ic));
664 for(
int ic(0);ic<10;++ic)
666 CPPUNIT_ASSERT_EQUAL(idata[(-ic+10)%10],*buff.
wptr(-ic));
669 for(
int ic(0);ic<10;++ic)
671 CPPUNIT_ASSERT_EQUAL(idata[(5+ic)%10],*buff.
wptr(ic));
673 for(
int ic(0);ic<10;++ic)
675 CPPUNIT_ASSERT_EQUAL(idata[(5-ic+10)%10],*buff.
wptr(-ic));
685 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
692 for(
int ic(0);ic<10;++ic)
702 CPPUNIT_ASSERT_EQUAL(idata[3],*buff.
wptr());
703 CPPUNIT_ASSERT_EQUAL((
size_t)8,buff.
readable());
705 CPPUNIT_ASSERT_EQUAL(idata[8],*buff.
wptr());
706 CPPUNIT_ASSERT_EQUAL((
size_t)3,buff.
readable());
714 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
716 for(
int ic(0);ic<10;++ic)
722 for(
int ic(0);ic<10;++ic)
724 CPPUNIT_ASSERT_EQUAL(idata[ic],*buff.
rptr(ic));
726 for(
int ic(0);ic<10;++ic)
728 CPPUNIT_ASSERT_EQUAL(idata[(-ic+10)%10],*buff.
rptr(-ic));
732 for(
int ic(0);ic<10;++ic)
734 CPPUNIT_ASSERT_EQUAL(idata[(5+ic)%10],*buff.
rptr(ic));
736 for(
int ic(0);ic<10;++ic)
738 CPPUNIT_ASSERT_EQUAL(idata[(5-ic+10)%10],*buff.
rptr(-ic));
742 for(
int ic(0);ic<10;++ic)
744 CPPUNIT_ASSERT_EQUAL(idata[ic],buff.
get());
747 CPPUNIT_ASSERT_EQUAL(idata[ic],ret);
757 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
767 for(
int ic(0);ic<10;++ic)
774 CPPUNIT_ASSERT_EQUAL(idata[8],*buff.
rptr());
775 CPPUNIT_ASSERT_EQUAL((
size_t)8,buff.
writable());
777 CPPUNIT_ASSERT_EQUAL(idata[3],*buff.
rptr());
778 CPPUNIT_ASSERT_EQUAL((
size_t)3,buff.
writable());
795 prop[
"write.full_policy"] =
"overwrite";
796 prop[
"read.empty_policy"] =
"readback";
812 std::cout << std::flush;
831 prop[
"write.full_policy"] =
"overwrite";
832 prop[
"read.empty_policy"] =
"do_nothing";
848 std::cout << std::flush;
868 prop[
"write.full_policy"] =
"overwrite";
869 prop[
"read.empty_policy"] =
"block";
870 prop[
"read.timeout"] =
"3.0";
886 std::cout << std::flush;
905 prop[
"write.full_policy"] =
"do_nothing";
906 prop[
"read.empty_policy"] =
"readback";
922 std::cout << std::flush;
941 prop[
"write.full_policy"] =
"do_nothing";
942 prop[
"read.empty_policy"] =
"do_nothing";
958 std::cout << std::flush;
978 prop[
"write.full_policy"] =
"do_nothing";
979 prop[
"read.empty_policy"] =
"block";
980 prop[
"read.timeout"] =
"1.0";
996 std::cout << std::flush;
1016 prop[
"write.full_policy"] =
"block";
1017 prop[
"read.empty_policy"] =
"readback";
1018 prop[
"write.timeout"] =
"1.0";
1034 std::cout << std::flush;
1054 prop[
"write.full_policy"] =
"block";
1055 prop[
"read.empty_policy"] =
"do_nothing";
1056 prop[
"write.timeout"] =
"1.0";
1072 std::cout << std::flush;
1093 prop[
"write.full_policy"] =
"block";
1094 prop[
"read.empty_policy"] =
"block";
1095 prop[
"write.timeout"] =
"3.0";
1096 prop[
"read.timeout"] =
"3.0";
1114 std::cout << std::flush;
1135 prop[
"write.full_policy"] =
"block";
1136 prop[
"read.empty_policy"] =
"block";
1137 prop[
"write.timeout"] =
"3.0";
1138 prop[
"read.timeout"] =
"3.0";
1150 do_test(buff2,
true);
1156 std::cout << std::flush;
1171 int main(
int argc,
char* argv[])
1184 std::string arg(argv[i]);
1185 std::string next_arg;
1186 if (i + 1 < argc) next_arg = argv[i + 1];
1189 if (arg ==
"--text") { format =
TEXT_OUT;
break; }
1202 ofs.open(fname.c_str());
1204 if ( arg ==
"--compiler" ) { format =
COMPILER_OUT;
break; }
1205 if ( arg ==
"--cerr" ) { target = 1;
break; }
1206 if ( arg ==
"--xsl" )
1208 if (next_arg ==
"") xsl =
"default.xsl";
1209 else xsl = next_arg;
1211 if ( arg ==
"--namespace" )
1215 std::cerr <<
"no namespace specified" << std::endl;
1225 CppUnit::TextUi::TestRunner runner;
1227 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
1229 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
1230 CppUnit::Outputter* outputter = 0;
1231 std::ostream* stream = target ? &std::cerr : &std::cout;
1235 outputter =
new CppUnit::TextOutputter(&runner.result(),*stream);
1238 std::cout <<
"XML_OUT" << std::endl;
1239 outputter =
new CppUnit::XmlOutputter(&runner.result(),
1241 static_cast<CppUnit::XmlOutputter*
>(outputter)->setStyleSheet(xsl);
1244 outputter =
new CppUnit::CompilerOutputter(&runner.result(),*stream);
1247 runner.setOutputter(outputter);
1252 #endif // RingBuffer_cpp
void test_block_block_wr()
void test_advanceWptr()
advanceWptr()メソッドのテスト
int main(int argc, char **argv)
void test_rptr_get()
rptr(),get()メソッドのテスト
virtual DataType * rptr(long int n=0)
Get the buffer length.
void test_advanceRptr()
advanceRptr()メソッドのテスト
CPPUNIT_TEST_SUITE_REGISTRATION(RingBuffer::RingBufferTests)
virtual ReturnCode read(DataType &value, long int sec=-1, long int nsec=0)
Readout data from the buffer.
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
RingBufferTests()
Constructor.
void test_block_dnothing()
void test_dnothing_block()
void test_isFull()
isFull()メソッドのテスト
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
virtual void tearDown()
Test finalization.
static const char * toString(Enum status)
Convert BufferStatus into the string.
RTC::RingBuffer< int > & m_buff
virtual void init(const coil::Properties &prop)
Set the buffer.
virtual DataType * wptr(long int n=0)
Get the buffer length.
Enum
DataPortStatus return codes.
void test_dnothing_dnothing()
virtual ReturnCode reset()
Get the buffer length.
thread_arg(RTC::RingBuffer< int > &buff, int loop=NLOOP)
virtual bool full(void) const
Check on whether the buffer is full.
void do_test(RTC::RingBuffer< int > &buff, int loop=NLOOP, bool read_first=false)
virtual ReturnCode advanceWptr(long int n=1)
Get the buffer length.
void test_dnothing_rback()
void test_length()
length()メソッドのテスト
virtual ReturnCode write(const DataType &value, long int sec=-1, long int nsec=0)
Write data into the buffer.
void test_owrite_dnothing()
::RTC::BufferStatus::Enum ReturnCode
prop
Organization::get_organization_property ();.
virtual size_t readable() const
Write data into the buffer.
virtual ReturnCode advanceRptr(long int n=1)
Get the buffer length.
void test_write_read_with_small_length()
write()メソッドおよびread()メソッドのテスト(バッファ長2の場合)
void test_init()
init()メソッドのテスト
Class represents a set of properties.
void test_reset()
reset()メソッドのテスト
virtual ReturnCode get(DataType &value)
Write data into the buffer.
~RingBufferTests()
Destructor.
virtual void setUp()
Test initialization.
virtual ReturnCode put(const DataType &value)
Write data into the buffer.
virtual size_t length(void) const
Get the buffer length.
void test_isEmpty()
isEmpty()メソッドのテスト
void test_wptr_put()
wptr(),put()メソッドのテスト
virtual bool empty(void) const
Check on whether the buffer is empty.
BufferBase abstract class.
void test_isNew()
isNew()メソッドのテスト
virtual size_t writable() const
Write data into the buffer.
void test_block_block_rw()
void test_write_read()
write()メソッドおよびread()メソッドのテスト