public function vectorReadElement() { $v = new Vector(); $world = new String('world'); $v->add($world); $this->assertEquals($world, $v[0]); }
Vector::$verbose = True; $vtxO = new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 0.0)); $vtxX = new Vertex(array('x' => 1.0, 'y' => 0.0, 'z' => 0.0)); $vtxY = new Vertex(array('x' => 0.0, 'y' => 1.0, 'z' => 0.0)); $vtxZ = new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 1.0)); $vtcXunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxX)); $vtcYunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxY)); $vtcZunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxZ)); print $vtcXunit . PHP_EOL; print $vtcYunit . PHP_EOL; print $vtcZunit . PHP_EOL; $dest1 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); Vertex::$verbose = True; $vtc1 = new Vector(array('dest' => $dest1)); Vertex::$verbose = False; $orig2 = new Vertex(array('x' => 23.87, 'y' => -37.95, 'z' => 78.34)); $dest2 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); $vtc2 = new Vector(array('orig' => $orig2, 'dest' => $dest2)); print 'Magnitude is ' . $vtc2->magnitude() . PHP_EOL; $nVtc2 = $vtc2->normalize(); print 'Normalized $vtc2 is ' . $nVtc2 . PHP_EOL; print 'Normalized $vtc2 magnitude is ' . $nVtc2->magnitude() . PHP_EOL; print '$vtc1 + $vtc2 is ' . $vtc1->add($vtc2) . PHP_EOL; print '$vtc1 - $vtc2 is ' . $vtc1->sub($vtc2) . PHP_EOL; print 'opposite of $vtc1 is ' . $vtc1->opposite() . PHP_EOL; print 'scalar product of $vtc1 and 42 is ' . $vtc1->scalarProduct(42) . PHP_EOL; print 'dot product of $vtc1 and $vtc2 is ' . $vtc1->dotProduct($vtc2) . PHP_EOL; print 'cross product of $vtc1 and $vtc2 is ' . $vtc1->crossProduct($vtc2) . PHP_EOL; print 'cross product of $vtcXunit and $vtcYunit is ' . $vtcXunit->crossProduct($vtcYunit) . 'aka $vtcZunit' . PHP_EOL; print 'cosinus of angle between $vtc1 and $vtc2 is ' . $vtc1->cos($vtc2) . PHP_EOL; print 'cosinus of angle between $vtcXunit and $vtcYunit is ' . $vtcXunit->cos($vtcYunit) . PHP_EOL;
/** * Axiom: c(A + B) = cA + cB * Additivity in the vector * @dataProvider dataProviderForTwoVectors */ public function testAdditivityInTheVectorForScalarMultiplication(array $A, array $B) { $A = new Vector($A); $B = new Vector($B); $c = 4; $c⟮A+B⟯ = $A->add($B)->scalarMultiply($c); $⟮cA+cB⟯ = $A->scalarMultiply($c)->add($B->scalarMultiply($c)); $this->assertEquals($c⟮A+B⟯, $⟮cA+cB⟯); $this->assertEquals($c⟮A+B⟯->getVector(), $⟮cA+cB⟯->getVector()); }
public function testAddExceptionSizeMisMatch() { $A = new Vector([1, 2, 3]); $B = new Vector([1, 2]); $this->setExpectedException('MathPHP\\Exception\\VectorException'); $A->add($B); }
/** * Runs suite * * @param string[] args * @return int exitcode */ public function run(array $args) { if (!$args) { return $this->usage(); } // Setup suite $suite = new TestSuite(); // Parse arguments $sources = new Vector(); $listener = TestListeners::$DEFAULT; $coverage = NULL; $arguments = array(); $colors = NULL; $cmap = array('' => NULL, '=on' => TRUE, '=off' => FALSE, '=auto' => NULL); try { for ($i = 0, $s = sizeof($args); $i < $s; $i++) { if ('-v' == $args[$i]) { $listener = TestListeners::$VERBOSE; } else { if ('-q' == $args[$i]) { $listener = TestListeners::$QUIET; } else { if ('-c' == $args[$i]) { $coverage = new CoverageListener(); foreach (explode(PATH_SEPARATOR, $this->arg($args, ++$i, 'c')) as $path) { $coverage->registerPath($path); } } else { if ('-cp' == $args[$i]) { foreach (explode(PATH_SEPARATOR, $this->arg($args, ++$i, 'cp')) as $element) { ClassLoader::registerPath($element, NULL); } } else { if ('-e' == $args[$i]) { $sources->add(new xp·unittest·sources·EvaluationSource($this->arg($args, ++$i, 'e'))); } else { if ('-l' == $args[$i]) { $arg = $this->arg($args, ++$i, 'l'); $class = XPClass::forName(strstr($arg, '.') ? $arg : 'xp.unittest.' . ucfirst($arg) . 'Listener'); $arg = $this->arg($args, ++$i, 'l'); if ('-?' == $arg || '--help' == $arg) { return $this->listenerUsage($class); } $output = $this->streamWriter($arg); $instance = $suite->addListener($class->newInstance($output)); // Get all @arg-annotated methods $options = array(); foreach ($class->getMethods() as $method) { if ($method->hasAnnotation('arg')) { $arg = $method->getAnnotation('arg'); if (isset($arg['position'])) { $options[$arg['position']] = $method; } else { $name = isset($arg['name']) ? $arg['name'] : strtolower(preg_replace('/^set/', '', $method->getName())); $short = isset($arg['short']) ? $arg['short'] : $name[0]; $options[$name] = $options[$short] = $method; } } } $option = 0; } else { if ('-o' == $args[$i]) { if (isset($options[$option])) { $name = '#' . ($option + 1); $method = $options[$option]; } else { $name = $this->arg($args, ++$i, 'o'); if (!isset($options[$name])) { $this->err->writeLine('*** Unknown listener argument ' . $name . ' to ' . $instance->getClassName()); return 2; } $method = $options[$name]; } $option++; if (0 == $method->numParameters()) { $pass = array(); } else { $pass = $this->arg($args, ++$i, 'o ' . $name); } try { $method->invoke($instance, $pass); } catch (TargetInvocationException $e) { $this->err->writeLine('*** Error for argument ' . $name . ' to ' . $instance->getClassName() . ': ' . $e->getCause()->toString()); return 2; } } else { if ('-?' == $args[$i] || '--help' == $args[$i]) { return $this->usage(); } else { if ('-a' == $args[$i]) { $arguments[] = $this->arg($args, ++$i, 'a'); } else { if ('--color' == substr($args[$i], 0, 7)) { $remainder = (string) substr($args[$i], 7); if (!array_key_exists($remainder, $cmap)) { throw new IllegalArgumentException('Unsupported argument for --color (must be <empty>, "on", "off", "auto" (default))'); } $colors = $cmap[$remainder]; } else { if (strstr($args[$i], '.ini')) { $sources->add(new xp·unittest·sources·PropertySource(new Properties($args[$i]))); } else { if (strstr($args[$i], xp::CLASS_FILE_EXT)) { $sources->add(new xp·unittest·sources·ClassFileSource(new File($args[$i]))); } else { if (strstr($args[$i], '.**')) { $sources->add(new xp·unittest·sources·PackageSource(Package::forName(substr($args[$i], 0, -3)), TRUE)); } else { if (strstr($args[$i], '.*')) { $sources->add(new xp·unittest·sources·PackageSource(Package::forName(substr($args[$i], 0, -2)))); } else { if (FALSE !== ($p = strpos($args[$i], '::'))) { $sources->add(new xp·unittest·sources·ClassSource(XPClass::forName(substr($args[$i], 0, $p)), substr($args[$i], $p + 2))); } else { if (is_dir($args[$i])) { $sources->add(new xp·unittest·sources·FolderSource(new Folder($args[$i]))); } else { $sources->add(new xp·unittest·sources·ClassSource(XPClass::forName($args[$i]))); } } } } } } } } } } } } } } } } } } catch (Throwable $e) { $this->err->writeLine('*** ', $e->getMessage()); xp::gc(); return 1; } if (isset($coverage)) { $suite->addListener($coverage); } if ($sources->isEmpty()) { $this->err->writeLine('*** No tests specified'); return 1; } // Set up suite $l = $suite->addListener($listener->newInstance($this->out)); if ($l instanceof ColorizingListener) { $l->setColor($colors); } foreach ($sources as $source) { try { $tests = $source->testCasesWith($arguments); foreach ($tests as $test) { $suite->addTest($test); } } catch (NoSuchElementException $e) { $this->err->writeLine('*** Warning: ', $e->getMessage()); continue; } catch (IllegalArgumentException $e) { $this->err->writeLine('*** Error: ', $e->getMessage()); return 1; } catch (MethodNotImplementedException $e) { $this->err->writeLine('*** Error: ', $e->getMessage(), ': ', $e->method, '()'); return 1; } } // Run it! if (0 == $suite->numTests()) { return 3; } else { $r = $suite->run(); return $r->failureCount() > 0 ? 1 : 0; } }
public function testAdd2() { $this->assertEquals(array(2.1, 3.1, 4.1), $this->object->add(1.1)->getValue()); }
public function iteration() { $v = new Vector(); for ($i = 0; $i < 5; $i++) { $v->add(new String('#' . $i)); } $i = 0; foreach ($v as $offset => $string) { $this->assertEquals($offset, $i); $this->assertEquals(new String('#' . $i), $string); $i++; } }