Added tests for StringBuilder, StringReader and StringWriter (for https://github.com/CosmosOS/Cosmos/issues/1027).

- FAT1 and FAT2 tests are joined in an unique FAT test
- IOTest refactored similarly as BCLTest
This commit is contained in:
fanoI 2018-11-04 14:51:02 +01:00
parent 6a375042c5
commit 4e7e5b5ff8
13 changed files with 669 additions and 212 deletions

View file

@ -146,8 +146,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XSharp.x86", "..\XSharp\sou
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmos.Compiler.Tests.TypeSystem", "Tests\Kernels\Cosmos.Compiler.Tests.TypeSystem\Cosmos.Compiler.Tests.TypeSystem.csproj", "{D21A7C6C-A696-4EC3-84EB-70700C1E3B34}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmos.Kernel.Tests.Fat2", "Tests\Kernels\Cosmos.Kernel.Tests.Fat2\Cosmos.Kernel.Tests.Fat2.csproj", "{D4B1618A-3653-43CD-B617-20482B12712B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTest", "Tests\Kernels\ConsoleTest\ConsoleTest.csproj", "{0DF97CAC-220B-4DAD-B397-42E394255763}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmos.TestRunner.UI", "Tests\Cosmos.TestRunner.UI\Cosmos.TestRunner.UI.csproj", "{2992AA07-E126-4EE0-B31C-D0B2ADE3393A}"
@ -566,14 +564,6 @@ Global
{D21A7C6C-A696-4EC3-84EB-70700C1E3B34}.Release|Any CPU.Build.0 = Release|Any CPU
{D21A7C6C-A696-4EC3-84EB-70700C1E3B34}.Release|x86.ActiveCfg = Release|Any CPU
{D21A7C6C-A696-4EC3-84EB-70700C1E3B34}.Release|x86.Build.0 = Release|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Debug|x86.ActiveCfg = Debug|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Debug|x86.Build.0 = Debug|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Release|Any CPU.Build.0 = Release|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Release|x86.ActiveCfg = Release|Any CPU
{D4B1618A-3653-43CD-B617-20482B12712B}.Release|x86.Build.0 = Release|Any CPU
{0DF97CAC-220B-4DAD-B397-42E394255763}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DF97CAC-220B-4DAD-B397-42E394255763}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DF97CAC-220B-4DAD-B397-42E394255763}.Debug|x86.ActiveCfg = Debug|Any CPU
@ -723,7 +713,6 @@ Global
{FF46829E-B612-4D36-80BE-ED04521AD91A} = {E9CD521E-C386-466D-B5F7-A5EB19A61625}
{D0EABA08-88C9-4F7C-BCA9-361B58B20D67} = {E9CD521E-C386-466D-B5F7-A5EB19A61625}
{D21A7C6C-A696-4EC3-84EB-70700C1E3B34} = {ECEA7778-E786-4317-90B9-A2D4427CB91C}
{D4B1618A-3653-43CD-B617-20482B12712B} = {29EEC029-6A2B-478A-B6E5-D63A91388ABA}
{0DF97CAC-220B-4DAD-B397-42E394255763} = {ECEA7778-E786-4317-90B9-A2D4427CB91C}
{2992AA07-E126-4EE0-B31C-D0B2ADE3393A} = {0E67EFE8-5944-4F6C-8B47-C5E06D4C79F5}
{3421E19D-16C7-4593-9F6B-291ECB86A3EB} = {F7C6CA93-1D02-443C-9C8B-A1988DE0306B}

View file

@ -15,7 +15,21 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Kernels\**\*.*proj" />
<ProjectReference Include="..\Kernels\BoxingTests\BoxingTests.csproj" />
<ProjectReference Include="..\Kernels\ConsoleTest\ConsoleTest.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Compiler.Tests.Bcl\Cosmos.Compiler.Tests.Bcl.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Compiler.Tests.Encryption\Cosmos.Compiler.Tests.Encryption.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Compiler.Tests.Exceptions\Cosmos.Compiler.Tests.Exceptions.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Compiler.Tests.Interfaces\Cosmos.Compiler.Tests.Interfaces.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Compiler.Tests.MethodTests\Cosmos.Compiler.Tests.MethodTests.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Compiler.Tests.SingleEchoTest\Cosmos.Compiler.Tests.SingleEchoTest.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Compiler.Tests.TypeSystem\Cosmos.Compiler.Tests.TypeSystem.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Kernel.Tests.DiskManager\Cosmos.Kernel.Tests.DiskManager.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Kernel.Tests.Fat\Cosmos.Kernel.Tests.Fat.csproj" />
<ProjectReference Include="..\Kernels\Cosmos.Kernel.Tests.IO\Cosmos.Kernel.Tests.IO.csproj" />
<ProjectReference Include="..\Kernels\GraphicTest\GraphicTest.csproj" />
<ProjectReference Include="..\Kernels\MemoryOperationsTest\MemoryOperationsTest.csproj" />
<ProjectReference Include="..\Kernels\SimpleStructsAndArraysTest\SimpleStructsAndArraysTest.csproj" />
</ItemGroup>
</Project>

View file

@ -23,7 +23,6 @@ namespace Cosmos.TestRunner.Full
yield return typeof(Cosmos.Compiler.Tests.MethodTests.Kernel);
yield return typeof(Cosmos.Compiler.Tests.SingleEchoTest.Kernel);
yield return typeof(Cosmos.Kernel.Tests.Fat.Kernel);
yield return typeof(Cosmos.Kernel.Tests.Fat2.Kernel);
yield return typeof(Cosmos.Kernel.Tests.IO.Kernel);
yield return typeof(SimpleStructsAndArraysTest.Kernel);
yield return typeof(Cosmos.Kernel.Tests.DiskManager.Kernel);

View file

@ -65,10 +65,11 @@ namespace Cosmos.Compiler.Tests.Bcl
// System.Collections.Generic
ListTest.Execute();
QueueTest.Execute();
QueueTest.Execute();
DictionaryTest.Execute();
// System.Text
StringBuilderTest.Execute();
EncodingTest.Execute();
TestController.Completed();

View file

@ -0,0 +1,203 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Cosmos.Debug.Kernel;
using Cosmos.TestRunner;
namespace Cosmos.Compiler.Tests.Bcl.System
{
public static class StringBuilderTest
{
public static void Execute()
{
StringBuilder sb = new StringBuilder();
Assert.IsTrue(sb.Capacity == 16, "StringBuilder.Capacity is wrong");
Assert.IsTrue(sb.MaxCapacity == Int32.MaxValue, "StringBuilder.MaxCapacity is wrong");
Assert.IsTrue(sb.Length == 0, "StringBuilder.MaxCapacity is wrong");
sb.Append("This ");
sb.Append("is ");
sb.Append("a test");
/* Now sb.Lenght should be 14 (the leng of 'This is a test') */
Assert.IsTrue(sb.Length == 14, "After Append StringBuilder.Lenght is wrong");
Assert.IsTrue(sb.ToString() == "This is a test", "StringBuilder.Append() does not work");
sb.Append("...again");
Assert.IsTrue(sb.Capacity == 32, "StringBuilder.Capacity is wrong (not doubled!)");
Assert.IsTrue(sb.Length == 22, "After Append StringBuilder.Lenght is wrong");
Assert.IsTrue(sb.ToString() == "This is a test...again", "StringBuilder.Append() again does not work");
sb.Clear();
// Capacity does not change after Clear
Assert.IsTrue(sb.Capacity == 32, "StringBuilder.Capacity after Clear is wrong)");
// ... but Lenght should be 0 again
Assert.IsTrue(sb.Length == 0, "After Clear StringBuilder.Lenght is wrong");
Assert.IsTrue(sb.ToString() == String.Empty, "StringBuilder.ToString() is not empty after Clear()");
/* This is required NumberBuffer to work it will be 90% managed code in Net Core 2.1 so better to wait */
#if false
int var1 = 111;
float var2 = 2.22F;
string var3 = "abcd";
object[] var4 = { 3, 4.4, 'X' };
sb.AppendFormat($"1) {var1}");
Assert.IsTrue(sb.ToString() == "1) 111", "StringBuilder.AppendFormat() with 1 arg does not work");
sb.Length = 0; // Same of sb.Clear() maybe faster
sb.AppendFormat("2) {0}, {1}", var1, var2);
Assert.IsTrue(sb.ToString() == "2) 111, 2.22", "StringBuilder.AppendFormat() with 2 args does not work");
sb.Length = 0; // Same of sb.Clear() maybe faster
sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
Assert.IsTrue(sb.ToString() == "111, 2.22, abcd", "StringBuilder.AppendFormat() with 3 args does not work");
sb.Length = 0; // Same of sb.Clear() maybe faster
sb.AppendFormat("4) {0}, {1}, {2}", var4);
Assert.IsTrue(sb.ToString() == "111, 2.22, abcd", "StringBuilder.AppendFormat() with arg array does not work");
sb.Length = 0; // Same of sb.Clear() maybe faster
#endif
sb.Append("This is a test");
sb.Insert("This is".Length, " indeed");
Assert.IsTrue(sb.ToString() == "This is indeed a test", $"StringBuilder.Insert() does not work: {sb.ToString()}");
string initialValue = "--[]--";
sb = new StringBuilder(initialValue);
Assert.IsTrue(sb.ToString() == initialValue, "Stribuilder(string) does not work");
string xyz = "xyz";
char[] abc = { 'a', 'b', 'c' };
char star = '*';
Object obj = 0;
bool xBool = true;
byte xByte = 1;
short xInt16 = 2;
int xInt32 = 3;
long xInt64 = 4;
//Decimal xDecimal = 5;
float xSingle = 6.6F;
double xDouble = 7.7;
// The following types are not CLS-compliant.
ushort xUInt16 = 8;
uint xUInt32 = 9;
ulong xUInt64 = 10;
sbyte xSByte = -11;
sb.Insert(3, xyz, 2);
Assert.IsTrue(sb.ToString() == "--[xyzxyz]--", "Insert #1 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, star);
Assert.IsTrue(sb.ToString() == "--[*]--", "Insert #2 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, abc);
Assert.IsTrue(sb.ToString() == "--[abc]--", "Insert #3 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, abc, 1, 2);
Assert.IsTrue(sb.ToString() == "--[bc]--", "Insert #4 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xBool);
Assert.IsTrue(sb.ToString() == "--[True]--", "Insert #5 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, obj);
Assert.IsTrue(sb.ToString() == "--[0]--", "Insert #6 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xByte);
Assert.IsTrue(sb.ToString() == "--[1]--", "Insert #7 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xInt16);
Assert.IsTrue(sb.ToString() == "--[2]--", "Insert #8 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xInt32);
Assert.IsTrue(sb.ToString() == "--[3]--", "Insert #9 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xInt64);
Assert.IsTrue(sb.ToString() == "--[4]--", "Insert #10 does not work");
sb = new StringBuilder(initialValue);
// Decimal has a totally managed implementation in .Net Core 2.1, let's wait for that
#if false
sb.Insert(3, xDecimal);
Assert.IsTrue(sb.ToString() == "--[5]--", "Insert #11 does not work");
sb = new StringBuilder(initialValue);
#endif
sb.Insert(3, xSingle);
Assert.IsTrue(sb.ToString() == "--[6.6]--", "Insert #11 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xDouble);
Assert.IsTrue(sb.ToString() == "--[7.7]--", "Insert #12 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xUInt16);
Assert.IsTrue(sb.ToString() == "--[8]--", "Insert #13 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xUInt32);
Assert.IsTrue(sb.ToString() == "--[9]--", "Insert #14 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xUInt64);
Assert.IsTrue(sb.ToString() == "--[10]--", "Insert #15 does not work");
sb = new StringBuilder(initialValue);
sb.Insert(3, xSByte);
Assert.IsTrue(sb.ToString() == "--[-11]--", "Insert #16 does not work");
sb.Clear();
sb = new StringBuilder("The quick brown fox jumps over the lazy dog.");
sb.Remove(10, 6); // Remove "brown "
Assert.IsTrue(sb.ToString() == "The quick fox jumps over the lazy dog.", "Remove does not work");
sb = new StringBuilder("The quick br!wn d#g jumps #ver the lazy cat.");
sb.Replace('#', '!', 15, 29); // Some '#' -> '!'
Assert.IsTrue(sb.ToString() == "The quick br!wn d!g jumps !ver the lazy cat.", "Replace #1 does not work");
sb.Replace('!', 'o'); // All '!' -> 'o'
Assert.IsTrue(sb.ToString() == "The quick brown dog jumps over the lazy cat.", "Replace #2 does not work");
sb.Replace("cat", "dog"); // All "cat" -> "dog"
Assert.IsTrue(sb.ToString() == "The quick brown dog jumps over the lazy dog.", "Replace #3 does not work");
sb.Replace("dog", "fox", 15, 20); // Some "dog" -> "fox"
Assert.IsTrue(sb.ToString() == "The quick brown fox jumps over the lazy dog.", "Replace #4 does not work");
sb = new StringBuilder("This is a simple sentence.");
Assert.IsTrue(sb[2] == 'i', "Index get operator does not work");
sb[2] = '1';
Assert.IsTrue(sb.ToString() == "Th1s is a simple sentence.", "Index set operator does not work");
}
}
}

View file

@ -43,7 +43,9 @@ namespace Cosmos.Kernel.Tests.Fat
StreamWriterStreamReaderTest.Execute(mDebugger);
BinaryWriterBinaryReaderTest.Execute(mDebugger);
FileInfoTest.Execute(mDebugger);
DriveInfoTest.Execute(mDebugger);
FileTest.Execute(mDebugger);
TestController.Completed();
}
catch (Exception e)

View file

@ -1,7 +1,7 @@
using Cosmos.TestRunner;
using Sys = Cosmos.System;
using System;
using System.IO;
using Cosmos.Kernel.Tests.IO.System.IO;
namespace Cosmos.Kernel.Tests.IO
{
@ -10,7 +10,7 @@ namespace Cosmos.Kernel.Tests.IO
private bool ExecuteFileStreamTests = false;
private Sys.FileSystem.VFS.VFSBase mVFS;
private byte[] xBytes = new byte[16]
static readonly public byte[] xBytes = new byte[16]
{
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
@ -31,22 +31,14 @@ namespace Cosmos.Kernel.Tests.IO
{
try
{
TestMemoryStreamByte();
TestMemoryStreamReadBuffer();
mDebugger.Send("Run");
using (var xMS = new MemoryStream())
{
TestBinaryWriterOnMemoryStream(xMS);
}
using (var nMS = new MemoryStream(xBytes))
{
TestBinaryReaderOnMemoryStream(nMS);
}
MemoryStreamTest.Execute(mDebugger);
BinaryWriterTest.Execute(mDebugger);
BinaryReaderTest.Execute(mDebugger);
StringReaderTest.Execute(mDebugger);
StringWriterTest.Execute(mDebugger);
if (ExecuteFileStreamTests)
{
// TestMemoryStreamFromFileStream();
}
TestController.Completed();
}
catch(Exception e)
@ -56,185 +48,5 @@ namespace Cosmos.Kernel.Tests.IO
TestController.Failed();
}
}
#region System.IO.MemoryStream Tests
private void TestMemoryStreamByte()
{
mDebugger.Send("START TEST: MemoryStream:");
mDebugger.Send("Start writing");
using (var xMS = new MemoryStream())
{
byte bWrite = 0x30;
xMS.WriteByte(bWrite);
mDebugger.Send("Byte written");
xMS.Position = 0;
byte bRead = (byte)xMS.ReadByte();
mDebugger.Send("Written byte: " + bWrite.ToString() + " Read byte: " + bRead.ToString());
Assert.IsTrue(bWrite == bRead, "Failed to write a byte and read it from memory.");
}
mDebugger.Send("END TEST");
}
private void TestMemoryStreamReadBuffer()
{
mDebugger.Send("START TEST: Create MemoryStream from byte array and read its bytes:");
mDebugger.Send("Loading buffer");
using (MemoryStream xMS = new MemoryStream(xBytes))
{
mDebugger.Send("Buffer loaded into memory");
xMS.Position = 0;
byte[] xBuffer = xMS.ToArray();
foreach (byte b in xBuffer)
{
mDebugger.Send("Byte: " + b.ToString());
}
Assert.IsTrue(ByteArrayAreEquals(xBytes, xBuffer), "Buffer was modified during its loading to memory.");
}
mDebugger.Send("END TEST");
}
private void TestMemoryStreamFromFileStream()
{
// For the following test to run we need .Net Core 2.1 or CopyTo has to be plugged
//
//using (var xFS = new FileStream(@"0:\test.txt", FileMode.OpenOrCreate))
//{
// mDebugger.Send(@"Writing bytes to file 0:\test.txt");
// xFS.Write(xBytes, 0, xBytes.Length);
//}
//using (var xFS = new FileStream(@"0:\test.txt", FileMode.Open))
//{
// mDebugger.Send(@"Reading bytes from file 0:\test.txt");
// byte[] xFileBytes = new byte[16];
// xFS.Read(xFileBytes, 0, xFileBytes.Length);
// Assert.IsTrue(ByteArrayAreEquals(xBytes, xFileBytes), "Bytes changed during FileStream write and read operations.");
// using (MemoryStream xMS = new MemoryStream())
// {
// xFS.CopyTo(xMS);
// byte[] xMemoryBytes = new byte[16];
// mDebugger.Send("Reading bytes from MemoryStream");
// xMS.Position = 0;
// xMS.Read(xMemoryBytes, 0, xMemoryBytes.Length);
// Assert.IsTrue(ByteArrayAreEquals(xBytes, xMemoryBytes), "Bytes changed during MemoryStream write and read operations.");
// }
//}
//mDebugger.Send("END TEST");
}
#endregion
#region System.IO.BinaryWriter Tests
private void TestBinaryWriterOnMemoryStream(MemoryStream xMS)
{
mDebugger.Send("START TEST: Write on MemoryStream using BinaryWriter");
mDebugger.Send("Writing data");
using (var xBW = new BinaryWriter(xMS))
{
xBW.Write(xBytes);
mDebugger.Send("Bytes written");
xMS.Position = 0;
long lengthO = xMS.Length;
int lengthN = xBytes.Length;
Assert.IsTrue(lengthO == lengthN, "Failed to write bytes to MemoryStream");
}
mDebugger.Send("END TEST");
}
#endregion
#region System.IO.BinaryReader Tests
private void TestBinaryReaderOnMemoryStream(MemoryStream xMS)
{
mDebugger.Send("START TEST: Read from MemoryStream using BinaryReader");
mDebugger.Send("Writing data");
byte[] xBuffer = new byte[16];
using (var xBR = new BinaryReader(xMS))
{
xBR.Read(xBuffer, 0, xBuffer.Length);
mDebugger.Send("Data retrieved");
foreach (byte b in xBuffer)
{
mDebugger.Send("Byte: " + b.ToString());
}
Assert.IsTrue(ByteArrayAreEquals(xBytes, xBuffer), "Bytes changed during BinaryWriter and BinaryReader opeartions on MemoryStream");
}
mDebugger.Send("END TEST");
}
#endregion
#region Helper Methods
/// <summary>
/// Utility method to test Byte[] equality.
/// </summary>
/// <param name="a1">Byte array.</param>
/// <param name="a2">Byte array.</param>
/// <returns>True if the elements in the arrays are equal otherwise false.</returns>
private bool ByteArrayAreEquals(byte[] a1, byte[] a2)
{
if (ReferenceEquals(a1, a2))
{
mDebugger.Send("a1 and a2 are the same Object");
return true;
}
if (a1 == null || a2 == null)
{
mDebugger.Send("a1 or a2 is null so are different");
return false;
}
if (a1.Length != a2.Length)
{
mDebugger.Send("a1.Length != a2.Length so are different");
return false;
}
for (int i = 0; i < a1.Length; i++)
{
if (a1[i] != a2[i])
{
mDebugger.Send("In position " + i + " a byte is different(" + a1[i].ToString() + " vs " + a2[i].ToString() + ")");
return false;
}
}
return true;
}
#endregion
}
}

View file

@ -0,0 +1,42 @@
using System.IO;
using Cosmos.TestRunner;
using Cosmos.Debug.Kernel;
namespace Cosmos.Kernel.Tests.IO.System.IO
{
public class BinaryReaderTest
{
/// <summary>
/// Tests System.IO.BinaryReader plugs.
/// </summary>
public static void Execute(Debugger mDebugger)
{
TestBinaryReaderOnMemoryStream(mDebugger);
}
private static void TestBinaryReaderOnMemoryStream(Debugger mDebugger)
{
mDebugger.Send("START TEST: Read from MemoryStream using BinaryReader");
mDebugger.Send("Writing data");
byte[] xBuffer = new byte[16];
using (var xMS = new MemoryStream(Kernel.xBytes))
using (var xBR = new BinaryReader(xMS))
{
xBR.Read(xBuffer, 0, xBuffer.Length);
mDebugger.Send("Data retrieved");
foreach (byte b in xBuffer)
{
mDebugger.Send("Byte: " + b.ToString());
}
Assert.IsTrue(HelperMethods.ByteArrayAreEquals(Kernel.xBytes, xBuffer), "Bytes changed during BinaryWriter and BinaryReader opeartions on MemoryStream");
}
mDebugger.Send("END TEST");
}
}
}

View file

@ -0,0 +1,37 @@
using System.IO;
using Cosmos.TestRunner;
using Cosmos.Debug.Kernel;
namespace Cosmos.Kernel.Tests.IO.System.IO
{
public class BinaryWriterTest
{
/// <summary>
/// Tests System.IO.BinaryWriter plugs.
/// </summary>
public static void Execute(Debugger mDebugger)
{
TestBinaryWriterOnMemoryStream(mDebugger);
}
private static void TestBinaryWriterOnMemoryStream(Debugger mDebugger)
{
mDebugger.Send("START TEST: Write on MemoryStream using BinaryWriter");
mDebugger.Send("Writing data");
using (var xMS = new MemoryStream())
using (var xBW = new BinaryWriter(xMS))
{
xBW.Write(Kernel.xBytes);
mDebugger.Send("Bytes written");
xMS.Position = 0;
long lengthO = xMS.Length;
int lengthN = Kernel.xBytes.Length;
Assert.IsTrue(lengthO == lengthN, "Failed to write bytes to MemoryStream");
}
mDebugger.Send("END TEST");
}
}
}

View file

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Cosmos.Kernel.Tests.IO.System.IO
{
class HelperMethods
{
/// <summary>
/// Utility method to test Byte[] equality.
/// </summary>
/// <param name="a1">Byte array.</param>
/// <param name="a2">Byte array.</param>
/// <returns>True if the elements in the arrays are equal otherwise false.</returns>
public static bool ByteArrayAreEquals(byte[] a1, byte[] a2)
{
if (ReferenceEquals(a1, a2))
{
//mDebugger.Send("a1 and a2 are the same Object");
return true;
}
if (a1 == null || a2 == null)
{
//mDebugger.Send("a1 or a2 is null so are different");
return false;
}
if (a1.Length != a2.Length)
{
//mDebugger.Send("a1.Length != a2.Length so are different");
return false;
}
for (int i = 0; i < a1.Length; i++)
{
if (a1[i] != a2[i])
{
//mDebugger.Send("In position " + i + " a byte is different");
return false;
}
}
return true;
}
/// <summary>
/// Utility method to test string[] equality.
/// </summary>
/// <param name="a1">String array.</param>
/// <param name="a2">String array.</param>
/// <returns>True if the elements in the arrays are equal otherwise false.</returns>
public static bool StringArrayAreEquals(string[] a1, string[] a2)
{
if (ReferenceEquals(a1, a2))
{
//mDebugger.Send("a1 and a2 are the same Object");
return true;
}
if (a1 == null || a2 == null)
{
//mDebugger.Send("a1 or a2 is null so are different");
return false;
}
if (a1.Length != a2.Length)
{
//mDebugger.Send("a1.Length != a2.Length so are different");
return false;
}
for (int i = 0; i < a1.Length; i++)
{
if (a1[i] != a2[i])
{
//mDebugger.Send("In position " + i + " a String is different");
return false;
}
}
return true;
}
}
}

View file

@ -0,0 +1,104 @@
using System.IO;
using Cosmos.TestRunner;
using Cosmos.Debug.Kernel;
namespace Cosmos.Kernel.Tests.IO.System.IO
{
public class MemoryStreamTest
{
/// <summary>
/// Tests System.IO.MemoryStream plugs.
/// </summary>
public static void Execute(Debugger mDebugger)
{
TestMemoryStreamByte(mDebugger);
TestMemoryStreamReadBuffer(mDebugger);
/* It needs Stream.CopyTo() to be plugged */
//if (ExecuteFileStreamTests)
//{
//TestMemoryStreamFromFileStream();
//}
}
private static void TestMemoryStreamByte(Debugger mDebugger)
{
mDebugger.Send("START TEST: MemoryStream:");
mDebugger.Send("Start writing");
using (var xMS = new MemoryStream())
{
byte bWrite = 0x30;
xMS.WriteByte(bWrite);
mDebugger.Send("Byte written");
xMS.Position = 0;
byte bRead = (byte)xMS.ReadByte();
mDebugger.Send("Written byte: " + bWrite.ToString() + " Read byte: " + bRead.ToString());
Assert.IsTrue(bWrite == bRead, "Failed to write a byte and read it from memory.");
}
mDebugger.Send("END TEST");
}
private static void TestMemoryStreamReadBuffer(Debugger mDebugger)
{
mDebugger.Send("START TEST: Create MemoryStream from byte array and read its bytes:");
mDebugger.Send("Loading buffer");
using (MemoryStream xMS = new MemoryStream(Kernel.xBytes))
{
mDebugger.Send("Buffer loaded into memory");
xMS.Position = 0;
byte[] xBuffer = xMS.ToArray();
foreach (byte b in xBuffer)
{
mDebugger.Send("Byte: " + b.ToString());
}
Assert.IsTrue(HelperMethods.ByteArrayAreEquals(Kernel.xBytes, xBuffer), "Buffer was modified during its loading to memory.");
}
mDebugger.Send("END TEST");
}
private static void TestMemoryStreamFromFileStream(Debugger mDebugger)
{
// For the following test to run we need .Net Core 2.1 or CopyTo has to be plugged
//
//using (var xFS = new FileStream(@"0:\test.txt", FileMode.OpenOrCreate))
//{
// mDebugger.Send(@"Writing bytes to file 0:\test.txt");
// xFS.Write(Kernel.xBytes, 0, Kernel.xBytes.Length);
//}
//using (var xFS = new FileStream(@"0:\test.txt", FileMode.Open))
//{
// mDebugger.Send(@"Reading bytes from file 0:\test.txt");
// byte[] xFileBytes = new byte[16];
// xFS.Read(xFileBytes, 0, xFileBytes.Length);
// Assert.IsTrue(ByteArrayAreEquals(Kernel.xBytes, xFileBytes), "Bytes changed during FileStream write and read operations.");
// using (MemoryStream xMS = new MemoryStream())
// {
// xFS.CopyTo(xMS);
// byte[] xMemoryBytes = new byte[16];
// mDebugger.Send("Reading bytes from MemoryStream");
// xMS.Position = 0;
// xMS.Read(xMemoryBytes, 0, xMemoryBytes.Length);
// Assert.IsTrue(ByteArrayAreEquals(Kernel.xBytes, xMemoryBytes), "Bytes changed during MemoryStream write and read operations.");
// }
//}
//mDebugger.Send("END TEST");
}
}
}

View file

@ -0,0 +1,118 @@
using System.IO;
using Cosmos.TestRunner;
using Cosmos.Debug.Kernel;
using System;
using System.Text;
namespace Cosmos.Kernel.Tests.IO.System.IO
{
class StringReaderTest
{
/// <summary>
/// Tests System.IO.StringReaderTest plugs.
/// </summary>
public static void Execute(Debugger mDebugger)
{
mDebugger.Send("START TEST: StreamWriter:");
StringBuilder stringToRead = new StringBuilder();
stringToRead.AppendLine("Characters in 1st line to read");
stringToRead.AppendLine("and 2nd line");
stringToRead.AppendLine("and the end");
mDebugger.Send("Testing ReadLine");
string Line;
using (StringReader reader = new StringReader(stringToRead.ToString()))
{
Line = reader.ReadLine();
Assert.IsTrue(Line == "Characters in 1st line to read", "StringReader.ReadLine() #1 does not work");
Line = reader.ReadLine();
Assert.IsTrue(Line == "and 2nd line", "StringReader.ReadLine() #2 does not work");
Line = reader.ReadLine();
Assert.IsTrue(Line == "and the end", "StringReader.ReadLine() #3 does not work");
// The text is finished we should read null to indicate EOF
Line = reader.ReadLine();
Assert.IsTrue(Line == null, "StringReader.ReadLine() #4 does not work");
}
mDebugger.Send("Testing Read");
using (StringReader reader = new StringReader("123"))
{
int val;
val = reader.Read();
Assert.IsTrue(val != -1, "StringReader.Read #1 does not work returns -1");
Assert.IsTrue((char)val == '1', "StringReader.Read #1 does not work: read wrong value");
val = reader.Read();
Assert.IsTrue(val != -1, "StringReader.Read #2 does not work returns -1");
Assert.IsTrue((char)val == '2', "StringReader.Read #2 does not work: read wrong value");
val = reader.Read();
Assert.IsTrue(val != -1, "StringReader.Read #3 does not work returns -1");
Assert.IsTrue((char)val == '3', "StringReader.Read #3 does not work: read wrong value");
val = reader.Read();
Assert.IsTrue(val == -1, "StringReader.Read #4 does not work EOF reached but it does not return -1");
}
mDebugger.Send("Testing ReadBlock");
using (StringReader reader = new StringReader("123"))
{
int count;
int charsRead;
char[] buffer = new char[3];
count = buffer.Length;
charsRead = reader.ReadBlock(buffer, 0, count);
Assert.IsTrue(charsRead == count, "StringReader.ReadBlock does not work the expected number of characters has not been read");
Assert.IsTrue(new string(buffer, 0, charsRead) == "123", "StringReader.ReadBlock does not work");
}
mDebugger.Send("Testing ReadBlock (with bigger buffer)");
using (StringReader reader = new StringReader("123"))
{
int count;
int charsRead;
char[] buffer = new char[4];
count = buffer.Length;
charsRead = reader.ReadBlock(buffer, 0, count);
// It should always read 3 chars (not 4 as count is)
Assert.IsTrue(charsRead == 3, "StringReader.ReadBlock read more characters that in the sting are");
Assert.IsTrue(charsRead != count, "StringReader.ReadBlock does not work the expected number of characters has not been read");
Assert.IsTrue(new string(buffer, 0, charsRead) == "123", "StringReader.ReadBlock does not work");
}
using (StringReader reader = new StringReader("12345"))
{
mDebugger.Send("Testing ReadBlock (with smaller buffer)");
int count;
int charsRead;
char[] buffer = new char[2];
count = buffer.Length;
charsRead = reader.ReadBlock(buffer, 0, count);
Assert.IsTrue(charsRead == count, "StringReader.ReadBlock does not work the expected number of characters has not been read");
Assert.IsTrue(new string(buffer, 0, charsRead) == "12", "StringReader.ReadBlock does not work");
mDebugger.Send("Testing ReadToEnd");
var EndOfReader = reader.ReadToEnd();
Assert.IsTrue(EndOfReader == "345", "StringReader.ReadToEnd does not work");
}
mDebugger.Send("END TEST");
}
}
}

View file

@ -0,0 +1,51 @@
using System.IO;
using Cosmos.TestRunner;
using Cosmos.Debug.Kernel;
using System;
using System.Text;
namespace Cosmos.Kernel.Tests.IO.System.IO
{
class StringWriterTest
{
/// <summary>
/// Tests System.IO.StringReaderTest plugs.
/// </summary>
public static void Execute(Debugger mDebugger)
{
StringWriter strWriter = new StringWriter();
strWriter.Write("This");
Assert.IsTrue(strWriter.ToString() == "This", "StringWriter.Write(String) does not work");
strWriter.Write(' ');
Assert.IsTrue(strWriter.ToString() == "This ", "StringWriter.Write(Char) does not work");
// This is a little weird in C#
char[] arr = { 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't' };
strWriter.Write(arr);
Assert.IsTrue(strWriter.ToString() == "This is a test", "StringWriter.Write(Char[]) does not work");
StringBuilder sb = strWriter.GetStringBuilder();
Assert.IsTrue(strWriter.ToString() == sb.ToString(), "StringWriter.GetStringBuilder() does not work");
// These need again NumberBuffer working. It should be all managed on Net Core 2.1 so let's wait for it
#if false
strWriter = new StringWriter();
strWriter.Write(1);
Assert.IsTrue(strWriter.ToString() == "1", "StringWriter.Write(Int[]) does not work");
strWriter.Write(' ');
strWriter.Write(42.42);
Assert.IsTrue(strWriter.ToString() == "1 42.42", "StringWriter.Write(Double[]) does not work");
#endif
}
}
}