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> 62 static const int NLOOP = 0x00000fff;
82 for (
int i = 0; i < t_arg->
m_n_loop; i++) {
84 ret = (*buff).read(output);
86 std::cout <<
"read() : " << output << std::endl;
94 std::cout <<
"ERROR!!: output=" << output;
95 std::cout <<
", correct data=" << i;
96 std::cout <<
", return code=" << ret << std::endl;
111 for (
int i = 0; i < t_arg->
m_n_loop; i++) {
113 (*buff).
write(input);
115 std::cout <<
"write(" << input <<
")" << std::endl;
129 :
public CppUnit::TestFixture
133 CPPUNIT_TEST(test_length);
134 CPPUNIT_TEST(test_isFull);
135 CPPUNIT_TEST(test_isEmpty);
136 CPPUNIT_TEST(test_init);
137 CPPUNIT_TEST(test_write_read);
138 CPPUNIT_TEST(test_write_read_with_small_length);
139 CPPUNIT_TEST(test_isNew);
140 CPPUNIT_TEST(test_reset);
141 CPPUNIT_TEST(test_wptr_put);
142 CPPUNIT_TEST(test_advanceWptr);
143 CPPUNIT_TEST(test_rptr_get);
144 CPPUNIT_TEST(test_advanceRptr);
146 CPPUNIT_TEST(test_owrite_rback);
147 CPPUNIT_TEST(test_owrite_dnothing);
148 CPPUNIT_TEST(test_owrite_block);
150 CPPUNIT_TEST(test_dnothing_rback);
151 CPPUNIT_TEST(test_dnothing_dnothing);
152 CPPUNIT_TEST(test_dnothing_block);
153 CPPUNIT_TEST(test_block_rback);
154 CPPUNIT_TEST(test_block_dnothing);
155 CPPUNIT_TEST(test_block_block_wr);
157 CPPUNIT_TEST(test_block_block_rw);
159 CPPUNIT_TEST_SUITE_END();
169 if (pthread_create(&tr, NULL ,
reader, static_cast<void *>(&buff)) != 0) {
170 perror(
"pthread_create(r)");
174 if (pthread_create(&tw, NULL ,
writer, static_cast<void *>(&buff)) != 0) {
175 perror(
"pthread_create(w)");
182 if (pthread_create(&tw, NULL ,
writer, &arg) != 0) {
183 perror(
"pthread_create(w)");
188 if (pthread_create(&tr, NULL ,
reader, &arg) != 0) {
189 perror(
"pthread_create(r)");
195 pthread_join(tw, NULL);
196 pthread_join(tr, NULL);
236 CPPUNIT_ASSERT(buff.
length() == 123);
240 CPPUNIT_ASSERT(buff.
length() == len);
254 long int length = 10;
258 int initialValue = 12345;
259 for (
unsigned int i(0); i < buff.
length(); ++i)
261 buff.
write(initialValue);
263 CPPUNIT_ASSERT_EQUAL(
false, buff.
empty());
269 CPPUNIT_ASSERT_EQUAL(
false, buff.
empty());
273 int writeValue = 98765;
275 CPPUNIT_ASSERT_EQUAL(
false, buff.
empty());
291 CPPUNIT_ASSERT_EQUAL(
false, buff1.
full());
297 for (
int i = 0; i < length2; i++) {
300 CPPUNIT_ASSERT_EQUAL(
true, buff2.
full());
306 for (
int i = 0; i < length3 / 2; i++) {
309 CPPUNIT_ASSERT_EQUAL(
false, buff3.
full());
320 long int length = 10;
324 for (
unsigned int i(0); i < buff.
length(); ++i)
330 int expected = 12345;
331 for (
long int i = 0; i < length; i++) {
334 CPPUNIT_ASSERT_EQUAL(expected, actual);
352 long int length1 = 3;
355 prop[
"write.full_policy"] =
"block";
356 prop[
"write.timeout"] =
"5.0";
357 prop[
"read.empty_policy"] =
"block";
358 prop[
"read.timeout"] =
"5.0";
362 for (
int writeValue = 0; writeValue < 100; writeValue++) {
366 std::cout <<
"### FULL ###" << std::endl;
369 if (writeValue % 5 == 0)
371 while (!buff1.
empty())
373 std::cout <<
"read timeout: " << 5 << std::endl;
374 buff1.
read(readValue, 5);
375 std::cout <<
"read: " << readValue << std::endl;
377 std::cout <<
"read timeout: " << 5 << std::endl;
378 std::cout <<
"waiting 5 sec" << std::endl;
380 std::cout <<
"read: " << readValue << std::endl;
384 buff1.
read(readValue);
385 std::cout <<
"read: " << readValue << std::endl;
390 std::cout <<
"??? still full" << std::endl;
394 std::cout <<
"buffer full was blown over." << std::endl;
399 std::cout <<
"### EMPTY ###" << std::endl;
410 buff1.
get(readValue);
412 std::cout << writeValue <<
" == " << readValue << std::endl;
423 long int length2 = 10;
425 for (
int i = 0; i < length2; i++) {
426 buff2.
write(i + 123);
430 for (
int writeValue = 0; writeValue < 100; writeValue++) {
432 buff2.
write(writeValue);
436 buff2.
read(readValue);
439 CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
444 long int length3 = 10;
446 for (
int i = 0; i < length3 / 2; i++) {
447 buff3.
write(i + 123);
451 for (
int writeValue = 0; writeValue < 100; writeValue++) {
453 buff3.
write(writeValue);
457 buff3.
read(readValue);
460 CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
478 long int length1 = 2;
482 for (
int writeValue = 0; writeValue < 100; writeValue++) {
484 buff1.
write(writeValue);
488 buff1.
read(readValue);
491 CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
497 long int length2 = 2;
500 prop[
"write.full_policy"] =
"overwrite";
503 for (
int i = 0; i < length2; i++) {
504 buff2.
write(i + 123);
524 CPPUNIT_ASSERT_EQUAL(
true, buff2.
full());
528 buff2.
read(readValue);
530 CPPUNIT_ASSERT_EQUAL(
false, buff2.
full());
532 CPPUNIT_ASSERT_EQUAL(1+123, readValue);
535 long int length3 = 2;
538 prop3[
"write.full_policy"] =
"overwrite";
541 for (
int i = 0; i < 1; i++) {
542 buff3.
write(i + 123);
550 buff3.
read(readValue);
553 CPPUNIT_ASSERT_EQUAL(123, readValue);
556 for (
int writeValue = 0; writeValue < 100; writeValue++) {
558 buff3.
write(writeValue);
562 buff3.
read(readValue);
565 CPPUNIT_ASSERT_EQUAL(writeValue-1, readValue);
585 long int length1 = 10;
587 CPPUNIT_ASSERT_EQUAL(
true, buff1.
empty());
590 long int length2 = 10;
593 for (
int i = 0; i < length2; i++) {
595 int writeValue = i + 123;
596 buff2.
write(writeValue);
597 CPPUNIT_ASSERT_EQUAL(
false, buff2.
empty());
602 buff2.
read(readValue);
603 CPPUNIT_ASSERT_EQUAL(
true, buff2.
empty());
607 long int length3 = 10;
609 for (
int i = 0; i < length3 / 2; i++) {
610 buff3.
write(i + 456);
613 for (
int i = 0; i < length3; i++) {
615 int writeValue = i + 123;
616 buff3.
write(writeValue);
617 CPPUNIT_ASSERT_EQUAL(
false, buff3.
empty());
622 buff3.
read(readValue);
623 CPPUNIT_ASSERT_EQUAL(
false, buff3.
empty());
632 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
635 for(
int ic(0);ic<8;++ic)
641 CPPUNIT_ASSERT_EQUAL(buff.
get(), idata[3]);
642 CPPUNIT_ASSERT_EQUAL(buff.
readable(), (size_t)5);
645 CPPUNIT_ASSERT(buff.
empty());
646 CPPUNIT_ASSERT_EQUAL(buff.
get(), idata[0]);
648 CPPUNIT_ASSERT_EQUAL(buff.
get(), idata[9]);
649 CPPUNIT_ASSERT_EQUAL(buff.
readable(), (size_t)0);
658 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
660 for(
int ic(0);ic<10;++ic)
666 for(
int ic(0);ic<10;++ic)
668 CPPUNIT_ASSERT_EQUAL(idata[ic],*buff.
wptr(ic));
670 for(
int ic(0);ic<10;++ic)
672 CPPUNIT_ASSERT_EQUAL(idata[(-ic+10)%10],*buff.
wptr(-ic));
675 for(
int ic(0);ic<10;++ic)
677 CPPUNIT_ASSERT_EQUAL(idata[(5+ic)%10],*buff.
wptr(ic));
679 for(
int ic(0);ic<10;++ic)
681 CPPUNIT_ASSERT_EQUAL(idata[(5-ic+10)%10],*buff.
wptr(-ic));
691 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
698 for(
int ic(0);ic<10;++ic)
708 CPPUNIT_ASSERT_EQUAL(idata[3],*buff.
wptr());
709 CPPUNIT_ASSERT_EQUAL((
size_t)8,buff.
readable());
711 CPPUNIT_ASSERT_EQUAL(idata[8],*buff.
wptr());
712 CPPUNIT_ASSERT_EQUAL((
size_t)3,buff.
readable());
720 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
722 for(
int ic(0);ic<10;++ic)
728 for(
int ic(0);ic<10;++ic)
730 CPPUNIT_ASSERT_EQUAL(idata[ic],*buff.
rptr(ic));
732 for(
int ic(0);ic<10;++ic)
734 CPPUNIT_ASSERT_EQUAL(idata[(-ic+10)%10],*buff.
rptr(-ic));
738 for(
int ic(0);ic<10;++ic)
740 CPPUNIT_ASSERT_EQUAL(idata[(5+ic)%10],*buff.
rptr(ic));
742 for(
int ic(0);ic<10;++ic)
744 CPPUNIT_ASSERT_EQUAL(idata[(5-ic+10)%10],*buff.
rptr(-ic));
748 for(
int ic(0);ic<10;++ic)
750 CPPUNIT_ASSERT_EQUAL(idata[ic],buff.
get());
753 CPPUNIT_ASSERT_EQUAL(idata[ic],ret);
763 int idata[10] = {123,456,789,321,654,987,1234,3456,5678,7890};
773 for(
int ic(0);ic<10;++ic)
780 CPPUNIT_ASSERT_EQUAL(idata[8],*buff.
rptr());
781 CPPUNIT_ASSERT_EQUAL((
size_t)8,buff.
writable());
783 CPPUNIT_ASSERT_EQUAL(idata[3],*buff.
rptr());
784 CPPUNIT_ASSERT_EQUAL((
size_t)3,buff.
writable());
801 prop[
"write.full_policy"] =
"overwrite";
802 prop[
"read.empty_policy"] =
"readback";
818 std::cout << std::flush;
837 prop[
"write.full_policy"] =
"overwrite";
838 prop[
"read.empty_policy"] =
"do_nothing";
854 std::cout << std::flush;
874 prop[
"write.full_policy"] =
"overwrite";
875 prop[
"read.empty_policy"] =
"block";
876 prop[
"read.timeout"] =
"3.0";
892 std::cout << std::flush;
911 prop[
"write.full_policy"] =
"do_nothing";
912 prop[
"read.empty_policy"] =
"readback";
928 std::cout << std::flush;
947 prop[
"write.full_policy"] =
"do_nothing";
948 prop[
"read.empty_policy"] =
"do_nothing";
964 std::cout << std::flush;
984 prop[
"write.full_policy"] =
"do_nothing";
985 prop[
"read.empty_policy"] =
"block";
986 prop[
"read.timeout"] =
"1.0";
1002 std::cout << std::flush;
1022 prop[
"write.full_policy"] =
"block";
1023 prop[
"read.empty_policy"] =
"readback";
1024 prop[
"write.timeout"] =
"1.0";
1040 std::cout << std::flush;
1060 prop[
"write.full_policy"] =
"block";
1061 prop[
"read.empty_policy"] =
"do_nothing";
1062 prop[
"write.timeout"] =
"1.0";
1078 std::cout << std::flush;
1099 prop[
"write.full_policy"] =
"block";
1100 prop[
"read.empty_policy"] =
"block";
1101 prop[
"write.timeout"] =
"3.0";
1102 prop[
"read.timeout"] =
"3.0";
1120 std::cout << std::flush;
1141 prop[
"write.full_policy"] =
"block";
1142 prop[
"read.empty_policy"] =
"block";
1143 prop[
"write.timeout"] =
"3.0";
1144 prop[
"read.timeout"] =
"3.0";
1156 do_test(buff2,
true);
1162 std::cout << std::flush;
1177 int main(
int argc,
char* argv[])
1190 std::string arg(argv[i]);
1191 std::string next_arg;
1192 if (i + 1 < argc) next_arg = argv[i + 1];
1195 if (arg ==
"--text") { format =
TEXT_OUT;
break; }
1208 ofs.open(fname.c_str());
1210 if ( arg ==
"--compiler" ) { format =
COMPILER_OUT;
break; }
1211 if ( arg ==
"--cerr" ) { target = 1;
break; }
1212 if ( arg ==
"--xsl" )
1214 if (next_arg ==
"") xsl =
"default.xsl";
1215 else xsl = next_arg;
1217 if ( arg ==
"--namespace" )
1221 std::cerr <<
"no namespace specified" << std::endl;
1231 CppUnit::TextUi::TestRunner runner;
1233 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
1235 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
1236 CppUnit::Outputter* outputter = 0;
1237 std::ostream* stream = target ? &std::cerr : &std::cout;
1241 outputter =
new CppUnit::TextOutputter(&runner.result(),*stream);
1244 std::cout <<
"XML_OUT" << std::endl;
1245 outputter =
new CppUnit::XmlOutputter(&runner.result(),
1247 static_cast<CppUnit::XmlOutputter*
>(outputter)->setStyleSheet(xsl);
1250 outputter =
new CppUnit::CompilerOutputter(&runner.result(),*stream);
1253 runner.setOutputter(outputter);
1258 #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()
virtual bool full(void) const
Check on whether the buffer is full.
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)
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.
virtual bool empty(void) const
Check on whether the buffer is empty.
virtual size_t writable() const
Write data into the buffer.
void test_dnothing_rback()
virtual size_t readable() const
Write data into the buffer.
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 ReturnCode advanceRptr(long int n=1)
Get the buffer length.
void test_write_read_with_small_length()
write()��åɤ����read()��åɤΥƥ��ȡʥХåե�Ĺ���ξ���
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.
void test_isEmpty()
isEmpty()��åɤΥƥ���
void test_wptr_put()
wptr(),put()��åɤΥƥ���
BufferBase abstract class.
void test_isNew()
isNew()��åɤΥƥ���
virtual size_t length(void) const
Get the buffer length.
void test_block_block_rw()
void test_write_read()
write()��åɤ����read()��åɤΥƥ���