Advertisement
Guest User

Triangle PHPUnit

a guest
May 30th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2. class NotAllArgumentsException extends Exception { }
  3. class NaNException extends Exception { }
  4. class NegativeNumberException extends Exception { }
  5. class NaTException extends Exception { }
  6.  
  7. class Triangle {
  8.     public function name_is($l1 = null, $l2 = null, $l3 = null){
  9.         try{
  10.             if(!$l1 || !$l2 || !$l3)
  11.                 throw new NotAllArgumentsException('Not All Arguments Exception');
  12.             $arr = array($l1,$l2, $l3);
  13.             foreach($arr as $n)
  14.                 if(!is_int($n))
  15.                     throw new NaNException('NaN Exception');
  16.                 else if($n < 0)
  17.                     throw new NegativeNumberException('Negative Number Exception');
  18.             sort($arr);
  19.             if($arr[0]+$arr[1] <= $arr[2])
  20.                 throw new NaTException('NaT Exception');
  21.            
  22.             if($arr[0]==$arr[1] && $arr[1]==$arr[2])
  23.                 return "Equilatero";
  24.             else if(($arr[0]==$arr[1] && $arr[1]!=$arr[2])||($arr[0]!=$arr[1] && $arr[1]==$arr[2]))
  25.                 return "Isosceles";
  26.             else return "Escaleno";
  27.         }catch(Exception $e){
  28.             return $e->getMessage();
  29.         }
  30.     }
  31. }
  32.  
  33. class TriangleTest extends PHPUnit_Framework_TestCase{
  34.     public function SetUp(){
  35.         $this->t = new Triangle();
  36.     }
  37.     public function test_it(){
  38.         $myfile = fopen("stdin.txt", "r") or die("Unable to open test file");
  39.         while(!feof($myfile)) {
  40.             $test = trim(fgets($myfile));
  41.             $res = fgets($myfile);
  42.             str_replace(" ", ",", $test);
  43.             eval("\$this->assertEquals(\"$res\", $this->t->name_is($test));");
  44.         }
  45.         fclose($myfile);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement