using System.IO; using NUnit.Framework; using System.Runtime.InteropServices; namespace XBeach.OpenMI.Tests { [TestFixture] public class libXBeachTest { #region Setup/Teardown [SetUp] public void Setup() { // Go to the test directory const string testDir = "../../../../test2"; Directory.SetCurrentDirectory(testDir); } [TearDown] public void TearDown() { // This should print the contents of the XBerror file /* var file = new FileInfo("XBerror.txt"); if (file.Exists) { var sr = new StreamReader("XBerror.txt"); // I want this in the unit output Console.WriteLine(sr.ReadToEnd()); } */ } #endregion [Test] public void TestFinalize() { // Assume that we can call init and then finalize int result; result = libXBeach.init(); result = libXBeach.finalize(); Assert.AreEqual(result, 0); } [Test] public void TestGetArray() { // Assume we can get variable "zb" int result = 1; result = libXBeach.init(); string name = "zb"; var zb = new FArrayType(); result = libXBeach.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 FArrayType(); result = libXBeach.getarray(name, ref ceqbg, name.Length); Assert.AreEqual(ceqbg.rank, 3); Assert.AreEqual(result, 0); } [Test] public void TestGet1dDoubleArray() { // Assume we can get variable "zb" int result = 1; result = libXBeach.init(); int nx = 0; int ny = 0; result = libXBeach.getintparameter("nx", ref nx, 2); result = libXBeach.getintparameter("ny", ref ny, 2); string name = "xz"; var xz = new double[nx+1]; result = libXBeach.get1ddoublearray(name, ref xz, name.Length); Assert.AreEqual(0, xz[0]); Assert.AreEqual(0, result); } [Test] public void TestGet2dDoubleArray() { // Assume we can get variable "zb" int result = 1; result = libXBeach.init(); int nx = 0; int ny = 0; result = libXBeach.getintparameter("nx", ref nx, 2); result = libXBeach.getintparameter("ny", ref ny, 2); string name = "zb"; var zb = new double[nx+1,ny+1]; result = libXBeach.get2ddoublearray(name, ref zb, name.Length); Assert.AreEqual(0, zb[0,0]); Assert.AreEqual(0, result); } [Test] public void TestGetDoubleParameter() { // Assume that we can get a parameter "t" int result = 1; result = libXBeach.init(); double t0 = 0.0; double t1 = 0.0; string param = "t"; result = libXBeach.getparameter(param, ref t0); result = libXBeach.executestep(); result = libXBeach.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 = libXBeach.getparameter(param, ref t0); Assert.Greater(t0, 0); // If we ask for another parameter we get -99 param = "q"; result = libXBeach.getparameter(param, ref t0); Assert.AreEqual(t0, -99); } [Test] public void TestGetIntParameter() { // Assume that we can get a parameter "t" int result = 1; result = libXBeach.init(); int t0 = 0; int t1 = 0; string param = "nx"; result = libXBeach.getparameter(param, ref t0); result = libXBeach.executestep(); result = libXBeach.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 = libXBeach.getparameter(param, ref t0); Assert.AreEqual(t0, -99); } [Test] public void TestInit() { // Test if we can do the init step int result = libXBeach.init(); Assert.AreEqual(result, 0); } [Test] public void TestOutput() { // Test if we can call init and then do a output int result; result = libXBeach.init(); result = libXBeach.outputext(); Assert.AreEqual(result, 0); } [Test] public void TestExecuteTimestep() { // Test if we can output, do a timestep and then output again int result; result = libXBeach.init(); result = libXBeach.executestep(); Assert.AreEqual(result, 0); result = libXBeach.finalize(); Assert.AreEqual(result, 0); } [Test] public void TestGeometry() { int result = -1; result = libXBeach.init(); result = libXBeach.executestep(); string param = "nx"; int nx = 0; result = libXBeach.getparameter(param, ref nx); Assert.Greater(nx, 0); param = "ny"; int ny = 0; result = libXBeach.getparameter(param, ref ny); Assert.Greater(ny, 0); param = "x"; FArrayType x = new FArrayType(); result = libXBeach.getarray(param, ref x); param = "y"; FArrayType y = new FArrayType(); result = libXBeach.getarray(param, ref y); // Todo use arrays from x,y Assert.AreEqual(0, result); } } }