Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions UnitTests/BadHashCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace UnitTests
{
struct BadHashCode
{
public int Value;

public override int GetHashCode() => 42;

public static implicit operator BadHashCode(int i) => new BadHashCode { Value = i };

public static implicit operator int(BadHashCode bhc) => bhc.Value;
}
}
49 changes: 36 additions & 13 deletions UnitTests/CacheTableTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace UnitTests
{
public abstract class CacheTableTestBase<TCacheTable> where TCacheTable : ICacheTable<int, int>
public abstract class CacheTableTestBase
{
protected ITestOutputHelper output;

Expand All @@ -17,12 +17,12 @@ protected CacheTableTestBase(ITestOutputHelper output)
this.output = output;
}

protected abstract TCacheTable CreateTable(int numRows, int numColumns);
protected abstract ICacheTable<TKey, TValue> CreateTable<TKey, TValue>(int numRows, int numColumns);

[Fact]
public void SetAndTryGetValue()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
table[1] = 2;
table.TryGetValue(1, out int val).Should().BeTrue();
val.Should().Be(2);
Expand All @@ -31,23 +31,23 @@ public void SetAndTryGetValue()
[Fact]
public void TryGetNonExistentValue()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
table.TryGetValue(1, out int val).Should().BeFalse();
val.Should().Be(default(int));
}

[Fact]
public void SetAndIndex()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
table[1] = 2;
table[1].Should().Be(2);
}

[Fact]
public void Update()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);

for (int i = 0; i < 4; i++)
{
Expand All @@ -72,7 +72,7 @@ public void Update()
[Fact]
public void IndexNonExistentKeyThrows()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
Action act = () => Console.WriteLine(table[0]);
act.Should().Throw<KeyNotFoundException>()
.WithMessage("No value found for 0");
Expand All @@ -81,7 +81,7 @@ public void IndexNonExistentKeyThrows()
[Fact]
public void ContainsKey()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
table.ContainsKey(0).Should().BeFalse();
table[0] = 1;
table.ContainsKey(0).Should().BeTrue();
Expand All @@ -90,7 +90,7 @@ public void ContainsKey()
[Fact]
public void Remove()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
table[1] = 2;
table.Remove(1).Should().BeTrue();
table.TryGetValue(1, out int val).Should().BeFalse();
Expand All @@ -103,7 +103,7 @@ public void Clear()
{
const int numInserts = 40;

var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
for (int i = 0; i < numInserts; i++)
{
table[i] = i;
Expand All @@ -123,7 +123,7 @@ public void Clear()
[Fact]
public void Count()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
table.Count.Should().Be(0);

for (int i = 0; i < 4; i++)
Expand All @@ -142,7 +142,7 @@ public void Count()
[Fact]
public void Enumerate()
{
var table = this.CreateTable(10, 4);
var table = this.CreateTable<int, int>(10, 4);
var rng = new Random();
List<KeyValuePair<int, int>> expected = new List<KeyValuePair<int, int>>();

Expand All @@ -160,7 +160,7 @@ public void Enumerate()
[Fact]
public void InsertOnFullTableOverwrites()
{
var table = this.CreateTable(5, 4);
var table = this.CreateTable<int, int>(5, 4);

int i = 0;
do
Expand All @@ -183,5 +183,28 @@ public void InsertOnFullTableOverwrites()
after.Count.Should().Be(1);
after.First().Should().Be(i);
}

[Theory]
[InlineData(4)]
[InlineData(7)]
public void ResolvesHashCollisions(int numColumns)
{
var table = this.CreateTable<BadHashCode, int>(5, numColumns);

for (int i = 0; i < numColumns; i++)
{
table[i] = i;
}

for (int i = 0; i < numColumns; i++)
{
table[i].Should().Be(i);
}

// This is going to overwrite a value
table[numColumns] = numColumns;
table[numColumns].Should().Be(numColumns);
table.Count.Should().Be(numColumns);
}
}
}
6 changes: 3 additions & 3 deletions UnitTests/CacheTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace UnitTests
{
public class CacheTableTests : CacheTableTestBase<CacheTable<int, int>>
public class CacheTableTests : CacheTableTestBase
{
public CacheTableTests(ITestOutputHelper output) : base(output)
{
}

protected override CacheTable<int, int> CreateTable(int numRows, int numColumns)
protected override ICacheTable<TKey, TValue> CreateTable<TKey, TValue>(int numRows, int numColumns)
{
return new CacheTable<int, int>(numRows, numColumns);
return new CacheTable<TKey, TValue>(numRows, numColumns);
}
}
}
8 changes: 4 additions & 4 deletions UnitTests/ConcurrentCacheTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

namespace UnitTests
{
public class ConcurrentCacheTableTests : CacheTableTestBase<ConcurrentCacheTable<int, int>>
public class ConcurrentCacheTableTests : CacheTableTestBase
{
public ConcurrentCacheTableTests(ITestOutputHelper output) : base(output)
{
}

protected override ConcurrentCacheTable<int, int> CreateTable(int numRows, int numColumns)
protected override ICacheTable<TKey, TValue> CreateTable<TKey, TValue>(int numRows, int numColumns)
{
return new ConcurrentCacheTable<int, int>(numRows, numColumns, 3);
return new ConcurrentCacheTable<TKey, TValue>(numRows, numColumns, 3);
}

[Fact]
public async Task ConcurrentWrites()
{
var table = CreateTable(5, 4);
var table = this.CreateTable<int, int>(5, 4);

const int numWrites = 1000;

Expand Down