Commit 782a7e76 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

random generator tests; fixed stuck at minimum

parent ca141799
......@@ -42,8 +42,8 @@ RandomGenerator::RandomGenerator(mt19937 & gen)
#define INT_RANDOM_VALUE(TYPE) RANDOM_VALUE(TYPE, \
uniform_int_distribution< TYPE > range( \
numeric_limits< TYPE >::min(), \
numeric_limits< TYPE >::min()); \
return range(this->gen); )
numeric_limits< TYPE >::max()); \
return range(this->gen); )
INT_RANDOM_VALUE(google::protobuf::int32);
INT_RANDOM_VALUE(google::protobuf::int64);
......@@ -53,15 +53,26 @@ INT_RANDOM_VALUE(google::protobuf::uint64);
#undef INT_RANDOM_VALUE
RANDOM_VALUE(google::protobuf::string,
// TODO
return "string";
std::string random;
std::string chars(
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
"!@#$%^&*()"
"`~-_=+[{]}\\|;:'\",<.>/? ");
uniform_int_distribution<> index_dist(0, chars.size() - 1);
for(int i = 0; i < this->RandomValue<uint>(); i++)
random += chars[index_dist(gen)];
return random;
)
#define REAL_RANDOM_VALUE(TYPE) RANDOM_VALUE(TYPE, \
uniform_real_distribution<> range( \
numeric_limits< TYPE >::min(), \
numeric_limits< TYPE >::max()); \
return range(this->gen); )
return range(this->gen); )
REAL_RANDOM_VALUE(double);
REAL_RANDOM_VALUE(float);
......@@ -96,12 +107,12 @@ void RandomGenerator::GenerateField(
const Reflection * reflection,
const FieldDescriptor * fd)
{
// TODO Enums
if (fd->is_repeated()) {
// TODO If is repeated, generate a random number of random fields
for (uint k = 0; k < this->RandomValue<google::protobuf::uint32>(); k++) {
switch (fd->cpp_type()) {
#define HANDLE_TYPE(CPPTYPE, METHOD, TYPE) \
case FieldDescriptor::CPPTYPE_##CPPTYPE: \
#define HANDLE_TYPE(CPPTYPE, METHOD, TYPE) \
case FieldDescriptor::CPPTYPE_##CPPTYPE: \
reflection->Add##METHOD(message, fd, this->RandomValue<TYPE>()); \
break;
......@@ -115,13 +126,16 @@ void RandomGenerator::GenerateField(
HANDLE_TYPE(BOOL , Bool , bool );
#undef HANDLE_TYPE
case FieldDescriptor::CPPTYPE_MESSAGE:
this->FillRandomly(reflection->AddMessage(message, fd));
}
}
}
else {
switch (fd->cpp_type()) {
#define HANDLE_TYPE(CPPTYPE, METHOD, TYPE) \
case FieldDescriptor::CPPTYPE_##CPPTYPE: \
#define HANDLE_TYPE(CPPTYPE, METHOD, TYPE) \
case FieldDescriptor::CPPTYPE_##CPPTYPE: \
reflection->Set##METHOD(message, fd, this->RandomValue<TYPE>()); \
break;
......@@ -135,11 +149,12 @@ void RandomGenerator::GenerateField(
HANDLE_TYPE(BOOL , Bool , bool );
#undef HANDLE_TYPE
case FieldDescriptor::CPPTYPE_MESSAGE:
this->FillRandomly(reflection->MutableMessage(message, fd));
break;
}
}
// TODO Message
}
}
......
......@@ -7,8 +7,8 @@ add_definitions(-DBOOST_TEST_DYN_LINK) # This is for Boost Test to use the share
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
include_directories (${Boost_INCLUDE_DIRS})
# file(GLOB Tests RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.test.cpp")
set(Tests marshaller.test.cpp)
file(GLOB Tests RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.test.cpp")
# set(Tests marshaller.test.cpp)
foreach(Test ${Tests})
get_filename_component(Name ${Test} NAME_WE)
......
#define BOOST_TEST_MODULE Marshaller
#include <boost/test/included/unit_test.hpp>
#include <boost/log/expressions.hpp>
#include <profanedb/test/protobuf/schema/test.pb.h>
#include <profanedb/protobuf/storage.pb.h>
......@@ -26,6 +28,10 @@ using boost::random::mt19937;
struct Format
{
private:
mt19937 gen;
public:
std::shared_ptr<Loader> loader;
std::shared_ptr<ProtobufMarshaller> marshaller;
std::shared_ptr<RocksStorage> storage;
......@@ -33,7 +39,7 @@ struct Format
RandomGenerator randomGen;
Format()
: gen(rng)
: gen(std::time(0))
, randomGen(gen)
{
rocksdb::Options rocksOptions;
......@@ -56,14 +62,8 @@ struct Format
this->marshaller = std::make_shared<ProtobufMarshaller>(storage, loader);
}
private:
random_device rng;
mt19937 gen;
};
BOOST_FIXTURE_TEST_SUITE(marshal, Format)
// TODO Acutal test
......@@ -84,5 +84,6 @@ RANDOM_TEST(NonKeyableNested);
RANDOM_TEST(KeyableNested);
RANDOM_TEST(MessageAsKey);
#undef RANDOM_TEST
BOOST_AUTO_TEST_SUITE_END()
#undef DEBUG_MESSAGE
#define BOOST_TEST_MODULE RandomGenerator
#include <boost/test/included/unit_test.hpp>
#include <profanedb/util/randomgenerator.h>
using profanedb::util::RandomGenerator;
using boost::random::mt19937;
using google::protobuf::int32;
using google::protobuf::int64;
using google::protobuf::uint32;
using google::protobuf::uint64;
using google::protobuf::string;
struct Generator
{
private:
mt19937 gen;
public:
RandomGenerator randomGen;
Generator()
: gen(std::time(0))
, randomGen(gen)
{}
};
BOOST_FIXTURE_TEST_SUITE(randomgenerator, Generator)
#define RANDOM(TYPE) \
BOOST_AUTO_TEST_CASE(test_##TYPE) \
{ \
TYPE a = randomGen.RandomValue< TYPE >(); \
TYPE b = randomGen.RandomValue< TYPE >(); \
BOOST_TEST_MESSAGE( #TYPE ); \
BOOST_TEST_MESSAGE(a); \
BOOST_TEST_MESSAGE(b); \
BOOST_TEST(a != b); \
}
RANDOM(int32);
RANDOM(int64);
RANDOM(uint32);
RANDOM(uint64);
RANDOM(string);
RANDOM(double);
RANDOM(float);
RANDOM(bool);
#undef RANDOM
BOOST_AUTO_TEST_SUITE_END()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment