Repository navigation
Expand file tree
/
Copy pathLazyCyclePathExample.cs
More file actions
286 lines (239 loc) · 8.58 KB
/
Copy pathLazyCyclePathExample.cs
File metadata and controls
286 lines (239 loc) · 8.58 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using Mammoth.LiteMapper;
namespace Mammoth.LiteMapper.Samples.Basic
{
public static class LazyCyclePathExample
{
public static void Run()
{
CheckFiniteDepthAndConverterRecovery();
CheckDirectCycleAndRecovery();
CheckIndirectCycle();
CheckBridgedCollectionCycle();
CheckSharedSiblingBranches();
}
private static void CheckFiniteDepthAndConverterRecovery()
{
var source = BuildDirectChain(256);
var mapped = LazyCyclePathMapper.MapDirect(source);
if (Count(mapped) != 256)
{
throw new InvalidOperationException("A finite recursive chain must map to its full depth.");
}
LazyCyclePathMapper.ThrowOnValue(256);
try
{
LazyCyclePathMapper.MapDirect(source);
throw new InvalidOperationException("The configured converter failure was not observed.");
}
catch (InvalidOperationException error) when (error.Message == "Expected converter failure.")
{
}
if (Count(LazyCyclePathMapper.MapDirect(source)) != 256)
{
throw new InvalidOperationException("Mapping must succeed after a converter exception.");
}
}
private static void CheckDirectCycleAndRecovery()
{
var source = new LazyDirectSource { Value = 7 };
source.Next = source;
ExpectCycle(
() => LazyCyclePathMapper.MapDirect(source),
typeof(LazyDirectSource),
typeof(LazyDirectTarget),
"MapDirect",
"Next");
source.Next = null;
var mapped = LazyCyclePathMapper.MapDirect(source);
if (mapped.Value != 7 || mapped.Next != null)
{
throw new InvalidOperationException("Mapping must succeed after a cycle exception.");
}
}
private static void CheckIndirectCycle()
{
var source = new LazyIndirectASource();
source.B = new LazyIndirectBSource { A = source };
ExpectCycle(
() => LazyCyclePathMapper.MapIndirect(source),
typeof(LazyIndirectASource),
typeof(LazyIndirectATarget),
"MapIndirect",
"B.A");
}
private static void CheckBridgedCollectionCycle()
{
var node = new LazyBridgeNodeSource { Value = 11 };
node.Next = node;
var source = new LazyBridgeRootSource
{
Envelope = new LazyBridgeEnvelopeSource
{
Nodes = new[] { node },
},
};
ExpectCycle(
() => LazyCyclePathMapper.MapBridged(source),
typeof(LazyBridgeNodeSource),
typeof(LazyBridgeNodeTarget),
"MapBridged",
"Envelope.Nodes.Next");
}
private static void CheckSharedSiblingBranches()
{
var shared = new LazySiblingNodeSource
{
Value = 13,
Next = new LazySiblingNodeSource { Value = 17 },
};
var mapped = LazyCyclePathMapper.MapSiblings(new LazySiblingRootSource
{
Left = shared,
Right = shared,
});
if (mapped.Left.Value != 13 || mapped.Right.Next?.Value != 17 ||
ReferenceEquals(mapped.Left, mapped.Right) ||
ReferenceEquals(mapped.Left.Next, mapped.Right.Next))
{
throw new InvalidOperationException("Shared sibling branches must map independently without being treated as cycles.");
}
}
private static LazyDirectSource BuildDirectChain(int depth)
{
var root = new LazyDirectSource { Value = 1 };
var current = root;
for (var value = 2; value <= depth; value++)
{
var next = new LazyDirectSource { Value = value };
current.Next = next;
current = next;
}
return root;
}
private static int Count(LazyDirectTarget source)
{
var count = 0;
for (LazyDirectTarget? current = source; current != null; current = current.Next)
{
count++;
}
return count;
}
private static void ExpectCycle(
Action map,
Type sourceType,
Type destinationType,
string mappingMethod,
string memberPath)
{
try
{
map();
}
catch (LiteMapperCycleException error) when (
error.SourceType == sourceType &&
error.DestinationType == destinationType &&
error.MappingMethod == mappingMethod &&
error.MemberPath == memberPath)
{
return;
}
throw new InvalidOperationException(
"Cycle detection did not report the expected public mapping method and member path.");
}
}
[LiteMapper(ReferenceHandling = ReferenceHandling.ThrowOnCycle)]
public static partial class LazyCyclePathMapper
{
private static int? throwOnValue;
public static partial LazyDirectTarget MapDirect(LazyDirectSource source);
public static partial LazyIndirectATarget MapIndirect(LazyIndirectASource source);
public static partial LazyBridgeRootTarget MapBridged(LazyBridgeRootSource source);
public static partial LazySiblingRootTarget MapSiblings(LazySiblingRootSource source);
internal static void ThrowOnValue(int value) => throwOnValue = value;
[MappingConverter]
private static int MapValue(int value)
{
if (throwOnValue == value)
{
throwOnValue = null;
throw new InvalidOperationException("Expected converter failure.");
}
return value;
}
}
public sealed class LazyDirectSource
{
public int Value { get; set; }
public LazyDirectSource? Next { get; set; }
}
public sealed class LazyDirectTarget
{
public int Value { get; set; }
public LazyDirectTarget? Next { get; set; }
}
public sealed class LazyIndirectASource
{
public LazyIndirectBSource? B { get; set; }
}
public sealed class LazyIndirectBSource
{
public LazyIndirectASource? A { get; set; }
}
public sealed class LazyIndirectATarget
{
public LazyIndirectBTarget? B { get; set; }
}
public sealed class LazyIndirectBTarget
{
public LazyIndirectATarget? A { get; set; }
}
public sealed class LazyBridgeRootSource
{
public LazyBridgeEnvelopeSource Envelope { get; set; } = new LazyBridgeEnvelopeSource();
}
public sealed class LazyBridgeEnvelopeSource
{
public LazyBridgeNodeSource[] Nodes { get; set; } = new LazyBridgeNodeSource[0];
}
public sealed class LazyBridgeNodeSource
{
public int Value { get; set; }
public LazyBridgeNodeSource? Next { get; set; }
}
public sealed class LazyBridgeRootTarget
{
public LazyBridgeEnvelopeTarget Envelope { get; set; } = new LazyBridgeEnvelopeTarget();
}
public sealed class LazyBridgeEnvelopeTarget
{
public List<LazyBridgeNodeTarget> Nodes { get; set; } = new List<LazyBridgeNodeTarget>();
}
public sealed class LazyBridgeNodeTarget
{
public int Value { get; set; }
public LazyBridgeNodeTarget? Next { get; set; }
}
public sealed class LazySiblingRootSource
{
public LazySiblingNodeSource Left { get; set; } = new LazySiblingNodeSource();
public LazySiblingNodeSource Right { get; set; } = new LazySiblingNodeSource();
}
public sealed class LazySiblingNodeSource
{
public int Value { get; set; }
public LazySiblingNodeSource? Next { get; set; }
}
public sealed class LazySiblingRootTarget
{
public LazySiblingNodeTarget Left { get; set; } = new LazySiblingNodeTarget();
public LazySiblingNodeTarget Right { get; set; } = new LazySiblingNodeTarget();
}
public sealed class LazySiblingNodeTarget
{
public int Value { get; set; }
public LazySiblingNodeTarget? Next { get; set; }
}
}