From ba899579700b13004beb596932383db7f742b849 Mon Sep 17 00:00:00 2001 From: vpuser Date: Mon, 11 Sep 2023 19:53:14 -0700 Subject: [PATCH 1/2] Added a "using" when accessing the DataPoints so it hopefully disposes correctly and fixes the TearDown error --- .../PhotonData/CollisionInfoDatabaseTests.cs | 28 ++-- .../MonteCarlo/PhotonData/pMCDatabaseTests.cs | 120 +++++++++--------- 2 files changed, 71 insertions(+), 77 deletions(-) diff --git a/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs b/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs index 088293f04..71bdd419f 100644 --- a/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs +++ b/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs @@ -13,7 +13,7 @@ public class CollisionInfoDatabaseTests /// /// list of temporary files created by these unit tests /// - readonly List listOfTestGeneratedFiles = new List() + private readonly List _listOfTestGeneratedFiles = new() { "testcollisioninfodatabase", "testcollisioninfodatabase.txt" @@ -24,12 +24,11 @@ public class CollisionInfoDatabaseTests /// [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); } } @@ -39,10 +38,10 @@ public void clear_folders_and_files() /// are working correctly. /// [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 @@ -56,11 +55,10 @@ public void validate_CollisionInfo_deserialized_class_is_correct_when_using_Writ // 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), @@ -68,7 +66,7 @@ public void validate_CollisionInfo_deserialized_class_is_correct_when_using_Writ }); dbWriter.Write( - new CollisionInfo(numberOfSubregions) + new CollisionInfo(numberOfSubRegions) { new SubRegionCollisionInfo(40.0, 4000), new SubRegionCollisionInfo(50.0, 5000), @@ -76,15 +74,15 @@ public void validate_CollisionInfo_deserialized_class_is_correct_when_using_Writ }); } - 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(); diff --git a/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs b/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs index f031b4b53..f73a38207 100644 --- a/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs +++ b/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs @@ -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; @@ -15,7 +14,7 @@ public class pMCDatabaseTests /// /// list of temporary files created by these unit tests /// - readonly List listOfTestGeneratedFiles = new List() + private readonly List _listOfTestGeneratedFiles = new() { "testpmcdatabase", "testpmcdatabase.txt", @@ -29,12 +28,11 @@ public class pMCDatabaseTests /// [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); } } @@ -43,50 +41,48 @@ public void clear_folders_and_files() /// are working correctly. /// [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); @@ -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); } } } From 23c132fca080bf17ba5e6a7476047654d5f2514c Mon Sep 17 00:00:00 2001 From: vpuser Date: Mon, 11 Sep 2023 20:20:10 -0700 Subject: [PATCH 2/2] Fixed possible null errors in the tests --- .../PhotonData/CollisionInfoDatabaseTests.cs | 37 ++++++++++--------- .../MonteCarlo/PhotonData/pMCDatabaseTests.cs | 4 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs b/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs index 71bdd419f..9ca0886a3 100644 --- a/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs +++ b/src/Vts.Test/MonteCarlo/PhotonData/CollisionInfoDatabaseTests.cs @@ -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; @@ -42,20 +41,20 @@ public void Validate_CollisionInfo_deserialized_class_is_correct_when_using_Writ { 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( // 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) @@ -87,22 +86,24 @@ public void Validate_CollisionInfo_deserialized_class_is_correct_when_using_Writ // 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); } } } diff --git a/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs b/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs index f73a38207..8f1ee3f57 100644 --- a/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs +++ b/src/Vts.Test/MonteCarlo/PhotonData/pMCDatabaseTests.cs @@ -97,7 +97,7 @@ public void Validate_pMCDatabase_deserialized_class_is_correct_when_using_WriteT 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 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); @@ -114,7 +114,7 @@ public void Validate_pMCDatabase_deserialized_class_is_correct_when_using_WriteT 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 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);