cauterize 0.0.1.pre1 → 0.0.1.pre5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/.rspec +1 -1
- data/Gemfile +1 -1
- data/README.md +0 -2
- data/Rakefile +19 -3
- data/bin/cauterize +14 -6
- data/example/Cauterize +22 -10
- data/example/build.sh +15 -2
- data/lib/cauterize/base_type.rb +2 -5
- data/lib/cauterize/builders.rb +1 -3
- data/lib/cauterize/builders/c/buildable.rb +3 -2
- data/lib/cauterize/builders/c/builtin.rb +46 -0
- data/lib/cauterize/builders/c/enumeration.rb +23 -2
- data/lib/cauterize/builders/c/fixed_array.rb +16 -7
- data/lib/cauterize/builders/c/group.rb +32 -3
- data/lib/cauterize/builders/c/scalar.rb +5 -0
- data/lib/cauterize/builders/c/variable_array.rb +1 -1
- data/lib/cauterize/builders/cs/buildable.rb +59 -0
- data/lib/cauterize/builders/cs/builtin.rb +23 -0
- data/lib/cauterize/builders/cs/composite.rb +21 -0
- data/lib/cauterize/builders/cs/csarray.rb +32 -0
- data/lib/cauterize/builders/cs/enumeration.rb +21 -0
- data/lib/cauterize/builders/cs/fixed_array.rb +25 -0
- data/lib/cauterize/builders/cs/group.rb +33 -0
- data/lib/cauterize/builders/cs/scalar.rb +10 -0
- data/lib/cauterize/builders/cs/variable_array.rb +34 -0
- data/lib/cauterize/builtin.rb +52 -0
- data/lib/cauterize/c_builder.rb +15 -1
- data/lib/cauterize/cauterize.rb +44 -13
- data/lib/cauterize/composite.rb +3 -3
- data/lib/cauterize/cs_builder.rb +53 -0
- data/lib/cauterize/enumeration.rb +20 -3
- data/lib/cauterize/fixed_array.rb +3 -3
- data/lib/cauterize/formatter.rb +7 -3
- data/lib/cauterize/group.rb +5 -8
- data/lib/cauterize/scalar.rb +16 -7
- data/lib/cauterize/variable_array.rb +6 -6
- data/lib/cauterize/version.rb +1 -1
- data/spec/base_type_spec.rb +133 -125
- data/spec/builders/c/buildable_spec.rb +18 -18
- data/spec/builders/c/builtin_spec.rb +22 -0
- data/spec/builders/c/composite_spec.rb +37 -33
- data/spec/builders/c/enumeration_spec.rb +84 -21
- data/spec/builders/c/fixed_array_spec.rb +6 -6
- data/spec/builders/c/group_spec.rb +97 -90
- data/spec/builders/c/scalar_spec.rb +24 -6
- data/spec/builders/c/variable_array_spec.rb +37 -37
- data/spec/builders/cs/buildable_spec.rb +8 -0
- data/spec/builders/cs/composite_spec.rb +32 -0
- data/spec/builders/cs/enumeration_spec.rb +33 -0
- data/spec/builders/cs/fixed_array_spec.rb +40 -0
- data/spec/builders/cs/group_spec.rb +56 -0
- data/spec/builders/cs/scalar_spec.rb +7 -0
- data/spec/builders/cs/variable_array_spec.rb +46 -0
- data/spec/builders_spec.rb +38 -38
- data/spec/builtin_spec.rb +46 -0
- data/spec/c_builder_spec.rb +116 -102
- data/spec/cauterize_spec.rb +8 -1
- data/spec/composite_spec.rb +52 -48
- data/spec/cs_builder_spec.rb +113 -0
- data/spec/enumeration_spec.rb +55 -16
- data/spec/fixed_array_spec.rb +7 -9
- data/spec/group_spec.rb +81 -76
- data/spec/scalar_spec.rb +20 -10
- data/spec/spec_helper.rb +103 -94
- data/spec/support/shared_examples_for_c_buildables.rb +68 -64
- data/spec/variable_array_spec.rb +12 -17
- data/{c → support/c}/src/cauterize.c +8 -7
- data/support/c/src/cauterize.h +59 -0
- data/{c → support/c}/src/cauterize_debug.h +0 -0
- data/support/c/src/cauterize_util.h +49 -0
- data/{c → support/c}/test/greatest.h +0 -0
- data/{c → support/c}/test/test.c +0 -0
- data/support/cs/src/CauterizeCompositeFormatter.cs +34 -0
- data/support/cs/src/CauterizeContainerFormatter.cs +18 -0
- data/support/cs/src/CauterizeEnumFormatter.cs +67 -0
- data/support/cs/src/CauterizeException.cs +15 -0
- data/support/cs/src/CauterizeFixedArrayFormatter.cs +39 -0
- data/support/cs/src/CauterizeFormatter.cs +40 -0
- data/support/cs/src/CauterizeGroupFormatter.cs +46 -0
- data/support/cs/src/CauterizePrimitiveFormatter.cs +33 -0
- data/support/cs/src/CauterizeTypeFormatterFactory.cs +39 -0
- data/support/cs/src/CauterizeTypes.cs +107 -0
- data/support/cs/src/CauterizeVariableArrayFormatter.cs +49 -0
- data/support/cs/src/ICauterizeTypeFormatter.cs +12 -0
- data/support/cs/src/OrderAttribute.cs +50 -0
- data/support/cs/src/PrimitiveSupport.cs +134 -0
- data/support/cs/src/SerializedRepresentationAttribute.cs +24 -0
- data/support/cs/test/CauterizeCompositeFormatterTest.cs +59 -0
- data/support/cs/test/CauterizeEnumFormatterTest.cs +110 -0
- data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +91 -0
- data/support/cs/test/CauterizeFormatterTest.cs +40 -0
- data/support/cs/test/CauterizeGroupFormatterTest.cs +147 -0
- data/support/cs/test/CauterizeIntegrationTest.cs +129 -0
- data/support/cs/test/CauterizePrimitiveFormatterTest.cs +98 -0
- data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +73 -0
- data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +130 -0
- data/support/cs/test/OrderAttributeTest.cs +39 -0
- data/support/cs/test/SerializedRepresentationAttributeTest.cs +39 -0
- metadata +68 -10
- data/c/src/cauterize.h +0 -46
- data/c/src/cauterize_util.h +0 -7
@@ -0,0 +1,12 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.IO;
|
4
|
+
|
5
|
+
namespace Cauterize
|
6
|
+
{
|
7
|
+
public interface ICauterizeTypeFormatter
|
8
|
+
{
|
9
|
+
object Deserialize(Stream serializationStream, Type t);
|
10
|
+
void Serialize(Stream serializationStream, object obj);
|
11
|
+
}
|
12
|
+
}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.ComponentModel;
|
4
|
+
using System.Reflection;
|
5
|
+
using System.Text;
|
6
|
+
|
7
|
+
namespace Cauterize
|
8
|
+
{
|
9
|
+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field,
|
10
|
+
Inherited = true, AllowMultiple = false)]
|
11
|
+
[ImmutableObject(true)]
|
12
|
+
public sealed class OrderAttribute : Attribute {
|
13
|
+
private readonly int _order;
|
14
|
+
public int Order { get { return _order; } }
|
15
|
+
public OrderAttribute(int order) {_order = order;}
|
16
|
+
public static IEnumerable<PropertyInfo> GetSortedProperties(Type t)
|
17
|
+
{
|
18
|
+
var propComp = new Comparison<PropertyInfo>(CompareProps);
|
19
|
+
var propList = new List<PropertyInfo>(t.GetProperties());
|
20
|
+
|
21
|
+
propList.Sort(propComp);
|
22
|
+
|
23
|
+
return propList;
|
24
|
+
}
|
25
|
+
|
26
|
+
public static PropertyInfo GetPropertyByOrder(Type t, int order)
|
27
|
+
{
|
28
|
+
foreach (var prop in t.GetProperties())
|
29
|
+
{
|
30
|
+
var orderAttr = ((OrderAttribute)prop.GetCustomAttributes(typeof(OrderAttribute), false)[0]);
|
31
|
+
|
32
|
+
if (orderAttr.Order == order)
|
33
|
+
{
|
34
|
+
return prop;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
return null;
|
39
|
+
}
|
40
|
+
|
41
|
+
private static int CompareProps(PropertyInfo a, PropertyInfo b)
|
42
|
+
{
|
43
|
+
OrderAttribute _a = (OrderAttribute)a.GetCustomAttributes(typeof(OrderAttribute), false)[0];
|
44
|
+
OrderAttribute _b = (OrderAttribute)b.GetCustomAttributes(typeof(OrderAttribute), false)[0];
|
45
|
+
|
46
|
+
return _a.Order - _b.Order;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
@@ -0,0 +1,134 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.Text;
|
4
|
+
|
5
|
+
namespace Cauterize
|
6
|
+
{
|
7
|
+
public class PrimitiveSupport
|
8
|
+
{
|
9
|
+
static public int TypeToByteSize(Type t)
|
10
|
+
{
|
11
|
+
if (t == typeof(Byte) || t == typeof(SByte))
|
12
|
+
{
|
13
|
+
return 1;
|
14
|
+
}
|
15
|
+
else if (t == typeof (UInt16) || t == typeof (Int16))
|
16
|
+
{
|
17
|
+
return 2;
|
18
|
+
}
|
19
|
+
else if (t == typeof (UInt32) || t == typeof (Int32))
|
20
|
+
{
|
21
|
+
return 4;
|
22
|
+
}
|
23
|
+
else if (t == typeof (UInt64) || t == typeof (Int64))
|
24
|
+
{
|
25
|
+
return 8;
|
26
|
+
}
|
27
|
+
else
|
28
|
+
{
|
29
|
+
throw new Exception("Invalid type to get byte size");
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
static public int TypeToInt(object value)
|
34
|
+
{
|
35
|
+
if (value is Int32)
|
36
|
+
{
|
37
|
+
return (int) value;
|
38
|
+
}
|
39
|
+
else if (value is Int16)
|
40
|
+
{
|
41
|
+
return (Int16)value;
|
42
|
+
}
|
43
|
+
else if (value is Byte)
|
44
|
+
{
|
45
|
+
return (Byte) value;
|
46
|
+
}
|
47
|
+
else if (value is UInt32)
|
48
|
+
{
|
49
|
+
return (int)(UInt32) value;
|
50
|
+
}
|
51
|
+
else if (value is UInt16)
|
52
|
+
{
|
53
|
+
return (UInt16) value;
|
54
|
+
}
|
55
|
+
else if (value is SByte)
|
56
|
+
{
|
57
|
+
return (SByte) value;
|
58
|
+
}
|
59
|
+
else if (value == null)
|
60
|
+
{
|
61
|
+
return 0;
|
62
|
+
}
|
63
|
+
else
|
64
|
+
{
|
65
|
+
throw new Exception("invalid array size type");
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
static public object IntToType(Type t, int value)
|
70
|
+
{
|
71
|
+
if (t == typeof(Int32))
|
72
|
+
{
|
73
|
+
return value;
|
74
|
+
}
|
75
|
+
else if (t == typeof(Int16))
|
76
|
+
{
|
77
|
+
return (Int16)value;
|
78
|
+
}
|
79
|
+
else if (t == typeof(Byte))
|
80
|
+
{
|
81
|
+
return (Byte)value;
|
82
|
+
}
|
83
|
+
else if (t == typeof (UInt32))
|
84
|
+
{
|
85
|
+
return (UInt32) value;
|
86
|
+
}
|
87
|
+
else if (t == typeof (UInt16))
|
88
|
+
{
|
89
|
+
return (UInt16) value;
|
90
|
+
}
|
91
|
+
else if (t == typeof (SByte))
|
92
|
+
{
|
93
|
+
return (SByte) value;
|
94
|
+
}
|
95
|
+
else
|
96
|
+
{
|
97
|
+
throw new Exception("Invalid type for array size type");
|
98
|
+
}
|
99
|
+
|
100
|
+
}
|
101
|
+
|
102
|
+
static public object TypeFromBytes(Type t, byte[] bytes)
|
103
|
+
{
|
104
|
+
if (t == typeof (Byte))
|
105
|
+
{
|
106
|
+
return bytes[0];
|
107
|
+
}
|
108
|
+
else if (t == typeof (SByte))
|
109
|
+
{
|
110
|
+
return (SByte) bytes[0];
|
111
|
+
}
|
112
|
+
else
|
113
|
+
{
|
114
|
+
return typeof(BitConverter)
|
115
|
+
.GetMethod("To" + t.Name, new Type[] {typeof(byte[]), typeof(int)})
|
116
|
+
.Invoke(null,new object[]{bytes, 0});
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
static public byte[] BytesFromValue(object value)
|
121
|
+
{
|
122
|
+
if (value is Byte || value is SByte)
|
123
|
+
{
|
124
|
+
return new[] {(byte) value};
|
125
|
+
}
|
126
|
+
else
|
127
|
+
{
|
128
|
+
return (byte[]) typeof (BitConverter)
|
129
|
+
.GetMethod("GetBytes", new Type[] {value.GetType()})
|
130
|
+
.Invoke(null, new object[] {value});
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.ComponentModel;
|
4
|
+
using System.Reflection;
|
5
|
+
using System.Text;
|
6
|
+
|
7
|
+
namespace Cauterize
|
8
|
+
{
|
9
|
+
[AttributeUsage(AttributeTargets.Enum, Inherited = true, AllowMultiple = false)]
|
10
|
+
[ImmutableObject(true)]
|
11
|
+
public sealed class SerializedRepresentationAttribute : Attribute {
|
12
|
+
public Type Type { get; private set; }
|
13
|
+
public SerializedRepresentationAttribute(Type type)
|
14
|
+
{
|
15
|
+
Type = type;
|
16
|
+
}
|
17
|
+
public static Type GetRepresentation(Type t)
|
18
|
+
{
|
19
|
+
var attrs = t.GetCustomAttributes(typeof(SerializedRepresentationAttribute), false);
|
20
|
+
return ((SerializedRepresentationAttribute)attrs[0]).Type;
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
using System;
|
2
|
+
using System.IO;
|
3
|
+
using System.Text;
|
4
|
+
using System.Collections.Generic;
|
5
|
+
using System.Linq;
|
6
|
+
using Cauterize;
|
7
|
+
using Moq;
|
8
|
+
using NUnit.Framework;
|
9
|
+
|
10
|
+
namespace Cauterize.Test
|
11
|
+
{
|
12
|
+
public class TestComposite : CauterizeComposite
|
13
|
+
{
|
14
|
+
[Order(0)]
|
15
|
+
public Int16 MyIntSmall { get; set; }
|
16
|
+
|
17
|
+
[Order(1)]
|
18
|
+
public Int32 MyIntNormal { get; set; }
|
19
|
+
}
|
20
|
+
|
21
|
+
[TestFixture]
|
22
|
+
public class CauterizeCompositeFormatterTest
|
23
|
+
{
|
24
|
+
[Test]
|
25
|
+
public void TestDeserialize()
|
26
|
+
{
|
27
|
+
var stream = new MemoryStream();
|
28
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
29
|
+
var sf1 = new Mock<ICauterizeTypeFormatter>();
|
30
|
+
var sf2 = new Mock<ICauterizeTypeFormatter>();
|
31
|
+
factory.Setup(f => f.GetFormatter(It.IsAny<Type>())).Returns((Type t) => t == typeof(Int16) ? sf1.Object : sf2.Object);
|
32
|
+
sf1.Setup(sf => sf.Deserialize(stream, typeof (Int16))).Returns((Int16) 5);
|
33
|
+
sf2.Setup(sf => sf.Deserialize(stream, typeof (Int32))).Returns(15);
|
34
|
+
var formatter = new CauterizeCompositeFormatter(factory.Object);
|
35
|
+
var output = (TestComposite) formatter.Deserialize(stream, typeof (TestComposite));
|
36
|
+
Assert.AreEqual(15, output.MyIntNormal);
|
37
|
+
Assert.AreEqual(5, output.MyIntSmall);
|
38
|
+
}
|
39
|
+
|
40
|
+
[Test]
|
41
|
+
public void TestSerialize()
|
42
|
+
{
|
43
|
+
var stream = new MemoryStream();
|
44
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
45
|
+
var sf1 = new Mock<ICauterizeTypeFormatter>();
|
46
|
+
var sf2 = new Mock<ICauterizeTypeFormatter>();
|
47
|
+
factory.Setup(f => f.GetFormatter(It.IsAny<Type>())).Returns((Type t) => t == typeof(Int16) ? sf1.Object : sf2.Object);
|
48
|
+
sf1.Setup(sf => sf.Serialize(stream, (Int16)2));
|
49
|
+
sf2.Setup(sf => sf.Serialize(stream, 2222));
|
50
|
+
var formatter = new CauterizeCompositeFormatter(factory.Object);
|
51
|
+
var testObj = new TestComposite();
|
52
|
+
testObj.MyIntNormal = 2222;
|
53
|
+
testObj.MyIntSmall = 2;
|
54
|
+
formatter.Serialize(stream, testObj);
|
55
|
+
sf2.VerifyAll();
|
56
|
+
sf1.VerifyAll();
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
@@ -0,0 +1,110 @@
|
|
1
|
+
using System;
|
2
|
+
using System.IO;
|
3
|
+
using System.Text;
|
4
|
+
using System.Collections.Generic;
|
5
|
+
using System.Linq;
|
6
|
+
using NUnit.Framework;
|
7
|
+
|
8
|
+
namespace Cauterize.Test
|
9
|
+
{
|
10
|
+
[SerializedRepresentation(typeof(SByte))]
|
11
|
+
public enum CauterizeEnumTestSmall
|
12
|
+
{
|
13
|
+
Cets01 = 0,
|
14
|
+
Cets02 = 2
|
15
|
+
}
|
16
|
+
|
17
|
+
[SerializedRepresentation(typeof(Int16))]
|
18
|
+
public enum CauterizeEnumTestMedium
|
19
|
+
{
|
20
|
+
Cetm01 = 0,
|
21
|
+
Cetm02 = 512
|
22
|
+
}
|
23
|
+
|
24
|
+
[SerializedRepresentation(typeof(Int64))]
|
25
|
+
public enum CauterizeEnumTestLarge
|
26
|
+
{
|
27
|
+
Cetl01 = 0,
|
28
|
+
Cetl02 = 131072
|
29
|
+
}
|
30
|
+
|
31
|
+
[TestFixture]
|
32
|
+
public class CauterizeEnumFormatterTest
|
33
|
+
{
|
34
|
+
[Test]
|
35
|
+
public void TestDeserialize_Small()
|
36
|
+
{
|
37
|
+
var bytes = new byte[1];
|
38
|
+
bytes[0] = 2;
|
39
|
+
var stream = new MemoryStream(bytes);
|
40
|
+
var formatter = new CauterizeEnumFormatter();
|
41
|
+
Assert.AreEqual(CauterizeEnumTestSmall.Cets02, formatter.Deserialize(stream, typeof (CauterizeEnumTestSmall)));
|
42
|
+
}
|
43
|
+
|
44
|
+
[Test]
|
45
|
+
public void TestDeserialize_Medium()
|
46
|
+
{
|
47
|
+
var bytes = new byte[2];
|
48
|
+
bytes[0] = 0;
|
49
|
+
bytes[1] = 2;
|
50
|
+
var stream = new MemoryStream(bytes);
|
51
|
+
var formatter = new CauterizeEnumFormatter();
|
52
|
+
Assert.AreEqual(CauterizeEnumTestMedium.Cetm02, formatter.Deserialize(stream, typeof (CauterizeEnumTestMedium)));
|
53
|
+
}
|
54
|
+
|
55
|
+
[Test]
|
56
|
+
public void TestDeserialize_Large()
|
57
|
+
{
|
58
|
+
var bytes = new byte[8];
|
59
|
+
bytes[0] = 0;
|
60
|
+
bytes[1] = 0;
|
61
|
+
bytes[2] = 2;
|
62
|
+
bytes[3] = 0;
|
63
|
+
bytes[4] = 0;
|
64
|
+
bytes[5] = 0;
|
65
|
+
bytes[6] = 0;
|
66
|
+
bytes[7] = 0;
|
67
|
+
var stream = new MemoryStream(bytes);
|
68
|
+
var formatter = new CauterizeEnumFormatter();
|
69
|
+
Assert.AreEqual(CauterizeEnumTestLarge.Cetl02, formatter.Deserialize(stream, typeof (CauterizeEnumTestLarge)));
|
70
|
+
}
|
71
|
+
|
72
|
+
[Test]
|
73
|
+
public void TestSerialize_Small()
|
74
|
+
{
|
75
|
+
var bytes = new byte[1];
|
76
|
+
var stream = new MemoryStream(bytes);
|
77
|
+
var formatter = new CauterizeEnumFormatter();
|
78
|
+
formatter.Serialize(stream, CauterizeEnumTestSmall.Cets02);
|
79
|
+
Assert.AreEqual(2, bytes[0]);
|
80
|
+
}
|
81
|
+
|
82
|
+
[Test]
|
83
|
+
public void TestSerialize_Medium()
|
84
|
+
{
|
85
|
+
var bytes = new byte[2];
|
86
|
+
var stream = new MemoryStream(bytes);
|
87
|
+
var formatter = new CauterizeEnumFormatter();
|
88
|
+
formatter.Serialize(stream, CauterizeEnumTestMedium.Cetm02);
|
89
|
+
Assert.AreEqual(0, bytes[0]);
|
90
|
+
Assert.AreEqual(2, bytes[1]);
|
91
|
+
}
|
92
|
+
|
93
|
+
[Test]
|
94
|
+
public void TestSerialize_Large()
|
95
|
+
{
|
96
|
+
var bytes = new byte[8];
|
97
|
+
var stream = new MemoryStream(bytes);
|
98
|
+
var formatter = new CauterizeEnumFormatter();
|
99
|
+
formatter.Serialize(stream, CauterizeEnumTestLarge.Cetl02);
|
100
|
+
Assert.AreEqual(0, bytes[0]);
|
101
|
+
Assert.AreEqual(0, bytes[1]);
|
102
|
+
Assert.AreEqual(2, bytes[2]);
|
103
|
+
Assert.AreEqual(0, bytes[3]);
|
104
|
+
Assert.AreEqual(0, bytes[4]);
|
105
|
+
Assert.AreEqual(0, bytes[5]);
|
106
|
+
Assert.AreEqual(0, bytes[6]);
|
107
|
+
Assert.AreEqual(0, bytes[7]);
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
@@ -0,0 +1,91 @@
|
|
1
|
+
using System;
|
2
|
+
using System.IO;
|
3
|
+
using System.Text;
|
4
|
+
using System.Collections.Generic;
|
5
|
+
using System.Linq;
|
6
|
+
using Moq;
|
7
|
+
using NUnit.Framework;
|
8
|
+
using Cauterize;
|
9
|
+
|
10
|
+
namespace Cauterize.Test
|
11
|
+
{
|
12
|
+
class TestFixedArray : CauterizeFixedArrayTyped<long>
|
13
|
+
{
|
14
|
+
public TestFixedArray()
|
15
|
+
{
|
16
|
+
Allocate(3);
|
17
|
+
}
|
18
|
+
public TestFixedArray(long[] data)
|
19
|
+
{
|
20
|
+
Allocate(data);
|
21
|
+
}
|
22
|
+
protected override int Size
|
23
|
+
{
|
24
|
+
get { return 3; }
|
25
|
+
}
|
26
|
+
}
|
27
|
+
[TestFixture]
|
28
|
+
public class CauterizeFixedArrayFormatterTest
|
29
|
+
{
|
30
|
+
[Test]
|
31
|
+
public void TestDeserialize()
|
32
|
+
{
|
33
|
+
var stream = new MemoryStream();
|
34
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
35
|
+
var sf = new Mock<ICauterizeTypeFormatter>();
|
36
|
+
factory.Setup(f => f.GetFormatter(typeof (Int64))).Returns(sf.Object);
|
37
|
+
var counter = 0;
|
38
|
+
sf.Setup(f => f.Deserialize(stream, typeof(Int64))).Returns((Stream str, Type t) =>
|
39
|
+
{
|
40
|
+
var ret = 0;
|
41
|
+
if (counter == 1)
|
42
|
+
{
|
43
|
+
ret = 101;
|
44
|
+
}
|
45
|
+
else if (counter == 2)
|
46
|
+
{
|
47
|
+
ret = 321;
|
48
|
+
}
|
49
|
+
counter++;
|
50
|
+
return ret;
|
51
|
+
});
|
52
|
+
var formatter = new CauterizeFixedArrayFormatter(factory.Object);
|
53
|
+
var result = (TestFixedArray) formatter.Deserialize(stream, typeof (TestFixedArray));
|
54
|
+
Assert.AreEqual(0, result[0]);
|
55
|
+
Assert.AreEqual(101, result[1]);
|
56
|
+
Assert.AreEqual(321, result[2]);
|
57
|
+
}
|
58
|
+
|
59
|
+
[Test]
|
60
|
+
public void TestSerialize()
|
61
|
+
{
|
62
|
+
var stream = new MemoryStream();
|
63
|
+
var testObj = new TestFixedArray();
|
64
|
+
testObj[0] = 4;
|
65
|
+
testObj[2] = 8;
|
66
|
+
var sf = new Mock<ICauterizeTypeFormatter>();
|
67
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
68
|
+
factory.Setup(f => f.GetFormatter(typeof (Int64))).Returns(sf.Object);
|
69
|
+
var counter = 0;
|
70
|
+
sf.Setup(f => f.Serialize(stream, It.IsAny<Int64>())).Callback((Stream str, object value) =>
|
71
|
+
{
|
72
|
+
if (counter == 0)
|
73
|
+
{
|
74
|
+
Assert.AreEqual(4, value);
|
75
|
+
}
|
76
|
+
else if (counter == 2)
|
77
|
+
{
|
78
|
+
Assert.AreEqual(8, value);
|
79
|
+
}
|
80
|
+
else
|
81
|
+
{
|
82
|
+
Assert.AreEqual(0, value);
|
83
|
+
}
|
84
|
+
counter++;
|
85
|
+
});
|
86
|
+
var formatter = new CauterizeFixedArrayFormatter(factory.Object);
|
87
|
+
formatter.Serialize(stream, testObj);
|
88
|
+
sf.VerifyAll();
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|