diff --git a/src/Numerics/LinearAlgebra/Vector.BCL.cs b/src/Numerics/LinearAlgebra/Vector.BCL.cs index 38246da8..8dca9181 100644 --- a/src/Numerics/LinearAlgebra/Vector.BCL.cs +++ b/src/Numerics/LinearAlgebra/Vector.BCL.cs @@ -32,6 +32,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Text; using MathNet.Numerics.LinearAlgebra.Storage; using MathNet.Numerics.Properties; @@ -251,113 +252,163 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Returns a that describes the type, dimensions and shape of this vector. + /// Returns a string that describes the type, dimensions and shape of this vector. /// public virtual string ToTypeString() { return string.Format("{0} {1}-{2}", GetType().Name, Count, typeof (T).Name); } - /// - /// Returns a that represents the content of this vector, row by row. - /// - public string ToVectorString(int maxLines, int maxPerLine, IFormatProvider provider = null) - { - return ToVectorString(maxLines, maxPerLine, 12, "G6", provider); - } - - /// - /// Returns a that represents the content of this vector, row by row. - /// - public string ToVectorString(int maxLines, int maxPerLine, int padding, string format = null, IFormatProvider provider = null) + public string[,] ToVectorStringArray(int maxPerColumn, int maxWidth, int padding, string ellipsis, Func formatValue) { - int fullLines = Count/maxPerLine; - int lastLine = Count%maxPerLine; - bool incomplete = false; - - if (fullLines > maxLines || fullLines == maxLines && lastLine > 0) + var columns = new List>(); + int chars = 0; + int offset = 0; + while (offset < Count) { - fullLines = maxLines - 1; - lastLine = maxPerLine - 1; - incomplete = true; + // full column + int height = Math.Min(maxPerColumn, Count - offset); + var candidate = FormatCompleteColumn(offset, height, formatValue); + chars += candidate.Item1 + padding; + if (chars > maxWidth) + { + break; + } + columns.Add(candidate); + offset += height; } - - const string separator = " "; - string pdots = "...".PadLeft(padding); - - if (format == null) + if (offset < Count) { - format = "G8"; + // we're not done yet, but adding the last column has failed + // --> make the last column partial + var last = columns[columns.Count - 1]; + var c = last.Item2; + c[c.Length - 4] = ellipsis; + c[c.Length - 3] = ellipsis; + c[c.Length - 2] = formatValue(At(Count - 2)); + c[c.Length - 1] = formatValue(At(Count - 1)); } - var stringBuilder = new StringBuilder(); - - var iterator = Enumerate().GetEnumerator(); - for (var line = 0; line < fullLines; line++) + int rows = columns[0].Item2.Length; + int cols = columns.Count; + var array = new string[rows, cols]; + int colIndex = 0; + foreach (var column in columns) { - if (line > 0) + for (int k = 0; k < column.Item2.Length; k++) { - stringBuilder.Append(Environment.NewLine); + array[k, colIndex] = column.Item2[k]; } - iterator.MoveNext(); - stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding)); - for (var column = 1; column < maxPerLine; column++) + for (int k = column.Item2.Length; k < rows; k++) { - stringBuilder.Append(separator); - iterator.MoveNext(); - stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding)); + array[k, colIndex] = ""; } + colIndex++; } + return array; + } + + static string FormatStringArrayToString(string[,] array, string columnSeparator, string rowSeparator) + { + var rows = array.GetLength(0); + var cols = array.GetLength(1); - if (lastLine > 0) + var widths = new int[cols]; + for (int i = 0; i < rows; i++) { - if (fullLines > 0) - { - stringBuilder.Append(Environment.NewLine); - } - iterator.MoveNext(); - stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding)); - for (var column = 1; column < lastLine; column++) + for (int j = 0; j < cols; j++) { - stringBuilder.Append(separator); - iterator.MoveNext(); - stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding)); + widths[j] = Math.Max(widths[j], array[i, j].Length); } - if (incomplete) + } + + var sb = new StringBuilder(); + for (int i = 0; i < rows; i++) + { + sb.Append(array[i, 0].PadLeft(widths[0])); + for (int j = 1; j < cols; j++) { - stringBuilder.Append(separator); - stringBuilder.Append(pdots); + sb.Append(columnSeparator); + sb.Append(array[i, j].PadLeft(widths[j])); } + sb.Append(rowSeparator); + } + return sb.ToString(); + } + + Tuple FormatCompleteColumn(int offset, int height, Func formatValue) + { + var c = new string[height]; + int index = 0; + for (var k = 0; k < height; k++) + { + c[index++] = formatValue(At(offset + k)); + } + int w = c.Max(x => x.Length); + return new Tuple(w, c); + } + + /// + /// Returns a string that represents the content of this vector, column by column. + /// + public string ToVectorString(int maxPerColumn, int maxWidth, string ellipsis, string columnSeparator, string rowSeparator, Func formatValue) + { + return FormatStringArrayToString( + ToVectorStringArray(maxPerColumn, maxWidth, columnSeparator.Length, ellipsis, formatValue), + columnSeparator, rowSeparator); + } + + /// + /// Returns a string that represents the content of this vector, column by column. + /// + public string ToVectorString(int maxPerColumn, int maxWidth, string format = null, IFormatProvider provider = null) + { + if (format == null) + { + format = "G6"; + } + + return ToVectorString(maxPerColumn, maxWidth, "..", " ", Environment.NewLine, x => x.ToString(format, provider)); + } + + /// + /// Returns a string that represents the content of this vector, column by column. + /// + public string ToVectorString(string format = null, IFormatProvider provider = null) + { + if (format == null) + { + format = "G6"; } - return stringBuilder.ToString(); + return ToVectorString(12, 80, "..", " ", Environment.NewLine, x => x.ToString(format, provider)); } /// - /// Returns a that summarizes this vector. + /// Returns a string that summarizes this vector. /// - public string ToString(int maxLines, int maxPerLine, IFormatProvider provider = null) + public string ToString(int maxPerColumn, int maxColumns, string format = null, IFormatProvider provider = null) { - return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(maxLines, maxPerLine, provider)); + return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(maxPerColumn, maxColumns, format, provider)); } /// - /// Returns a that summarizes this vector. + /// Returns a string that summarizes this vector. /// The maximum number of cells can be configured in the class. /// public override sealed string ToString() { - return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(Control.MaxToStringRows, Control.MaxToStringColumns)); + return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString()); } /// - /// Returns a that summarizes this vector. + /// Returns a string that summarizes this vector. /// The maximum number of cells can be configured in the class. /// The format string is ignored. /// - public string ToString(string format, IFormatProvider formatProvider) + public string ToString(string format = null, IFormatProvider formatProvider = null) { - return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(Control.MaxToStringRows, Control.MaxToStringColumns, formatProvider)); + return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(format, formatProvider)); } } } diff --git a/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs deleted file mode 100644 index ff6a4893..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorTest.TextHandling.cs +++ /dev/null @@ -1,115 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex -{ - using LinearAlgebra.Complex; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Dense vector text handling tests. - /// - [TestFixture, Category("LA")] - public class DenseVectorTextHandlingTest - { - /// - /// Can parse a Complex dense vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "(2, 0)")] - [TestCase("(3)", "(3, 0)")] - [TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")] - [TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")] - [TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")] - [TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")] - [TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")] - [TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")] - public void CanParseComplexDenseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a Complex dense vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")] - //[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH -#if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")] -#endif - public void CanParseComplexDenseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - DenseVector vector; - var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs deleted file mode 100644 index 2dce1647..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.TextHandling.cs +++ /dev/null @@ -1,114 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex -{ - using LinearAlgebra.Complex; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Sparse vector text handling tests. - /// - public class SparseVectorTextHandlingTest - { - /// - /// Can parse a Complex sparse vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "(2, 0)")] - [TestCase("(3)", "(3, 0)")] - [TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")] - [TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")] - [TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")] - [TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")] - [TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")] - [TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")] - public void CanParseComplexSparseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a Complex sparse vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")] - //[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH -#if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")] -#endif - public void CanParseComplexSparseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - SparseVector vector; - var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs index 41c45774..da6a753c 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs @@ -85,17 +85,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex } #endif - /// - /// Can convert vector to string. - /// - [Test] - public void CanConvertVectorToString() - { - var vector = CreateVector(Data); - var str = vector.ToVectorString(1, int.MaxValue, 1); - Assert.AreEqual("(1, 1) (2, 1) (3, 1) (4, 1) (5, 1)", str); - } - /// /// Can copy part of a vector to another vector. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs deleted file mode 100644 index dde03fa5..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorTest.TextHandling.cs +++ /dev/null @@ -1,115 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 -{ - using LinearAlgebra.Complex32; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Dense vector text handling tests. - /// - [TestFixture, Category("LA")] - public class DenseVectorTextHandlingTest - { - /// - /// Can parse a Complex32 dense vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "(2, 0)")] - [TestCase("(3)", "(3, 0)")] - [TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")] - [TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")] - [TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")] - [TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")] - [TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")] - [TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")] - public void CanParseComplex32DenseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a Complex32 dense vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")] - //[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH -#if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")] -#endif - public void CanParseComplex32DenseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - DenseVector vector; - var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs deleted file mode 100644 index 104c2388..00000000 --- a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.TextHandling.cs +++ /dev/null @@ -1,114 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 -{ - using LinearAlgebra.Complex32; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Sparse vector text handling tests. - /// - public class SparseVectorTextHandlingTest - { - /// - /// Can parse a Complex32 sparse vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "(2, 0)")] - [TestCase("(3)", "(3, 0)")] - [TestCase("[1,2,3]", "(1, 0) (2, 0) (3, 0)")] - [TestCase(" [ 1.1 , 2.1 , 3.1 ] ", "(1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [ -1.1 , 2.1 , +3.1 ] ", "(-1.1, 0) (2.1, 0) (3.1, 0)")] - [TestCase(" [1.2,3.4 , 5.6] ", "(1.2, 0) (3.4, 0) (5.6, 0)")] - [TestCase("[1+1i,2+1i,3+1i]", "(1, 1) (2, 1) (3, 1)")] - [TestCase(" [ 1.1 + 1i , 2.1+1i , 3.1+1i ] ", "(1.1, 1) (2.1, 1) (3.1, 1)")] - [TestCase(" [ -1.1 + 1i , 2.1-1i , +3.1+1i ] ", "(-1.1, 1) (2.1, -1) (3.1, 1)")] - [TestCase(" [1.2+2.3i ,3.4+4.5i , 5.6+ 6.7i] ", "(1.2, 2.3) (3.4, 4.5) (5.6, 6.7)")] - public void CanParseComplex32SparseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a Complex32 sparse vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2 + 1i , 3.4 + 1i , 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "en-US")] - //[TestCase(" 1.2 + 1i ; 3.4 + 1i ; 5.6 + 1i ", "(1.2, 1) (3.4, 1) (5.6, 1)", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH -#if !PORTABLE - [TestCase(" 1,2 + 1i ; 3,4 + 1i ; 5,6 + 1i ", "(1,2, 1) (3,4, 1) (5,6, 1)", "de-DE")] -#endif - public void CanParseComplex32SparseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - SparseVector vector; - var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs index fefe3a91..75c1b699 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs @@ -81,17 +81,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 } #endif - /// - /// Can convert vector to string. - /// - [Test] - public void CanConvertVectorToString() - { - var vector = CreateVector(Data); - var str = vector.ToVectorString(1, int.MaxValue, 1); - Assert.AreEqual("(1, 1) (2, 1) (3, 1) (4, 1) (5, 1)", str); - } - /// /// Can copy part of a vector to another vector. /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs deleted file mode 100644 index f83d2c5d..00000000 --- a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTest.TextHandling.cs +++ /dev/null @@ -1,150 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double -{ - using LinearAlgebra.Double; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Dense vector text handling tests. - /// - [TestFixture, Category("LA")] - public class DenseVectorTextHandlingTest - { - /// - /// Can parse a double dense vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "2")] - [TestCase("(3)", "3")] - [TestCase("[1,2,3]", "1 2 3")] - [TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")] - [TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")] - [TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")] - public void CanParseDoubleDenseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a double dense vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")] - //[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")] - public void CanParseDoubleDenseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse double dense vectors. - /// - /// Vector as string. - [TestCase("15")] - [TestCase("1{0}2 3{0}4 5{0}6")] - public void CanParseDoubleDenseVectors(string vectorAsString) - { - var mappedString = String.Format( - vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); - - var vector = DenseVector.Parse(mappedString); - - Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1)); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Can try parse a double dense vector. - /// - [Test] - public void CanTryParseDoubleDenseVector() - { - var data = new[] { 1.2, 3.4, 5.6e-78 }; - var text = String.Format( - "{1}{0}{2}{0}{3}", - CultureInfo.CurrentCulture.TextInfo.ListSeparator, - data[0], - data[1], - data[2]); - - DenseVector vector; - var ret = DenseVector.TryParse(text, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, (double[])vector, 14); - - ret = DenseVector.TryParse(text, CultureInfo.CurrentCulture, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, (double[])vector, 14); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - DenseVector vector; - var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs deleted file mode 100644 index 4edd092e..00000000 --- a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.TextHandling.cs +++ /dev/null @@ -1,149 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double -{ - using LinearAlgebra.Double; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Sparse vector text handling tests. - /// - public class SparseVectorTextHandlingTest - { - /// - /// Can parse a double sparse vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "2")] - [TestCase("(3)", "3")] - [TestCase("[1,2,3]", "1 2 3")] - [TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")] - [TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")] - [TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")] - public void CanParseDoubleSparseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a double sparse vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")] - //[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")] - public void CanParseDoubleSparseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse double sparse vectors. - /// - /// Vector as string. - [TestCase("15")] - [TestCase("1{0}2 3{0}4 5{0}6")] - public void CanParseDoubleSparseVectors(string vectorAsString) - { - var mappedString = String.Format( - vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); - - var vector = SparseVector.Parse(mappedString); - - Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1)); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Can try parse a double sparse vector. - /// - [Test] - public void CanTryParseDoubleSparseVector() - { - var data = new[] { 1.2, 3.4, 5.6e-78 }; - var text = String.Format( - "{1}{0}{2}{0}{3}", - CultureInfo.CurrentCulture.TextInfo.ListSeparator, - data[0], - data[1], - data[2]); - - SparseVector vector; - var ret = SparseVector.TryParse(text, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14); - - ret = SparseVector.TryParse(text, CultureInfo.CurrentCulture, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - SparseVector vector; - var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs index c07378eb..4094e96f 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs @@ -77,17 +77,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } #endif - /// - /// Can convert vector to string. - /// - [Test] - public void CanConvertVectorToString() - { - var vector = CreateVector(Data); - var str = vector.ToVectorString(1, int.MaxValue, 1); - Assert.AreEqual("1 2 3 4 5", str); - } - /// /// Can copy part of a vector to another vector. /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs deleted file mode 100644 index f918efa8..00000000 --- a/src/UnitTests/LinearAlgebraTests/Single/DenseVectorTest.TextHandling.cs +++ /dev/null @@ -1,150 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single -{ - using LinearAlgebra.Single; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Dense vector text handling tests. - /// - [TestFixture, Category("LA")] - public class DenseVectorTextHandlingTest - { - /// - /// Can parse a float dense vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "2")] - [TestCase("(3)", "3")] - [TestCase("[1,2,3]", "1 2 3")] - [TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")] - [TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")] - [TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")] - public void CanParseSingleDenseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a float dense vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")] - //[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")] - public void CanParseSingleDenseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = DenseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse float dense vectors. - /// - /// Vector as string. - [TestCase("15")] - [TestCase("1{0}2 3{0}4 5{0}6")] - public void CanParseSingleDenseVectors(string vectorAsString) - { - var mappedString = String.Format( - vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); - - var vector = DenseVector.Parse(mappedString); - - Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1, "G")); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Can try parse a float dense vector. - /// - [Test] - public void CanTryParseSingleDenseVector() - { - var data = new[] { 1.2f, 3.4f, 5.6e-78f }; - var text = String.Format( - "{1}{0}{2}{0}{3}", - CultureInfo.CurrentCulture.TextInfo.ListSeparator, - data[0], - data[1], - data[2]); - - DenseVector vector; - var ret = DenseVector.TryParse(text, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, (float[])vector, 14); - - ret = DenseVector.TryParse(text, CultureInfo.CurrentCulture, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, (float[])vector, 14); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - DenseVector vector; - var ret = DenseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs deleted file mode 100644 index 30386410..00000000 --- a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.TextHandling.cs +++ /dev/null @@ -1,149 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2013 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single -{ - using LinearAlgebra.Single; - using NUnit.Framework; - using System; - using System.Globalization; - - /// - /// Sparse vector text handling tests. - /// - public class SparseVectorTextHandlingTest - { - /// - /// Can parse a float sparse vectors with invariant culture. - /// - /// String to parse. - /// Expected result. - [TestCase("2", "2")] - [TestCase("(3)", "3")] - [TestCase("[1,2,3]", "1 2 3")] - [TestCase(" [ 1 , 2 , 3 ] ", "1 2 3")] - [TestCase(" [ -1 , 2 , +3 ] ", "-1 2 3")] - [TestCase(" [1.2,3.4 , 5.6] ", "1.2 3.4 5.6")] - public void CanParseSingleSparseVectorsWithInvariant(string stringToParse, string expectedToString) - { - var formatProvider = CultureInfo.InvariantCulture; - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse a float sparse vectors with culture. - /// - /// String to parse. - /// Expected result. - /// Culture name. - [TestCase(" 1.2,3.4 , 5.6 ", "1.2 3.4 5.6", "en-US")] - //[TestCase(" 1.2;3.4 ; 5.6 ", "1.2 3.4 5.6", "de-CH")] Windows 8.1 issue, see http://bit.ly/W81deCH - [TestCase(" 1,2;3,4 ; 5,6 ", "1,2 3,4 5,6", "de-DE")] - public void CanParseSingleSparseVectorsWithCulture(string stringToParse, string expectedToString, string culture) - { - var formatProvider = new CultureInfo(culture); - var vector = SparseVector.Parse(stringToParse, formatProvider); - - Assert.AreEqual(expectedToString, vector.ToVectorString(1, int.MaxValue, 1, "G", formatProvider)); - } - - /// - /// Can parse float sparse vectors. - /// - /// Vector as string. - [TestCase("15")] - [TestCase("1{0}2 3{0}4 5{0}6")] - public void CanParseSingleSparseVectors(string vectorAsString) - { - var mappedString = String.Format( - vectorAsString, - CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); - - var vector = SparseVector.Parse(mappedString); - - Assert.AreEqual(mappedString, vector.ToVectorString(1, int.MaxValue, 1, "G")); - } - - /// - /// Parse if missing closing paren throws FormatException. - /// - [Test] - public void ParseIfMissingClosingParenThrowsFormatException() - { - Assert.That(() => SparseVector.Parse("(1"), Throws.TypeOf()); - Assert.That(() => SparseVector.Parse("[1"), Throws.TypeOf()); - } - - /// - /// Can try parse a float sparse vector. - /// - [Test] - public void CanTryParseSingleSparseVector() - { - var data = new[] { 1.2f, 3.4f, 5.6e-10f }; - var text = String.Format( - "{1}{0}{2}{0}{3}", - CultureInfo.CurrentCulture.TextInfo.ListSeparator, - data[0], - data[1], - data[2]); - - SparseVector vector; - var ret = SparseVector.TryParse(text, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14); - - ret = SparseVector.TryParse(text, CultureInfo.CurrentCulture, out vector); - Assert.IsTrue(ret); - AssertHelpers.AlmostEqualRelative(data, vector.ToArray(), 14); - } - - /// - /// Try parse a bad value with invariant returns false. - /// - /// Input string. - [TestCase(null)] - [TestCase("")] - [TestCase(",")] - [TestCase("1e+")] - [TestCase("1e")] - [TestCase("()")] - [TestCase("[ ]")] - public void TryParseBadValueWithInvariantReturnsFalse(string str) - { - SparseVector vector; - var ret = SparseVector.TryParse(str, CultureInfo.InvariantCulture, out vector); - Assert.IsFalse(ret); - Assert.IsNull(vector); - } - } -} diff --git a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs index b7a17fdb..427aefda 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs @@ -77,17 +77,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single CollectionAssert.AreEqual(vector, clone); } #endif - - /// - /// Can convert vector to string. - /// - [Test] - public void CanConvertVectorToString() - { - var vector = CreateVector(Data); - var str = vector.ToVectorString(1, int.MaxValue, 1); - Assert.AreEqual("1 2 3 4 5", str); - } /// /// Can copy part of a vector to another vector. diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 069ba696..96b31628 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -153,7 +153,6 @@ - @@ -191,7 +190,6 @@ - @@ -202,7 +200,6 @@ - @@ -240,7 +237,6 @@ - @@ -257,7 +253,6 @@ - @@ -289,7 +284,6 @@ - @@ -304,7 +298,6 @@ - @@ -342,7 +335,6 @@ -