Skip to content

Commit 14ac3c9

Browse files
[RGen] Add a new type to track versions.
When adding a platform to a symbol it might be the case that the addition has an implication for another platform. For example, when we add support for iOS we are adding support for Catalyst. This new struct keeps track of the reason why a version was added and will allow to choose always the Explicit verison when two versions are compared. This will later can be used with the Builder class to add Implicit versions for a platform and be able to calculate which version to use.
1 parent 3d5538a commit 14ac3c9

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
namespace Microsoft.Macios.Generator.Availability;
7+
8+
/// <summary>
9+
/// Represents a platform support version, combining a version number and a support kind.
10+
/// </summary>
11+
public readonly record struct PlatformSupportVersion {
12+
/// <summary>
13+
/// Gets the version number.
14+
/// </summary>
15+
public Version Version { get; init; }
16+
/// <summary>
17+
/// Gets the kind of support (e.g., explicit, implicit).
18+
/// </summary>
19+
public SupportKind Kind { get; init; }
20+
21+
/// <summary>
22+
/// Gets a default platform support version with an implicit kind.
23+
/// </summary>
24+
public static PlatformSupportVersion ImplicitDefault { get; } = new () {
25+
Version = new (),
26+
Kind = SupportKind.Implicit
27+
};
28+
29+
/// <summary>
30+
/// Gets a default platform support version with an explicit kind.
31+
/// </summary>
32+
public static PlatformSupportVersion ExplicitDefault { get; } = new () {
33+
Version = new (),
34+
Kind = SupportKind.Explicit
35+
};
36+
37+
/// <summary>
38+
/// Returns the platform support version with the highest precedence.
39+
/// </summary>
40+
/// <param name="v1">The first platform support version to compare.</param>
41+
/// <param name="v2">The second platform support version to compare.</param>
42+
/// <returns>
43+
/// The platform support version with the highest precedence. If the kinds are the same, it returns the one with the greater version.
44+
/// If the kinds are different, it returns the one with the higher kind value.
45+
/// </returns>
46+
public static PlatformSupportVersion Max (PlatformSupportVersion v1, PlatformSupportVersion v2)
47+
{
48+
if (v1.Kind == v2.Kind) {
49+
return v1.Version >= v2.Version ? v1 : v2;
50+
}
51+
return (int) v1.Kind > (int) v2.Kind ? v1 : v2;
52+
}
53+
54+
/// <summary>
55+
/// Returns the platform support version with the lowest version if the kinds are the same, otherwise returns the one with the highest precedence kind.
56+
/// </summary>
57+
/// <param name="v1">The first platform support version to compare.</param>
58+
/// <param name="v2">The second platform support version to compare.</param>
59+
/// <returns>
60+
/// The platform support version with the lowest version if the kinds are the same.
61+
/// If the kinds are different, it returns the one with the higher kind value.
62+
/// </returns>
63+
public static PlatformSupportVersion Min (PlatformSupportVersion v1, PlatformSupportVersion v2)
64+
{
65+
if (v1.Kind == v2.Kind) {
66+
return v1.Version <= v2.Version ? v1 : v2;
67+
}
68+
return (int) v1.Kind > (int) v2.Kind ? v1 : v2;
69+
}
70+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Macios.Generator.Availability;
5+
6+
public enum SupportKind {
7+
Implicit,
8+
Explicit
9+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Microsoft.Macios.Generator.Availability;
6+
using Xunit;
7+
8+
namespace Microsoft.Macios.Generator.Tests.Availability;
9+
10+
public class PlatformSupportVersionTests {
11+
[Fact]
12+
public void ImplicitDefaultTest ()
13+
{
14+
var implicitDefault = PlatformSupportVersion.ImplicitDefault;
15+
Assert.Equal (new Version (0, 0), implicitDefault.Version);
16+
Assert.Equal (SupportKind.Implicit, implicitDefault.Kind);
17+
}
18+
19+
[Fact]
20+
public void ExplicitDefaultTest ()
21+
{
22+
var explicitDefault = PlatformSupportVersion.ExplicitDefault;
23+
Assert.Equal (new Version (0, 0), explicitDefault.Version);
24+
Assert.Equal (SupportKind.Explicit, explicitDefault.Kind);
25+
}
26+
27+
public static TheoryData<PlatformSupportVersion, PlatformSupportVersion, PlatformSupportVersion> MaxTestData =>
28+
new () {
29+
// Same kind, v1 > v2
30+
{
31+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit },
32+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
33+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }
34+
},
35+
// Same kind, v2 > v1
36+
{
37+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
38+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit },
39+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }
40+
},
41+
// Same kind, v1 == v2
42+
{
43+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
44+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
45+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }
46+
},
47+
// Different kind, v1.Kind > v2.Kind
48+
{
49+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit },
50+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit },
51+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit }
52+
},
53+
// Different kind, v2.Kind > v1.Kind
54+
{
55+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit },
56+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit },
57+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit }
58+
},
59+
// ExplicitDefault vs ImplicitDefault
60+
{
61+
PlatformSupportVersion.ExplicitDefault,
62+
PlatformSupportVersion.ImplicitDefault,
63+
PlatformSupportVersion.ExplicitDefault
64+
},
65+
// ExplicitDefault vs other Implicit
66+
{
67+
PlatformSupportVersion.ExplicitDefault,
68+
new PlatformSupportVersion { Version = new Version (10, 0), Kind = SupportKind.Implicit },
69+
PlatformSupportVersion.ExplicitDefault
70+
},
71+
// ImplicitDefault vs other Implicit
72+
{
73+
PlatformSupportVersion.ImplicitDefault,
74+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
75+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }
76+
},
77+
// ExplicitDefault vs other Explicit
78+
{
79+
PlatformSupportVersion.ExplicitDefault,
80+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit },
81+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit }
82+
},
83+
};
84+
85+
[Theory]
86+
[MemberData (nameof (MaxTestData))]
87+
public void MaxTests (PlatformSupportVersion v1, PlatformSupportVersion v2, PlatformSupportVersion expected)
88+
{
89+
Assert.Equal (expected, PlatformSupportVersion.Max (v1, v2));
90+
// Test commutativity
91+
Assert.Equal (expected, PlatformSupportVersion.Max (v2, v1));
92+
}
93+
94+
public static TheoryData<PlatformSupportVersion, PlatformSupportVersion, PlatformSupportVersion> MinTestData =>
95+
new () {
96+
// Same kind, v1 < v2
97+
{
98+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
99+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit },
100+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }
101+
},
102+
// Same kind, v2 < v1
103+
{
104+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit },
105+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
106+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }
107+
},
108+
// Same kind, v1 == v2
109+
{
110+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
111+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
112+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }
113+
},
114+
// Different kind, v1.Kind > v2.Kind
115+
{
116+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit },
117+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
118+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit }
119+
},
120+
// Different kind, v2.Kind > v1.Kind
121+
{
122+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
123+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit },
124+
new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit }
125+
},
126+
// ExplicitDefault vs ImplicitDefault
127+
{
128+
PlatformSupportVersion.ExplicitDefault,
129+
PlatformSupportVersion.ImplicitDefault,
130+
PlatformSupportVersion.ExplicitDefault
131+
},
132+
// ExplicitDefault vs other Implicit
133+
{
134+
PlatformSupportVersion.ExplicitDefault,
135+
new PlatformSupportVersion { Version = new Version (10, 0), Kind = SupportKind.Implicit },
136+
PlatformSupportVersion.ExplicitDefault
137+
},
138+
// ImplicitDefault vs other Implicit
139+
{
140+
PlatformSupportVersion.ImplicitDefault,
141+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit },
142+
PlatformSupportVersion.ImplicitDefault
143+
},
144+
// ExplicitDefault vs other Explicit
145+
{
146+
PlatformSupportVersion.ExplicitDefault,
147+
new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit },
148+
PlatformSupportVersion.ExplicitDefault
149+
},
150+
};
151+
152+
[Theory]
153+
[MemberData (nameof (MinTestData))]
154+
public void MinTests (PlatformSupportVersion v1, PlatformSupportVersion v2, PlatformSupportVersion expected)
155+
{
156+
Assert.Equal (expected, PlatformSupportVersion.Min (v1, v2));
157+
// Test commutativity
158+
Assert.Equal (expected, PlatformSupportVersion.Min (v2, v1));
159+
}
160+
}

0 commit comments

Comments
 (0)