Permalink
Browse files

Fixed InMemorySparseMatrixReader ctor. Added tests for usage of empty…

… sparce matrices in CachedMatrixMarketReader.
  • Loading branch information...
1 parent e111ab3 commit 8a0390f1d888bbc643c092ce8adf5767d85850d5 @dimchansky committed Aug 10, 2011
@@ -53,6 +53,24 @@ public void InitializedCachedMatrixMarketReaderReturnsTheSamePropertiesValues()
}
[TestMethod]
+ public void InitializedCachedMatrixMarketReaderReturnsTheSamePropertiesValuesForEmptyRowsArray()
+ {
+ // arrange
+ var reader = new InMemorySparseMatrixReader();
+
+ using (var cachedReader = new CachedMatrixMarketReader<double>(reader))
+ {
+ // act
+
+ // assert
+ Assert.AreEqual(reader.ColumnsCount, cachedReader.ColumnsCount);
+ Assert.AreEqual(reader.RowsCount, cachedReader.RowsCount);
+ Assert.AreEqual(reader.ElementsCount, cachedReader.ElementsCount);
+ }
+
+ }
+
+ [TestMethod]
public void ReadRowsReturnSameVectorsInFirstScan()
{
// arrange
@@ -73,6 +91,26 @@ public void ReadRowsReturnSameVectorsInFirstScan()
}
[TestMethod]
+ public void ReadRowsReturnSameVectorsInFirstScanForEmptyRowsArray()
+ {
+ // arrange
+ const int rows = 0;
+ const int columns = 0;
+ const double zeroProbability = 0.99;
+ var originalVectors = SparseVectorHelper.GenerateSparceVectors(rows, columns, zeroProbability).ToArray();
+ var originalReader = new InMemorySparseMatrixReader(originalVectors);
+
+ using (var cachedReader = new CachedMatrixMarketReader<double>(originalReader))
+ {
+ // act
+ var firstScanResults = originalReader.ReadRows().ZipFull(cachedReader.ReadRows(), (originalVector, cachedVector) => originalVector == cachedVector).All(b => b);
+
+ // assert
+ Assert.IsTrue(firstScanResults);
+ }
+ }
+
+ [TestMethod]
public void ReadRowsReturnSameVectorsInSecondScan()
{
// arrange
@@ -96,6 +134,29 @@ public void ReadRowsReturnSameVectorsInSecondScan()
}
[TestMethod]
+ public void ReadRowsReturnSameVectorsInSecondScanForEmptyRowsArray()
+ {
+ // arrange
+ const int rows = 0;
+ const int columns = 0;
+ const double zeroProbability = 0.99;
+ var originalVectors = SparseVectorHelper.GenerateSparceVectors(rows, columns, zeroProbability).ToArray();
+ var originalReader = new InMemorySparseMatrixReader(originalVectors);
+
+ using (var cachedReader = new CachedMatrixMarketReader<double>(originalReader))
+ {
+ // act
+ foreach (var row in cachedReader.ReadRows())
+ {
+ }
+ var secondScanResults = originalReader.ReadRows().ZipFull(cachedReader.ReadRows(), (originalVector, cachedVector) => originalVector == cachedVector).All(b => b);
+
+ // assert
+ Assert.IsTrue(secondScanResults);
+ }
+ }
+
+ [TestMethod]
public void ReadRowsInSecondScanDoNotUseOriginalReader()
{
// arrange
@@ -124,6 +185,35 @@ public void ReadRowsInSecondScanDoNotUseOriginalReader()
}
}
+ [TestMethod]
+ public void ReadRowsInSecondScanDoNotUseOriginalReaderForEmptyRowsArray()
+ {
+ // arrange
+ const int rows = 0;
+ const int columns = 0;
+ const double zeroProbability = 0.99;
+ var originalVectors = SparseVectorHelper.GenerateSparceVectors(rows, columns, zeroProbability).ToArray();
+ var originalReader = new InMemorySparseMatrixReader(originalVectors);
+ var originalReaderWithCounters = new SparseMatrixReaderWithMemberInvocationCounters<double>(originalReader);
+
+ using (var cachedReader = new CachedMatrixMarketReader<double>(originalReaderWithCounters))
+ {
+ // act
+ foreach (var row in cachedReader.ReadRows())
+ {
+ }
+ foreach (var row in cachedReader.ReadRows())
+ {
+ }
+
+ // assert
+ Assert.AreEqual(1, originalReaderWithCounters.ColumnsCountInvocations);
+ Assert.AreEqual(1, originalReaderWithCounters.RowsCountInvocations);
+ Assert.AreEqual(1, originalReaderWithCounters.ElementsCountInvocations);
+ Assert.AreEqual(1, originalReaderWithCounters.ReadRowsInvocations);
+ }
+ }
+
#region Helpers
private class SparseMatrixReaderWithMemberInvocationCounters<T> : ISparseMatrixReader<T>
@@ -23,8 +23,22 @@ public InMemorySparseMatrixReader(params SparseVector<double>[] rows)
}
this.rows = rows;
- this.columnsCount = (from r in rows from e in r select e.Key).Max() + 1;
- this.elementsCount = (from r in rows select r.NonZeroValuesCount).Sum();
+ var cc = 0;
+ var ec = 0;
+ foreach (var row in rows)
+ {
+ ec += row.NonZeroValuesCount;
+ foreach (var pair in row)
+ {
+ if(pair.Key >= cc)
+ {
+ cc = pair.Key + 1;
+ }
+ }
+ }
+
+ this.columnsCount = cc;
+ this.elementsCount = ec;
}
#region Implementation of ISparseMatrixReader

0 comments on commit 8a0390f

Please sign in to comment.