1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-20 03:29:32 +02:00

Replaced assertions with exceptions for invalid input in RNG

This commit is contained in:
Ivan Savenko 2024-02-19 00:35:21 +02:00
parent 868b41c8ee
commit 10b35ceecb
2 changed files with 16 additions and 12 deletions

View File

@ -36,14 +36,18 @@ namespace RandomGeneratorUtil
template<typename Container> template<typename Container>
auto nextItem(const Container & container, vstd::RNG & rand) -> decltype(std::begin(container)) auto nextItem(const Container & container, vstd::RNG & rand) -> decltype(std::begin(container))
{ {
assert(!container.empty()); if(container.empty())
throw std::runtime_error("Unable to select random item from empty container!");
return std::next(container.begin(), rand.getInt64Range(0, container.size() - 1)()); return std::next(container.begin(), rand.getInt64Range(0, container.size() - 1)());
} }
template<typename Container> template<typename Container>
auto nextItem(Container & container, vstd::RNG & rand) -> decltype(std::begin(container)) auto nextItem(Container & container, vstd::RNG & rand) -> decltype(std::begin(container))
{ {
assert(!container.empty()); if(container.empty())
throw std::runtime_error("Unable to select random item from empty container!");
return std::next(container.begin(), rand.getInt64Range(0, container.size() - 1)()); return std::next(container.begin(), rand.getInt64Range(0, container.size() - 1)());
} }

View File

@ -37,25 +37,25 @@ void CRandomGenerator::resetSeed()
TRandI CRandomGenerator::getIntRange(int lower, int upper) TRandI CRandomGenerator::getIntRange(int lower, int upper)
{ {
assert(lower <= upper); if (lower <= upper)
return std::bind(TIntDist(lower, upper), std::ref(rand)); return std::bind(TIntDist(lower, upper), std::ref(rand));
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
} }
vstd::TRandI64 CRandomGenerator::getInt64Range(int64_t lower, int64_t upper) vstd::TRandI64 CRandomGenerator::getInt64Range(int64_t lower, int64_t upper)
{ {
assert(lower <= upper); if(lower <= upper)
return std::bind(TInt64Dist(lower, upper), std::ref(rand)); return std::bind(TInt64Dist(lower, upper), std::ref(rand));
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
} }
int CRandomGenerator::nextInt(int upper) int CRandomGenerator::nextInt(int upper)
{ {
assert(0 <= upper);
return getIntRange(0, upper)(); return getIntRange(0, upper)();
} }
int CRandomGenerator::nextInt(int lower, int upper) int CRandomGenerator::nextInt(int lower, int upper)
{ {
assert(lower <= upper);
return getIntRange(lower, upper)(); return getIntRange(lower, upper)();
} }
@ -66,19 +66,19 @@ int CRandomGenerator::nextInt()
vstd::TRand CRandomGenerator::getDoubleRange(double lower, double upper) vstd::TRand CRandomGenerator::getDoubleRange(double lower, double upper)
{ {
assert(lower <= upper); if(lower <= upper)
return std::bind(TRealDist(lower, upper), std::ref(rand)); return std::bind(TRealDist(lower, upper), std::ref(rand));
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
} }
double CRandomGenerator::nextDouble(double upper) double CRandomGenerator::nextDouble(double upper)
{ {
assert(0 <= upper);
return getDoubleRange(0, upper)(); return getDoubleRange(0, upper)();
} }
double CRandomGenerator::nextDouble(double lower, double upper) double CRandomGenerator::nextDouble(double lower, double upper)
{ {
assert(lower <= upper);
return getDoubleRange(lower, upper)(); return getDoubleRange(lower, upper)();
} }