git.fiddlerwoaroof.com
system_test.cc
23c0bd4d
 /* 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.
  */
 
2587e1ec
 #include <memory>
 #include <ctime>
 
 #include "test_util.h"
 #include "system.h"
 
 #include "sys_time.h"
 #include "sys_stdlib.h"
 
23c0bd4d
 class mock_stdlib : public stdlib_ifc
 {
 public:
2587e1ec
     mutable unsigned captured_seed {0};
23c0bd4d
     void srand (unsigned int seed) const
     {
2587e1ec
         captured_seed = seed;
     }
 };
 
23c0bd4d
 class fake_sys_time : public sys_time_ifc
 {
 private:
     unsigned value_;
 public:
     fake_sys_time (unsigned value) : value_ (value) {}
     time_t time (time_t *timer) const override
     {
         return static_cast<time_t> (value_);
     }
2587e1ec
 };
 
23c0bd4d
 int initializes_random ()
 {
2587e1ec
     // given
23c0bd4d
     unsigned seed (20392);
2587e1ec
     auto test_stdlib = std::make_shared<mock_stdlib>();
23c0bd4d
     stdlib stdlib (test_stdlib);
     auto test_time = std::make_shared<fake_sys_time> (seed);
     sys_time time (test_time);
2587e1ec
 
     // when
f8e147cf
     class system sys (stdlib, time);
2587e1ec
 
     // then
23c0bd4d
     check (test_stdlib->captured_seed == seed, "seed not cpatured");
2587e1ec
     succeed();
 }
 int run_tests()
 {
     test (initializes_random);
     succeed();
 }
 
 int main (int argc, char *argv[])
 {
     return !run_tests();
 }