Skip to content

Instantly share code, notes, and snippets.

@discoveryluca
Last active June 9, 2024 09:46
Show Gist options
  • Save discoveryluca/e5d251856ad335e84ac7ab225a0e3781 to your computer and use it in GitHub Desktop.
Save discoveryluca/e5d251856ad335e84ac7ab225a0e3781 to your computer and use it in GitHub Desktop.
DivisibleBy kata - 11988 tests that run in under 3 seconds, from 302 lines of code, using dataProviders
<?php
namespace App;
class DivisibleBy
{
public function isDivisibleByTwo($number)
{
// if last number is divisible by 2
return is_int(substr($number, -1) / 2);
}
public function isDivisibleByThree($number)
{
$digits = str_split($number);
$digitTotal = 0;
// add up each digit, divide that by three, return is_int
foreach ($digits as $digit) {
$digitTotal += $digit;
}
return is_int($digitTotal / 3);
}
public function isDivisibleByFour($number)
{
// if last two numbers are divisible by 4
return is_int(substr($number, -2, 2) / 4);
}
public function isDivisibleByFive($number)
{
// if last number is 0 or 5
return (substr($number, -1) == 0 || substr($number, -1) == 5);
}
public function isDivisibleBySix($number)
{
// if divisible by 2 and 3
return
$this->isDivisibleByTwo($number) &&
$this->isDivisibleByThree($number);
}
public function isDivisibleBySeven($number)
{
// remove last digit, double it, subtract it from the original, if divisible by 7
return is_int( $number-(substr($number, -3, 3)*2)/ 7);
}
public function isDivisibleByEight($number)
{
// if last three digits are divisible by 8
return is_int(substr($number, -3, 3) / 8);
}
public function isDivisibleByNine($number)
{
$digits = str_split($number);
$digitTotal = 0;
// add up each digit, divide that by 9, return is_int
foreach ($digits as $digit) {
$digitTotal += $digit;
}
return is_int($digitTotal / 9);
}
public function isDivisibleByTen($number)
{
// if last number is 0
return substr($number, -1) == 0;
}
public function isDivisibleByEleven($number)
{
// if sum of numbers in even places minus the sum of numbers in odd places equal 0 or 11
// get sum of even placed numbers
$evenTotal = 0;
for ($i = 0; $i <= strlen($number); $i+=2) {
$evenTotal += intval(substr($number, $i, 1));
}
// get sum of odd placed numbers
$oddTotal = 0;
for ($i = 1; $i <= strlen($number); $i+=2) {
$oddTotal += intval(substr($number, $i, 1));
}
// subtract odd from even
$subtracted = $evenTotal - $oddTotal;
return ($subtracted == 0 || $subtracted == 11);
}
public function isDivisibleByTwelve($number)
{
// if divisible by both 3 and 4
return
$this->isDivisibleByThree($number) &&
$this->isDivisibleByFour($number);
}
public function isDivisibleByThirteen($number)
{
// remove last digit, double it, subtract it from the original, if divisible by 7
$toCheck = $number-(substr($number, -3, 3)*9);
return ($toCheck == 0 || is_int($toCheck / 13));
}
}
<?php
use App\DivisibleBy;
use PHPUnit\Framework\TestCase;
function generateProvider($divider) {
$toReturn = [];
for ($i = 1; $i < 1000; $i++) {
$toReturn[] = [$i, is_int($i/$divider)];
}
return $toReturn;
}
class DivisibleByTest extends TestCase
{
private $ToDivide;
protected function setUp(): void
{
$this->ToDivide = new DivisibleBy();
parent::setUp(); // TODO: Change the autogenerated stub
}
/**
* @test
* @dataProvider twosToDivide
*/
function testDivisibleByTwo($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByTwo($number));
}
/**
* @test
* @dataProvider threesToDivide
*/
function testDivisibleByThree($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByThree($number));
}
/**
* @test
* @dataProvider foursToDivide
*/
function testDivisibleByFour($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByFour($number));
}
/**
* @test
* @dataProvider fivesToDivide
*/
function testDivisibleByFive($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByFive($number));
}
/**
* @test
* @dataProvider sixesToDivide
*/
function testDivisibleBySix($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleBySix($number));
}
/**
* @test
* @dataProvider sevensToDivide
*/
function testDivisibleBySeven($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleBySeven($number));
}
/**
* @test
* @dataProvider eightsToDivide
*/
function testDivisibleByEight($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByEight($number));
}
/**
* @test
* @dataProvider ninesToDivide
*/
function testDivisibleByNine($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByNine($number));
}
/**
* @test
* @dataProvider tensToDivide
*/
function testDivisibleByTen($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByTen($number));
}
/**
* @test
* @dataProvider elevensToDivide
*/
function testDivisibleByEleven($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByEleven($number));
}
/**
* @test
* @dataProvider twelvesToDivide
*/
function testDivisibleByTwelve($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByTwelve($number));
}
/**
* @test
* @dataProvider thirteensToDivide
*/
function testDivisibleByThirteen($number, $expected)
{
$this->assertEquals($expected, $this->ToDivide->isDivisibleByThirteen($number));
}
public static function twosToDivide()
{
return generateProvider(2);
}
public static function threesToDivide()
{
return generateProvider(3);
}
public static function foursToDivide()
{
return generateProvider(4);
}
public static function fivesToDivide()
{
return generateProvider(5);
}
public static function sixesToDivide()
{
return generateProvider(6);
}
public static function sevensToDivide()
{
return generateProvider(7);
}
public static function eightsToDivide()
{
return generateProvider(8);
}
public static function ninesToDivide()
{
return generateProvider(9);
}
public static function tensToDivide()
{
return generateProvider(10);
}
public static function elevensToDivide()
{
return generateProvider(11);
}
public static function twelvesToDivide()
{
return generateProvider(12);
}
public static function thirteensToDivide()
{
return generateProvider(13);
}
}
@discoveryluca
Copy link
Author

could do this within one associative array

@discoveryluca
Copy link
Author

there's a faster way to generate at least

@discoveryluca
Copy link
Author

discoveryluca commented Sep 11, 2023

11988 tests that run in under 3 seconds, from 302 lines of code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment