using System; using System.IO; using System.Runtime.InteropServices; using DeltaShell.Plugins.XBeach.Api; using NUnit.Framework; using SharpTestsEx; namespace XBeachCSharpTest { [TestFixture] [Ignore] public class XBeachDllTest { private string currentDirectory; #region Setup/Teardown [TestFixtureSetUp] public void TestFixtureSetUp() { currentDirectory = Directory.GetCurrentDirectory(); // TODO: COPY TO TEST DIRECTRY INSTEAD OF RUNNING IN test-data var testDir = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(currentDirectory)), "XBeachApiTest"); Directory.SetCurrentDirectory(testDir); } [TestFixtureTearDown] public void TestFixtureTearDown() { Directory.SetCurrentDirectory(currentDirectory); } [SetUp] public void SetUp() { } [TearDown] public void TearDown() { /* XBlog.txt XBwarning.txt */ // This should print the contents of the XBerror file var file = new FileInfo("XBerror.txt"); if (file.Exists && file.Length > 0) { var sr = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.None); // I want this in the unit output Console.WriteLine(new StreamReader(sr).ReadToEnd()); } } #endregion [Test] public void TestExecuteTimestep() { // Test if we can output, do a timestep and then output again int result; result = XBeachDll.init(); result = XBeachDll.executestep(); Assert.AreEqual(result, 0); result = XBeachDll.finalize(); Assert.AreEqual(result, 0); } [Test] public void TestFinalize() { // Assume that we can call init and then finalize int result; result = XBeachDll.init(); result = XBeachDll.finalize(); Assert.AreEqual(result, 0); } [Test] public void TestGet1dDoubleArray() { // Assume we can get variable "zb" int result = 1; result = XBeachDll.init(); string name = "xyzs01"; var xyzs01 = new double[2]; result = XBeachDll.get1ddoublearray(name, ref xyzs01); Assert.AreEqual(0, xyzs01[0]); Assert.AreEqual(0, result); } [Test] public void TestGet2dDoubleArray() { // Assume we can get variable "zb" int result = 1; result = XBeachDll.init(); int nx = 0; int ny = 0; result = XBeachDll.getintparameter("nx", ref nx, 2); result = XBeachDll.getintparameter("ny", ref ny, 2); string name = "zb"; IntPtr cptr = new IntPtr(); double[] zbflat = new double[(ny + 1) * (nx + 1)]; result = XBeachDll.get2ddoublearray("zb", ref cptr, 2); Marshal.Copy(cptr, zbflat, 0, zbflat.GetLength(0)); double[,] zb = new double[ny + 1, nx + 1]; Buffer.BlockCopy(zbflat, 0, zb, 0, sizeof(double) * zb.Length); Assert.AreEqual(-3.394, zb[0, 0]); Assert.AreEqual(0, result); } [Test] public void TestGetNonExisting2dDoubleArray() { // Assume we can get a -1 when we get the non existing variable "qqq" int result = 1; result = XBeachDll.init(); int nx = 0; int ny = 0; result = XBeachDll.getintparameter("nx", ref nx, 2); result = XBeachDll.getintparameter("ny", ref ny, 2); string name = "qqq"; IntPtr cptr = new IntPtr(); result = XBeachDll.get2ddoublearray(name, ref cptr, name.Length); result.Should().Be.EqualTo(-1); } [Test] public void TestSet2dDoubleArray() { // Assume we can get variable "zb" int result = 1; result = XBeachDll.init(); int nx = 0; int ny = 0; result = XBeachDll.getintparameter("nx", ref nx, 2); result = XBeachDll.getintparameter("ny", ref ny, 2); string name = "zb"; double[,] zb = new double[(ny + 1),(nx + 1)]; for (int i = 0; i < zb.GetLength(1); i++) { for (int j = 0; j < zb.GetLength(0); j++) { zb[j,i] = i*zb.GetLength(0) + j; } } double[] zbflat = new double[zb.Length]; // Manually copy all data over int size = zbflat.Length*sizeof (double); Buffer.BlockCopy(zb, 0, zbflat, 0, size); // Allocate memory IntPtr cptr = Marshal.AllocHGlobal(size); // Copy data to the pointer Marshal.Copy(zbflat, 0, cptr, zbflat.Length); // Send over data to xbeach. result = XBeachDll.set2ddoublearray("zb", ref cptr, 2); result.Should().Be.EqualTo(0); } [Test] public void TestGetArray() { // Assume we can get variable "zb" int result = 1; result = XBeachDll.init(); string name = "zb"; var zb = new XBeachDll.FArrayType(); result = XBeachDll.getarray(name, ref zb, name.Length); Assert.AreEqual(2, zb.rank); // You can use the pointer in here. var b = new double[3]; Marshal.Copy(zb.array, b, 0, b.Length); Assert.AreEqual(1.0, b[0]); name = "ceqbg"; var ceqbg = new XBeachDll.FArrayType(); result = XBeachDll.getarray(name, ref ceqbg, name.Length); Assert.AreEqual(ceqbg.rank, 3); Assert.AreEqual(result, 0); } [Test] public void TestGetDoubleParameter() { // Assume that we can get a parameter "t" int result = 1; result = XBeachDll.init(); double t0 = 0.0; double t1 = 0.0; string param = "t"; result = XBeachDll.getparameter(param, ref t0); result = XBeachDll.executestep(); result = XBeachDll.getparameter(param, ref t1); // Assume that t is 0 at the first time step Assert.AreEqual(t0, 0.0); // And that t increased after calling timestep Assert.Greater(t1, t0); Assert.AreEqual(result, 0); param = "tstop"; result = XBeachDll.getparameter(param, ref t0); Assert.Greater(t0, 0); // If we ask for another parameter we get -99 param = "q"; result = XBeachDll.getparameter(param, ref t0); Assert.AreEqual(result, -1); } [Test] public void TestGetIntParameter() { // Assume that we can get a parameter "t" int result = XBeachDll.init(); int t0 = 0; int t1 = 0; string param = "nx"; result = XBeachDll.getparameter(param, ref t0); result = XBeachDll.executestep(); result = XBeachDll.getparameter(param, ref t1); Assert.AreEqual(param, "nx"); // Assume that nx is >0 at the first time step Assert.Greater(t0, 0); // And that nx is the same after calling timestep Assert.AreEqual(t1, t0); Assert.AreEqual(result, 0); // If we ask for another parameter we get -99 param = "q"; result = XBeachDll.getparameter(param, ref t0); Assert.AreEqual(-1, result,"Parameter not found"); Assert.AreEqual(178, t0,"Parameter remains unchanged"); } [Test] public void TestInit() { // Test if we can do the init step int result = XBeachDll.init(); Assert.AreEqual(result, 0); } [Test] public void TestOutput() { // Test if we can call init and then do a output int result = XBeachDll.init(); result = XBeachDll.outputext(); Assert.AreEqual(result, 0); } [Test] public void TestLoggingEvent() { var count = 0; var del = new XBeachDll.MyDelegate((ref int code,string message, ref int len) => TestLogMessage(code,message,len,ref count, 10)); XBeachDll.assignlogdelegate(del); XBeachDll.writetolog(); Assert.AreEqual(10,count); } [Test] public void TestLoggingWhileInitializing() { var count = 0; var del = new XBeachDll.MyDelegate((ref int code, string message, ref int len) => TestLogMessage(code, message,len, ref count, 3)); XBeachDll.assignlogdelegate(del); var result = XBeachDll.init(); Assert.AreEqual(0, result); Assert.AreEqual(304, count); } [Test] public void LoggingShouldNotCrash() { var count = 0; var del = new XBeachDll.MyDelegate((ref int code, string message, ref int len) => TestLogMessage(code, message, len, ref count, 3)); XBeachDll.assignlogdelegate(del); XBeachDll.assignlogdelegate(null); var result = XBeachDll.init(); Assert.AreEqual(0,result); Assert.AreEqual(0,count); } private static void TestLogMessage(int code, string message,int len, ref int count, int maxCode) { Assert.LessOrEqual(code,maxCode); count++; Console.WriteLine(string.Format("{0} - {1}",code,message.Substring(0,len))); } } }