Skip to content

Commit

Permalink
Merge pull request #98 from VirtualPhotonics/feature/fix_unit_test_cl…
Browse files Browse the repository at this point in the history
…eanup_issue

Fix the TearDown error
  • Loading branch information
Lisa Malenfant authored Sep 12, 2023
2 parents 9d99aa7 + 23c132f commit 4633d56
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 94 deletions.
63 changes: 31 additions & 32 deletions src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using NUnit.Framework;
using System.Collections.Generic;
using NUnit.Framework;
using Vts.IO;
using Vts.MonteCarlo;
using Vts.MonteCarlo.PhotonData;
Expand All @@ -13,7 +12,7 @@ public class CollisionInfoDatabaseTests
/// <summary>
/// list of temporary files created by these unit tests
/// </summary>
readonly List<string> listOfTestGeneratedFiles = new List<string>()
private readonly List<string> _listOfTestGeneratedFiles = new()
{
"testcollisioninfodatabase",
"testcollisioninfodatabase.txt"
Expand All @@ -24,12 +23,11 @@ public class CollisionInfoDatabaseTests
/// </summary>
[OneTimeSetUp]
[OneTimeTearDown]
public void clear_folders_and_files()
public void Clear_folders_and_files()
{
// delete any previously generated files
foreach (var file in listOfTestGeneratedFiles)
foreach (var file in _listOfTestGeneratedFiles)
{
GC.Collect();
FileIO.FileDelete(file);
}
}
Expand All @@ -39,72 +37,73 @@ public void clear_folders_and_files()
/// are working correctly.
/// </summary>
[Test]
public void validate_CollisionInfo_deserialized_class_is_correct_when_using_WriteToFile()
public void Validate_CollisionInfo_deserialized_class_is_correct_when_using_WriteToFile()
{
int numberOfSubregions = 3;
string databaseFilename = "testcollisioninfodatabase";
const int numberOfSubRegions = 3;
const string databaseFilename = "testcollisioninfodatabase";

#region Notes on implementation...
// which do we like? (#1 requires writing a separate class, #2 requires a little more comfort
// with using generics day-to-day
// 1) using (var dbWriter = new CollisionInfoDatabaseWriter("testcollisioninfodatabase", numberOfSubregions))
// 1) using (var dbWriter = new CollisionInfoDatabaseWriter("testcollisioninfodatabase", numberOfSubRegions))
// 2) (below)
// 3) another option would be to "wire" this up with Unity and get both
//using (var dbWriter = new DatabaseWriter<CollisionInfoDatabase, CollisionInfo>(
// databaseFilename,
// new CollisionInfoDatabase(numberOfSubregions),
// new CollisionInfoSerializer(numberOfSubregions)))
// new CollisionInfoDatabase(numberOfSubRegions),
// new CollisionInfoSerializer(numberOfSubRegions)))
#endregion

using (var dbWriter = new CollisionInfoDatabaseWriter(
VirtualBoundaryType.pMCDiffuseReflectance,"testcollisioninfodatabase", numberOfSubregions))
using (var dbWriter = new CollisionInfoDatabaseWriter(VirtualBoundaryType.pMCDiffuseReflectance, "testcollisioninfodatabase", numberOfSubRegions))
{
dbWriter.Write(
new CollisionInfo(numberOfSubregions)
new CollisionInfo(numberOfSubRegions)
{
new SubRegionCollisionInfo(10.0, 1000),
new SubRegionCollisionInfo(20.0, 2000),
new SubRegionCollisionInfo(30.0, 3000)
});

dbWriter.Write(
new CollisionInfo(numberOfSubregions)
new CollisionInfo(numberOfSubRegions)
{
new SubRegionCollisionInfo(40.0, 4000),
new SubRegionCollisionInfo(50.0, 5000),
new SubRegionCollisionInfo(60.0, 6000)
});
}

var dbcloned = CollisionInfoDatabase.FromFile(databaseFilename);
var dbCloned = CollisionInfoDatabase.FromFile(databaseFilename);

Assert.AreEqual(3, dbcloned.NumberOfSubRegions);
Assert.AreEqual(2, dbcloned.NumberOfElements);
Assert.AreEqual(3, dbCloned.NumberOfSubRegions);
Assert.AreEqual(2, dbCloned.NumberOfElements);

// manually enumerate through the first two elements (same as foreach)
// PhotonDatabase is designed so you don't have to have the whole thing
// in memory, so .ToArray() loses the benefits of the lazy-load data points
var enumerator = dbcloned.DataPoints.GetEnumerator();
using var enumerator = dbCloned.DataPoints.GetEnumerator();

// advance to the first point and test that the point is valid
enumerator.MoveNext();
var dp1 = enumerator.Current;
Assert.AreEqual(10.0,dp1[0].PathLength);
Assert.AreEqual(1000,dp1[0].NumberOfCollisions);
Assert.AreEqual(20.0,dp1[1].PathLength);
Assert.AreEqual(2000,dp1[1].NumberOfCollisions);
Assert.AreEqual(30.0,dp1[2].PathLength);
Assert.AreEqual(3000,dp1[2].NumberOfCollisions);
Assert.IsNotNull(dp1);
Assert.AreEqual(10.0, dp1[0].PathLength);
Assert.AreEqual(1000, dp1[0].NumberOfCollisions);
Assert.AreEqual(20.0, dp1[1].PathLength);
Assert.AreEqual(2000, dp1[1].NumberOfCollisions);
Assert.AreEqual(30.0, dp1[2].PathLength);
Assert.AreEqual(3000, dp1[2].NumberOfCollisions);

// advance to the second point and test that the point is valid
enumerator.MoveNext();
var dp2 = enumerator.Current;
Assert.AreEqual(40.0,dp2[0].PathLength);
Assert.AreEqual(4000,dp2[0].NumberOfCollisions);
Assert.AreEqual(50.0,dp2[1].PathLength);
Assert.AreEqual(5000,dp2[1].NumberOfCollisions);
Assert.IsNotNull(dp2);
Assert.AreEqual(40.0, dp2[0].PathLength);
Assert.AreEqual(4000, dp2[0].NumberOfCollisions);
Assert.AreEqual(50.0, dp2[1].PathLength);
Assert.AreEqual(5000, dp2[1].NumberOfCollisions);
Assert.AreEqual(60.0, dp2[2].PathLength);
Assert.AreEqual(6000,dp2[2].NumberOfCollisions);
Assert.AreEqual(6000, dp2[2].NumberOfCollisions);
}
}
}
120 changes: 58 additions & 62 deletions src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Vts.Common;
using Vts.IO;
using Vts.MonteCarlo;
Expand All @@ -15,7 +14,7 @@ public class pMCDatabaseTests
/// <summary>
/// list of temporary files created by these unit tests
/// </summary>
readonly List<string> listOfTestGeneratedFiles = new List<string>()
private readonly List<string> _listOfTestGeneratedFiles = new()
{
"testpmcdatabase",
"testpmcdatabase.txt",
Expand All @@ -29,12 +28,11 @@ public class pMCDatabaseTests
/// </summary>
[OneTimeSetUp]
[OneTimeTearDown]
public void clear_folders_and_files()
public void Clear_folders_and_files()
{
// delete any previously generated files
foreach (var file in listOfTestGeneratedFiles)
foreach (var file in _listOfTestGeneratedFiles)
{
GC.Collect();
FileIO.FileDelete(file);
}
}
Expand All @@ -43,50 +41,48 @@ public void clear_folders_and_files()
/// are working correctly.
/// </summary>
[Test]
public void validate_pMCDatabase_deserialized_class_is_correct_when_using_WriteToFile()
public void Validate_pMCDatabase_deserialized_class_is_correct_when_using_WriteToFile()
{
// test serialization
new SimulationInput().ToFile("SimulationInputTest.txt");

string photonDbFilename = "testpmcdatabase";
string collisionDbFilename = "testpmccollisiondatabase";
int numberOfSubregions = 3;
const string photonDbFilename = "testpmcdatabase";
const string collisionDbFilename = "testpmccollisiondatabase";
const int numberOfSubRegions = 3;

using(var dbWriter = new PhotonDatabaseWriter(
using (var dbWriter = new PhotonDatabaseWriter(
VirtualBoundaryType.pMCDiffuseReflectance, photonDbFilename))
{
using (var collisionDbWriter = new CollisionInfoDatabaseWriter(
VirtualBoundaryType.pMCDiffuseReflectance, collisionDbFilename, numberOfSubregions))
using var collisionDbWriter = new CollisionInfoDatabaseWriter(
VirtualBoundaryType.pMCDiffuseReflectance, collisionDbFilename, numberOfSubRegions);
// write data for first photon (exit DP and collision info)
dbWriter.Write(new PhotonDataPoint(
new Position(1, 2, 3),
new Direction(0, 0, 1),
1.0, // weight
10, // time
PhotonStateType.None));
collisionDbWriter.Write(new CollisionInfo(numberOfSubRegions)
{
// write data for first photon (exit DP and collision info)
dbWriter.Write(new PhotonDataPoint(
new Position(1, 2, 3),
new Direction(0, 0, 1),
1.0, // weight
10, // time
PhotonStateType.None));
collisionDbWriter.Write(new CollisionInfo(numberOfSubregions)
{
new SubRegionCollisionInfo(10.0, 1000),
new SubRegionCollisionInfo(20.0, 2000),
new SubRegionCollisionInfo(30.0, 3000)
});
// write data for second photon (exit DP and collision info)
dbWriter.Write(new PhotonDataPoint(
new Position(4, 5, 6),
new Direction(1, 0, 0),
0.50,
100,
PhotonStateType.None));
collisionDbWriter.Write(new CollisionInfo(numberOfSubregions)
{
new SubRegionCollisionInfo(40.0, 4000),
new SubRegionCollisionInfo(50.0, 5000),
new SubRegionCollisionInfo(60.0, 6000)
});
}
new SubRegionCollisionInfo(10.0, 1000),
new SubRegionCollisionInfo(20.0, 2000),
new SubRegionCollisionInfo(30.0, 3000)
});
// write data for second photon (exit DP and collision info)
dbWriter.Write(new PhotonDataPoint(
new Position(4, 5, 6),
new Direction(1, 0, 0),
0.50,
100,
PhotonStateType.None));
collisionDbWriter.Write(new CollisionInfo(numberOfSubRegions)
{
new SubRegionCollisionInfo(40.0, 4000),
new SubRegionCollisionInfo(50.0, 5000),
new SubRegionCollisionInfo(60.0, 6000)
});
}

// read the database from file, and verify the correct number of photons were written
var dbCloned = pMCDatabase.FromFile(photonDbFilename, collisionDbFilename);

Expand All @@ -95,41 +91,41 @@ public void validate_pMCDatabase_deserialized_class_is_correct_when_using_WriteT
// manually enumerate through the first two elements (same as foreach)
// PhotonDatabase is designed so you don't have to have the whole thing
// in memory, so .ToArray() loses the benefits of the lazy-load data points
var enumerator = dbCloned.DataPoints.GetEnumerator();
using var enumerator = dbCloned.DataPoints.GetEnumerator();

// advance to the first point and test that the point is valid
enumerator.MoveNext();
var dp1 = enumerator.Current;
// verify photon database entries for first photon
Assert.AreEqual(new Position(1, 2, 3),dp1.PhotonDataPoint.Position);
Assert.AreEqual(new Direction(0, 0, 1),dp1.PhotonDataPoint.Direction);
Assert.AreEqual(1.0,dp1.PhotonDataPoint.Weight);
Assert.AreEqual(10,dp1.PhotonDataPoint.TotalTime);
Assert.AreEqual(new Position(1, 2, 3), dp1?.PhotonDataPoint.Position);
Assert.AreEqual(new Direction(0, 0, 1), dp1.PhotonDataPoint.Direction);
Assert.AreEqual(1.0, dp1.PhotonDataPoint.Weight);
Assert.AreEqual(10, dp1.PhotonDataPoint.TotalTime);
Assert.IsTrue(dp1.PhotonDataPoint.StateFlag.HasFlag(PhotonStateType.None));
// verify collision info for first photon
Assert.AreEqual(10.0,dp1.CollisionInfo[0].PathLength);
Assert.AreEqual(1000,dp1.CollisionInfo[0].NumberOfCollisions);
Assert.AreEqual(20.0,dp1.CollisionInfo[1].PathLength);
Assert.AreEqual(2000,dp1.CollisionInfo[1].NumberOfCollisions);
Assert.AreEqual(30.0,dp1.CollisionInfo[2].PathLength);
Assert.AreEqual(3000,dp1.CollisionInfo[2].NumberOfCollisions);
Assert.AreEqual(10.0, dp1.CollisionInfo[0].PathLength);
Assert.AreEqual(1000, dp1.CollisionInfo[0].NumberOfCollisions);
Assert.AreEqual(20.0, dp1.CollisionInfo[1].PathLength);
Assert.AreEqual(2000, dp1.CollisionInfo[1].NumberOfCollisions);
Assert.AreEqual(30.0, dp1.CollisionInfo[2].PathLength);
Assert.AreEqual(3000, dp1.CollisionInfo[2].NumberOfCollisions);

// advance to the second point and test that the point is valid
enumerator.MoveNext();
// verify photon database entries for second photon
var dp2 = enumerator.Current;
Assert.AreEqual(new Position(4, 5, 6),dp2.PhotonDataPoint.Position);
Assert.AreEqual(new Direction(1, 0, 0),dp2.PhotonDataPoint.Direction);
Assert.AreEqual(0.5,dp2.PhotonDataPoint.Weight);
Assert.AreEqual(100,dp2.PhotonDataPoint.TotalTime);
Assert.AreEqual(new Position(4, 5, 6), dp2?.PhotonDataPoint.Position);
Assert.AreEqual(new Direction(1, 0, 0), dp2.PhotonDataPoint.Direction);
Assert.AreEqual(0.5, dp2.PhotonDataPoint.Weight);
Assert.AreEqual(100, dp2.PhotonDataPoint.TotalTime);
Assert.IsTrue(dp2.PhotonDataPoint.StateFlag.HasFlag(PhotonStateType.None));
// verify collision info for second photon
Assert.AreEqual(40.0,dp2.CollisionInfo[0].PathLength);
Assert.AreEqual(4000,dp2.CollisionInfo[0].NumberOfCollisions);
Assert.AreEqual(50.0,dp2.CollisionInfo[1].PathLength);
Assert.AreEqual(5000,dp2.CollisionInfo[1].NumberOfCollisions);
Assert.AreEqual(60.0,dp2.CollisionInfo[2].PathLength);
Assert.AreEqual(6000,dp2.CollisionInfo[2].NumberOfCollisions);
Assert.AreEqual(40.0, dp2.CollisionInfo[0].PathLength);
Assert.AreEqual(4000, dp2.CollisionInfo[0].NumberOfCollisions);
Assert.AreEqual(50.0, dp2.CollisionInfo[1].PathLength);
Assert.AreEqual(5000, dp2.CollisionInfo[1].NumberOfCollisions);
Assert.AreEqual(60.0, dp2.CollisionInfo[2].PathLength);
Assert.AreEqual(6000, dp2.CollisionInfo[2].NumberOfCollisions);
}
}
}

0 comments on commit 4633d56

Please sign in to comment.