git.fiddlerwoaroof.com
generator_test.cc
7d075ab4
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
  * terms of the  GNU General Public License  version 2  or
  * later.
  *
  * You should have received a copy of the license with this
  * file. If not, you will find a copy in the "LICENSE" file
  * at https://github.com/cjdev/dual-control.
  */
46cb80e7
 #include <memory>
55563eab
 #include <algorithm>
46cb80e7
 #include <initializer_list>
 #include <vector>
cff139be
 #include <climits>
 
55563eab
 #include "generator.h"
46cb80e7
 #include "sys_stdlib.h"
55563eab
 #include "test_util.h"
 
23c0bd4d
 class fake_stdlib : public stdlib_ifc
 {
 
 private:
     std::vector<int> samples_;
     mutable std::vector<int>::iterator current_;
 public:
     fake_stdlib ( const std::initializer_list<int> &samples)
         : samples_ (samples.begin(), samples.end()),
           current_ (samples_.begin()) {}
     int rand() const override
     {
         if (current_ != samples_.end()) {
             auto rval = *current_;
             current_ += 1;
             return rval;
         }
 
         return 0;
46cb80e7
     }
 };
 
7d075ab4
 int six_digits()
 {
     // given
cff139be
     std::initializer_list<int> samples { 1 };
23c0bd4d
     auto test_stdlib = std::make_shared<fake_stdlib> (samples);
     stdlib stdlib (test_stdlib);
     generator generator = make_generator (stdlib);
55563eab
 
7d075ab4
     // when
     auto actual = generator();
55563eab
 
7d075ab4
     // then
     check (actual.size() == 6, "size is wrong");
     check (std::all_of (actual.begin(), actual.end(), [] (char c) {
         return c >= '0' && c <= '9';
     }), "not just digits");
     succeed();
55563eab
 }
 
23c0bd4d
 int modulated_source_modulates_tokens()
 {
46cb80e7
     // given
     std::initializer_list<int> samples { 1, 2, 3 };
23c0bd4d
     auto test_stdlib = std::make_shared<fake_stdlib> (samples);
     stdlib stdlib (test_stdlib);
     generator generator = make_generator (stdlib);
46cb80e7
 
     // when
     auto actual1 = generator();
     auto actual2 = generator();
 
     // then
     check (actual1 != actual2, "samples should be different");
     succeed();
 }
 
cff139be
 int int_max()
 {
     // given
     std::initializer_list<int> samples { INT_MAX };
23c0bd4d
     auto test_stdlib = std::make_shared<fake_stdlib> (samples);
     stdlib stdlib (test_stdlib);
     generator generator = make_generator (stdlib);
cff139be
 
     // when
     auto actual = generator();
 
     // then
     check (actual.size() == 6, "size is wrong");
     check (std::all_of (actual.begin(), actual.end(), [] (char c) {
         return c >= '0' && c <= '9';
     }), "not just digits");
     succeed();
 }
 
 int int_min()
 {
     // given
     std::initializer_list<int> samples { INT_MIN };
23c0bd4d
     auto test_stdlib = std::make_shared<fake_stdlib> (samples);
     stdlib stdlib (test_stdlib);
     generator generator = make_generator (stdlib);
cff139be
 
     // when
     auto actual = generator();
 
     // then
     check (actual.size() == 6, "size is wrong");
     check (std::all_of (actual.begin(), actual.end(), [] (char c) {
         return c >= '0' && c <= '9';
     }), "not just digits");
     succeed();
 }
 
7d075ab4
 int run_tests()
 {
     test (six_digits);
46cb80e7
     test (modulated_source_modulates_tokens);
cff139be
     test (int_max);
     test (int_min);
55563eab
     succeed();
 }
 
7d075ab4
 int main (int argc, char *argv[])
 {
55563eab
     return !run_tests();
 }