Przeglądaj źródła

better any/wany fix, tests

mikemirzayanov 7 miesięcy temu
rodzic
commit
60c9c2f0f9

+ 3 - 6
testlib.h

@@ -173,6 +173,7 @@ const char *latestFeatures[] = {
 #include <map>
 #include <set>
 #include <cmath>
+#include <iterator>
 #include <iostream>
 #include <sstream>
 #include <fstream>
@@ -901,7 +902,6 @@ public:
         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;
@@ -910,10 +910,9 @@ public:
     /* 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;
@@ -1105,7 +1104,6 @@ public:
         size_t size = 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;
@@ -1114,11 +1112,10 @@ public:
     /* 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;

+ 6 - 0
tests/test-003_run-rnd/refs/r1/stdout

@@ -51,3 +51,9 @@ vutwaahqooeqoxzxwetlpecqiwgdbogiqqulttysyohwhzxzphvsfmnplizxoebzcvvfyppqbhxjksuz
 1880
 1973
 1850
+a
+d
+c
+f
+d
+b

+ 6 - 0
tests/test-003_run-rnd/refs/r2/stdout

@@ -141,3 +141,9 @@ clizwkchataumicxkohcrpqnyrjyzbjvsypznpembvkkkbyzvzckcmhbjbuopfbwbkntswhwsdfzabjg
 1363
 1897
 1175
+c
+d
+b
+f
+d
+c

+ 15 - 0
tests/test-003_run-rnd/src/gen.cpp

@@ -127,4 +127,19 @@ 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;
+
+    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;
 }