Skip to content

Commit ab824bc

Browse files
committed
fixed logic of 'truncate out-of-range bits of last byte' in EnumSet::setBinaryBitset*()
1 parent 6e08fb1 commit ab824bc

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/EnumSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function setBinaryBitsetLe($bitset)
449449
if ($lastByteMaxOrd === 0) {
450450
$this->bitset = $bitset;
451451
} else {
452-
$lastByte = chr($lastByteMaxOrd) & $bitset[$size - 1];
452+
$lastByte = chr((1 << $lastByteMaxOrd) - 1) & $bitset[$size - 1];
453453
$this->bitset = substr($bitset, 0, -1) . $lastByte;
454454
}
455455

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MabeEnumTest\TestAsset\Enum32;
1010
use MabeEnumTest\TestAsset\Enum64;
1111
use MabeEnumTest\TestAsset\Enum65;
12+
use MabeEnumTest\TestAsset\Enum66;
1213
use PHPUnit_Framework_TestCase as TestCase;
1314

1415
/**
@@ -370,8 +371,11 @@ public function testSetBinaryBitsetLe()
370371

371372
public function testSetBinaryBitsetLeTruncateHighBits()
372373
{
373-
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
374-
foreach (Enum65::getEnumerators() as $enumerator) {
374+
// using Enum66 to make sure the max. ordinal number gets converted into a bitset
375+
// Enum65 has max. ordinal number of 1 of the last byte. -> 00000001
376+
// Enum66 has max. ordinal number of 2 of the last byte. -> 00000011
377+
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum66');
378+
foreach (Enum66::getEnumerators() as $enumerator) {
375379
$enumSet->attach($enumerator);
376380
}
377381

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\Enum;
6+
7+
/**
8+
* Unit tests for the class MabeEnum\Enum
9+
*
10+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
11+
* @copyright Copyright (c) 2013 Marc Bennewitz
12+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
13+
*/
14+
class Enum66 extends Enum65
15+
{
16+
const SIXTYSIX = 66;
17+
}

0 commit comments

Comments
 (0)