Skip to content

Instantly share code, notes, and snippets.

@tgkprog
Created March 25, 2021 14:10
Show Gist options
  • Save tgkprog/7cd650b94c671bf20978ac9f47c30e3c to your computer and use it in GitHub Desktop.
Save tgkprog/7cd650b94c671bf20978ac9f47c30e3c to your computer and use it in GitHub Desktop.
Php sample function with unit test cases
<?php
$count= 0;
$errs = 0;
/**
* Target function, this function is the main function to implement. The signature should be same as
in your question.
* Sample Question:
Given two temperatures, return true if one is less than 0 and the other is greater than 100.
icyHot(120, -1) : true
icyHot(-1, 120) : true
icyHot(2, 120) : false
For debug can have system out println here but in reference website need to comment out.
public boolean icyHot($temp1, $temp2) {
if(temp1 < 0 && temp2> 100)return true;//sample answer, incomplete
return false;
}
*/
function icyHot($temp1, $temp2) {
if($temp1 < 0 && $temp2 > 100)return true;
return false;
}
function testIcyHot($temp1, $temp2, $expectedReturn) {
global $count, $errs;
$actualReturn = false ;
$count++;
try {
$actualReturn = icyHot($temp1, $temp2);
} catch (Exception $e) {
echo ("err " . $e);
}
if ($actualReturn != $expectedReturn) {
echo("Actual :" . $actualReturn . ", expected :" . $expectedReturn . ", for temp1 :" . $temp1 . ", temp2 :" . $temp2
. ", count :" . $count .".");
$errs++;
}
}
function testCases(){
global $count, $errs;
echo("testCases :" );
testIcyHot(0, 0, false);
testIcyHot(0, 101, false);
testIcyHot(-1, 101, true);
testIcyHot(500, -101, true);
testIcyHot(0, 101,false);
testIcyHot(-100, 1999, true);
echo("\n\nIcy Hot test cases result :- count : $count,\n\n Errors (test case expected value wrong or implmentaion wrong or problem understanding wrong): $errs . \n\n");
}
testCases();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment