bool TestCodeError::TestUnknownObjectMethod() { VE(UnknownObjectMethod, "<?php class T {} " "function test() { $a = new T(); print $a->a(); }"); // negatve cases VEN(UnknownObjectMethod, "<?php class T { function __call($name, $args) {} } " "function t() { $a = new T(); print $a->a(); }"); VEN(UnknownObjectMethod, "<?php class T { function __call($name, $args) {}} class R extends T {} " "function test(R $a) { print $a->a();}"); return true; }
bool TestCodeError::TestInvalidInstantiation() { VE(InvalidInstantiation, "<?php interface T {}; $a = new T();"); VE(InvalidInstantiation, "<?php abstract class T { abstract function foo(); };" "$a = new T();"); VEN(InvalidInstantiation, "<?php class T {}; $a = new T();"); return true; }
bool TestCodeError::TestBadConstructorCall() { VE(BadConstructorCall, "<?php class T {} $a = new T(1);"); // negative cases VEN(BadConstructorCall, "<?php class B { function __construct($a) {}} " "class A extends B {} $a = new A(1);"); return true; }
bool TestCodeError::TestUnknownClass() { VE(UnknownClass, "<?php $a = new T();"); VEN(UnknownClass, "<?php class A { function foo(self $a) {}}"); VEN(UnknownClass, "<?php " "trait A {" " function foo($f) {" " echo self::FOO+parent::FOO;" " echo self::$f()+parent::$f();" " var_dump(new self, new parent);" " echo self::bar()+parent::bar();" " echo self::$foo+parent::$foo;" " try {} catch (self $p) {}" " try {} catch (parent $p) {}" " }" "}"); return true; }
bool TestCodeError::TestMissingAbstractMethodImpl() { VE(MissingAbstractMethodImpl, "<?php " "abstract class A {" " abstract function foo();" "}" "class B extends A {" "}"); VE(MissingAbstractMethodImpl, "<?php " "interface A {" " function foo();" "}" "class B implements A {" "}"); VEN(MissingAbstractMethodImpl, "<?php " "if (true) {" " abstract class A {" " abstract function foo();" " }" "} else {" " abstract class A {" " }" "}" "class B extends A {" "}"); VEN(MissingAbstractMethodImpl, "interface A {" " public function foo();" "}" "class B implements A {" " public function foo() {" " return;" " }" "}" "interface C extends A { }" "class D extends B implements C {}" "class E extends D { }"); return true; }
bool TestCodeError::TestTooManyArgument() { VE(TooManyArgument, "<?php function test() {} test(1);"); VE(TooManyArgument, "<?php class A { function t() {}} " "function test() { $a = new A(); $a->t(1);}"); VE(TooManyArgument, "<?php class T { function __construct($b) {}} $a = new T(1, 2);"); // negative cases VEN(TooManyArgument, "<?php function t() {} function t($a) {} t($a);"); return true; }
bool TestCodeError::TestMissingObjectContext() { VE(MissingObjectContext, "<?php class A { public $a = 123; " "public static function test() { print $this->a;}} A::test();"); VE(MissingObjectContext, "<?php class A { public function a() { } " "public static function test() { $this->a();}} A::test();"); // negative case VEN(MissingObjectContext, "<?php class A { public function a() {} } " "class B extends A { public function b() { A::a();}}"); VEN(MissingObjectContext, "<?php class A { public function a() {} } print A::a();"); VEN(MissingObjectContext, "<?php class A { public function a() {} } " "class B { public function b() { A::a();}}"); return true; }
double GuptaPotentialEnergy::EnergyValue(Clusters& cluster) { int N = cluster.GetAtomsNumber(); _atomEnergy.resize(N); double *dis = cluster.GetDistancePointer(); double E = 0; vector<double> VEN(N,0); vector<double> PEN(N,0); Alloy alloy = cluster.GetAlloy(); for (int i = 0; i < N - 1; i ++) { for (int j = i + 1; j < N; j ++) { double r = dis[ i * N + j ]; int note1 = cluster.GetAtomAtIndex(i).GetNote(); int note2 = cluster.GetAtomAtIndex(j).GetNote(); Gupta_AtomParamter parameter = (Gupta_AtomParamter&)ReturnAtomParameter(alloy[note1],alloy[note2]); double FMJN = r / parameter.r0 - 1; double FMJV = parameter.A * exp( -parameter.P * FMJN ); double FMJP = parameter.Xi * parameter.Xi * exp( -2 * parameter.q *FMJN ); VEN[i] += FMJV; VEN[j] += FMJV; PEN[i] += FMJP; PEN[j] += FMJP; } } for (int i = 0; i < N; i++) { _atomEnergy[i] = VEN[i] - sqrt(PEN[i]); E += _atomEnergy[i]; } cluster.SetEnergyVectorOfAtoms(_atomEnergy); cluster.SetEnergy(E); return E; }
bool TestCodeError::TestInvalidAttribute() { VE(InvalidAttribute, "<?php abstract class F { abstract $f; }"); VE(InvalidAttribute, "<?php class F { abstract $f; }"); VE(InvalidAttribute, "<?php final class F { final $f; }"); VE(InvalidAttribute, "<?php class F { final $f; }"); VE(InvalidAttribute, "<?php interface I { final function foo(); }"); VE(InvalidAttribute, "<?php interface I { private function foo(); }"); VE(InvalidAttribute, "<?php interface I { protected function foo(); }"); VE(InvalidAttribute, "<?php interface I { abstract function foo(); }"); VE(InvalidAttribute, "<?php class a { static function a() {} }"); VE(InvalidAttribute, "<?php class a { static function __construct() {} }"); VEN(InvalidAttribute, "<?php class a { function __construct() {} static function a() {} }"); return true; }
bool TestCodeError::TestPHPIncludeFileNotFound() { VE(PHPIncludeFileNotFound, "<?php include $_SERVER['PHP_ROOT'].'a.php';"); VEN(PHPIncludeFileNotFound, "<?php include_once $template_path;"); return true; }
bool TestCodeError::TestBadPassByReference() { VE(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "set_to_null(1);"); VE(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "class A { const C = 1; }" "set_to_null(A::C);"); VE(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "set_to_null($a + $b);"); VE(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "set_to_null(foo() + foo());"); VE(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "set_to_null(array(1));"); VE(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "define('A', 1);" "set_to_null(A);"); VE(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "set_to_null($a ? $b : $c);"); VE(BadPassByReference, "<?php " "class A { function foo(&$a) { echo $a;} }" "class B { function bar() { $obj = new A; $obj->foo(1); } }"); VEN(BadPassByReference, "<?php " "function set_to_null(&$i) { $i = null; }" "function foo() { return 1; }" "class A { var $m; static $n; function f() { return 1;} }" "set_to_null($a);" "set_to_null($a = 1);" "set_to_null(($a = 1));" "set_to_null(new A);" "set_to_null(foo());" "$a = 'foo';" "$b = 'a';" "set_to_null($a());" "set_to_null($$b);" "$i = 1;" "set_to_null(++$i); set_to_null($i--);" "set_to_null(--$i); set_to_null($i--);" "$obj = new A;" "set_to_null($obj->f());" "set_to_null($obj->m);" "set_to_null(A::$n);"); VEN(BadPassByReference, "$ar = array(" " array('10', 11, 100, 100, 'a')," " array( 1, 2, '2', 3, 1)" " );" "array_multisort($ar[0], SORT_ASC, SORT_STRING," " $ar[1], SORT_NUMERIC, SORT_DESC);"); return true; }
bool TestCodeError::TestInvalidDerivation() { VE(InvalidDerivation, "<?php " "interface RecurisiveFooFar extends RecurisiveFooFar {}"); VE(InvalidDerivation, "<?php " "class RecurisiveFooFar extends RecurisiveFooFar {}"); VE(InvalidDerivation, "<?php " "interface a extends b {}" "interface b extends a {}"); VE(InvalidDerivation, "<?php " "interface a extends B {}" "interface b extends a {}"); VE(InvalidDerivation, "<?php " "interface a extends b {}" "interface B extends a {}"); VE(InvalidDerivation, "<?php " "interface a extends b {}" "interface b extends A {}"); VE(InvalidDerivation, "<?php " "interface A extends B {}" "interface b extends a {}"); VE(InvalidDerivation, "<?php " "interface a extends B {}" "interface B extends a {}"); VE(InvalidDerivation, "<?php " "interface a extends b {}" "interface B extends A {}"); VE(InvalidDerivation, "<?php " "interface A extends B {}" "interface B extends a {}"); VE(InvalidDerivation, "<?php " "interface a extends B {}" "interface B extends A {}"); VE(InvalidDerivation, "<?php " "class a extends b {}" "class B extends A {}"); VE(InvalidDerivation, "<?php " "interface a extends b {}" "class B extends A {}"); VE(InvalidDerivation, "<?php interface A {} class T implements A, A {}"); VE(InvalidDerivation, "<?php class A {} class B implements A {}"); VE(InvalidDerivation, "<?php class A {} interface B extends A {}"); VEN(InvalidDerivation, "<?php " "class A {} " "interface B {} " "class C extends A implements B {}"); VE(InvalidDerivation, "<?php interface I {} class C extends I {}"); return true; }