Example #1
0
 public function range($stop = null)
 {
     $args = self::_wrapArgs(func_get_args(), 1);
     $__ = new self();
     $args = $__->reject($args, function ($val) {
         return is_null($val);
     });
     $num_args = count($args);
     switch ($num_args) {
         case 1:
             list($start, $stop, $step) = array(0, $args[0], 1);
             break;
         case 2:
             list($start, $stop, $step) = array($args[0], $args[1], 1);
             if ($stop < $start) {
                 return self::_wrap(array());
             }
             break;
         default:
             list($start, $stop, $step) = array($args[0], $args[1], $args[2]);
             if ($step > 0 && $step > $stop) {
                 return self::_wrap(array($start));
             }
     }
     $results = range($start, $stop, $step);
     // Switch inclusive to exclusive
     if ($step > 0 && $__->last($results) >= $stop) {
         array_pop($results);
     } elseif ($step < 0 && $__->last($results) <= $stop) {
         array_pop($results);
     }
     return self::_wrap($results);
 }
Example #2
0
 /**
  * Run a self test of the Zmsg class.
  *
  * @return boolean
  * @todo See if assert returns
  */
 public static function test()
 {
     $result = true;
     $context = new ZMQContext();
     $output = new ZMQSocket($context, ZMQ::SOCKET_DEALER);
     $output->bind("inproc://zmsg_selftest");
     $input = new ZMQSocket($context, ZMQ::SOCKET_ROUTER);
     $input->connect("inproc://zmsg_selftest");
     //  Test send and receive of single-part message
     $zmsgo = new self($output);
     $zmsgo->body_set("Hello");
     $result &= assert($zmsgo->body() == "Hello");
     $zmsgo->send();
     $zmsgi = new self($input);
     $zmsgi->recv();
     $result &= assert($zmsgi->parts() == 2);
     $result &= assert($zmsgi->body() == "Hello");
     echo $zmsgi;
     //  Test send and receive of multi-part message
     $zmsgo = new self($output);
     $zmsgo->body_set("Hello");
     $zmsgo->wrap("address1", "");
     $zmsgo->wrap("address2");
     $result &= assert($zmsgo->parts() == 4);
     echo $zmsgo;
     $zmsgo->send();
     $zmsgi = new self($input);
     $zmsgi->recv();
     $result &= assert($zmsgi->parts() == 5);
     $zmsgi->unwrap();
     $result &= assert($zmsgi->unwrap() == "address2");
     $zmsgi->body_fmt("%s%s", 'W', "orld");
     $result &= assert($zmsgi->body() == "World");
     //  Pull off address 1, check that empty part was dropped
     $zmsgi->unwrap();
     $result &= assert($zmsgi->parts() == 1);
     //  Check that message body was correctly modified
     $part = $zmsgi->pop();
     $result &= assert($part == "World");
     $result &= assert($zmsgi->parts() == 0);
     // Test load and save
     $zmsg = new self();
     $zmsg->body_set("Hello");
     $zmsg->wrap("address1", "");
     $zmsg->wrap("address2");
     $result &= assert($zmsg->parts() == 4);
     $fh = fopen(sys_get_temp_dir() . "/zmsgtest.zmsg", 'w');
     $zmsg->save($fh);
     fclose($fh);
     $fh = fopen(sys_get_temp_dir() . "/zmsgtest.zmsg", 'r');
     $zmsg2 = new self();
     $zmsg2->load($fh);
     assert($zmsg2->last() == $zmsg->last());
     fclose($fh);
     $result &= assert($zmsg2->parts() == 4);
     echo $result ? "OK" : "FAIL", PHP_EOL;
     return $result;
 }