Commit 9d66af6e authored by evolutional's avatar evolutional

Ported some of the python fuzz tests to C#

* Refactored the test runner to use attribute based test discovery
* Ported value and vtable/object fuzzing tests from python to C#
parent 94680f54
......@@ -39,6 +39,25 @@ namespace FlatBuffers.Test
}
}
public class AssertArrayFailedException : Exception
{
private readonly int _index;
private readonly object _expected;
private readonly object _actual;
public AssertArrayFailedException(int index, object expected, object actual)
{
_index = index;
_expected = expected;
_actual = actual;
}
public override string Message
{
get { return string.Format("Expected {0} at index {1} but saw {2}", _expected, _index, _actual); }
}
}
public class AssertUnexpectedThrowException : Exception
{
private readonly object _expected;
......@@ -64,6 +83,22 @@ namespace FlatBuffers.Test
}
}
public static void ArrayEqual<T>(T[] expected, T[] actual)
{
if (expected.Length != actual.Length)
{
throw new AssertFailedException(expected, actual);
}
for(var i = 0; i < expected.Length; ++i)
{
if (!expected[i].Equals(actual[i]))
{
throw new AssertArrayFailedException(i, expected, actual);
}
}
}
public static void IsTrue(bool value)
{
if (!value)
......
......@@ -18,9 +18,11 @@ using System;
namespace FlatBuffers.Test
{
[FlatBuffersTestClass]
public class ByteBufferTests
{
[FlatBuffersTestMethod]
public void ByteBuffer_Length_MatchesBufferLength()
{
var buffer = new byte[1000];
......@@ -28,6 +30,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(buffer.Length, uut.Length);
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutBytePopulatesBufferAtZeroOffset()
{
var buffer = new byte[1];
......@@ -37,6 +40,7 @@ namespace FlatBuffers.Test
Assert.AreEqual((byte)99, buffer[0]);
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutByteCannotPutAtOffsetPastLength()
{
var buffer = new byte[1];
......@@ -44,6 +48,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutByte(1, 99));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortPopulatesBufferCorrectly()
{
var buffer = new byte[2];
......@@ -55,6 +60,7 @@ namespace FlatBuffers.Test
Assert.AreEqual((byte)0, buffer[1]);
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortCannotPutAtOffsetPastLength()
{
var buffer = new byte[2];
......@@ -62,6 +68,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(2, 99));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLength()
{
var buffer = new byte[1];
......@@ -69,6 +76,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(0, 99));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLengthAndOffset()
{
var buffer = new byte[2];
......@@ -76,6 +84,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(1, 99));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntPopulatesBufferCorrectly()
{
var buffer = new byte[4];
......@@ -89,6 +98,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(0x0A, buffer[3]);
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntCannotPutAtOffsetPastLength()
{
var buffer = new byte[4];
......@@ -96,6 +106,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLength()
{
var buffer = new byte[1];
......@@ -103,6 +114,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(0, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLengthAndOffset()
{
var buffer = new byte[4];
......@@ -110,6 +122,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongPopulatesBufferCorrectly()
{
var buffer = new byte[8];
......@@ -127,6 +140,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(0x01, buffer[7]);
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongCannotPutAtOffsetPastLength()
{
var buffer = new byte[8];
......@@ -134,6 +148,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLength()
{
var buffer = new byte[1];
......@@ -141,6 +156,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(0, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLengthAndOffset()
{
var buffer = new byte[8];
......@@ -148,6 +164,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetByteReturnsCorrectData()
{
var buffer = new byte[1];
......@@ -156,6 +173,7 @@ namespace FlatBuffers.Test
Assert.AreEqual((byte)99, uut.Get(0));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetByteChecksOffset()
{
var buffer = new byte[1];
......@@ -163,6 +181,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(()=>uut.Get(1));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortReturnsCorrectData()
{
var buffer = new byte[2];
......@@ -172,6 +191,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(1, uut.GetShort(0));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksOffset()
{
var buffer = new byte[2];
......@@ -179,6 +199,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(2));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksLength()
{
var buffer = new byte[2];
......@@ -186,6 +207,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(1));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntReturnsCorrectData()
{
var buffer = new byte[4];
......@@ -197,6 +219,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksOffset()
{
var buffer = new byte[4];
......@@ -204,6 +227,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(4));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksLength()
{
var buffer = new byte[2];
......@@ -211,6 +235,7 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(0));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongReturnsCorrectData()
{
var buffer = new byte[8];
......@@ -226,6 +251,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(0x010203040A0B0C0D, uut.GetLong(0));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksOffset()
{
var buffer = new byte[8];
......@@ -233,39 +259,44 @@ namespace FlatBuffers.Test
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(8));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksLength()
{
var buffer = new byte[7];
var uut = new ByteBuffer(buffer);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0));
}
[FlatBuffersTestMethod]
public void ByteBuffer_ReverseBytesUshort()
{
ushort original = (ushort)0x1234U;
ushort reverse = ByteBuffer.ReverseBytes(original);
const ushort original = (ushort)0x1234U;
var reverse = ByteBuffer.ReverseBytes(original);
Assert.AreEqual(0x3412U, reverse);
ushort rereverse = ByteBuffer.ReverseBytes(reverse);
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
[FlatBuffersTestMethod]
public void ByteBuffer_ReverseBytesUint()
{
uint original = 0x12345678;
uint reverse = ByteBuffer.ReverseBytes(original);
const uint original = 0x12345678;
var reverse = ByteBuffer.ReverseBytes(original);
Assert.AreEqual(0x78563412U, reverse);
uint rereverse = ByteBuffer.ReverseBytes(reverse);
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
[FlatBuffersTestMethod]
public void ByteBuffer_ReverseBytesUlong()
{
ulong original = 0x1234567890ABCDEFUL;
ulong reverse = ByteBuffer.ReverseBytes(original);
const ulong original = 0x1234567890ABCDEFUL;
var reverse = ByteBuffer.ReverseBytes(original);
Assert.AreEqual(0xEFCDAB9078563412UL, reverse);
ulong rereverse = ByteBuffer.ReverseBytes(reverse);
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
}
......
......@@ -41,9 +41,9 @@
<Compile Include="..\..\net\FlatBuffers\ByteBuffer.cs">
<Link>FlatBuffers\ByteBuffer.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\Offset.cs">
<Link>FlatBuffers\Offset.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\Offset.cs">
<Link>FlatBuffers\Offset.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\FlatBufferBuilder.cs">
<Link>FlatBuffers\FlatBufferBuilder.cs</Link>
</Compile>
......@@ -79,9 +79,15 @@
</Compile>
<Compile Include="Assert.cs" />
<Compile Include="ByteBufferTests.cs" />
<Compile Include="FlatBuffersFuzzTests.cs" />
<Compile Include="FlatBuffersTestClassAttribute.cs" />
<Compile Include="FlatBuffersTestMethodAttribute.cs" />
<Compile Include="FuzzTestData.cs" />
<Compile Include="Lcg.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FlatBuffersExampleTests.cs" />
<Compile Include="TestTable.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\monsterdata_test.mon">
......
......@@ -19,6 +19,7 @@ using MyGame.Example;
namespace FlatBuffers.Test
{
[FlatBuffersTestClass]
public class FlatBuffersExampleTests
{
public void RunTests()
......@@ -28,6 +29,7 @@ namespace FlatBuffers.Test
TestEnums();
}
[FlatBuffersTestMethod]
public void CanCreateNewFlatBufferFromScratch()
{
// Second, let's create a FlatBuffer from scratch in C#, and test it also.
......@@ -184,6 +186,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(false, monster.Testbool);
}
[FlatBuffersTestMethod]
public void CanReadCppGeneratedWireFile()
{
var data = File.ReadAllBytes(@"Resources/monsterdata_test.mon");
......@@ -191,6 +194,7 @@ namespace FlatBuffers.Test
TestBuffer(bb);
}
[FlatBuffersTestMethod]
public void TestEnums()
{
Assert.AreEqual("Red", Color.Red.ToString());
......
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FlatBuffers.Test
{
[AttributeUsage(AttributeTargets.Class)]
public class FlatBuffersTestClassAttribute : Attribute
{
}
}
using System;
namespace FlatBuffers.Test
{
[AttributeUsage(AttributeTargets.Method)]
public class FlatBuffersTestMethodAttribute : Attribute
{
}
}
\ No newline at end of file
using System;
namespace FlatBuffers.Test
{
internal static class FuzzTestData
{
private static readonly byte[] _overflowInt32 = new byte[] {0x83, 0x33, 0x33, 0x33};
private static readonly byte[] _overflowInt64 = new byte[] { 0x84, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44 };
public static readonly bool BoolValue = true;
public static readonly sbyte Int8Value = -127; // 0x81
public static readonly byte UInt8Value = 255; // 0xFF
public static readonly short Int16Value = -32222; // 0x8222;
public static readonly ushort UInt16Value = 65262; // 0xFEEE
public static readonly int Int32Value = BitConverter.ToInt32(_overflowInt32, 0);
public static readonly uint UInt32Value = 0xFDDDDDDD;
public static readonly long Int64Value = BitConverter.ToInt64(_overflowInt64, 0);
public static readonly ulong UInt64Value = 0xFCCCCCCCCCCCCCCC;
public static readonly float Float32Value = 3.14159f;
public static readonly double Float64Value = 3.14159265359;
}
}
\ No newline at end of file
namespace FlatBuffers.Test
{
/// <summary>
/// Lcg Pseudo RNG
/// </summary>
internal sealed class Lcg
{
private const uint InitialValue = 10000;
private uint _state;
public Lcg()
{
_state = InitialValue;
}
public uint Next()
{
return (_state = 69069 * _state + 362437);
}
public void Reset()
{
_state = InitialValue;
}
}
}
\ No newline at end of file
......@@ -15,6 +15,7 @@
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
......@@ -24,39 +25,43 @@ namespace FlatBuffers.Test
{
public static int Main(string[] args)
{
var tests = new FlatBuffersExampleTests();
try
{
tests.RunTests();
}
catch (Exception ex)
{
Console.WriteLine("FlatBuffersExampleTests FAILED - {0}", ex.GetBaseException());
return -1;
}
var testResults = new List<bool>();
// Run ByteBuffers Tests
var testClass = new ByteBufferTests();
var testClasses = Assembly.GetExecutingAssembly().GetExportedTypes()
.Where(t => t.IsClass && t.GetCustomAttributes(typeof (FlatBuffersTestClassAttribute), false).Length > 0);
var methods = testClass.GetType().GetMethods(BindingFlags.Public |
BindingFlags.Instance)
.Where(m => m.Name.StartsWith("ByteBuffer_"));
foreach (var method in methods)
foreach (var testClass in testClasses)
{
try
{
method.Invoke(testClass, new object[] { });
}
catch (Exception ex)
var methods = testClass.GetMethods(BindingFlags.Public |
BindingFlags.Instance)
.Where(m => m.GetCustomAttributes(typeof(FlatBuffersTestMethodAttribute), false).Length > 0);
var inst = Activator.CreateInstance(testClass);
foreach (var method in methods)
{
Console.WriteLine("ByteBufferTests FAILED when invoking {0} with error {1}",
method.Name, ex.GetBaseException());
return -1;
try
{
method.Invoke(inst, new object[] { });
testResults.Add(true);
}
catch (Exception ex)
{
Console.WriteLine("{0}: FAILED when invoking {1} with error {2}",
testClass.Name ,method.Name, ex.GetBaseException());
testResults.Add(false);
}
}
}
Console.WriteLine("FlatBuffers test: completed successfully");
var failedCount = testResults.Count(i => i == false);
Console.WriteLine("{0} tests run, {1} failed", testResults.Count, failedCount);
if (failedCount > 0)
{
return -1;
}
return 0;
}
}
......
namespace FlatBuffers.Test
{
/// <summary>
/// A test Table object that gives easy access to the slot data
/// </summary>
internal class TestTable : Table
{
public TestTable(ByteBuffer bb, int pos)
{
base.bb = bb;
base.bb_pos = pos;
}
public bool GetSlot(int slot, bool def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetSbyte(bb_pos + off) != 0;
}
public sbyte GetSlot(int slot, sbyte def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetSbyte(bb_pos + off);
}
public byte GetSlot(int slot, byte def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.Get(bb_pos + off);
}
public short GetSlot(int slot, short def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetShort(bb_pos + off);
}
public ushort GetSlot(int slot, ushort def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetUshort(bb_pos + off);
}
public int GetSlot(int slot, int def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetInt(bb_pos + off);
}
public uint GetSlot(int slot, uint def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetUint(bb_pos + off);
}
public long GetSlot(int slot, long def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetLong(bb_pos + off);
}
public ulong GetSlot(int slot, ulong def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetUlong(bb_pos + off);
}
public float GetSlot(int slot, float def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetFloat(bb_pos + off);
}
public double GetSlot(int slot, double def)
{
var off = base.__offset(slot);
if (off == 0)
{
return def;
}
return bb.GetDouble(bb_pos + off);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment