Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix BitArray.CopyTo to byte[] regression - update offset for extra bi…
Browse files Browse the repository at this point in the history
…ts. (#40441) (#40442)
  • Loading branch information
ahsonkhan authored and ericstj committed Aug 20, 2019
1 parent 91a30d4 commit 67ba0b6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/System.Collections/src/System/Collections/BitArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public void CopyTo(Array array, int index)
Debug.Assert(span.Length > 0);
Debug.Assert(m_array.Length > quotient);
// mask the final byte
span[span.Length - 1] = (byte)((m_array[quotient] >> (remainder * 8)) & ((1 << (int)extraBits) - 1));
span[remainder] = (byte)((m_array[quotient] >> (remainder * 8)) & ((1 << (int)extraBits) - 1));
}

switch (remainder)
Expand Down
100 changes: 100 additions & 0 deletions src/System.Collections/tests/BitArray/BitArray_GetSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,106 @@ public static void CopyTo<T>(BitArray bitArray, int length, int index, T[] expec
}
}

[Fact]
public static void CopyToByteArray()
{
for (int size = 4; size < 100; size++)
{
var bitArray = new BitArray(size);
bitArray[1] = true;
bitArray[3] = true;

for (int i = 0; i < 100; i++)
{
byte[] expectedOutput = new byte[100 + (size / 8 + 1)];
byte[] actualOutput = new byte[expectedOutput.Length];

expectedOutput[i] = 10;
bitArray.CopyTo(actualOutput, i);

Assert.Equal(expectedOutput, actualOutput);
}
}
}

[Fact]
public static void CopyToBoolArray()
{
for (int size = 4; size < 100; size++)
{
var bitArray = new BitArray(size);
bitArray[1] = true;
bitArray[3] = true;

for (int i = 0; i < 100; i++)
{
bool[] expectedOutput = new bool[100 + size];
bool[] actualOutput = new bool[expectedOutput.Length];

expectedOutput[i + 1] = true;
expectedOutput[i + 3] = true;
bitArray.CopyTo(actualOutput, i);

Assert.Equal(expectedOutput, actualOutput);
}
}
}

[Fact]
public static void CopyToIntArray()
{
for (int size = 10; size < 100; size++)
{
var bitArray = new BitArray(size);
bitArray[1] = true;
bitArray[3] = true;
bitArray[9] = true;

for (int i = 0; i < 100; i++)
{
int[] expectedOutput = new int[100 + (size / 32 + 1)];
int[] actualOutput = new int[expectedOutput.Length];

expectedOutput[i] = 522;
bitArray.CopyTo(actualOutput, i);

Assert.Equal(expectedOutput, actualOutput);
}
}
}

// https://github.com/dotnet/corefx/issues/39929
[Fact]
public static void CopyToByteArray_Regression39929()
{
bool[] directionBools = { true, true, true, true };
bool[] levelBools = { false, false, false, true };
byte[] byteHolder = new byte[2];
BitArray directionBits = new BitArray(directionBools);
BitArray levelBits = new BitArray(levelBools);

directionBits.CopyTo(byteHolder, 0);
levelBits.CopyTo(byteHolder, 1);

byte[] expectedOutput = { 0x0F, 0x08 };
Assert.Equal(expectedOutput, byteHolder);
}

// https://github.com/dotnet/core/issues/3194
[Fact]
public static void CopyToByteArray_Regression3194()
{
byte[] actualOutput = new byte[10];
BitArray bitArray = new BitArray(1);
bitArray[0] = true;

bitArray.CopyTo(actualOutput, 5);

byte[] expectedOutput = new byte[10];
expectedOutput[5] = 1;
Assert.Equal(expectedOutput, actualOutput);
}

[Fact]
public static void CopyTo_Type_Invalid()
{
Expand Down

0 comments on commit 67ba0b6

Please sign in to comment.