cauterize 0.0.1.pre1 → 0.0.1.pre5
Sign up to get free protection for your applications and to get access to all the features.
- 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,40 @@
|
|
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
|
+
using Cauterize;
|
8
|
+
using Moq;
|
9
|
+
|
10
|
+
namespace Cauterize.Test
|
11
|
+
{
|
12
|
+
[TestFixture]
|
13
|
+
public class CauterizeFormatterTest
|
14
|
+
{
|
15
|
+
[Test]
|
16
|
+
public void TestDeserialize()
|
17
|
+
{
|
18
|
+
var subFormatter = new Mock<ICauterizeTypeFormatter>();
|
19
|
+
var serializationStream = new MemoryStream();
|
20
|
+
subFormatter.Setup(sf => sf.Deserialize(serializationStream, typeof (string))).Returns("hello world");
|
21
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
22
|
+
factory.Setup(f => f.GetFormatter(typeof (string))).Returns(subFormatter.Object);
|
23
|
+
var formatter = new CauterizeFormatter(factory.Object);
|
24
|
+
Assert.AreEqual("hello world", formatter.Deserialize<string>(serializationStream));
|
25
|
+
}
|
26
|
+
|
27
|
+
[Test]
|
28
|
+
public void TestSerialize()
|
29
|
+
{
|
30
|
+
var subFormatter = new Mock<ICauterizeTypeFormatter>();
|
31
|
+
var serializationStream = new MemoryStream();
|
32
|
+
subFormatter.Setup(sf => sf.Serialize(serializationStream, "hello world"));
|
33
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
34
|
+
factory.Setup(f => f.GetFormatter(typeof (string))).Returns(subFormatter.Object);
|
35
|
+
var formatter = new CauterizeFormatter(factory.Object);
|
36
|
+
formatter.Serialize(serializationStream, "hello world");
|
37
|
+
subFormatter.VerifyAll();
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}
|
@@ -0,0 +1,147 @@
|
|
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
|
+
enum TestGroupType
|
13
|
+
{
|
14
|
+
TestGroupTypeFoo = 0,
|
15
|
+
TestGroupTypeBar,
|
16
|
+
TestGroupTypeBaz
|
17
|
+
}
|
18
|
+
class TestGroup : CauterizeGroup
|
19
|
+
{
|
20
|
+
[Order(0)]
|
21
|
+
public TestGroupType Type { get; set; }
|
22
|
+
|
23
|
+
[Order(1)]
|
24
|
+
public int Foo { get; set; }
|
25
|
+
/* unused for 2/Bar */
|
26
|
+
[Order(3)]
|
27
|
+
public byte Baz { get; set; }
|
28
|
+
}
|
29
|
+
|
30
|
+
[TestFixture]
|
31
|
+
public class CauterizeGroupFormatterTest
|
32
|
+
{
|
33
|
+
[Test]
|
34
|
+
public void TestDeserialize()
|
35
|
+
{
|
36
|
+
var stream = new MemoryStream();
|
37
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
38
|
+
var enumFormatter = new Mock<ICauterizeTypeFormatter>();
|
39
|
+
var intFormatter = new Mock<ICauterizeTypeFormatter>();
|
40
|
+
factory.Setup(f => f.GetFormatter(It.IsAny<Type>())).Returns((Type t) =>
|
41
|
+
{
|
42
|
+
if (t == typeof (TestGroupType))
|
43
|
+
{
|
44
|
+
return enumFormatter.Object;
|
45
|
+
}
|
46
|
+
else if (t == typeof (int))
|
47
|
+
{
|
48
|
+
return intFormatter.Object;
|
49
|
+
}
|
50
|
+
else
|
51
|
+
{
|
52
|
+
return null;
|
53
|
+
}
|
54
|
+
});
|
55
|
+
enumFormatter.Setup(f => f.Deserialize(stream, typeof (TestGroupType)))
|
56
|
+
.Returns(TestGroupType.TestGroupTypeFoo);
|
57
|
+
intFormatter.Setup(f => f.Deserialize(stream, typeof (int)))
|
58
|
+
.Returns(1024);
|
59
|
+
var formatter = new CauterizeGroupFormatter(factory.Object);
|
60
|
+
var result = (TestGroup) formatter.Deserialize(stream, typeof (TestGroup));
|
61
|
+
Assert.AreEqual(TestGroupType.TestGroupTypeFoo, result.Type);
|
62
|
+
Assert.AreEqual(1024, result.Foo);
|
63
|
+
}
|
64
|
+
|
65
|
+
[Test]
|
66
|
+
public void TestSerialize()
|
67
|
+
{
|
68
|
+
var stream = new MemoryStream();
|
69
|
+
var group = new TestGroup();
|
70
|
+
group.Type = TestGroupType.TestGroupTypeBaz;
|
71
|
+
group.Baz = 4;
|
72
|
+
var enumFormatter = new Mock<ICauterizeTypeFormatter>();
|
73
|
+
var byteFormatter = new Mock<ICauterizeTypeFormatter>();
|
74
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
75
|
+
factory.Setup(f => f.GetFormatter(It.IsAny<Type>())).Returns((Type t) =>
|
76
|
+
{
|
77
|
+
if (t == typeof (TestGroupType))
|
78
|
+
{
|
79
|
+
return enumFormatter.Object;
|
80
|
+
}
|
81
|
+
else if (t == typeof (Byte))
|
82
|
+
{
|
83
|
+
return byteFormatter.Object;
|
84
|
+
}
|
85
|
+
else
|
86
|
+
{
|
87
|
+
return null;
|
88
|
+
}
|
89
|
+
});
|
90
|
+
enumFormatter.Setup(f => f.Serialize(stream, TestGroupType.TestGroupTypeBaz));
|
91
|
+
byteFormatter.Setup(f => f.Serialize(stream, (Byte)4));
|
92
|
+
var formatter = new CauterizeGroupFormatter(factory.Object);
|
93
|
+
formatter.Serialize(stream, group);
|
94
|
+
enumFormatter.VerifyAll();
|
95
|
+
byteFormatter.VerifyAll();
|
96
|
+
}
|
97
|
+
|
98
|
+
[Test]
|
99
|
+
public void TestDeserialized_UnusedGroupData()
|
100
|
+
{
|
101
|
+
var stream = new MemoryStream();
|
102
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
103
|
+
var enumFormatter = new Mock<ICauterizeTypeFormatter>();
|
104
|
+
factory.Setup(f => f.GetFormatter(It.IsAny<Type>())).Returns((Type t) =>
|
105
|
+
{
|
106
|
+
if (t == typeof (TestGroupType))
|
107
|
+
{
|
108
|
+
return enumFormatter.Object;
|
109
|
+
}
|
110
|
+
else
|
111
|
+
{
|
112
|
+
return null;
|
113
|
+
}
|
114
|
+
});
|
115
|
+
enumFormatter.Setup(f => f.Deserialize(stream, typeof (TestGroupType)))
|
116
|
+
.Returns(TestGroupType.TestGroupTypeBar);
|
117
|
+
var formatter = new CauterizeGroupFormatter(factory.Object);
|
118
|
+
var result = (TestGroup) formatter.Deserialize(stream, typeof (TestGroup));
|
119
|
+
Assert.AreEqual(TestGroupType.TestGroupTypeBar, result.Type);
|
120
|
+
}
|
121
|
+
|
122
|
+
[Test]
|
123
|
+
public void TestSerialized_UnusedGroupData()
|
124
|
+
{
|
125
|
+
var stream = new MemoryStream();
|
126
|
+
var group = new TestGroup();
|
127
|
+
group.Type = TestGroupType.TestGroupTypeBar;
|
128
|
+
var enumFormatter = new Mock<ICauterizeTypeFormatter>();
|
129
|
+
var factory = new Mock<CauterizeTypeFormatterFactory>();
|
130
|
+
factory.Setup(f => f.GetFormatter(It.IsAny<Type>())).Returns((Type t) =>
|
131
|
+
{
|
132
|
+
if (t == typeof (TestGroupType))
|
133
|
+
{
|
134
|
+
return enumFormatter.Object;
|
135
|
+
}
|
136
|
+
else
|
137
|
+
{
|
138
|
+
return null;
|
139
|
+
}
|
140
|
+
});
|
141
|
+
enumFormatter.Setup(f => f.Serialize(stream, TestGroupType.TestGroupTypeBar));
|
142
|
+
var formatter = new CauterizeGroupFormatter(factory.Object);
|
143
|
+
formatter.Serialize(stream, group);
|
144
|
+
enumFormatter.VerifyAll();
|
145
|
+
}
|
146
|
+
}
|
147
|
+
}
|
@@ -0,0 +1,129 @@
|
|
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
|
+
using Cauterize;
|
8
|
+
|
9
|
+
namespace Cauterize.Test
|
10
|
+
{
|
11
|
+
class TestTopLevel : CauterizeComposite
|
12
|
+
{
|
13
|
+
[Order(0)]
|
14
|
+
public TestSubComp SubComp { get; set; }
|
15
|
+
|
16
|
+
[Order(1)]
|
17
|
+
public TestSubFixed SubFixed { get; set; }
|
18
|
+
|
19
|
+
[Order(2)]
|
20
|
+
public TestSubGroup SubGroup { get; set; }
|
21
|
+
|
22
|
+
[Order(3)]
|
23
|
+
public Int32 SubInt { get; set; }
|
24
|
+
}
|
25
|
+
|
26
|
+
class TestSubComp : CauterizeComposite
|
27
|
+
{
|
28
|
+
[Order(0)]
|
29
|
+
public Byte Byte1 { get; set; }
|
30
|
+
|
31
|
+
[Order(1)]
|
32
|
+
public Byte Byte2 { get; set; }
|
33
|
+
}
|
34
|
+
|
35
|
+
class TestSubFixed : CauterizeFixedArrayTyped<Int64>
|
36
|
+
{
|
37
|
+
public TestSubFixed()
|
38
|
+
{
|
39
|
+
Allocate(4);
|
40
|
+
}
|
41
|
+
|
42
|
+
public TestSubFixed(Int64[] data)
|
43
|
+
{
|
44
|
+
Allocate(data);
|
45
|
+
}
|
46
|
+
|
47
|
+
protected override int Size
|
48
|
+
{
|
49
|
+
get { return 4; }
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
[SerializedRepresentation(typeof(Byte))]
|
54
|
+
public enum TestSubGroupType
|
55
|
+
{
|
56
|
+
TestSubGroupTypeVariable,
|
57
|
+
TestSUbGroupTypeNothing,
|
58
|
+
TestSubGroupTypeShort
|
59
|
+
}
|
60
|
+
|
61
|
+
class TestSubGroup : CauterizeGroup
|
62
|
+
{
|
63
|
+
[Order(0)]
|
64
|
+
public TestSubGroupType Type { get; set; }
|
65
|
+
|
66
|
+
[Order(1)]
|
67
|
+
public TestSubVariable SubVar { get; set; }
|
68
|
+
|
69
|
+
/* no data for nothing */
|
70
|
+
[Order(3)]
|
71
|
+
public Int16 SubShort { get; set; }
|
72
|
+
}
|
73
|
+
|
74
|
+
class TestSubVariable : CauterizeVariableArrayTyped<Byte>
|
75
|
+
{
|
76
|
+
public static Type SizeType = typeof (Byte);
|
77
|
+
|
78
|
+
public TestSubVariable(int size)
|
79
|
+
{
|
80
|
+
Allocate(size);
|
81
|
+
}
|
82
|
+
|
83
|
+
public TestSubVariable(Byte[] data)
|
84
|
+
{
|
85
|
+
Allocate(data);
|
86
|
+
}
|
87
|
+
|
88
|
+
protected override int MaxSize
|
89
|
+
{
|
90
|
+
get { return Byte.MaxValue; }
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
[TestFixture]
|
96
|
+
public class CauterizeIntegrationTest
|
97
|
+
{
|
98
|
+
[Test]
|
99
|
+
public void TestThereAndBackAgain()
|
100
|
+
{
|
101
|
+
var inputTopLevel = new TestTopLevel();
|
102
|
+
inputTopLevel.SubComp = new TestSubComp();
|
103
|
+
inputTopLevel.SubComp.Byte1 = 101;
|
104
|
+
inputTopLevel.SubComp.Byte2 = 202;
|
105
|
+
inputTopLevel.SubFixed = new TestSubFixed();
|
106
|
+
inputTopLevel.SubFixed[2] = 1234123412341234;
|
107
|
+
inputTopLevel.SubGroup = new TestSubGroup();
|
108
|
+
inputTopLevel.SubGroup.Type = TestSubGroupType.TestSubGroupTypeVariable;
|
109
|
+
inputTopLevel.SubGroup.SubVar = new TestSubVariable(3);
|
110
|
+
inputTopLevel.SubGroup.SubVar[1] = 55;
|
111
|
+
inputTopLevel.SubInt = 1000000;
|
112
|
+
|
113
|
+
var formatter = new CauterizeFormatter();
|
114
|
+
var stream = new MemoryStream(2048);
|
115
|
+
formatter.Serialize(stream, inputTopLevel);
|
116
|
+
|
117
|
+
stream.Position = 0;
|
118
|
+
var outputTopLevel = formatter.Deserialize<TestTopLevel>(stream);
|
119
|
+
Assert.AreEqual(inputTopLevel.SubComp.Byte1, outputTopLevel.SubComp.Byte1);
|
120
|
+
Assert.AreEqual(inputTopLevel.SubComp.Byte2, outputTopLevel.SubComp.Byte2);
|
121
|
+
Assert.AreEqual(inputTopLevel.SubFixed[0], outputTopLevel.SubFixed[0]);
|
122
|
+
Assert.AreEqual(inputTopLevel.SubFixed[2], outputTopLevel.SubFixed[2]);
|
123
|
+
Assert.AreEqual(inputTopLevel.SubGroup.Type, outputTopLevel.SubGroup.Type);
|
124
|
+
Assert.AreEqual(inputTopLevel.SubGroup.SubVar[0], outputTopLevel.SubGroup.SubVar[0]);
|
125
|
+
Assert.AreEqual(inputTopLevel.SubGroup.SubVar[1], outputTopLevel.SubGroup.SubVar[1]);
|
126
|
+
Assert.AreEqual(inputTopLevel.SubInt, outputTopLevel.SubInt);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}
|
@@ -0,0 +1,98 @@
|
|
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
|
+
[TestFixture]
|
11
|
+
public class CauterizePrimitiveFormatterTest
|
12
|
+
{
|
13
|
+
[Test]
|
14
|
+
public void TestDeserialize()
|
15
|
+
{
|
16
|
+
var formatter = new CauterizePrimitiveFormatter();
|
17
|
+
var bytes = new byte[8];
|
18
|
+
bytes[0] = 2;
|
19
|
+
bytes[1] = 128;
|
20
|
+
bytes[2] = 1;
|
21
|
+
bytes[3] = 0;
|
22
|
+
bytes[4] = 1;
|
23
|
+
var stream = new MemoryStream(bytes);
|
24
|
+
object value = formatter.Deserialize(stream, typeof(Byte));
|
25
|
+
Assert.AreEqual(2,value);
|
26
|
+
stream = new MemoryStream(bytes);
|
27
|
+
value = formatter.Deserialize(stream, typeof(Int16));
|
28
|
+
Assert.AreEqual(Int16.MinValue + 2, value);
|
29
|
+
stream = new MemoryStream(bytes);
|
30
|
+
value = formatter.Deserialize(stream, typeof(UInt16));
|
31
|
+
Assert.AreEqual(32770, value);
|
32
|
+
stream = new MemoryStream(bytes);
|
33
|
+
value = formatter.Deserialize(stream, typeof(Int32));
|
34
|
+
Assert.AreEqual(98306, value);
|
35
|
+
stream = new MemoryStream(bytes);
|
36
|
+
value = formatter.Deserialize(stream, typeof(Int64));
|
37
|
+
Assert.AreEqual(4295065602, value);
|
38
|
+
}
|
39
|
+
|
40
|
+
[Test]
|
41
|
+
public void TestDeserializeTwoBytesInARow()
|
42
|
+
{
|
43
|
+
var formatter = new CauterizePrimitiveFormatter();
|
44
|
+
var bytes = new byte[2];
|
45
|
+
bytes[0] = 2;
|
46
|
+
bytes[1] = 128;
|
47
|
+
var stream = new MemoryStream(bytes);
|
48
|
+
object value = formatter.Deserialize(stream, typeof(Byte));
|
49
|
+
Assert.AreEqual(2,value);
|
50
|
+
value = formatter.Deserialize(stream, typeof(Byte));
|
51
|
+
Assert.AreEqual(128,value);
|
52
|
+
}
|
53
|
+
|
54
|
+
[Test]
|
55
|
+
public void TestSerialize()
|
56
|
+
{
|
57
|
+
var formatter = new CauterizePrimitiveFormatter();
|
58
|
+
var bytes = new byte[8];
|
59
|
+
var stream = new MemoryStream(bytes);
|
60
|
+
formatter.Serialize(stream, (Byte)2);
|
61
|
+
Assert.AreEqual(2, bytes[0]);
|
62
|
+
Assert.AreEqual(0, bytes[1]);
|
63
|
+
|
64
|
+
bytes = new byte[8];
|
65
|
+
stream = new MemoryStream(bytes);
|
66
|
+
formatter.Serialize(stream, (Int16)(Int16.MinValue + 2));
|
67
|
+
Assert.AreEqual(2, bytes[0]);
|
68
|
+
Assert.AreEqual(128, bytes[1]);
|
69
|
+
Assert.AreEqual(0, bytes[2]);
|
70
|
+
|
71
|
+
bytes = new byte[8];
|
72
|
+
stream = new MemoryStream(bytes);
|
73
|
+
formatter.Serialize(stream, (UInt16)(32770));
|
74
|
+
Assert.AreEqual(2, bytes[0]);
|
75
|
+
Assert.AreEqual(128, bytes[1]);
|
76
|
+
Assert.AreEqual(0, bytes[2]);
|
77
|
+
|
78
|
+
bytes = new byte[8];
|
79
|
+
stream = new MemoryStream(bytes);
|
80
|
+
formatter.Serialize(stream, 98306);
|
81
|
+
Assert.AreEqual(2, bytes[0]);
|
82
|
+
Assert.AreEqual(128, bytes[1]);
|
83
|
+
Assert.AreEqual(1, bytes[2]);
|
84
|
+
Assert.AreEqual(0, bytes[3]);
|
85
|
+
Assert.AreEqual(0, bytes[4]);
|
86
|
+
|
87
|
+
bytes = new byte[8];
|
88
|
+
stream = new MemoryStream(bytes);
|
89
|
+
formatter.Serialize(stream, 4295065602);
|
90
|
+
Assert.AreEqual(2, bytes[0]);
|
91
|
+
Assert.AreEqual(128, bytes[1]);
|
92
|
+
Assert.AreEqual(1, bytes[2]);
|
93
|
+
Assert.AreEqual(0, bytes[3]);
|
94
|
+
Assert.AreEqual(1, bytes[4]);
|
95
|
+
Assert.AreEqual(0, bytes[5]);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
@@ -0,0 +1,73 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Text;
|
3
|
+
using System.Collections.Generic;
|
4
|
+
using System.Linq;
|
5
|
+
using NUnit.Framework;
|
6
|
+
using Cauterize;
|
7
|
+
|
8
|
+
namespace Cauterize.Test
|
9
|
+
{
|
10
|
+
class MyComposite : CauterizeComposite
|
11
|
+
{
|
12
|
+
public int MyInt1 { get; set; }
|
13
|
+
public int MyInt2 { get; set; }
|
14
|
+
}
|
15
|
+
|
16
|
+
enum GroupType
|
17
|
+
{
|
18
|
+
GroupTypeInt32,
|
19
|
+
GroupTypeInt16
|
20
|
+
}
|
21
|
+
class MyGroup : CauterizeGroup
|
22
|
+
{
|
23
|
+
public GroupType Type { get; set; }
|
24
|
+
|
25
|
+
public Int32 NormalInt { get; set; }
|
26
|
+
public Int16 SmallInt { get; set; }
|
27
|
+
}
|
28
|
+
|
29
|
+
class MyFixedArray : CauterizeFixedArray
|
30
|
+
{
|
31
|
+
private int[] _data;
|
32
|
+
MyFixedArray()
|
33
|
+
{
|
34
|
+
_data = new int[4];
|
35
|
+
}
|
36
|
+
int this[int i]
|
37
|
+
{
|
38
|
+
get { return _data[i]; }
|
39
|
+
set { _data[i] = value; }
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
class MyVariableArray : CauterizeVariableArray
|
44
|
+
{
|
45
|
+
private int[] _data;
|
46
|
+
MyVariableArray(int size)
|
47
|
+
{
|
48
|
+
_data = new int[size];
|
49
|
+
}
|
50
|
+
int this[int i]
|
51
|
+
{
|
52
|
+
get { return _data[i]; }
|
53
|
+
set { _data[i] = value; }
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
[TestFixture]
|
58
|
+
public class CauterizeTypeFormatterFactoryTest
|
59
|
+
{
|
60
|
+
[Test]
|
61
|
+
public void TestGetFormatter()
|
62
|
+
{
|
63
|
+
var factory = new CauterizeTypeFormatterFactory();
|
64
|
+
Assert.AreEqual(typeof(CauterizeCompositeFormatter), factory.GetFormatter(typeof(MyComposite)).GetType());
|
65
|
+
Assert.AreEqual(typeof(CauterizeGroupFormatter), factory.GetFormatter(typeof(MyGroup)).GetType());
|
66
|
+
Assert.AreEqual(typeof(CauterizeFixedArrayFormatter), factory.GetFormatter(typeof(MyFixedArray)).GetType());
|
67
|
+
Assert.AreEqual(typeof(CauterizeVariableArrayFormatter), factory.GetFormatter(typeof(MyVariableArray)).GetType());
|
68
|
+
Assert.AreEqual(typeof(CauterizePrimitiveFormatter), factory.GetFormatter(typeof(int)).GetType());
|
69
|
+
Assert.AreEqual(typeof(CauterizePrimitiveFormatter), factory.GetFormatter(typeof(double)).GetType());
|
70
|
+
Assert.AreEqual(typeof(CauterizeEnumFormatter), factory.GetFormatter(typeof(GroupType)).GetType());
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|