How can I write every element of array of unspecified rank and size?
So as to hopefully be more explicit, I’ve also written some test cases for MSTest.
The three tests check scalar, one dimensional and two dimensional data as well as string, double and integer data.
[TestMethod()]
public void TestWrittenScalarIsCorrect()
{
Array data = Array.CreateInstance(typeof(string), 1);
data.SetValue("hello", 0);
Assert.AreEqual(0, data.Rank);
// nothing fancy
Assert.AreEqual("[hello]", WriteArray(data));
}
[TestMethod()]
public void TestWritten1DIsCorrect()
{
// note: I'm expecting 1.0 => '1'
Assert.AreEqual("[1, 1.1]",
YamlWriter.WriteArray(new double[] { 1.0, 1.1 }));
}
[TestMethod()]
public void TestWritten2DIsCorrect()
{
// Note: expecting 00 and 01 to be 0 and 1, respectively
Assert.AreEqual("[[0, 1], [10, 11]]",
WriteArray(new int[,]{ {0, 1}, {10, 11}}));
}
[TestMethod()]
public void TestWritten3DIsCorrect()
{
var data = new int[3, 3, 3];
for (int kk = 0; kk < 3; kk++)
{
for (int jj = 0; jj < 3; jj++)
{
for (int ii = 0; ii < 3; ii++)
{
data[kk, jj, ii] = kk * 100 + jj * 10 + ii;
}
}
}
// Note: expecting 00 and 01 to be 0 and 1, respectively
Assert.AreEqual("[[[0, 1, 2], [10, 11, 12], [20, 21, 22]], "
+ "[[100, 101, 102], [110, 111, 112], [120, 121, 122]], "
+ "[[200, 201, 202], [210, 211, 212], [220, 221, 222]]]",
WriteArray(data));
}
Thanks!
If I understand you correctly, you want something like this:
Edit: Added missing .Reverse()