Browse Source

add test program for the library.

sync
Roker 4 years ago
parent
commit
987e5c21be
2 changed files with 121 additions and 1 deletions
  1. +4
    -1
      Makefile
  2. +117
    -0
      test_library.cc

+ 4
- 1
Makefile View File

@ -27,7 +27,7 @@ clean:
distclean: clean
rm -Rf .gnupg .pEp_management*
test: test_adapter
test: test_adapter test_library
ifeq ($(HOME),$(PREFIX))
-ln -fs $$HOME/lib
endif
@ -36,6 +36,9 @@ endif
test_adapter: test_adapter.o $(TARGET)
$(CXX) -o $@ -L$(PEP)/lib -lpEpEngine -L. -lpEpAdapter $<
test_library: test_library.o $(TARGET)
$(CXX) -o $@ -L$(PEP)/lib -lpEpEngine -L. -lpEpAdapter $<
install: $(TARGET)
-mkdir -p $(PEP)/include
cp $(HEADERS) $(PEP)/include


+ 117
- 0
test_library.cc View File

@ -0,0 +1,117 @@
// small unittest program for libpEpAdapter
// TODO: use Gtest or the like for more modular unit tests!
#include "pc_container.hh"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <thread>
using namespace pEp;
// Producer's data:
class P
{
public:
P(int i)
: data{new char[64]}
{
snprintf(data, 63, "%i", i);
}
P(const P&) = delete;
void operator=(const P&) = delete;
~P()
{
std::cerr << "~P(): data=" << (data ? data : "(NULL)" ) << '\n';
delete[] data;
}
char* data;
};
// Consumer's data:
class C
{
public:
C(int _i) : i(_i) {}
int i;
};
pc_container<P,C> pc;
void consumer_thread()
{
bool keep_running = true;
int sum = 0;
while(keep_running)
{
auto q = pc.get_changed(); // might block, that is okay.
switch(q.state())
{
case PC_State::Created:
{
const int value = atoi( q.pdata->data );
if(value<0)
{
std::cerr << "Q\n";
keep_running = false;
}else{
std::cerr << "C";
q.cdata = new C( value );
sum += q.cdata->i;
}
break;
}
case PC_State::Deleted:
{
std::cerr << "D";
sum -= q.cdata->i;
delete q.cdata;
break;
}
case PC_State::Changed:
{
std::cerr << "X";
sum -= q.cdata->i;
delete q.cdata;
q.cdata = new C( atoi( q.pdata->data ) );
sum += q.cdata->i;
break;
}
default: throw "Illegal state";
}
}
std::cout << "Consumer sum: " << sum << ".\n";
}
int main()
{
for(int i=0; i<10; ++i)
{
pc.insert( new P(i) );
}
std::thread t{ &consumer_thread };
for(int i=10; i<100; ++i)
{
pc.insert( new P(i) );
}
while( !pc.empty() )
{
auto q = pc.begin();
delete q->pdata;
pc.erase( q );
}
pc.insert( new P(-1) );
t.join();
}

Loading…
Cancel
Save