mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2025-01-08 20:46:59 +03:00
770df2a92d
time I copied to Moses was getting annoying. git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@3587 1f5c12ca-751b-0410-a591-d2e778427230
76 lines
2.1 KiB
C++
76 lines
2.1 KiB
C++
#include "util/key_value_packing.hh"
|
|
|
|
#include <boost/random/mersenne_twister.hpp>
|
|
#include <boost/random/uniform_int.hpp>
|
|
#include <boost/random/variate_generator.hpp>
|
|
#include <boost/scoped_array.hpp>
|
|
#define BOOST_TEST_MODULE KeyValueStoreTest
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <limits>
|
|
#include <stdlib.h>
|
|
|
|
namespace util {
|
|
namespace {
|
|
|
|
BOOST_AUTO_TEST_CASE(basic_in_out) {
|
|
typedef ByteAlignedPacking<uint64_t, unsigned char> Packing;
|
|
void *backing = malloc(Packing::kBytes * 2);
|
|
Packing::MutableIterator i(Packing::FromVoid(backing));
|
|
i->SetKey(10);
|
|
BOOST_CHECK_EQUAL(10, i->GetKey());
|
|
i->SetValue(3);
|
|
BOOST_CHECK_EQUAL(3, i->GetValue());
|
|
++i;
|
|
i->SetKey(5);
|
|
BOOST_CHECK_EQUAL(5, i->GetKey());
|
|
i->SetValue(42);
|
|
BOOST_CHECK_EQUAL(42, i->GetValue());
|
|
|
|
Packing::ConstIterator c(i);
|
|
BOOST_CHECK_EQUAL(5, c->GetKey());
|
|
--c;
|
|
BOOST_CHECK_EQUAL(10, c->GetKey());
|
|
BOOST_CHECK_EQUAL(42, i->GetValue());
|
|
|
|
BOOST_CHECK_EQUAL(5, i->GetKey());
|
|
free(backing);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(simple_sort) {
|
|
typedef ByteAlignedPacking<uint64_t, unsigned char> Packing;
|
|
char foo[Packing::kBytes * 4];
|
|
Packing::MutableIterator begin(Packing::FromVoid(foo));
|
|
Packing::MutableIterator i = begin;
|
|
i->SetKey(0); ++i;
|
|
i->SetKey(2); ++i;
|
|
i->SetKey(3); ++i;
|
|
i->SetKey(1); ++i;
|
|
std::sort(begin, i);
|
|
BOOST_CHECK_EQUAL(0, begin[0].GetKey());
|
|
BOOST_CHECK_EQUAL(1, begin[1].GetKey());
|
|
BOOST_CHECK_EQUAL(2, begin[2].GetKey());
|
|
BOOST_CHECK_EQUAL(3, begin[3].GetKey());
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(big_sort) {
|
|
typedef ByteAlignedPacking<uint64_t, unsigned char> Packing;
|
|
boost::scoped_array<char> memory(new char[Packing::kBytes * 1000]);
|
|
Packing::MutableIterator begin(Packing::FromVoid(memory.get()));
|
|
|
|
boost::mt19937 rng;
|
|
boost::uniform_int<uint64_t> range(0, std::numeric_limits<uint64_t>::max());
|
|
boost::variate_generator<boost::mt19937&, boost::uniform_int<uint64_t> > gen(rng, range);
|
|
|
|
for (size_t i = 0; i < 1000; ++i) {
|
|
(begin + i)->SetKey(gen());
|
|
}
|
|
std::sort(begin, begin + 1000);
|
|
for (size_t i = 0; i < 999; ++i) {
|
|
BOOST_CHECK(begin[i] < begin[i+1]);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace util
|