Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 121 additions & 50 deletions exercises/practice/list-ops/ListOpsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ public static function setUpBeforeClass(): void
require_once 'ListOps.php';
}


/**
* uuid 485b9452-bf94-40f7-a3db-c3cf4850066a
*/
#[TestDox('Append entries to a list and return the new list -> Empty lists')]
public function testAppendEmptyLists()
{
$listOps = new ListOps();
$this->assertEquals([], $listOps->append([], []));
$list1 = [];
$list2 = [];

$result = $listOps->append($list1, $list2);

$this->assertEquals([], $result);
}

/**
Expand All @@ -29,7 +35,12 @@ public function testAppendEmptyLists()
public function testAppendNonEmptyListToEmptyList()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 3, 4], $listOps->append([1, 2, 3, 4], []));
$list1 = [];
$list2 = [1, 2, 3, 4];

$result = $listOps->append($list1, $list2);

$this->assertEquals([1, 2, 3, 4], $result);
}

/**
Expand All @@ -39,7 +50,12 @@ public function testAppendNonEmptyListToEmptyList()
public function testAppendEmptyListToNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 3, 4], $listOps->append([], [1, 2, 3, 4]));
$list1 = [1, 2, 3, 4];
$list2 = [];

$result = $listOps->append($list1, $list2);

$this->assertEquals([1, 2, 3, 4], $result);
}

/**
Expand All @@ -49,7 +65,12 @@ public function testAppendEmptyListToNonEmptyList()
public function testAppendNonEmptyLists()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 2, 3, 4, 5], $listOps->append([1, 2], [2, 3, 4, 5]));
$list1 = [1, 2];
$list2 = [2, 3, 4, 5];

$result = $listOps->append($list1, $list2);

$this->assertEquals([1, 2, 2, 3, 4, 5], $result);
}

/**
Expand All @@ -59,7 +80,11 @@ public function testAppendNonEmptyLists()
public function testConcatEmptyLists()
{
$listOps = new ListOps();
$this->assertEquals([], $listOps->concat([], []));
$lists = [];

$result = $listOps->concat($lists);

$this->assertEquals([], $result);
}

/**
Expand All @@ -69,7 +94,11 @@ public function testConcatEmptyLists()
public function testConcatLists()
{
$listOps = new ListOps();
$this->assertEquals([1, 2, 3, 4, 5, 6], $listOps->concat([1, 2], [3], [], [4, 5, 6]));
$lists = [[1, 2], [3], [], [4, 5, 6]];

$result = $listOps->concat($lists);

$this->assertEquals([1, 2, 3, 4, 5, 6], $result);
}

/**
Expand All @@ -79,7 +108,11 @@ public function testConcatLists()
public function testConcatNestedLists()
{
$listOps = new ListOps();
$this->assertEquals([[1], [2], [3], [], [4, 5, 6]], $listOps->concat([[1], [2]], [[3]], [[]], [[4, 5, 6]]));
$lists = [[[1], [2]], [[3]], [[]], [[4, 5, 6]]];

$result = $listOps->concat($lists);

$this->assertEquals([[1], [2], [3], [], [4, 5, 6]], $result);
}

/**
Expand All @@ -89,10 +122,12 @@ public function testConcatNestedLists()
public function testFilterEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[],
$listOps->filter(static fn ($el) => $el % 2 === 1, [])
);
$list = [];
$function = static fn ($el) => $el % 2 === 1;

$result = $listOps->filter($list, $function);

$this->assertEquals([], $result);
}

/**
Expand All @@ -102,10 +137,12 @@ public function testFilterEmptyList()
public function testFilterNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[1, 3, 5],
$listOps->filter(static fn ($el) => $el % 2 === 1, [1, 2, 3, 5])
);
$list = [1, 2, 3, 5];
$function = static fn ($el) => $el % 2 === 1;

$result = $listOps->filter($list, $function);

$this->assertEquals([1, 3, 5], $result);
}

/**
Expand All @@ -115,7 +152,11 @@ public function testFilterNonEmptyList()
public function testLengthEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(0, $listOps->length([]));
$list = [];

$result = $listOps->length($list);

$this->assertEquals(0, $result);
}

/**
Expand All @@ -125,7 +166,11 @@ public function testLengthEmptyList()
public function testLengthNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(4, $listOps->length([1, 2, 3, 4]));
$list = [1, 2, 3, 4];

$result = $listOps->length($list);

$this->assertEquals(4, $result);
}

/**
Expand All @@ -135,10 +180,12 @@ public function testLengthNonEmptyList()
public function testMapEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[],
$listOps->map(static fn ($el) => $el + 1, [])
);
$list = [];
$function = static fn ($el) => $el + 1;

$result = $listOps->map($list, $function);

$this->assertEquals([], $result);
}

/**
Expand All @@ -148,10 +195,12 @@ public function testMapEmptyList()
public function testMapNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
[2, 4, 6, 8],
$listOps->map(static fn ($el) => $el + 1, [1, 3, 5, 7])
);
$list = [1, 3, 5, 7];
$function = static fn ($el) => $el + 1;

$result = $listOps->map($list, $function);

$this->assertEquals([2, 4, 6, 8], $result);
}

/**
Expand All @@ -161,10 +210,13 @@ public function testMapNonEmptyList()
public function testFoldlEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
2,
$listOps->foldl(static fn ($acc, $el) => $el * $acc, [], 2)
);
$list = [];
$initial = 2;
$function = static fn ($acc, $el) => $el * $acc;

$result = $listOps->foldl($list, $initial, $function);

$this->assertEquals(2, $result);
}

/**
Expand All @@ -174,10 +226,13 @@ public function testFoldlEmptyList()
public function testFoldlDirectionIndependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
15,
$listOps->foldl(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
);
$list = [1, 2, 3, 4];
$initial = 5;
$function = static fn ($acc, $el) => $el + $acc;

$result = $listOps->foldl($list, $initial, $function);

$this->assertEquals(15, $result);
}

/**
Expand All @@ -187,10 +242,13 @@ public function testFoldlDirectionIndependentNonEmptyList()
public function testFoldlDirectionDependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
64,
$listOps->foldl(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
);
$list = [1, 2, 3, 4];
$initial = 24;
$function = static fn ($acc, $el) => $el / $acc;

$result = $listOps->foldl($list, $initial, $function);

$this->assertEquals(64, $result);
}

/**
Expand All @@ -200,10 +258,13 @@ public function testFoldlDirectionDependentNonEmptyList()
public function testFoldrEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
2,
$listOps->foldr(static fn ($acc, $el) => $el * $acc, [], 2)
);
$list = [];
$initial = 2;
$function = static fn ($acc, $el) => $el * $acc;

$result = $listOps->foldr($list, $initial, $function);

$this->assertEquals(2, $result);
}

/**
Expand All @@ -213,10 +274,13 @@ public function testFoldrEmptyList()
public function testFoldrDirectionIndependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
15,
$listOps->foldr(static fn ($acc, $el) => $acc + $el, [1, 2, 3, 4], 5)
);
$list = [1, 2, 3, 4];
$initial = 5;
$function = static fn ($acc, $el) => $el + $acc;

$result = $listOps->foldr($list, $initial, $function);

$this->assertEquals(15, $result);
}

/**
Expand All @@ -226,10 +290,13 @@ public function testFoldrDirectionIndependentNonEmptyList()
public function testFoldrDirectionDependentNonEmptyList()
{
$listOps = new ListOps();
$this->assertEquals(
9,
$listOps->foldr(static fn ($acc, $el) => $el / $acc, [1, 2, 3, 4], 24)
);
$list = [1, 2, 3, 4];
$initial = 24;
$function = static fn ($acc, $el) => $el / $acc;

$result = $listOps->foldr($list, $initial, $function);

$this->assertEquals(9, $result);
}

/**
Expand Down Expand Up @@ -269,6 +336,10 @@ public function testReverseNonEmptyOddLengthList()
public function testReverseNonEmptyListIsNotFlattened()
{
$listOps = new ListOps();
$this->assertEquals([[4, 5, 6], [], [3], [1, 2]], $listOps->reverse([[1, 2], [3], [], [4, 5, 6]]));
$list = [[1, 2], [3], [], [4, 5, 6]];

$result = $listOps->reverse($list);

$this->assertEquals([[4, 5, 6], [], [3], [1, 2]], $result);
}
}
65 changes: 65 additions & 0 deletions exercises/practice/list-ops/ListOpsTest.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

{% set callbacks = {
'(x) -> x modulo 2 == 1': 'static fn ($el) => $el % 2 === 1',
'(x) -> x + 1': 'static fn ($el) => $el + 1',
'(acc, el) -> el * acc': 'static fn ($acc, $el) => $el * $acc',
'(acc, el) -> el + acc': 'static fn ($acc, $el) => $el + $acc',
'(acc, el) -> el / acc': 'static fn ($acc, $el) => $el / $acc',
}
-%}

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

use PHPUnit\Framework\ExpectationFailedException;

class ListOpsTest extends PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass(): void
{
require_once 'ListOps.php';
}


{% for case0 in cases -%}
{% for case in case0.cases -%}
/**
* @testdox {{ case0.description }} -> {{ case.description }}
*/
public function {{ testfn(case0.description ~ ' with ' ~ case.description) }}()
{
$listOps = new ListOps();
{% for property, value in case.input -%}
${{ property }} = {{ property == 'function' ? callbacks[value] : export(value) }};
{% endfor %}

$result = $listOps->{{ case.property }}({{ case.input | keys | map(p => '$' ~ p) | join(', ')}});

$this->assertEquals({{ export(case.expected) }}, $result);
}

{% endfor -%}
{% endfor -%}
}
3 changes: 3 additions & 0 deletions test-generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.phpunit.cache/
.phpcs-cache
vendor/
Loading
Loading