Skip to content

Commit

Permalink
Rewrite BitSetTests
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Dec 3, 2024
1 parent c521ad8 commit 001b83e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
9 changes: 0 additions & 9 deletions tests/phpstan/configs/phpstan-bugs.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,3 @@ parameters:
message: "#^Parameter \\#3 \\$packets of static method pocketmine\\\\network\\\\mcpe\\\\protocol\\\\serializer\\\\PacketBatch\\:\\:encodePackets\\(\\) expects array\\<int, pocketmine\\\\network\\\\mcpe\\\\protocol\\\\Packet\\>, array\\<int\\|string, pocketmine\\\\network\\\\mcpe\\\\protocol\\\\Packet\\> given\\.$#"
count: 1
path: ../../../src/serializer/PacketBatch.php
-
message: "#^Parameter \\#2 \\$parts of class pocketmine\\\\network\\\\mcpe\\\\protocol\\\\serializer\\\\BitSet constructor expects array\\<int\\>, array\\<int, float\\> given\\.$#"
count: 1
path: tests/phpunit/BitSetTest.php

-
message: "#^Parameter \\#2 \\$parts of class pocketmine\\\\network\\\\mcpe\\\\protocol\\\\serializer\\\\BitSet constructor expects array\\<int\\>, array\\<int, float\\|int\\> given\\.$#"
count: 1
path: tests/phpunit/BitSetTest.php
24 changes: 9 additions & 15 deletions tests/phpunit/BitSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PHPUnit\Framework\TestCase;
use pocketmine\network\mcpe\protocol\serializer\BitSet;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use function PHPUnit\Framework\assertTrue;

class BitSetTest extends TestCase{

Expand All @@ -33,12 +32,12 @@ public function testBitSet() : void{
$packetSerializer = PacketSerializer::decoder($packetSerializer->getBuffer(), 0);
$readTest = BitSet::read($packetSerializer, 65);

assertTrue($this->setsEqual($writeTest, $readTest));
self::assertEqualBitSets($writeTest, $readTest);
}

public function testBitSetConstructor() : void{
$test = new BitSet(65, [-9223372036854775808, 1]);
$test2 = new BitSet(65, [-9223372036854775808]);
$test = new BitSet(65, [-9223372036854775807 - 1, 1]);
$test2 = new BitSet(65, [-9223372036854775807 - 1]);

$test2->set(64, true);

Expand All @@ -61,7 +60,7 @@ public function testBitSetParts() : void{
$packetSerializer = PacketSerializer::decoder($packetSerializer->getBuffer(), 0);
$readTest = BitSet::read($packetSerializer, 128);

assertTrue($this->setsEqual($writeTest, $readTest));
self::assertEqualBitSets($writeTest, $readTest);
}

public function testVarUnsignedLongCompatibility() : void{
Expand All @@ -74,21 +73,16 @@ public function testVarUnsignedLongCompatibility() : void{
$expectedResult = new BitSet(64);
$expectedResult->set(63, true);

assertTrue($this->setsEqual($expectedResult, $readTest));
self::assertEqualBitSets($expectedResult, $readTest);
}

private function setsEqual(BitSet $a, BitSet $b) : bool{
$length = $a->getLength();
if($length !== $b->getLength()){
return false;
}
private static function assertEqualBitSets(BitSet $a, BitSet $b) : void{
self::assertEquals($length = $a->getLength(), $b->getLength(), "BitSet lengths are not equal");

for($i = 0; $i < $length; ++$i){
if($a->get($i) !== $b->get($i)){
return false;
}
self::assertEquals($a->get($i), $b->get($i), "BitSet values at index $i are not equal");
}

return $a->getPartsCount() === $b->getPartsCount();
self::assertEquals($a->getPartsCount(), $b->getPartsCount(), "BitSet parts count is not equal");
}
}

0 comments on commit 001b83e

Please sign in to comment.