using System; using System.Data; using System.IO; using System.Linq; using DelftTools.Utils.Remoting; using NUnit.Framework; using SharpTestsEx; using log4net; using log4net.Config; namespace DeltaShell.Plugins.XBeach.Api.Tests.Api { [TestFixture] //[Ignore] // crashes on build server, make sure all tests run public class XBeachApiTest { private static readonly ILog log = LogManager.GetLogger(typeof(XBeachApiTest)); private IXBeachApi xbeach; private bool useRemoting = false; // NOTE: set this one to true if you want to step into FORTRAN code and debug ....! [TestFixtureSetUp] public void TestFixtureSetUp() { BasicConfigurator.Configure(); } [SetUp] public void SetUp() { if (useRemoting) { // starts a new server and returns remote instance or use a local version if useremoting is false xbeach = RemoteInstanceContainer.CreateInstance(); } else { xbeach = new XBeachApi(); } var testDir = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())), "XBeachApiTest"); xbeach.Initialize(testDir); log.Info("Initializing ..."); } [TearDown] public void TearDown() { if (useRemoting) { // kills process RemoteInstanceContainer.RemoveInstance(xbeach); } else { xbeach.Dispose(); } } [Test] public void GetGrid() { CurvilinearGrid grid = xbeach.Grid; grid.SizeN .Should("size of 1st dimension of the grid").Be.EqualTo(3); grid.SizeM .Should("size of 2nd dimension of the grid").Be.EqualTo(179); grid.X.Length .Should("x size").Be.EqualTo(3 * 179); grid.Y.Length .Should("y size").Be.EqualTo(3 * 179); } [Test] [ExpectedException(typeof(ConstraintException), ExpectedMessage = "Grid n size doesn't match.")] public void SetGrid() { CurvilinearGrid grid = xbeach.Grid; grid = grid.Clone(); grid.X.Length .Should("x size").Be.EqualTo(3 * 179); grid.Y.Length .Should("y size").Be.EqualTo(3 * 179); for (int i = 0; i < grid.X.Length; i++) { grid.X[i] = grid.X[i] + 1; } xbeach.Grid = grid; CurvilinearGrid newgrid = grid.Clone(); newgrid.SizeN = 2; xbeach.Grid = newgrid; } [Test] public void SetBathymetry() { double[] updatedBathymetry = new double[3*179]; for (int i = 0; i < updatedBathymetry.Length; i++) { updatedBathymetry[i] = 0; } xbeach.Bathymetry = updatedBathymetry; } [Test] public void GetGridUsingRemoteProcess() { CurvilinearGrid grid = xbeach.Grid; log.DebugFormat("Grid dimensions: {0}x{1}", grid.SizeN, grid.SizeM); } [Test] public void GetBathymetry() { double[] bathymetry = xbeach.Bathymetry; bathymetry.Length .Should("Length of bathymetry").Be.EqualTo(xbeach.Grid.Length); } [Test] public void GetDoubleOutputArray() { double[] waveEnergy0 = xbeach.GetDoubleOutputArray("H"); waveEnergy0.Length .Should().Be.GreaterThan(0); xbeach.ExecuteStep(); double[] waveEnergy = xbeach.GetDoubleOutputArray("H"); waveEnergy.Length .Should("Length of output array").Be.EqualTo(xbeach.Grid.Length); xbeach.ExecuteStep(); double[] waveEnergyStep2 = xbeach.GetDoubleOutputArray("H"); waveEnergyStep2.Average() .Should("Length of output array").Not.Be.EqualTo(waveEnergy.Average()); } [Test] [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Parameter \"wrongname\" not found")] public void GetOutputArrayThowsOnWrongOutputName() { xbeach.ExecuteStep(); double[] bathymetry = xbeach.GetDoubleOutputArray("wrongname"); } [Test] public void GetCurrentTime() { // Assert that the current time is 0 when the model is just started xbeach.CurrentTime .Should().Be.EqualTo(new DateTime(1970, 1, 1)); } [Test] public void ExecuteStepIncrementsCurrentTime() { xbeach.ExecuteStep(); xbeach.CurrentTime .Should().Be.GreaterThan(new DateTime(1970, 1, 1)); } [Test] public void SetBathymetryAndExecuteStep() { xbeach.ExecuteStep(); double[] initialBathymetry = xbeach.Bathymetry; double[] updatedBathymetry = (double[]) initialBathymetry.Clone(); for (int i = 0; i < updatedBathymetry.Length; i++) { updatedBathymetry[i]+=1; } xbeach.Bathymetry = updatedBathymetry; xbeach.ExecuteStep(); log.InfoFormat("Average bathymetry before update: {0}", initialBathymetry.Average()); log.InfoFormat("Average bathymetry after update: {0}", xbeach.Bathymetry.Average()); xbeach.Bathymetry.Average() .Should("updated bathymetry is used by model").Be.GreaterThan(initialBathymetry.Average()); } [Test] public void SetGridExecuteStep() { xbeach.ExecuteStep(); CurvilinearGrid newgrid = xbeach.Grid.Clone(); for (int i = 0; i < newgrid.X.Length; i++) { newgrid.X[i] += 1; } xbeach.Grid = newgrid; xbeach.ExecuteStep(); } [Test] public void GetParameter() { foreach (string name in xbeach.GetParameterNames()) { double value = xbeach.GetDoubleParameter(name); switch (name) { case ("tstart"): value.Should().Be.EqualTo(100); break; case ("morstart"): value.Should().Be.EqualTo(100); break; case ("morfac"): value.Should().Be.EqualTo(1); break; default: break; } } double morph = xbeach.GetIntParameter("morphology"); morph.Should().Be.EqualTo(1); } } }