My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
´╗┐using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ProtoBuf;

namespace Examples
{
[ProtoContract]
public class FieldData
{
public FieldData() {}
public FieldData(object value) {
Value = value;}
public object Value { get; set; }

private bool Is<T>() {
return Value != null && Value is T;
}
private T Get<T>() {
return Is<T>() ? (T) Value : default(T);
}
[ProtoMember(1)]
private int ValueInt32
{
get { return Get<int>(); }
set { Value = value; }
}
private bool ValueInt32Specified { get { return Is<int>(); } }

[ProtoMember(2)]
private float ValueSingle
{
get { return Get<float>();}
set { Value = value; }
}
private bool ValueSingleSpecified { get { return Is<float>(); } }

[ProtoMember(3)]
private double ValueDouble
{
get { return Get<double>(); ; }
set { Value = value; }
}
private bool ValueDoubleSpecified { get { return Is<double>(); } }

// etc for expected types
}

[ProtoContract]
public class FieldDataViaNullable
{
public FieldDataViaNullable() { }
public FieldDataViaNullable(object value)
{
Value = value;
}
public object Value { get; set; }

private T? Get<T>() where T : struct
{
return (Value != null && Value is T) ? (T?)Value : (T?)null;
}
[ProtoMember(1)]
private int? ValueInt32
{
get { return Get<int>(); }
set { Value = value; }
}
[ProtoMember(2)]
private float? ValueSingle
{
get { return Get<float>(); }
set { Value = value; }
}
[ProtoMember(3)]
private double? ValueDouble
{
get { return Get<double>(); ; }
set { Value = value; }
}
// etc for expected types
}

[ProtoContract]
class Int32Simple
{
[ProtoMember(1)]
public int Value { get; set; }
}
[ProtoContract]
class SingleSimple
{
[ProtoMember(2)]
public float Value { get; set; }
}
[ProtoContract]
class DoubleSimple
{
[ProtoMember(3)]
public double Value { get; set; }
}

[TestFixture]
public class ValueWrapperTests
{
static byte[] GetBytes<T>(T item)
{
MemoryStream ms = new MemoryStream();
Serializer.Serialize(ms, item);
return ms.ToArray();
}
[Test]
public void TestRaw()
{
Assert.IsTrue(Program.CheckBytes(new FieldData()), "Empty");
Assert.AreEqual(null, Serializer.DeepClone(new FieldData()).Value);

}
[Test]
public void TestInt32()
{

Assert.IsTrue(Program.CheckBytes(new FieldData {Value = 123},
GetBytes(new Int32Simple {Value = 123})), "Int32");
Assert.AreEqual(123, Serializer.DeepClone(new FieldData(123)).Value);
}
[Test]
public void TestSingle()
{
Assert.IsTrue(Program.CheckBytes(new FieldData {Value = 123.45F},
GetBytes(new SingleSimple {Value = 123.45F})), "Single");
Assert.AreEqual(123.45F, Serializer.DeepClone(new FieldData(123.45F)).Value);

}
[Test]
public void TestDouble()
{
Assert.IsTrue(Program.CheckBytes(new FieldData { Value = 123.45 },
GetBytes(new DoubleSimple { Value = 123.45 })), "Double");
Assert.AreEqual(123.45, Serializer.DeepClone(new FieldData(123.45)).Value);
}

}

[TestFixture]
public class ValueWrapperTestsViaNullable
{
static byte[] GetBytes<T>(T item)
{
MemoryStream ms = new MemoryStream();
Serializer.Serialize(ms, item);
return ms.ToArray();
}
[Test]
public void TestRaw()
{
Assert.IsTrue(Program.CheckBytes(new FieldDataViaNullable()), "Empty");
Assert.AreEqual(null, Serializer.DeepClone(new FieldDataViaNullable()).Value);

}

[Test]
public void TestInt32()
{

Assert.IsTrue(Program.CheckBytes(new FieldDataViaNullable {Value = 123},
GetBytes(new Int32Simple {Value = 123})), "Int32");
Assert.AreEqual(123, Serializer.DeepClone(new FieldDataViaNullable(123)).Value);
}
[Test]
public void TestSingle()
{
Assert.IsTrue(Program.CheckBytes(new FieldDataViaNullable {Value = 123.45F},
GetBytes(new SingleSimple {Value = 123.45F})), "Single");
Assert.AreEqual(123.45F, Serializer.DeepClone(new FieldDataViaNullable(123.45F)).Value);

}
[Test]
public void TestDouble()
{
Assert.IsTrue(Program.CheckBytes(new FieldDataViaNullable { Value = 123.45 },
GetBytes(new DoubleSimple { Value = 123.45 })), "Double");
Assert.AreEqual(123.45, Serializer.DeepClone(new FieldDataViaNullable(123.45)).Value);
}

}
}

Change log

r238 by marc.gravell on Feb 18, 2009   Diff
Nullable<T> tests of value-wrapper
Go to: 
Project members, sign in to write a code review

Older revisions

r237 by marc.gravell on Feb 18, 2009   Diff
Better support for *Specified
All revisions of this file

File info

Size: 5707 bytes, 190 lines
Powered by Google Project Hosting