yaroslavche/BitMask

Unexpected AssociativeBitMask set method result

pixeldust opened this issue · 2 comments

Hi,

I like what you've been working on here. Not sure how active the project is for you still but I had an issue to discuss. Hopefully I've missed something here, so before I investigate too much further I thought I would ask you first.

The following test results in an unexpected Exception.

$bitmask = new AssociativeBitMask(['readable', 'writable', 'executable'], 1);

$bitmask->isReadable(); // True
$bitmask->isWritable(); // Exception thrown  "Undefined offset: 1" from line 68 (AssociativeBitMask class).

It looks like the constructor (set() method) is not correctly initialising the internal map property for the AssociativeBitMask.

The above example, yields

map = [
 0 => true
]

But I'd expect,

map = [
 0 => true,
 1 => false,
 2 => false
]

Am I missing something, or does the set() method need to initialise all map values?

A similar issue occurs if the mask value is initially set with all bits on, and then altered.

$bitmask = new AssociativeBitMask(['readable', 'writable', 'executable'], 7);

Correctly generates the following map,

map = [
 0 => true,
 1 => true,
 2 => true
]

Then updating the mask,

$bitmask->set(1);

Yields an unexpected map array,

map = [
 0 => true,
 1 => true,
 2 => true
]

Thanks.

Hi. Thanks for your interest and feedback. I'm glad, that you like =)

Yes, you completely right about wrong behavior. I've fixed this with follows:

$this->map = array_fill(0, count($keys), false);

You can check this commit (I made a typo, so it not showed here) =)
Also I've added tests cases described in your issue. Also I should fix some things and will close this issue with commit. Feel free to respond here.

Hi,

Thanks for the quick response and your update! Appreciated. That's really helpful.

Stay safe, in these strange times.