public function testAdd() { require 'Calculator.php'; $Controller = new Calculator(); $expected = 5; $this->assertEquals($expected, $Controller->add(3, 2)); }
function calculate($problem) { $operationFactory = new OperationFactory(); $parser = new ProblemParser($operationFactory); $calculator = new Calculator($parser); return $calculator->calculate($problem); }
public function testAddWithMultipleArgsAndLineBreaks() { $calc = new Calculator(); $expected = 6; $value = $calc->add("1\n2,3"); $this->assertEqual($expected, $value); }
public function testAdd() { $calc = new Calculator(); $result = $calc->add(30, 12); // assert that your calculator added the numbers correctly! $this->assertEquals(42, $result); }
public function testDivide() { $calc = new Calculator(); $quotient = $calc->divide(10, 2); $this->assertEquals(5, $quotient); //check if 10/2 is equal to 5 }
public function testAdd() { $calc = new Calculator(); $result = $calc->add(30, 12); // ¡acierta que nuestra calculadora suma dos números correctamente! $this->assertEquals(42, $result); }
/** * @test * @expectedException InvalidArgumentException */ public function shouldThrowExceptionForDivisionByZero() { // given $calculator = new \Calculator(); // when $calculator->divide(10, 0); // then $this->fail('exception should be thrown'); }
/** * @test */ public function shouldDivideTwoNumbers() { // given $calculator = new \Calculator(); // when $result = $calculator->divide(10, 5); // then $this->assertEquals(2, $result); }
/** * Check if the given data is ok for the given codice fiscale. * * @returns Returns true if the codice fiscale is ok, false otherwise. */ public function check() { $calculator = new Calculator($this->subject, array('omocodiaLevel' => $this->omocodiaLevel)); if ($this->omocodiaLevel == self::ALL_OMOCODIA_LEVELS) { $values = $calculator->calculateAllPossibilities(); } else { $values = array($calculator->calculate()); } return in_array($this->codiceFiscaleToCheck, $values); }
public function testCalculate() { $calculator = new Calculator(32); $p1Elo = 1613; $p2Elo = 1388; $win = -1; list($newP1Elo, $newP2Elo) = $calculator->calculate($p1Elo, $p2Elo, $win); $this->assertEquals(1620, $newP1Elo); $this->assertEquals(1381, $newP2Elo); $this->assertEquals($newP1Elo - $p1Elo, -($newP2Elo - $p2Elo)); }
/** * @test */ public function shouldDivideTwoNumbers() { // given $a = rand(0, 100); $b = rand(1, 100); $calculator = new \Calculator(); // when $result = $calculator->divide($a, $b); // then $this->assertEquals(round($a / $b), $result); }
public function testCalculate() { $calc = new Calculator('2*5+3*10*2-4*3'); $this->assertEquals(58, $calc->calculate()); $calc = new Calculator('(2+3)*(4-5)'); $this->assertEquals(-5, $calc->calculate()); $calc = new Calculator('-(2+3)*(4-5)'); $this->assertEquals(5, $calc->calculate()); $calc = new Calculator('-4*(2+3)*(4-5)'); $this->assertEquals(20, $calc->calculate()); $calc = new Calculator('-4*(2+3)*(4-5)/4'); $this->assertEquals(5, $calc->calculate()); }
public static function run() { require_once dirname(__FILE__) . '/model.php'; $sum = Calculator::calculate(); require_once dirname(__FILE__) . '/view.php'; CalculatorView::build($sum, Calculator::getFirstVariable(), Calculator::getSecondVariable()); }
public function showSinglestat($statID) { if (Auth::check()) { return Redirect::route('connect.connect'); } else { try { $user = User::find(1); Auth::login($user); $currentMetrics = Calculator::currentMetrics(); $metricValues = Metric::where('user', Auth::user()->id)->where('date', '<', Carbon::now())->orderBy('date', 'desc')->take(31)->get(); foreach ($currentMetrics as $metricID => $statClassName) { if ($metricID == $statID) { $metricsArray = array(); foreach ($metricValues as $metric) { $metricsArray[$metric->date] = $metric->{$metricID}; } ksort($metricsArray); $allMetrics[$metricID] = $metricsArray; } } if (isset($currentMetrics[$statID])) { return View::make('demo.single_stat', array('data' => $currentMetrics[$statID]['metricClass']::show($allMetrics[$statID], true), 'metricDetails' => $currentMetrics[$statID], Auth::logout())); } return Redirect::route('demo.dashboard')->with('error', 'Statistic does not exist.'); } catch (Exception $e) { Auth::logout(); Log::error($e); return Redirect::route('demo.dashboard')->with('error', 'Something went wrong, we will return shortly.'); } } }
public function run() { DB::table('events')->delete(); $currentDay = time(); $user = User::find(1); // days generated before the current date $generatedDay = $currentDay - 370 * 24 * 60 * 60; // generating data for ~2 years for ($i = 0; $i < 750; $i++) { // timestamp of the current day $dailyTimeStamp = $generatedDay + $i * 24 * 60 * 60; $date = date('Y-m-d', $dailyTimeStamp); $monthStats = EventSeeder::monthlyStats($dailyTimeStamp); $eventTimes = EventSeeder::generateEventTimes($monthStats, $dailyTimeStamp); foreach ($eventTimes as $event) { $temparray = EventSeeder::getEventType($monthStats, $event); if ($temparray) { DB::table('events')->insert(array('user' => $user->id, 'created' => date('Y-m-d H:i:s', $event), 'eventID' => $temparray['id'], 'type' => $temparray['type'], 'object' => $temparray['object'], 'provider' => 'stripe', 'previousAttributes' => $temparray['previousAttributes'], 'date' => date('Y-m-d', $event))); } } // set time to last timestamp +1 $event++; // calculate all Calculator::calculateMetrics($user, $event); } }
public function power($n, $p) { $obj = new Calculator(); if ($n < 0 || $p < 0) { throw new Exception("n and p should be non-negative"); } else { if ($p === 0) { return 1; } } $ans = $obj->power($n, intval($p / 2)); $ans *= $ans; if ($p % 2 === 1) { $ans *= $n; } return $ans; }
public function runMath($value) { $calc = new Calculator(); $calc->setValue($value[1], $value[3]); $result = ""; if ($value[2] == iCalc::iMathSignalPlus) { $result = $calc->getPlus(); } else { if ($value[2] == iCalc::iMathSignalDivi) { $result = $calc->getDivide(); } else { if ($value[2] == iCalc::iMathSignalMulti) { $result = $calc->getMultiply(); } else { if ($value[2] == iCalc::iMathSignalMinus) { $result = $calc->getMinus(); } else { if ($value[2] == iCalc::iMathSignalModu) { $result = $calc->getModule(); } else { $result = iCalc::iHelpMessage; } } } } } return $result; }
/** * Execute the console command. * * @return mixed */ public function fire() { Log::info('GetEvents fired'); // going through the users foreach (User::all() as $user) { // saving events Calculator::saveEvents($user); } Log::info('GetEvents finished'); }
public function fire($job, $data) { // we only get the user ID, get the user from it $user = User::find($data['userID']); Calculator::calculateMetricsOnConnect($user); Log::info('Sending "ready" email for user: '******'emails.connected')->to($user->email)->subject('Your metrics are ready!')->send(); $user->ready = 'connected'; $user->save(); $job->delete(); }
/** * This function calculates the sum of 2 variables * @access public * @author Eleri<*****@*****.**> * @return number result */ public static function calculate() { if (isset($_GET['first'])) { Calculator::$firstVariable = filter_var($_GET['first'], FILTER_VALIDATE_FLOAT); } if (isset($_GET['second'])) { Calculator::$secondVariable = filter_var($_GET['second'], FILTER_VALIDATE_FLOAT); } $result = Calculator::$firstVariable + Calculator::$secondVariable; return $result; }
public function test_numbers() { $a = new Calculator(); $this->assertEquals(0, $a->Add("")); $this->assertEquals(2, $a->Add("2")); $this->assertEquals(5, $a->Add("2,3")); $this->assertEquals(30, $a->Add("10,2,3,15")); $this->assertEquals(30, $a->Add("10,2\n3,15")); $this->assertEquals(30, $a->Add("//[;;;]\n10;;;20")); }
/** * * Calcula o modulo de 10, neste caso fazendo: * Quando o resultado da multiplica��o for um n�mero com 2 d�gitos, somar os 2 algarismos * Se o Total da Soma for inferior a 10, o DV corresponde � diferen�a entre 10 e o Total da Soma * Se o resto da divis�o for 0 (zero), o DV ser� 0 (zero) * * @param $numero * @return int */ public static function calculaModulo10($numero) { $soma_resultados = Calculator::getResultadoSomaModulo10($numero); if ($soma_resultados < 10) { return 10 - $soma_resultados; } else { $resto_divisao = $soma_resultados % 10; if ($resto_divisao === 0) { return $resto_divisao; } return 10 - $resto_divisao; } }
/** * Execute the console command. * * @return mixed */ public function fire() { Log::info('CalculateMetrics fired'); // going through the users foreach (User::all() as $user) { // check if user is connected and ready (in case he is just connecting), and trial not ended yet // or it's demo user if ($user->isStripeConnected() && $user->ready == 'connected' && !$user->isTrialEnded() || $user->id == 1) { // saving events Calculator::calculateMetrics($user, time()); } } Log::info('CalculateMetrics finished'); }
/** * daily metric calculator - called from cron script * @param $user * * @return null */ public static function calculateMetrics($user, $time) { // get needed time vars if ($time) { $timestamp = $time; } else { $timestamp = time(); } $today = date('Y-m-d', $timestamp); $yesterday = date('Y-m-d', $timestamp - 86400); // get today's metrics $metrics = Metric::firstOrNew(array('user' => $user->id, 'date' => $today)); // get yesterday's metrics // this should never return an empty array $yesterdayMetric = Metric::where('user', $user->id)->where('date', $yesterday)->first(); if (!$yesterdayMetric) { Calculator::calculateMetrics($user, $timestamp - 86400); $yesterdayMetric = Metric::where('user', $user->id)->where('date', $yesterday)->first(); } // get today's events $events = Event::where('user', $user->id)->where('date', $today)->get(); // events have a provider, needs merging // FIXME!!!! // calculate all the metrics // monthly recurring revenue // return int $metrics->mrr = MrrStat::calculate($yesterdayMetric->mrr, $events); // active users // return int $metrics->au = AUStat::calculate($yesterdayMetric->au, $events); // annual recurring revenue // return int $metrics->arr = ArrStat::calculate($metrics->mrr); // average recurring revenue per active user // return int $metrics->arpu = ArpuStat::calculate($metrics->mrr, $metrics->au); // daily and monthly cancellations // return array list($daily, $monthly) = CancellationStat::calculate($events, $user); $metrics->cancellations = $daily; $metrics->monthlyCancellations = $monthly; // user churn // return float $metrics->uc = UserChurnStat::calculate($metrics->monthlyCancellations, $user, $timestamp); // save everything $metrics->save(); }
public function fire($job, $data) { // we only get the user ID, get the user from it $user = User::find($data['userID']); Calculator::calculateMetricsOnConnect($user); $user->ready = 'connected'; $user->save(); try { Log::info('Sending "ready" email for user: '******'emails.connected') // ->to($user->email) // ->subject('Your metrics are ready!') // ->send(); } catch (Exception $e) { Log::info('Caught exception: ' . $e->getMessage()); } $job->delete(); }
public function formatMetrics() { // get currently calculated metrics $currentMetrics = Calculator::currentMetrics(); $this->date = Carbon::createFromFormat('Y-m-d', $this->date)->format('Y-m-d, l'); // go through them foreach ($currentMetrics as $statID => $statName) { if (is_null($this->{$statID})) { $this->{$statID} = 'Not enough data'; } else { switch ($statID) { // money formats fall through case 'mrr': case 'arr': case 'arpu': // divide the value by 100 $this->{$statID} /= 100; // format it into money setlocale(LC_MONETARY, 'en_US'); $this->{$statID} = money_format('%n', $this->{$statID}); break; // percent formats fall through // percent formats fall through case 'uc': $this->{$statID} = $this->{$statID} . '%'; break; // pieces formats fall through // pieces formats fall through case 'cancellations': case 'au': break; default: break; } // /switch } // /if } // /foreach }
<?php require_once "calculator.php"; $calculator = new Calculator(); $calculator->add(1, 2); $calculator->showResult();
public function browserCount() { $ie = $ff = $saf = $chr = $op = $kon = $lyn = $sum = 0; $result = $this->db->select(); while ($array = mysql_fetch_array($result)) { $favBrowser = $array['FavouriteBrowser']; if ($favBrowser == 'internet explorer') { ++$ie; } elseif ($favBrowser == 'firefox') { ++$ff; } elseif ($favBrowser == 'safari') { ++$saf; } elseif ($favBrowser == 'chrome') { ++$chr; } elseif ($favBrowser == 'opera') { ++$op; } elseif ($favBrowser == 'konqueror') { ++$kon; } elseif ($favBrowser == 'lynx') { ++$lyn; } ++$sum; } $calculator = new Calculator($ie, $ff, $saf, $chr, $op, $kon, $lyn, $sum); $this->rawCountArray = $calculator->convertToArray(); $this->percentageArray = $calculator->browserPercentage(); }
public function run($value, $result) { return $result + $value; } } class Subtractor implements CalculatorInterface { public function run($value, $result) { return $result - $value; } } class Divider implements CalculatorInterface { public function run($value, $result) { return $result / $value; } } class Multiplier implements CalculatorInterface { public function run($value, $result) { return $result * $value; } } /** * Calculation */ $c = new Calculator(); echo $c->setFunction(new Adder())->run(100, 97)->setFunction(new Subtractor())->run(117)->setFunction(new Adder())->run(20)->setFunction(new Divider())->run(5)->setFunction(new Multiplier())->run(17)->getResult();
/** * @dataProvider multiplProvider * @param $ex * @param $a * @param $b */ public function testMultipl($ex, $a, $b) { $calc = new Calculator(); $this->assertEquals($ex, $calc->multipl($a, $b)); }