abstract out lpmwrapper, add test

This commit is contained in:
bert hubert 2022-08-29 16:17:42 +02:00
parent 2744d576a7
commit e7897a9832
3 changed files with 46 additions and 39 deletions

38
lpmwrapper.hh Normal file
View File

@ -0,0 +1,38 @@
extern "C" {
#include "ext/lpm.h"
}
class LPMWrapper
{
public:
LPMWrapper()
{
d_lpm = lpm_create();
}
~LPMWrapper()
{
lpm_destroy(d_lpm);
}
void insert(const std::string& str, void* val=(void*)1)
{
char addr[16];
size_t len=sizeof(addr);
unsigned preflen=0;
lpm_strtobin(str.c_str(), addr, &len, &preflen);
if(lpm_insert(d_lpm, addr, len, preflen, val) < 0)
throw std::runtime_error("Error inserting prefix");
}
void* lookup(const char*str)
{
char addr[16];
size_t len=sizeof(addr);
unsigned preflen=0;
lpm_strtobin(str, addr, &len, &preflen);
return lpm_lookup(d_lpm, addr, len);
}
private:
lpm_t* d_lpm;
};

View File

@ -6,46 +6,10 @@
#include <atomic> #include <atomic>
#include <pcaudiolib/audio.h> #include <pcaudiolib/audio.h>
#include "ext/toml.hpp" #include "ext/toml.hpp"
extern "C" { #include "lpmwrapper.hh"
#include "ext/lpm.h"
}
using namespace std; using namespace std;
class LPMWrapper
{
public:
LPMWrapper()
{
d_lpm = lpm_create();
}
~LPMWrapper()
{
lpm_destroy(d_lpm);
}
void insert(const std::string& str, void* val=(void*)1)
{
char addr[16];
size_t len=sizeof(addr);
unsigned preflen=0;
lpm_strtobin(str.c_str(), addr, &len, &preflen);
if(lpm_insert(d_lpm, addr, len, preflen, val) < 0)
throw std::runtime_error("Error inserting prefix");
}
void* lookup(const char*str)
{
char addr[16];
size_t len=sizeof(addr);
unsigned preflen=0;
lpm_strtobin(str, addr, &len, &preflen);
return lpm_lookup(d_lpm, addr, len);
}
private:
lpm_t* d_lpm;
};
struct TrackerConf struct TrackerConf
{ {

View File

@ -1,10 +1,15 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "ext/doctest.h" #include "ext/doctest.h"
#include "ext/lpm.h" #include "lpmwrapper.hh"
using namespace std; using namespace std;
TEST_CASE("basic test") { TEST_CASE("basic test") {
CHECK(1==1); LPMWrapper t;
void* ptr = (void*)1;
t.insert("127.0.0.1/32", ptr);
CHECK(t.lookup("127.0.0.1") == ptr);
CHECK(t.lookup("127.0.0.2") == 0);
} }