public void Equals_ComparedToObjectWithDifferentValuesReadFromR_ReturnsFalse() { using (var service = new Rservice()) { // Arrange var value1A = service.RConnection["matrix( c( 1.1 , 2.6 , NA , -4.2 , -5.8 , 6.3 ) , ncol = 3 , byrow = TRUE )"]; var value1B = new SexpArrayDouble(new[] { 1.1, -4.2, 2.6, -5.8, double.NaN, 6.3 }); var value2A = service.RConnection["c( -4.4 , -2.2 , -1 )"]; var value2B = new SexpArrayDouble(new[] { -4.4, -2.2, -1 }); var value3A = service.RConnection["-.9"]; var value3B = new SexpArrayDouble(-.9); var value4A = service.RConnection["numeric()"]; var value4B = new SexpArrayDouble(); // Act & Assert Assert.False(value3B.Equals(value2A)); Assert.False(value2A.Equals(value4A)); Assert.False(value3B.Equals(value2B)); Assert.False(value4A.Equals(value1B)); Assert.False(value3B.Equals(value1A)); Assert.False(value4A.Equals(value3A)); Assert.False(value1A.Equals(value2B)); Assert.False(value2A.Equals(value3A)); Assert.False(value3A.Equals(value1A)); Assert.False(value1B.Equals(value4B)); } }
public void Equals_ComparedToObjectWithDifferentValuesReadFromR_ReturnsFalse() { using (var service = new Rservice()) { // Arrange var value1A = service.RConnection["matrix( as.integer( c( NA , 2 , 3 , -4 , -5 , 6 ) ) , ncol = 3 , byrow = TRUE )"]; var value1B = new SexpArrayInt(new[] { SexpArrayInt.Na, -4, 2, -5, 3, 6 }); var value2A = service.RConnection["as.integer( c( -4 , -2 , -1 ) )"]; var value2B = new SexpArrayInt(new[] { -4, -2, -1 }); var value3A = service.RConnection["as.integer( -9 )"]; var value3B = new SexpArrayInt(-9); var value4A = service.RConnection["integer()"]; var value4B = new SexpArrayInt(); // Act & Assert Assert.False(value3B.Equals(value2A)); Assert.False(value2A.Equals(value4A)); Assert.False(value3B.Equals(value2B)); Assert.False(value4A.Equals(value1B)); Assert.False(value3B.Equals(value1A)); Assert.False(value4A.Equals(value3A)); Assert.False(value1A.Equals(value2B)); Assert.False(value2A.Equals(value3A)); Assert.False(value3A.Equals(value1A)); Assert.False(value1B.Equals(value4B)); } }
public void Various_ListTest() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var mylist = new Dictionary <string, object> { { "One", 1 }, { "Two", 2.0 }, { "Three", "three" }, { "Four", true }, { "Five", new DateTime(2012, 01, 01) } }; var x1 = Sexp.Make(mylist); service.RConnection["x1"] = x1; Assert.Equal(x1.Count, mylist.Count); service.RConnection.Eval("x2 <- list(One=1,Two=2.0,Three='three',Four=TRUE,Five=as.Date('2012-01-01'))"); var x2 = service.RConnection["x2"]; Assert.Equal(x1.Count, x2.Count); for (int i = 0; i < x1.Count; i++) { Assert.True(x1[i].Equals(x2[i])); Assert.Equal(x1.Names[i], x2.Names[i]); Assert.True(x1[x1.Names[i]].Equals(x2[x2.Names[i]])); } } }
public void Various_Bool() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var sexpTrue = new SexpArrayBool(true); var sexpFalse = new SexpArrayBool(false); var sexpNa = new SexpArrayBool(( bool? )null); // ReSharper disable EqualExpressionComparison Assert.True(sexpTrue.Equals(sexpTrue)); Assert.True(sexpFalse.Equals(sexpFalse)); Assert.True(sexpNa.Equals(sexpNa)); // ReSharper restore EqualExpressionComparison Assert.True(!sexpTrue.Equals(sexpFalse)); Assert.True(!sexpTrue.Equals(sexpNa)); Assert.True(sexpTrue.Equals(true)); Assert.True(sexpFalse.Equals(false)); Assert.True(!sexpNa.Equals(true)); Assert.True(!sexpNa.Equals(false)); foreach (var a in new Sexp[] { sexpTrue, sexpFalse, sexpNa }) { Assert.True(!a.Equals(new SexpNull())); } service.RConnection["x.bool"] = Sexp.Make(true); } }
public void Various_LinearAlgebraFunctionsOf2DArrayDouble_MeetsExpectation() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { // Same as for integers -- we'll divide by two to get floating point values that aren't integers var matA = new double[, ] { { 14, 9, 3 }, { 2, 11, 15 }, { 0, 12, 17 }, { 5, 2, 3 } }; var matB = new double[, ] { { 12, 25 }, { 9, 10 }, { 8, 5 } }; var matC = new double[, ] { { 273, 455 }, { 243, 235 }, { 244, 205 }, { 102, 160 } }; var sexpA = Sexp.Make(matA); service.RConnection["a"] = sexpA; service.RConnection["b"] = Sexp.Make(matB); // Some simple tests with A for (int i = 0; i <= 1; i++) { Assert.Equal(matA.GetLength(i), sexpA.GetLength(i)); Assert.Equal(matA.GetLength(i), service.RConnection["a"].GetLength(i)); } for (var row = 0; row < matA.GetLength(0); row++) { for (var col = 0; col < matA.GetLength(1); col++) { Assert.Equal(matA[row, col], sexpA[row, col].AsDouble); Assert.Equal(matA[row, col], service.RConnection["a"][row, col].AsDouble); } } var matD = service.RConnection["a %*% b"]; // check that C and D are equal for (var i = 0; i <= 1; i++) { Assert.Equal(matC.GetLength(i), matD.GetLength(i)); } for (var row = 0; row < matC.GetLength(0); row++) { for (var col = 0; col < matD.GetLength(1); col++) { Assert.Equal(matC[row, col], matD[row, col].AsDouble); } } } }
public void IsNa_NaValueReadFromR_ReturnTrue() { using (var rWrapper = new Rservice()) { // Arrange & Act Sexp naSexp = rWrapper.RConnection["as.numeric( NA )"]; // Assert Assert.IsType <SexpArrayDouble>(naSexp); Assert.True(naSexp.IsNa); } }
public void Various_ArrayIntTests() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var testInts = new[] { -3, 0, 1, 2, 524566, 0 }; var x1 = Sexp.Make(testInts); x1[x1.Count - 1] = new SexpArrayInt(SexpArrayInt.Na); for (int i = 0; i < x1.Count; i++) { if (!x1[i].IsNa) { Assert.Equal(testInts[i], x1[i].AsInt); } } service.RConnection.Eval("x2 <- as.integer(c(-3,0,1,2,524566,NA))"); var x2 = service.RConnection["x2"]; Assert.Equal(x1.Count, x2.Count); for (int i = 0; i < x1.Count; i++) { if (x1[i].IsNa) { Assert.True(x2[i].IsNa); } else { Assert.True(x1[i].AsDouble == x2[i].AsDouble); } } service.RConnection["x1"] = x1; var equals = service.RConnection["x1 == x2"]; Assert.Equal(x1.Count, equals.Count); for (int i = 0; i < x1.Count; i++) { if (!x1[i].IsNa) { Assert.True(( bool )equals[i].AsBool, equals.ToString()); } } Assert.Equal(x1.IndexOf(new SexpArrayInt(1)), 2); x1.AsList[0] = -5; Assert.Equal(x1[0].AsInt, -5); } }
public void Various_ArrayBool() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { var testvals = new bool?[] { true, false, null }; var x1 = new SexpArrayBool(testvals); Assert.Equal(testvals.Length, x1.Count); service.RConnection["x1"] = x1; for (int i = 0; i < x1.Count; i++) { Assert.Equal(new SexpArrayBool(testvals[i]).AsBool, x1[i].AsBool); } service.RConnection.Eval("x2 <- as.logical(c(TRUE,FALSE,NA))"); var x2 = service.RConnection["x2"]; Assert.Equal(x1.Count, x2.Count); for (int i = 0; i < x1.Count; i++) { if (x1[i].IsNa) { Assert.True(x2[i].IsNa); } else { Assert.Equal(x1[i].AsBool, x2[i].AsBool); } } var equals = service.RConnection["x1 == x2"]; Assert.Equal(x1.Count, equals.Count); for (int i = 0; i < x1.Count; i++) { if (!x1[i].IsNa) { Assert.True(( bool )(equals[i].AsBool), equals.ToString()); } } Assert.Equal(x1.IndexOf(Sexp.Make(false)), 1); x1.AsList[0] = false; Assert.Equal(x1[0].AsBool, false); } }
public void AsBools_WriteSexpArrayBoolToRAndReadBack_ReturnsArrayOfNullableBools() { using ( var service = new Rservice() ) { // Arrange Sexp bool1 = new SexpArrayBool(); Sexp bool2 = new SexpArrayBool( true ); Sexp bool3 = new SexpArrayBool( false ); Sexp bool4 = new SexpArrayBool( ( bool? )null ); Sexp bool5 = new SexpArrayBool( new bool?[] { true } ); Sexp bool6 = new SexpArrayBool( new bool?[] { false } ); Sexp bool7 = new SexpArrayBool( new bool?[] { null } ); Sexp bool8 = new SexpArrayBool( new bool?[] { true , null , false } ); Sexp bool9 = new SexpArrayBool( new bool?[] { true , false , null , true , false , null } ); bool9.Attributes.Add( "dim" , Sexp.Make( new[] { 2 , 3 } ) ); // Act service.RConnection[ "bool1" ] = bool1; service.RConnection[ "bool2" ] = bool2; service.RConnection[ "bool3" ] = bool3; service.RConnection[ "bool4" ] = bool4; service.RConnection[ "bool5" ] = bool5; service.RConnection[ "bool6" ] = bool6; service.RConnection[ "bool7" ] = bool7; service.RConnection[ "bool8" ] = bool8; service.RConnection[ "bool9" ] = bool9; bool1 = service.RConnection[ "bool1" ]; bool2 = service.RConnection[ "bool2" ]; bool3 = service.RConnection[ "bool3" ]; bool4 = service.RConnection[ "bool4" ]; bool5 = service.RConnection[ "bool5" ]; bool6 = service.RConnection[ "bool6" ]; bool7 = service.RConnection[ "bool7" ]; bool8 = service.RConnection[ "bool8" ]; bool9 = service.RConnection[ "bool9" ]; // Assert Assert.Equal( new bool?[] { } , bool1.AsBools ); Assert.Equal( new bool?[] { true } , bool2.AsBools ); Assert.Equal( new bool?[] { false } , bool3.AsBools ); Assert.Equal( new bool?[] { null } , bool4.AsBools ); Assert.Equal( new bool?[] { true } , bool5.AsBools ); Assert.Equal( new bool?[] { false } , bool6.AsBools ); Assert.Equal( new bool?[] { null } , bool7.AsBools ); Assert.Equal( new bool?[] { true , null , false } , bool8.AsBools ); Assert.Equal( new bool?[] { true , false , null , true , false , null } , bool9.AsBools ); } }
public void AsDoubles_SexpConstructedFromR_ReturnsSameSetOfDoubles() { using (var service = new Rservice()) { // Arrange & Act Sexp sexp1 = service.RConnection["numeric()"]; Sexp sexp2 = service.RConnection["c( 1.1 , 2.1 , 3.1 )"]; Sexp sexp3 = service.RConnection["matrix( c( 1.1 , NA , 3.1 , 4.1 , 5.1 , 6.1 ) , nrow = 2 )"]; // Assert Assert.Equal(new double[] { }, sexp1.AsDoubles); Assert.Equal(new[] { 1.1, 2.1, 3.1 }, sexp2.AsDoubles); Assert.Equal(new[] { 1.1, double.NaN, 3.1, 4.1, 5.1, 6.1 }, sexp3.AsDoubles); } }
public void AsInts_SexpConstructedFromR_ReturnsSameSetOfInts() { using (var service = new Rservice()) { // Arrange & Act Sexp sexp1 = service.RConnection["integer()"]; Sexp sexp2 = service.RConnection["as.integer( c( 1 , 2 , 3 ) )"]; Sexp sexp3 = service.RConnection["matrix( as.integer( c( 1 , 2 , 3 , 4 , 5 , NA ) ) , nrow = 2 )"]; // Assert Assert.Equal(new int[] { }, sexp1.AsInts); Assert.Equal(new[] { 1, 2, 3 }, sexp2.AsInts); Assert.Equal(new[] { 1, 2, 3, 4, 5, SexpArrayInt.Na }, sexp3.AsInts); } }
public void AsBools_WriteSexpArrayBoolToRAndReadBack_ReturnsArrayOfNullableBools() { using (var service = new Rservice()) { // Arrange Sexp bool1 = new SexpArrayBool(); Sexp bool2 = new SexpArrayBool(true); Sexp bool3 = new SexpArrayBool(false); Sexp bool4 = new SexpArrayBool(( bool? )null); Sexp bool5 = new SexpArrayBool(new bool?[] { true }); Sexp bool6 = new SexpArrayBool(new bool?[] { false }); Sexp bool7 = new SexpArrayBool(new bool?[] { null }); Sexp bool8 = new SexpArrayBool(new bool?[] { true, null, false }); Sexp bool9 = new SexpArrayBool(new bool?[] { true, false, null, true, false, null }); bool9.Attributes.Add("dim", Sexp.Make(new[] { 2, 3 })); // Act service.RConnection["bool1"] = bool1; service.RConnection["bool2"] = bool2; service.RConnection["bool3"] = bool3; service.RConnection["bool4"] = bool4; service.RConnection["bool5"] = bool5; service.RConnection["bool6"] = bool6; service.RConnection["bool7"] = bool7; service.RConnection["bool8"] = bool8; service.RConnection["bool9"] = bool9; bool1 = service.RConnection["bool1"]; bool2 = service.RConnection["bool2"]; bool3 = service.RConnection["bool3"]; bool4 = service.RConnection["bool4"]; bool5 = service.RConnection["bool5"]; bool6 = service.RConnection["bool6"]; bool7 = service.RConnection["bool7"]; bool8 = service.RConnection["bool8"]; bool9 = service.RConnection["bool9"]; // Assert Assert.Equal(new bool?[] { }, bool1.AsBools); Assert.Equal(new bool?[] { true }, bool2.AsBools); Assert.Equal(new bool?[] { false }, bool3.AsBools); Assert.Equal(new bool?[] { null }, bool4.AsBools); Assert.Equal(new bool?[] { true }, bool5.AsBools); Assert.Equal(new bool?[] { false }, bool6.AsBools); Assert.Equal(new bool?[] { null }, bool7.AsBools); Assert.Equal(new bool?[] { true, null, false }, bool8.AsBools); Assert.Equal(new bool?[] { true, false, null, true, false, null }, bool9.AsBools); } }
public void As2DArrayInt_MatrixCreatedInR_Returns2DArrayWithProperValues() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test = matrix( as.integer( c( 1 , NA , 3 , -4 , -5 , 6 ) ) , ncol = 3 , byrow = TRUE )" ); var expected = new int[ 2 , 3 ] { { 1 , SexpArrayInt.Na , 3 } , { -4 , -5 , 6 } }; // Act Sexp matrix = service.RConnection[ "test" ]; // Assert Assert.IsType<SexpArrayInt>( matrix ); Assert.Equal( expected , matrix.As2DArrayInt ); } }
public void As2DArrayDouble_MatrixCreatedInR_Returns2DArrayWithProperValues() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test = matrix( c( 1.1 , 2.6 , 3.9 , -4.2 , -5.8 , NA ) , ncol = 3 , byrow = TRUE )" ); var expected = new double[ 2 , 3 ] { { 1.1 , 2.6 , 3.9 } , { -4.2 , -5.8 , double.NaN } }; // Act Sexp matrix = service.RConnection[ "test" ]; // Assert Assert.IsType<SexpArrayDouble>( matrix ); Assert.Equal( expected , matrix.As2DArrayDouble ); } }
public void AsDates_SexpConstructedFromR_ReturnsSameSetOfDates() { using (var service = new Rservice()) { // Arrange & Act Sexp sexp1 = service.RConnection["as.Date( '2012-01-01' )"]; Sexp sexp2 = service.RConnection["as.Date( c( '2012-01-01' , '1970-01-01' , '1950-06-08' ) )"]; service.RConnection.VoidEval("test = as.Date( c( '2012-01-01' , '1970-01-01' , '1950-06-08' ) )"); service.RConnection.VoidEval("mode( test ) = 'integer'"); Sexp sexp3 = service.RConnection["test"]; // Assert Assert.Equal(new[] { new DateTime(2012, 01, 01) }, sexp1.AsDates); Assert.Equal(new[] { new DateTime(2012, 01, 01), new DateTime(1970, 01, 01), new DateTime(1950, 06, 08) }, sexp2.AsDates); Assert.Equal(new[] { new DateTime(2012, 01, 01), new DateTime(1970, 01, 01), new DateTime(1950, 06, 08) }, sexp3.AsDates); } }
public void Various_LargeArray() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using (var service = new Rservice()) { service.RConnection.Eval("x <- 1:1000000"); var x = service.RConnection["x"]; Assert.Equal(x.Count, 1000000); for (int i = 0; i < x.Count; i++) { Assert.Equal(x[i].AsInt, i + 1); } } }
public void ColNamesGet_MatrixCreatedInRWithOnlyRowNames_ReturnsNullColNames() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test1 = matrix( c( 1 , 2 , 3 , -4 , -5 , 6 ) , ncol = 3 , byrow = TRUE , dimnames = list( c( 'RowA' , 'RowB' ) , NULL ) )" ); service.RConnection.VoidEval( "test2 = matrix( c( 1 , 2 , 3 , -4 , -5 , 6 ) , ncol = 3 , byrow = TRUE ); rownames( test2 ) = c( 'RowA' , 'RowB' )" ); // Act Sexp matrix1 = service.RConnection[ "test1" ]; Sexp matrix2 = service.RConnection[ "test2" ]; // Assert Assert.Null( matrix1.ColNames ); Assert.Null( matrix2.ColNames ); } }
public void WriteFile_TransferSmallFile_CanReadSameFileBack() { using ( var service = new Rservice() ) { // Arrange const string fileName = "myfile.dat"; byte[] originalFile = CreateAndTransferFile( service.RConnection , fileName , 100 ); // Act byte[] fileFromServer = ReadFile( service.RConnection , fileName ); // Assert Assert.Equal( originalFile , fileFromServer ); // Cleanup service.RConnection.RemoveFile( fileName ); } }
public void AsDates_SexpConstructedFromR_ReturnsSameSetOfDates() { using ( var service = new Rservice() ) { // Arrange & Act Sexp sexp1 = service.RConnection[ "as.Date( '2012-01-01' )" ]; Sexp sexp2 = service.RConnection[ "as.Date( c( '2012-01-01' , '1970-01-01' , '1950-06-08' ) )" ]; service.RConnection.VoidEval( "test = as.Date( c( '2012-01-01' , '1970-01-01' , '1950-06-08' ) )" ); service.RConnection.VoidEval( "mode( test ) = 'integer'" ); Sexp sexp3 = service.RConnection[ "test" ]; // Assert Assert.Equal( new[] { new DateTime( 2012 , 01 , 01 ) } , sexp1.AsDates ); Assert.Equal( new[] { new DateTime( 2012 , 01 , 01 ) , new DateTime( 1970 , 01 , 01 ) , new DateTime( 1950 , 06 , 08 ) } , sexp2.AsDates ); Assert.Equal( new[] { new DateTime( 2012 , 01 , 01 ) , new DateTime( 1970 , 01 , 01 ) , new DateTime( 1950 , 06 , 08 ) } , sexp3.AsDates ); } }
public void As2DArrayDouble_MatrixCreatedInR_Returns2DArrayWithProperValues() { using (var service = new Rservice()) { // Arrange service.RConnection.VoidEval("test = matrix( c( 1.1 , 2.6 , 3.9 , -4.2 , -5.8 , NA ) , ncol = 3 , byrow = TRUE )"); var expected = new double[2, 3] { { 1.1, 2.6, 3.9 }, { -4.2, -5.8, double.NaN } }; // Act Sexp matrix = service.RConnection["test"]; // Assert Assert.IsType <SexpArrayDouble>(matrix); Assert.Equal(expected, matrix.As2DArrayDouble); } }
public void ColNamesGet_MatrixCreatedInRWithColNames_ReturnsMatrixColNames() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test1 = matrix( c( 1 , 2 , 3 , -4 , -5 , 6 ) , ncol = 3 , byrow = TRUE , dimnames = list( NULL , c( 'ColA' , 'ColB' , 'ColC' ) ) )" ); service.RConnection.VoidEval( "test2 = matrix( c( 1 , 2 , 3 , -4 , -5 , 6 ) , ncol = 3 , byrow = TRUE , dimnames = list( c( 'RowA' , 'RowB' ) , c( 'ColA' , 'ColB' , 'ColC' ) ) )" ); // Act Sexp matrix1 = service.RConnection[ "test1" ]; Sexp matrix2 = service.RConnection[ "test2" ]; // Assert Assert.Equal( new[] { "ColA" , "ColB" , "ColC" } , matrix1.ColNames ); Assert.Equal( new[] { "ColA" , "ColB" , "ColC" } , matrix2.ColNames ); } }
public void WriteFile_TransferSmallFile_CanReadSameFileBack() { using (var service = new Rservice()) { // Arrange const string fileName = "myfile.dat"; byte[] originalFile = CreateAndTransferFile(service.RConnection, fileName, 100); // Act byte[] fileFromServer = ReadFile(service.RConnection, fileName); // Assert Assert.Equal(originalFile, fileFromServer); // Cleanup service.RConnection.RemoveFile(fileName); } }
public void As2DArrayInt_MatrixCreatedInR_Returns2DArrayWithProperValues() { using (var service = new Rservice()) { // Arrange service.RConnection.VoidEval("test = matrix( as.integer( c( 1 , NA , 3 , -4 , -5 , 6 ) ) , ncol = 3 , byrow = TRUE )"); var expected = new int[2, 3] { { 1, SexpArrayInt.Na, 3 }, { -4, -5, 6 } }; // Act Sexp matrix = service.RConnection["test"]; // Assert Assert.IsType <SexpArrayInt>(matrix); Assert.Equal(expected, matrix.As2DArrayInt); } }
public void ColNamesGet_DataDoesNotHaveColNames_ReturnsNull() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test1 = matrix( c( 1 , 2 , 3 , -4 , -5 , 6 ) )" ); service.RConnection.VoidEval( "test2 = c( 1 , 2 , 3 , -4 , -5 , 6 )" ); service.RConnection.VoidEval( "test3 = list( A = 1 )" ); // Act Sexp data1 = service.RConnection[ "test1" ]; Sexp data2 = service.RConnection[ "test2" ]; Sexp data3 = service.RConnection[ "test3" ]; // Assert Assert.Null( data1.ColNames ); Assert.Null( data2.ColNames ); Assert.Null( data3.ColNames ); } }
public void AsBools_ReadLogicalFromR_ReturnsArrayOfNullableBools() { using ( var service = new Rservice() ) { // Arrange / Act Sexp bool1 = service.RConnection[ "logical()" ]; Sexp bool2 = service.RConnection[ "TRUE" ]; Sexp bool3 = service.RConnection[ "FALSE" ]; Sexp bool4 = service.RConnection[ "as.logical( NA )" ]; Sexp bool5 = service.RConnection[ "c( TRUE , NA , FALSE )" ]; Sexp bool6 = service.RConnection[ "matrix( c( TRUE , NA , FALSE , FALSE , TRUE , NA ) , nrow = 2 , byrow = TRUE )" ]; // Assert Assert.Equal( new bool?[] { } , bool1.AsBools ); Assert.Equal( new bool?[] { true } , bool2.AsBools ); Assert.Equal( new bool?[] { false } , bool3.AsBools ); Assert.Equal( new bool?[] { null } , bool4.AsBools ); Assert.Equal( new bool?[] { true , null , false } , bool5.AsBools ); Assert.Equal( new bool?[] { true , false , null , true , false , null } , bool6.AsBools ); } }
public void AsBools_ReadLogicalFromR_ReturnsArrayOfNullableBools() { using (var service = new Rservice()) { // Arrange / Act Sexp bool1 = service.RConnection["logical()"]; Sexp bool2 = service.RConnection["TRUE"]; Sexp bool3 = service.RConnection["FALSE"]; Sexp bool4 = service.RConnection["as.logical( NA )"]; Sexp bool5 = service.RConnection["c( TRUE , NA , FALSE )"]; Sexp bool6 = service.RConnection["matrix( c( TRUE , NA , FALSE , FALSE , TRUE , NA ) , nrow = 2 , byrow = TRUE )"]; // Assert Assert.Equal(new bool?[] { }, bool1.AsBools); Assert.Equal(new bool?[] { true }, bool2.AsBools); Assert.Equal(new bool?[] { false }, bool3.AsBools); Assert.Equal(new bool?[] { null }, bool4.AsBools); Assert.Equal(new bool?[] { true, null, false }, bool5.AsBools); Assert.Equal(new bool?[] { true, false, null, true, false, null }, bool6.AsBools); } }
public void Command_AssignAndEvalLargeSexpArrayBool_ProperlyEncodesAndDecodesArrayOfBoolean() { using (var service = new Rservice()) { // Arrange var random = new Random(); const int count = 25497500; var sexpBool1ToEncode = new SexpArrayBool(Enumerable.Range(0, count).Select(x => { var y = random.Next(1, 4); return(y == 1 ? true : y == 2 ? false : ( bool? )null); })); var sexpBool2ToEncode = new SexpArrayBool(Enumerable.Range(0, count + 1).Select(x => { var y = random.Next(1, 4); return(y == 1 ? true : y == 2 ? false : ( bool? )null); })); var sexpBool3ToEncode = new SexpArrayBool(Enumerable.Range(0, count + 3).Select(x => { var y = random.Next(1, 4); return(y == 1 ? true : y == 2 ? false : ( bool? )null); })); var sexpBool4ToEncode = new SexpArrayBool(Enumerable.Range(0, count + 4).Select(x => { var y = random.Next(1, 4); return(y == 1 ? true : y == 2 ? false : ( bool? )null); })); // Act service.RConnection["bools1"] = sexpBool1ToEncode; service.RConnection["bools2"] = sexpBool2ToEncode; service.RConnection["bools3"] = sexpBool3ToEncode; service.RConnection["bools4"] = sexpBool4ToEncode; Sexp sexpBool1ToDecoded = service.RConnection["bools1"]; Sexp sexpBool2ToDecoded = service.RConnection["bools2"]; Sexp sexpBool3ToDecoded = service.RConnection["bools3"]; Sexp sexpBool4ToDecoded = service.RConnection["bools4"]; // Assert Assert.IsType <SexpArrayBool>(sexpBool1ToDecoded); Assert.IsType <SexpArrayBool>(sexpBool2ToDecoded); Assert.IsType <SexpArrayBool>(sexpBool3ToDecoded); Assert.IsType <SexpArrayBool>(sexpBool4ToDecoded); Assert.True(sexpBool1ToEncode.Values.SequenceEqual(sexpBool1ToDecoded.Values)); Assert.True(sexpBool2ToEncode.Values.SequenceEqual(sexpBool2ToDecoded.Values)); Assert.True(sexpBool3ToEncode.Values.SequenceEqual(sexpBool3ToDecoded.Values)); Assert.True(sexpBool4ToEncode.Values.SequenceEqual(sexpBool4ToDecoded.Values)); } }
public void Various_ListTest() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format using ( var service = new Rservice() ) { var mylist = new Dictionary<string , object> { { "One" , 1 } , { "Two" , 2.0 } , { "Three" , "three" } , { "Four" , true } , { "Five" , new DateTime( 2012 , 01 , 01 ) } }; var x1 = Sexp.Make( mylist ); service.RConnection[ "x1" ] = x1; Assert.Equal( x1.Count , mylist.Count ); service.RConnection.Eval( "x2 <- list(One=1,Two=2.0,Three='three',Four=TRUE,Five=as.Date('2012-01-01'))" ); var x2 = service.RConnection[ "x2" ]; Assert.Equal( x1.Count , x2.Count ); for ( int i = 0 ; i < x1.Count ; i++ ) { Assert.True( x1[ i ].Equals( x2[ i ] ) ); Assert.Equal( x1.Names[ i ] , x2.Names[ i ] ); Assert.True( x1[ x1.Names[ i ] ].Equals( x2[ x2.Names[ i ] ] ) ); } } }
public void Command_AssignAndEvalLargeSexpArrayBool_ProperlyEncodesAndDecodesArrayOfBoolean() { using ( var service = new Rservice() ) { // Arrange var random = new Random(); const int count = 25497500; var sexpBool1ToEncode = new SexpArrayBool( Enumerable.Range( 0 , count ).Select( x => { var y = random.Next( 1 , 4 ); return ( y == 1 ? true : y == 2 ? false : ( bool? )null ); } ) ); var sexpBool2ToEncode = new SexpArrayBool( Enumerable.Range( 0 , count + 1 ).Select( x => { var y = random.Next( 1 , 4 ); return ( y == 1 ? true : y == 2 ? false : ( bool? )null ); } ) ); var sexpBool3ToEncode = new SexpArrayBool( Enumerable.Range( 0 , count + 3 ).Select( x => { var y = random.Next( 1 , 4 ); return ( y == 1 ? true : y == 2 ? false : ( bool? )null ); } ) ); var sexpBool4ToEncode = new SexpArrayBool( Enumerable.Range( 0 , count + 4 ).Select( x => { var y = random.Next( 1 , 4 ); return ( y == 1 ? true : y == 2 ? false : ( bool? )null ); } ) ); // Act service.RConnection[ "bools1" ] = sexpBool1ToEncode; service.RConnection[ "bools2" ] = sexpBool2ToEncode; service.RConnection[ "bools3" ] = sexpBool3ToEncode; service.RConnection[ "bools4" ] = sexpBool4ToEncode; Sexp sexpBool1ToDecoded = service.RConnection[ "bools1" ]; Sexp sexpBool2ToDecoded = service.RConnection[ "bools2" ]; Sexp sexpBool3ToDecoded = service.RConnection[ "bools3" ]; Sexp sexpBool4ToDecoded = service.RConnection[ "bools4" ]; // Assert Assert.IsType<SexpArrayBool>( sexpBool1ToDecoded ); Assert.IsType<SexpArrayBool>( sexpBool2ToDecoded ); Assert.IsType<SexpArrayBool>( sexpBool3ToDecoded ); Assert.IsType<SexpArrayBool>( sexpBool4ToDecoded ); Assert.True( sexpBool1ToEncode.Values.SequenceEqual( sexpBool1ToDecoded.Values ) ); Assert.True( sexpBool2ToEncode.Values.SequenceEqual( sexpBool2ToDecoded.Values ) ); Assert.True( sexpBool3ToEncode.Values.SequenceEqual( sexpBool3ToDecoded.Values ) ); Assert.True( sexpBool4ToEncode.Values.SequenceEqual( sexpBool4ToDecoded.Values ) ); } }
public void NamesSet_VectorDoesNotOriginallyContainNames_NamesAreAddedToVector() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "c( 1 , 2 , 3 , 4 )" ]; Sexp test2 = service.RConnection[ "c( 1.1 , 2.2 , 3.3 , 4.4 )" ]; Sexp test3 = service.RConnection[ "c( TRUE , NA , FALSE )" ]; Sexp test4 = service.RConnection[ "'Hitchcock'" ]; var newNames1 = new[] { "E" , "F" , "G" , "H" }; var newNames2 = new[] { "D1" , "D2" , "D3" , "D4" }; var newNames3 = new[] { "Fourth" , "Fifth" , "Sixth" }; var newNames4 = new[] { "Al" }; // Act test1.Names = newNames1; test2.Names = newNames2; test3.Names = newNames3; test4.Names = newNames4; service.RConnection[ "test1" ] = test1; service.RConnection[ "test2" ] = test2; service.RConnection[ "test3" ] = test3; service.RConnection[ "test4" ] = test4; // Assert Assert.Equal( newNames1 , test1.Names ); Assert.Equal( newNames2 , test2.Names ); Assert.Equal( newNames3 , test3.Names ); Assert.Equal( newNames4 , test4.Names ); Assert.Equal( newNames1 , service.RConnection[ "names( test1 )" ].AsStrings ); Assert.Equal( newNames2 , service.RConnection[ "names( test2 )" ].AsStrings ); Assert.Equal( newNames3 , service.RConnection[ "names( test3 )" ].AsStrings ); Assert.Equal( newNames4 , service.RConnection[ "names( test4 )" ].AsStrings ); } }
public void NamesSet_SetToNull_ClearsNames() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "c( A = 1 , B = 2 , C = 3 , D = 4 )" ]; Sexp test2 = service.RConnection[ "c( 1.2 , 2.1 , 3.2 , 4.4 )" ]; // Act test1.Names = null; test2.Names = null; service.RConnection[ "test1" ] = test1; service.RConnection[ "test2" ] = test2; // Act & Assert Assert.Null( test1.Names ); Assert.Null( test2.Names ); Assert.IsType<SexpNull>( service.RConnection[ "names( test1 )" ] ); Assert.IsType<SexpNull>( service.RConnection[ "names( test2 )" ] ); } }
public void NamesSet_AppliedToMatrix_ThrowsNotSupportedException() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "matrix( 1 : 6 , nrow = 2 )" ]; Sexp test2 = Sexp.Make( new double[ , ] { { 2 , 3 } , { 4 , 5 } } ); // Act & Assert Assert.Throws<NotSupportedException>( () => test1.Names = new [] { "abcd" } ); Assert.Throws<NotSupportedException>( () => test2.Names = new[] { "abcd" } ); } }
public void NamesGet_VectorHasNoNames_ReturnsNull() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "c( 1 , 2 , 3 , 4 )" ]; Sexp test2 = service.RConnection[ "numeric()" ]; Sexp test3 = service.RConnection[ "c( TRUE , NA , FALSE )" ]; Sexp test4 = service.RConnection[ "'abcde'" ]; // Act & Assert Assert.Null( test1.Names ); Assert.Null( test2.Names ); Assert.Null( test3.Names ); Assert.Null( test4.Names ); } }
public void NamesGet_Matrix_ReturnsNull() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 )" ]; Sexp test2 = service.RConnection[ "matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , dimnames = list( c( 'Row1' , 'Row2' ) , c( 'Col1' , 'Col2' , 'Col3' ) ) )" ]; // Act & Assert Assert.Null( test1.Names ); Assert.Null( test2.Names ); } }
public void ColNamesSet_SetToNull_ClearsColNamesInSexp() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test1 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE )" ); service.RConnection.VoidEval( "test2 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( c( 'RowA2' , 'RowB2' ) , c( 'ColA2' , 'ColB2' , 'ColC2' ) ) )" ); service.RConnection.VoidEval( "test3 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( c( 'RowA3' , 'RowB3' ) , NULL ) )" ); service.RConnection.VoidEval( "test4 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( NULL , c( 'ColA4' , 'ColB4' , 'ColC4' ) ) )" ); Sexp test1 = service.RConnection[ "test1" ]; Sexp test2 = service.RConnection[ "test2" ]; Sexp test3 = service.RConnection[ "test3" ]; Sexp test4 = service.RConnection[ "test4" ]; // Act test1.ColNames = null; test2.ColNames = null; test3.ColNames = null; test4.ColNames = null; // Assert Assert.Null( test1.ColNames ); Assert.Null( test2.ColNames ); Assert.Null( test3.ColNames ); Assert.Null( test4.ColNames ); Assert.Null( test1.RowNames ); Assert.Equal( new[] { "RowA2" , "RowB2" } , test2.RowNames ); Assert.Equal( new[] { "RowA3" , "RowB3" } , test3.RowNames ); Assert.Null( test4.RowNames ); } }
public void ColNamesSet_SetToNull_ClearsColNamesInR() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test1 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE )" ); service.RConnection.VoidEval( "test2 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( c( 'RowA2' , 'RowB2' ) , c( 'ColA2' , 'ColB2' , 'ColC2' ) ) )" ); service.RConnection.VoidEval( "test3 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( c( 'RowA3' , 'RowB3' ) , NULL ) )" ); service.RConnection.VoidEval( "test4 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( NULL , c( 'ColA4' , 'ColB4' , 'ColC4' ) ) )" ); Sexp test1 = service.RConnection[ "test1" ]; Sexp test2 = service.RConnection[ "test2" ]; Sexp test3 = service.RConnection[ "test3" ]; Sexp test4 = service.RConnection[ "test4" ]; // Act test1.ColNames = null; test2.ColNames = null; test3.ColNames = null; test4.ColNames = null; service.RConnection[ "test1" ] = test1; service.RConnection[ "test2" ] = test2; service.RConnection[ "test3" ] = test3; service.RConnection[ "test4" ] = test4; // Assert Assert.IsType<SexpNull>( service.RConnection[ "colnames(test1)" ] ); Assert.IsType<SexpNull>( service.RConnection[ "colnames(test2)" ] ); Assert.IsType<SexpNull>( service.RConnection[ "colnames(test3)" ] ); Assert.IsType<SexpNull>( service.RConnection[ "colnames(test4)" ] ); Assert.IsType<SexpNull>( service.RConnection[ "rownames(test1)" ] ); Assert.Equal( new[] { "RowA2" , "RowB2" } , service.RConnection[ "rownames(test2)" ].AsStrings ); Assert.Equal( new[] { "RowA3" , "RowB3" } , service.RConnection[ "rownames(test3)" ].AsStrings ); Assert.IsType<SexpNull>( service.RConnection[ "rownames(test4)" ] ); } }
public void ColNamesSet_LengthOfColNamesDoesNotMatchNumberOfCols_ThrowsNotSupportedException() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE )" ]; Sexp test2 = service.RConnection[ "matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( c( 'RowA2' , 'RowB2' ) , c( 'ColA2' , 'ColB2' , 'ColC2' ) ) )" ]; // Act & Assert Assert.Throws<NotSupportedException>( () => test1.ColNames = new[] { "ColA" } ); Assert.Throws<NotSupportedException>( () => test1.ColNames = new[] { "ColA" , "ColB" , "ColC" , "ColD" } ); Assert.Throws<NotSupportedException>( () => test2.ColNames = new[] { "ColA" } ); Assert.Throws<NotSupportedException>( () => test2.ColNames = new[] { "ColA" , "ColB" , "ColC" , "ColD" } ); } }
public void Make_WithIEnumerableStringAndNames_CreatesNamedCharacterVector() { using ( var service = new Rservice() ) { // Arrange & Act Sexp test1A = Sexp.Make( new[] { "charming" } , new[] { "one" } ); Sexp test2A = Sexp.Make( new[] { "almost" , "done" , "here" } , new[] { "one" , "two" , "three" } ); service.RConnection[ "test1" ] = test1A; service.RConnection[ "test2" ] = test2A; Sexp test1B = service.RConnection[ "test1" ]; Sexp test2B = service.RConnection[ "test2" ]; // Assert Assert.IsType<SexpArrayString>( test1A ); Equals( new[] { "one" } , test1A.Names ); Equals( new[] { "charming" } , test1A.AsStrings ); Assert.IsType<SexpArrayString>( test1B ); Equals( new[] { "one" } , test1B.Names ); Equals( new[] { "charming" } , test1B.AsStrings ); Assert.IsType<SexpArrayString>( test2A ); Equals( new[] { "one" , "two" , "three" } , test2A.Names ); Equals( new[] { "almost" , "done" , "here" } , test2A.AsStrings ); Assert.IsType<SexpArrayString>( test2B ); Equals( new[] { "one" , "two" , "three" } , test2B.Names ); Equals( new[] { "almost" , "done" , "here" } , test2B.AsStrings ); } }
public void ColNamesSet_SexpDoesNotHaveDimAttribute_ThrowsNotSupportedException() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = new SexpArrayDouble(); Sexp test2 = service.RConnection[ "c( 1 , 2 , 3 )" ]; // Act & Assert Assert.Throws<NotSupportedException>( () => test1.ColNames = new[] { "RowA" } ); Assert.Throws<NotSupportedException>( () => test2.ColNames = new[] { "RowA" } ); } }
public void NamesGet_VectorHasNames_ReturnsNames() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "c( A = 1 , B = 2 , C = 3 , D = 4 )" ]; Sexp test2 = service.RConnection[ "c( Double1 = 1.1 , Double2 = 2.2 , Double3 = 3.3 , Double4 = 4.4 )" ]; Sexp test3 = service.RConnection[ "c( First = TRUE , Second = NA , Third = FALSE )" ]; Sexp test4 = service.RConnection[ "c( Alfred = 'Hitchcock' )" ]; service.RConnection.VoidEval( "test5 = c( 1 , 2 ); names( test5 ) = c( 'Go' , 'Fast' )" ); Sexp test5 = service.RConnection[ "test5" ]; // Act & Assert Assert.Equal( new[] { "A" , "B" , "C" , "D" } , test1.Names ); Assert.Equal( new[] { "Double1" , "Double2" , "Double3" , "Double4" } , test2.Names ); Assert.Equal( new[] { "First" , "Second" , "Third" } , test3.Names ); Assert.Equal( new[] { "Alfred" } , test4.Names ); Assert.Equal( new[] { "Go" , "Fast" } , test5.Names ); } }
public void Make_2dArrayOfIntWithRowAndColumnNames_ProperlyAssignsRowAndColumnNames() { using ( var service = new Rservice() ) { // Arrange // ReSharper disable RedundantArgumentDefaultValue var matrixValues = new int[ 2 , 3 ] { { 3 , -4 , 5 } , { -6 , 7 , -8 } }; var matrix1 = Sexp.Make( matrixValues , null , null ); var matrix2 = Sexp.Make( matrixValues , new[] { "RowA" , "RowB" } , null ); var matrix3 = Sexp.Make( matrixValues , null , new[] { "ColA" , "ColB" , "ColC" } ); var matrix4 = Sexp.Make( matrixValues , new[] { "RowA" , "RowB" } , new[] { "ColA" , "ColB" , "ColC" } ); // ReSharper restore RedundantArgumentDefaultValue // Act service.RConnection[ "matrix1" ] = matrix1; service.RConnection[ "matrix2" ] = matrix2; service.RConnection[ "matrix3" ] = matrix3; service.RConnection[ "matrix4" ] = matrix4; Sexp matrix1FromR = service.RConnection[ "matrix1" ]; Sexp matrix2FromR = service.RConnection[ "matrix2" ]; Sexp matrix3FromR = service.RConnection[ "matrix3" ]; Sexp matrix4FromR = service.RConnection[ "matrix4" ]; // Assert Assert.Null( matrix1FromR.RowNames ); Assert.Null( matrix1FromR.ColNames ); Assert.Equal( new[] { "RowA" , "RowB" } , matrix2FromR.RowNames ); Assert.Null( matrix2FromR.ColNames ); Assert.Null( matrix3FromR.RowNames ); Assert.Equal( new[] { "ColA" , "ColB" , "ColC" } , matrix3FromR.ColNames ); Assert.Equal( new[] { "RowA" , "RowB" } , matrix4FromR.RowNames ); Assert.Equal( new[] { "ColA" , "ColB" , "ColC" } , matrix4FromR.ColNames ); } }
public void ColNamesSet_DataDoesNotHaveColNames_AddsColNames() { using ( var service = new Rservice() ) { // Arrange service.RConnection.VoidEval( "test1 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE )" ); service.RConnection.VoidEval( "test2 = matrix( c( 1 , 2 , 3 , 4 , 5 , 6 ) , nrow = 2 , byrow = TRUE , dimnames = list( c( 'RowA2' , 'RowB2' ) , NULL ) )" ); Sexp test1 = service.RConnection[ "test1" ]; Sexp test2 = service.RConnection[ "test2" ]; // Act test1.ColNames = new[] { "ColA1" , "ColB1" , "ColC1" }; test2.ColNames = new[] { "ColA2" , "ColB2" , "ColC2" }; service.RConnection[ "test1" ] = test1; service.RConnection[ "test2" ] = test2; test1 = service.RConnection[ "test1" ]; test2 = service.RConnection[ "test2" ]; // Assert Assert.Null( test1.RowNames ); Assert.IsType<SexpNull>( service.RConnection[ "rownames( test1 )" ] ); Assert.Equal( new[] { "ColA1" , "ColB1" , "ColC1" } , test1.ColNames ); Assert.Equal( new[] { "ColA1" , "ColB1" , "ColC1" } , service.RConnection[ "colnames(test1)" ].AsStrings ); Assert.Equal( new[] { "RowA2" , "RowB2" } , test2.RowNames ); Assert.Equal( new[] { "RowA2" , "RowB2" } , service.RConnection[ "rownames(test2)" ].AsStrings ); Assert.Equal( new[] { "ColA2" , "ColB2" , "ColC2" } , test2.ColNames ); Assert.Equal( new[] { "ColA2" , "ColB2" , "ColC2" } , service.RConnection[ "colnames(test2)" ].AsStrings ); } }
public void Various_ArrayDoubleTest() { // note: this test was ported from RserveCLI. Not in the typical Arrange/Act/Assert format const double tol = 1e-15; using (var service = new Rservice()) { var testDoubles = new[] { -3.5, 0.0, 1.0, 2.0, 1.0E20, double.NaN, double.NaN }; var x1 = Sexp.Make(testDoubles); x1[x1.Count - 1] = new SexpArrayDouble(SexpArrayDouble.Na); for (int i = 0; i < x1.Count; i++) { Assert.Equal(testDoubles[i], x1[i].AsDouble); } service.RConnection.Eval("x2 <- as.numeric(c(-3.5,0,1,2,1E20,NaN,NA))"); var x2 = service.RConnection["x2"]; Assert.Equal(x1.Count, x2.Count); for (int i = 0; i < x1.Count; i++) { if (x1[i].IsNa) { Assert.True(x2[i].IsNa); } else if (x1[i].IsNull) { Assert.True(x2[i].IsNull); } else if (double.IsNaN(x1[i].AsDouble)) { Assert.True(double.IsNaN(x2[i].AsDouble)); } else { var res = Math.Abs(x1[i].AsDouble - x2[i].AsDouble) < tol; Assert.True(res); } } service.RConnection["x1"] = x1; var equals = service.RConnection["x1 == x2"]; Assert.Equal(x1.Count, equals.Count); for (int i = 0; i < x1.Count; i++) { if (!double.IsNaN(x1[i].AsDouble)) { Assert.True(( bool )equals[i].AsBool, equals.ToString()); } } Assert.Equal(x1.IndexOf(new SexpArrayDouble(1.0)), 2); x1.AsList[0] = -5.5; Assert.Equal(x1[0].AsDouble, -5.5); } }
public void NamesSet_NamesCountDoesNotMatchValuesCount_ThrowsNotSupportedException() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "c( A = 1 , B = 2 , C = 3 , D = 4 )" ]; Sexp test2 = service.RConnection[ "c( 1.1 , 2.2 , 3.3 , 4.4 )" ]; Sexp test3 = service.RConnection[ "c( First = TRUE , Second = NA , Third = FALSE )" ]; Sexp test4 = service.RConnection[ "'Hitchcock'" ]; // Act & Assert Assert.Throws<NotSupportedException>( () => test1.Names = new string[] { } ); Assert.Throws<NotSupportedException>( () => test1.Names = new [] { "one" , "two" , "three" } ); Assert.Throws<NotSupportedException>( () => test1.Names = new [] { "one" , "two" , "three" , "four" , "five" } ); Assert.Throws<NotSupportedException>( () => test2.Names = new string[] { } ); Assert.Throws<NotSupportedException>( () => test2.Names = new[] { "one" , "two" , "three" } ); Assert.Throws<NotSupportedException>( () => test2.Names = new[] { "one" , "two" , "three" , "four" , "five" } ); Assert.Throws<NotSupportedException>( () => test3.Names = new[] { "one" } ); Assert.Throws<NotSupportedException>( () => test3.Names = new[] { "one" , "two" } ); Assert.Throws<NotSupportedException>( () => test3.Names = new[] { "one" , "two" , "three" , "four" } ); Assert.Throws<NotSupportedException>( () => test4.Names = new string[] { } ); Assert.Throws<NotSupportedException>( () => test4.Names = new [] { "one" , "two" , "three" } ); } }
public void Make_WithIEnumerableDateAndNames_CreatesNamedDateVector() { using ( var service = new Rservice() ) { // Arrange & Act Sexp test1A = Sexp.Make( new[] { new DateTime( 2011 , 01 , 01 ) } , new[] { "one" } ); Sexp test2A = Sexp.Make( new[] { new DateTime( 2012 , 04 , 05 ) , new DateTime( 2009 , 07 , 15 ) , new DateTime( 2008 , 12 , 06 ) } , new[] { "one" , "two" , "three" } ); service.RConnection[ "test1" ] = test1A; service.RConnection[ "test2" ] = test2A; Sexp test1B = service.RConnection[ "test1" ]; Sexp test2B = service.RConnection[ "test2" ]; // Assert Assert.IsType<SexpArrayDate>( test1A ); Equals( new[] { new DateTime( 2011 , 01 , 01 ) } , test1A.Names ); Equals( new[] { 2.2 } , test1A.AsDates ); Assert.IsType<SexpArrayDate>( test1B ); Equals( new[] { new DateTime( 2011 , 01 , 01 ) } , test1B.Names ); Equals( new[] { 2.2 } , test1B.AsDates ); Assert.IsType<SexpArrayDate>( test2A ); Equals( new[] { "one" , "two" , "three" } , test2A.Names ); Equals( new[] { new DateTime( 2012 , 04 , 05 ) , new DateTime( 2009 , 07 , 15 ) , new DateTime( 2008 , 12 , 06 ) } , test2A.AsDates ); Assert.IsType<SexpArrayDate>( test2B ); Equals( new[] { "one" , "two" , "three" } , test2B.Names ); Equals( new[] { new DateTime( 2012 , 04 , 05 ) , new DateTime( 2009 , 07 , 15 ) , new DateTime( 2008 , 12 , 06 ) } , test2B.AsDates ); } }
public void NamesSet_VectorAlreadyContainsNames_NamesAreOverwritten() { using ( var service = new Rservice() ) { // Arrange Sexp test1 = service.RConnection[ "c( A = 1 , B = 2 , C = 3 , D = 4 )" ]; Sexp test2 = service.RConnection[ "c( Double1 = 1.1 , Double2 = 2.2 , Double3 = 3.3 , Double4 = 4.4 )" ]; Sexp test3 = service.RConnection[ "c( First = TRUE , Second = NA , Third = FALSE )" ]; Sexp test4 = service.RConnection[ "c( Alfred = 'Hitchcock' )" ]; var newNames1 = new[] { "E" , "F" , "G" , "H" }; var newNames2 = new[] { "D1" , "D2" , "D3" , "D4" }; var newNames3 = new[] { "Fourth" , "Fifth" , "Sixth" }; var newNames4 = new[] { "Al" }; // Act test1.Names = newNames1; test2.Names = newNames2; test3.Names = newNames3; test4.Names = newNames4; service.RConnection[ "test1" ] = test1; service.RConnection[ "test2" ] = test2; service.RConnection[ "test3" ] = test3; service.RConnection[ "test4" ] = test4; // Assert Assert.Equal( newNames1 , test1.Names ); Assert.Equal( newNames2 , test2.Names ); Assert.Equal( newNames3 , test3.Names ); Assert.Equal( newNames4 , test4.Names ); Assert.Equal( newNames1 , service.RConnection[ "names( test1 )" ].AsStrings ); Assert.Equal( newNames2 , service.RConnection[ "names( test2 )" ].AsStrings ); Assert.Equal( newNames3 , service.RConnection[ "names( test3 )" ].AsStrings ); Assert.Equal( newNames4 , service.RConnection[ "names( test4 )" ].AsStrings ); } }
public void Make_WithIEnumerableDecimalAndNames_CreatesNamedNumericVector() { using ( var service = new Rservice() ) { // Arrange & Act Sexp test1A = Sexp.Make( new[] { 2.2m } , new[] { "one" } ); Sexp test2A = Sexp.Make( new[] { -1.1m , 4.1m , 5.6m } , new[] { "one" , "two" , "three" } ); service.RConnection[ "test1" ] = test1A; service.RConnection[ "test2" ] = test2A; Sexp test1B = service.RConnection[ "test1" ]; Sexp test2B = service.RConnection[ "test2" ]; // Assert Assert.IsType<SexpArrayDouble>( test1A ); Equals( new[] { "one" } , test1A.Names ); Equals( new[] { 2.2m } , test1A.AsDoubles ); Assert.IsType<SexpArrayDouble>( test1B ); Equals( new[] { "one" } , test1B.Names ); Equals( new[] { 2.2m } , test1B.AsDoubles ); Assert.IsType<SexpArrayDouble>( test2A ); Equals( new[] { "one" , "two" , "three" } , test2A.Names ); Equals( new[] { -1.1m , 4.1m , 5.6m } , test2A.AsDoubles ); Assert.IsType<SexpArrayDouble>( test2B ); Equals( new[] { "one" , "two" , "three" } , test2B.Names ); Equals( new[] { -1.1m , 4.1m , 5.6m } , test2B.AsDoubles ); } }
public void Command_ReadLargeDataFrame_ProperlyReceivesAndDecodesSexpFromServer(int rowsToGenerate) { using (var service = new Rservice(false, 524288)) { // Arrange var random = new Random(); DateTime today = DateTime.Today; IEnumerable <DateTime> dates = Enumerable.Range(0, rowsToGenerate).Select(i => today.AddHours(-i).Date); var date1 = new DateTime(2050, 3, 4); var date2 = new DateTime(2060, 12, 31); var date3 = new DateTime(2070, 04, 14); List <DateTime> allDates = dates.Concat(new[] { date1 }).Concat(dates).Concat(new[] { date2 }).Concat(dates).Concat(new[] { date3 }).ToList(); List <double> doubles = Enumerable.Range(0, rowsToGenerate).Select(i => random.NextDouble()).ToList(); const double double1 = -31.123d; const double double2 = -9.35d; const double double3 = 83.559d; const double double4 = -54.332d; List <double> allDoubles = doubles.Concat(new[] { double2 }).Concat(doubles).Concat(new[] { double3 }).Concat(doubles).Concat(new[] { double4 }).ToList(); allDoubles[0] = double1; const int integer1 = -99; const int integer2 = -100; const int integer3 = -541; List <int> allIntegers = Enumerable.Range(0, rowsToGenerate).Concat(new[] { integer1 }).Concat(Enumerable.Range(rowsToGenerate + 1, rowsToGenerate).Concat(new[] { integer2 })).Concat(Enumerable.Range((rowsToGenerate * 2) + 1, rowsToGenerate)).Concat(new[] { integer3 }).ToList(); var dateColumn = new KeyValuePair <string, object>("Date", allDates); var integerColumn = new KeyValuePair <string, object>("Integers", allIntegers); var doubleColumn = new KeyValuePair <string, object>("Doubles", allDoubles); Sexp toAssign1 = Sexp.MakeDataFrame(new[] { dateColumn, integerColumn, doubleColumn }); IEnumerable <string> rowNames = Enumerable.Range(2, (rowsToGenerate * 3) + 3).Select(x => x.ToString()); Sexp toAssign2 = Sexp.MakeDataFrame(new[] { dateColumn, integerColumn, doubleColumn }, rowNames); service.RConnection["assigns1"] = toAssign1; // service.RConnection[ "assigns2" ] = toAssign2; // Act Sexp toRead1 = service.RConnection["assigns1"]; // Sexp toRead2 = service.RConnection[ "assigns2" ]; // Assert Assert.IsType <SexpList>(toRead1); // Assert.IsType<SexpList>( toRead2 ); Assert.Equal(3, toRead1.Attributes.Count); //Assert.Equal( 3 , toRead2.Attributes.Count ); Assert.IsType <SexpArrayString>(toRead1.Attributes["class"]); //Assert.IsType<SexpArrayString>( toRead2.Attributes[ "class" ] ); Assert.Equal("data.frame", toRead1.Attributes["class"].AsString); //Assert.Equal( "data.frame" , toRead2.Attributes[ "class" ].AsString ); Assert.IsType <SexpArrayString>(toRead1.Attributes["names"]); //Assert.IsType<SexpArrayString>( toRead2.Attributes[ "names" ] ); Assert.True(new[] { "Date", "Integers", "Doubles" }.SequenceEqual(toRead1.Attributes["names"].AsStrings)); // Assert.True( new[] { "Date" , "Integers" , "Doubles" }.SequenceEqual( toRead2.Attributes[ "names" ].AsStrings ) ); Assert.IsType <SexpArrayInt>(toRead1.Attributes["row.names"]); //Assert.IsType<SexpArrayString>( toRead2.Attributes[ "row.names" ] ); Assert.True(new[] { SexpArrayInt.NaValue, -1 * allIntegers.Count }.SequenceEqual(toRead1.Attributes["row.names"].AsInts)); // Assert.True( rowNames.SequenceEqual( toRead2.Attributes[ "row.names" ].AsStrings ) ); Assert.IsType <SexpArrayDate>(toRead1.AsList[0]); //Assert.IsType<SexpArrayDate>( toRead2.AsList[ 0 ] ); Assert.True(allDates.SequenceEqual((( SexpList )toRead1).Value[0].AsDates)); // Assert.True( allDates.SequenceEqual( ( ( SexpList )toRead2 ).Value[ 0 ].AsDates ) ); Assert.IsType <SexpArrayInt>(toRead1.AsList[1]); //Assert.IsType<SexpArrayDate>( toRead2.AsList[ 1 ] ); Assert.True(allIntegers.SequenceEqual((( SexpList )toRead1).Value[1].AsInts)); // Assert.True( allIntegers.SequenceEqual( ( ( SexpList )toRead2 ).Value[ 1 ].AsInts ) ); Assert.IsType <SexpArrayDouble>(toRead1.AsList[2]); //Assert.IsType<SexpArrayDouble>( toRead2.AsList[ 2 ] ); Assert.True(allDoubles.SequenceEqual((( SexpList )toRead1).Value[2].AsDoubles)); // Assert.True( allDoubles.SequenceEqual( ( ( SexpList )toRead2 ).Value[ 2 ].AsDoubles ) ); } }
public void Make_WithIEnumerableIntAndNames_CreatesNamedIntVector() { using ( var service = new Rservice() ) { // Arrange & Act Sexp test1A = Sexp.Make( new [] { 1 } , new[] { "one" } ); Sexp test2A = Sexp.Make( new [] { -1 , SexpArrayInt.Na , 5 } , new[] { "one" , "two" , "three" } ); service.RConnection[ "test1" ] = test1A; service.RConnection[ "test2" ] = test2A; Sexp test1B = service.RConnection[ "test1" ]; Sexp test2B = service.RConnection[ "test2" ]; // Assert Assert.IsType<SexpArrayInt>( test1A ); Equals( new[] { "one" } , test1A.Names ); Equals( new [] { 1 } , test1A.AsInts ); Assert.IsType<SexpArrayInt>( test1B ); Equals( new[] { "one" } , test1B.Names ); Equals( new[] { 1 } , test1B.AsInts ); Assert.IsType<SexpArrayInt>( test2A ); Equals( new[] { "one" , "two" , "three" } , test2A.Names ); Equals( new [] { -1 , SexpArrayInt.Na , 5 } , test2A.AsInts ); Assert.IsType<SexpArrayInt>( test2B ); Equals( new[] { "one" , "two" , "three" } , test2B.Names ); Equals( new[] { -1 , SexpArrayInt.Na , 5 } , test2B.AsInts ); } }
public void Command_AssignLargeDataFrame_ProperlyEncodesAndSendsSexpToServer(int rowsToGenerate) { // Arrange var random = new Random(); DateTime today = DateTime.Today; IEnumerable <DateTime> dates = Enumerable.Range(0, rowsToGenerate).Select(i => today.AddHours(-i).Date); var date1 = new DateTime(2050, 3, 4); var date2 = new DateTime(2060, 12, 31); var date3 = new DateTime(2070, 04, 14); List <DateTime> allDates = dates.Concat(new[] { date1 }).Concat(dates).Concat(new[] { date2 }).Concat(dates).Concat(new[] { date3 }).ToList(); List <double> doubles = Enumerable.Range(0, rowsToGenerate).Select(i => random.NextDouble()).ToList(); const double double1 = -31.123d; const double double2 = -9.35d; const double double3 = 83.559d; const double double4 = -54.332d; List <double> allDoubles = doubles.Concat(new[] { double2 }).Concat(doubles).Concat(new[] { double3 }).Concat(doubles).Concat(new[] { double4 }).ToList(); allDoubles[0] = double1; const int integer1 = -99; const int integer2 = -100; const int integer3 = -541; List <int> allIntegers = Enumerable.Range(0, rowsToGenerate).Concat(new[] { integer1 }).Concat(Enumerable.Range(rowsToGenerate + 1, rowsToGenerate).Concat(new[] { integer2 })).Concat(Enumerable.Range((rowsToGenerate * 2) + 1, rowsToGenerate)).Concat(new[] { integer3 }).ToList(); var dateColumn = new KeyValuePair <string, object>("Date", allDates); var integerColumn = new KeyValuePair <string, object>("Integers", allIntegers); var doubleColumn = new KeyValuePair <string, object>("Doubles", allDoubles); Sexp toAssign1 = Sexp.MakeDataFrame(new[] { dateColumn, integerColumn, doubleColumn }); IEnumerable <string> rowNames = Enumerable.Range(2, (rowsToGenerate * 3) + 3).Select(x => x.ToString()); Sexp toAssign2 = Sexp.MakeDataFrame(new[] { dateColumn, integerColumn, doubleColumn }, rowNames); using (var service = new Rservice(false, 524288)) { // Act service.RConnection["assigns1"] = toAssign1; service.RConnection["assigns2"] = toAssign2; // Assert for (int x = 1; x <= 2; x++) { Assert.Equal(today, service.RConnection[string.Format("assigns{0}$Date[ 1 ]", x)].AsDate); Assert.Equal(date1, service.RConnection[string.Format("assigns{0}$Date[ {1} ]", x, rowsToGenerate + 1)].AsDate); Assert.Equal(date2, service.RConnection[string.Format("assigns{0}$Date[ {1} ]", x, (rowsToGenerate * 2) + 2)].AsDate); Assert.Equal(date3, service.RConnection[string.Format("assigns{0}$Date[ {1} ]", x, (rowsToGenerate * 3) + 3)].AsDate); Assert.Equal(double1, service.RConnection[string.Format("assigns{0}$Doubles[ 1 ]", x)].AsDouble); Assert.Equal(double2, service.RConnection[string.Format("assigns{0}$Doubles[ {1} ]", x, rowsToGenerate + 1)].AsDouble); Assert.Equal(double3, service.RConnection[string.Format("assigns{0}$Doubles[ {1} ]", x, (rowsToGenerate * 2) + 2)].AsDouble); Assert.Equal(double4, service.RConnection[string.Format("assigns{0}$Doubles[ {1} ]", x, (rowsToGenerate * 3) + 3)].AsDouble); Assert.Equal(0, service.RConnection[string.Format("assigns{0}$Integers[ 1 ]", x)].AsInt); Assert.Equal(integer1, service.RConnection[string.Format("assigns{0}$Integers[ {1} ]", x, rowsToGenerate + 1)].AsInt); Assert.Equal(integer2, service.RConnection[string.Format("assigns{0}$Integers[ {1} ]", x, (rowsToGenerate * 2) + 2)].AsInt); Assert.Equal(integer3, service.RConnection[string.Format("assigns{0}$Integers[ {1} ]", x, (rowsToGenerate * 3) + 3)].AsInt); } Assert.Equal("2", service.RConnection["rownames( assigns2 )[ 1 ]"].AsString); Assert.Equal((rowsToGenerate + 2).ToString(), service.RConnection[string.Format("rownames( assigns2 )[ {0} ]", rowsToGenerate + 1)].AsString); Assert.Equal(((rowsToGenerate * 2) + 3).ToString(), service.RConnection[string.Format("rownames( assigns2 )[ {0} ]", (rowsToGenerate * 2) + 2)].AsString); Assert.Equal(((rowsToGenerate * 3) + 4).ToString(), service.RConnection[string.Format("rownames( assigns2 )[ {0} ]", (rowsToGenerate * 3) + 3)].AsString); } }
public void Make_WithIEnumerableNullableBoolAndNames_CreatesNamedLogicalVector() { using ( var service = new Rservice() ) { // Arrange & Act Sexp test1A = Sexp.Make( new bool?[] { true } , new[] { "one" } ); Sexp test2A = Sexp.Make( new bool?[] { false , true , null } , new[] { "one" , "two" , "three" } ); service.RConnection[ "test1" ] = test1A; service.RConnection[ "test2" ] = test2A; Sexp test1B = service.RConnection[ "test1" ]; Sexp test2B = service.RConnection[ "test2" ]; // Assert Assert.IsType<SexpArrayBool>( test1A ); Equals( new[] { "one" } , test1A.Names ); Equals( new bool?[] { true } , test1A.AsBools ); Assert.IsType<SexpArrayBool>( test1B ); Equals( new[] { "one" } , test1B.Names ); Equals( new bool?[] { true } , test1B.AsBools ); Assert.IsType<SexpArrayBool>( test2A ); Equals( new[] { "one" , "two" , "three" } , test2A.Names ); Equals( new bool?[] { false , true , null } , test2A.AsBools ); Assert.IsType<SexpArrayBool>( test2B ); Equals( new[] { "one" , "two" , "three" } , test2B.Names ); Equals( new bool?[] { false , true , null } , test2B.AsBools ); } }