Skip to content

Instantly share code, notes, and snippets.

@iCaspar
Last active November 4, 2018 02:02
Show Gist options
  • Save iCaspar/ffc5328d8db236940a980d87e58940c8 to your computer and use it in GitHub Desktop.
Save iCaspar/ffc5328d8db236940a980d87e58940c8 to your computer and use it in GitHub Desktop.
Cata: Count of Positives / Sum of Negatives
<?php
/**
* Unit tests for "Count of Positives / Sum of Negatives" puzzle
*
* Requirements: @see https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives/php
* Given an array of integers.
* Return an array, where the first element is the count of positives numbers
* and the second element is sum of negative numbers.
*
* If the input array is empty or null, return an empty array.
*
* @package Cata\Tests\Unit\CountPosSumNeg
* @author Caspar Green <[email protected]>
* @since 1.0.0
*/
declare(strict_types=1);
namespace Cata\Tests\Unit\CountPosSumNeg;
use Cata\CountPosSumNeg\CounterSummer;
use PHPUnit\Framework\TestCase;
class CounterSummerTest extends TestCase
{
/**
* A CountSummer instance.
*
* @var CounterSummer
*/
private $countSum;
/**
* Sample input.
*
* @var array
*/
private $sampleInput;
/**
* Set up each test.
*
* @since 1.0.0
*
* @return void
*/
protected function setUp()
{
$this->countSum = new CounterSummer();
$this->sampleInput = [1, -4, 0, 3, -8, 5, 0, -25, 7, -7];
}
/**
* Test main() should return an empty array when input array is empty or null.
*
* @since 1.0.0
*
* @return void
*/
public function testMainShouldReturnEmptyArrayWhenInputEmptyOrNull(): void
{
// Test null case.
$result1 = $this->countSum->main();
$this->assertIsArray($result1);
$this->assertEmpty($result1);
// Test empty array case.
$result2 = $this->countSum->main([]);
$this->assertIsArray($result2);
$this->assertEmpty($result2);
}
/**
* Test main() should return the count of positives in the first item of an array.
*
* @since 1.0.0
*
* @return void
*/
public function testMainShouldReturnCountOfPositivesInFirstArrayItem(): void
{
$result = $this->countSum->main($this->sampleInput);
$this->assertEquals(4, $result[0]);
}
/**
* Test main() should return the sum of negatives in the second item of an array.
*
* @since 1.0.0
*
* @return void
*/
public function testMainShouldReturnSumOfNegativesInSecondArrayItem(): void
{
$result = $this->countSum->main($this->sampleInput);
$this->assertEquals(-44, $result[1]);
}
}
<?php
/**
* Count of Positives / Sum of Negatives
*
* @package Cata\CountPosSumNeg
* @author Caspar Green <[email protected]>
* @since 1.0.0
*/
declare(strict_types=1);
namespace Cata\CountPosSumNeg;
/**
* Class CounterSummer
*
* @package Cata\CountPosSumNeg
* @since 1.0.0
*/
class CounterSummer
{
/**
* Count the positives and sum the negatives.
*
* @param array $input
*
* @since 1.0.0
*
* @return array
*/
public function main(array $input = []): array
{
if (empty($input)) {
return [];
}
$count = 0;
$sum = 0;
foreach ($input as $item) {
($item <= 0)
? $sum += $item
: $count++;
}
return [$count, $sum];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment