Skip to content

Commit

Permalink
Merge pull request #188 from MikeMirzayanov/HeRaNO-fix-171-wany
Browse files Browse the repository at this point in the history
rnd.any/wany use distance/advance instead of -/+
  • Loading branch information
MikeMirzayanov authored Sep 29, 2023
2 parents c6c2dbd + 7cf5a1e commit 5dab275
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
15 changes: 9 additions & 6 deletions testlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ const char *latestFeatures[] = {
#include <map>
#include <set>
#include <cmath>
#include <iterator>
#include <iostream>
#include <sstream>
#include <fstream>
Expand Down Expand Up @@ -901,7 +902,6 @@ class random_t {
int size = int(c.size());
if (size <= 0)
__testlib_fail("random_t::any(const Container& c): c.size() must be positive");
//return *(c.begin() + next(size));
typename Container::const_iterator it = c.begin();
std::advance(it, next(size));
return *it;
Expand All @@ -910,10 +910,9 @@ class random_t {
/* Returns random element from iterator range. */
template<typename Iter>
typename Iter::value_type any(const Iter &begin, const Iter &end) {
int size = int(end - begin);
int size = static_cast<int>(std::distance(begin, end));
if (size <= 0)
__testlib_fail("random_t::any(const Iter& begin, const Iter& end): range must have positive length");
// return *(begin + next(size));
Iter it = begin;
std::advance(it, next(size));
return *it;
Expand Down Expand Up @@ -1105,17 +1104,21 @@ class random_t {
int size = int(c.size());
if (size <= 0)
__testlib_fail("random_t::wany(const Container& c, int type): c.size() must be positive");
return *(c.begin() + wnext(size, type));
typename Container::const_iterator it = c.begin();
std::advance(it, wnext(size, type));
return *it;
}

/* Returns weighted random element from iterator range. */
template<typename Iter>
typename Iter::value_type wany(const Iter &begin, const Iter &end, int type) {
int size = int(end - begin);
int size = static_cast<int>(std::distance(begin, end));
if (size <= 0)
__testlib_fail(
"random_t::any(const Iter& begin, const Iter& end, int type): range must have positive length");
return *(begin + wnext(size, type));
Iter it = begin;
std::advance(it, wnext(size, type));
return *it;
}

/* Returns random permutation of the given size (values are between `first` and `first`+size-1)*/
Expand Down
12 changes: 12 additions & 0 deletions tests/test-003_run-rnd/refs/r1/stdout
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ vutwaahqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuz
2116
441
e
a
c
f
e
d
b
f
c
f
f
b
b
12 changes: 12 additions & 0 deletions tests/test-003_run-rnd/refs/r2/stdout
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ clizwkchataumicxkohcrpqnyrjyzbjvsypznpembvkkkbyzvzckcmhbjbuopfbwbkntswhwsdfzabjg
3249
3481
e
d
c
f
d
a
a
e
e
f
f
d
c
38 changes: 36 additions & 2 deletions tests/test-003_run-rnd/src/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,45 @@ int main(int argc, char* argv[])
std::cout << rnd.wnext((signed long long) 42, (signed long long) 2011, 4) << std::endl;
std::cout << rnd.wnext((signed int) 42, (signed int) 2011, 4) << std::endl;
std::cout << rnd.wnext((signed short) 42, (signed short) 2011, 4) << std::endl;

println(rnd.wany(a, 1));
println(rnd.wany(a, -1));

set<string> b;
b.insert("a"); b.insert("b"); b.insert("c"); b.insert("d"); b.insert("e"); b.insert("f"); b.insert("g");
println(rnd.any(b));

{
std::set<std::string> string_set;
string_set.insert("a");
string_set.insert("b");
string_set.insert("c");
string_set.insert("d");
string_set.insert("e");
string_set.insert("f");

std::cout << rnd.any(string_set) << std::endl;
std::cout << rnd.any(string_set.begin(), string_set.end()) << std::endl;
std::cout << rnd.wany(string_set, 1) << std::endl;
std::cout << rnd.wany(string_set.begin(), string_set.end(), 1) << std::endl;
std::cout << rnd.wany(string_set, -1) << std::endl;
std::cout << rnd.wany(string_set.begin(), string_set.end(), -1) << std::endl;
}

{
std::multiset<std::string> string_multiset;
string_multiset.insert("a");
string_multiset.insert("b"); string_multiset.insert("b");
string_multiset.insert("c"); string_multiset.insert("c"); string_multiset.insert("c");
string_multiset.insert("d");
string_multiset.insert("e"); string_multiset.insert("e"); string_multiset.insert("e");
string_multiset.insert("f"); string_multiset.insert("f"); string_multiset.insert("f"); string_multiset.insert("f");

std::cout << rnd.any(string_multiset) << std::endl;
std::cout << rnd.any(string_multiset.begin(), string_multiset.end()) << std::endl;
std::cout << rnd.wany(string_multiset, 2) << std::endl;
std::cout << rnd.wany(string_multiset.begin(), string_multiset.end(), 1) << std::endl;
std::cout << rnd.wany(string_multiset, -1) << std::endl;
std::cout << rnd.wany(string_multiset.begin(), string_multiset.end(), -2) << std::endl;
}
}

0 comments on commit 5dab275

Please sign in to comment.