-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TestSerializer.cs
131 lines (110 loc) · 4.28 KB
/
TestSerializer.cs
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using VW;
using VW.Reflection;
using VW.Serializer;
using VW.Serializer.Attributes;
using VW.Serializer.Intermediate;
namespace cs_unittest
{
public class CustomClass
{
public int X { get; set; }
public bool HasVisited = false;
}
public class MyContext
{
[Feature]
public CustomClass Feature { get; set; }
}
public class CustomFeaturizer
{
public void MarshalFeature(VowpalWabbitMarshalContext context, Namespace ns, Feature feature, CustomClass value)
{
Assert.IsNotNull(context);
Assert.IsNotNull(ns);
Assert.IsNotNull(feature);
Assert.IsNotNull(value);
Assert.AreEqual(5, value.X);
value.HasVisited = true;
}
}
public class MyDictifyContext
{
[Feature]
public int A { get; set; }
[Feature(Dictify = true)]
public float[] B { get; set; }
}
[TestClass]
public class TestSerializer
{
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestCustomFeaturizer()
{
var context = new MyContext() { Feature = new CustomClass() { X = 5 }};
using (var vw = new VowpalWabbit(""))
{
var serializer = VowpalWabbitSerializerFactory.CreateSerializer<MyContext>(new VowpalWabbitSettings { CustomFeaturizer = new List<Type> { typeof(CustomFeaturizer) } })
.Create(vw);
var example = serializer.Serialize(context);
Assert.IsNotNull(example);
example.Dispose();
}
Assert.IsTrue(context.Feature.HasVisited);
}
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestCustomFeaturizerOverideMethod()
{
var context = new MyContext() { Feature = new CustomClass() { X = 5 } };
using (var vw = new VowpalWabbit(""))
{
var serializer = VowpalWabbitSerializerFactory.CreateSerializer<MyContext>(new VowpalWabbitSettings
{
Schema = new Schema
{
Features = new List<FeatureExpression>
{
new FeatureExpression(typeof(CustomClass), "Feature",
// TODO: looks a bit awkward for an API. The compiler needs to know what property to access to copy the value into the Feature<T> object
valueExpression => Expression.Property(valueExpression, (PropertyInfo)ReflectionHelper.GetInfo((MyContext m) => m.Feature)),
overrideSerializeMethod: (MethodInfo)ReflectionHelper.GetInfo((CustomFeaturizer c) => c.MarshalFeature(null, null, null, null)))
}
}
}).Create(vw);
var example = serializer.Serialize(context);
Assert.IsNotNull(example);
example.Dispose();
}
Assert.IsTrue(context.Feature.HasVisited);
}
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestDictify()
{
using (var vw = new VowpalWabbit(new VowpalWabbitSettings { EnableStringExampleGeneration = true, EnableStringFloatCompact = true }))
using (var serializer = VowpalWabbitSerializerFactory.CreateSerializer<MyDictifyContext>(vw.Settings).Create(vw))
{
var dictionary = new Dictionary<string, string>();
var ctx = new MyDictifyContext
{
A = 5,
B = new[] { 1f, 2f, 3f }
};
var str = serializer.SerializeToString(ctx, dictionary: dictionary);
Assert.AreEqual(" | A:5 d0", str);
Assert.AreEqual(1, dictionary.Count);
Assert.IsTrue(dictionary.ContainsKey(" 0:1 1:2 2:3"));
Assert.AreEqual("d0", dictionary[" 0:1 1:2 2:3"]);
}
}
}
}