未验证 提交 789a25bb 编写于 作者: C Charles Stoner 提交者: GitHub

Throw IndexOutOfRangeException from BitVector.this[int] if index < 0 (#46627)

上级 a8ee63e8
......@@ -3,16 +3,13 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.UnitTests.Collections
{
public class BitArrayTests
public class BitVectorTests
{
private const int seed = 128129347;
private const int rounds = 200;
......@@ -252,6 +249,75 @@ private void CheckTrueBitsCore(int capacity, Random r1, Random r2)
Assert.False(i2.MoveNext());
i2.Dispose();
}
[Theory]
[InlineData(0, 0)]
[InlineData(0, 1)]
[InlineData(0, 128)]
[InlineData(65, 64)]
[InlineData(65, 65)]
[InlineData(65, 128)]
public void IndexerGet(int capacity, int index)
{
var b = BitVector.Create(capacity);
Assert.Equal(capacity, b.Capacity);
Assert.False(b[index]);
Assert.Equal(capacity, b.Capacity);
}
[Theory]
[InlineData(0, 0)]
[InlineData(0, 1)]
[InlineData(0, 128)]
[InlineData(65, 64)]
[InlineData(65, 65)]
[InlineData(65, 128)]
public void IndexerSet(int capacity, int index)
{
var b = BitVector.Create(capacity);
Assert.Equal(capacity, b.Capacity);
capacity = Math.Max(capacity, index + 1);
b[index] = true;
Assert.True(b[index]);
Assert.Equal(capacity, b.Capacity);
b[index] = false;
Assert.False(b[index]);
Assert.Equal(capacity, b.Capacity);
}
[Theory]
[InlineData(0, -1)]
[InlineData(0, -128)]
[InlineData(65, -1)]
[InlineData(65, -128)]
public void IndexerGet_OutOfRange(int capacity, int index)
{
var b = BitVector.Create(capacity);
Assert.Equal(capacity, b.Capacity);
Assert.Throws<IndexOutOfRangeException>(() => _ = b[index]);
Assert.Equal(capacity, b.Capacity);
}
[Theory]
[InlineData(0, -1)]
[InlineData(0, -128)]
[InlineData(65, -1)]
[InlineData(65, -128)]
public void IndexerSet_OutOfRange(int capacity, int index)
{
var b = BitVector.Create(capacity);
Assert.Equal(capacity, b.Capacity);
Assert.Throws<IndexOutOfRangeException>(() => b[index] = false);
Assert.Equal(capacity, b.Capacity);
Assert.Throws<IndexOutOfRangeException>(() => b[index] = true);
Assert.Equal(capacity, b.Capacity);
}
}
internal static class RandomExtensions
......
......@@ -340,6 +340,8 @@ public bool UnionWith(in BitVector other)
{
get
{
if (index < 0)
throw new IndexOutOfRangeException();
if (index >= _capacity)
return false;
int i = (index >> Log2BitsPerWord) - 1;
......@@ -350,6 +352,8 @@ public bool UnionWith(in BitVector other)
set
{
if (index < 0)
throw new IndexOutOfRangeException();
if (index >= _capacity)
EnsureCapacity(index + 1);
int i = (index >> Log2BitsPerWord) - 1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册