Skip to content

Commit 13c57f7

Browse files
committed
Tests for parametric template resolution
1 parent 4a53904 commit 13c57f7

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

ParserTests/SortResolutionTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Semgus.Model.Smt;
2+
using Semgus.Model.Smt.Theories;
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
using Xunit;
11+
12+
namespace Semgus.Parser.Tests
13+
{
14+
public class SortResolutionTests
15+
{
16+
/// <summary>
17+
/// Test that a template sort will be recursively matched against a concrete sort,
18+
/// and that all the sub-pieces of the template will be resolved.
19+
/// </summary>
20+
[Fact]
21+
public void TemplatesRecursivelyResolved()
22+
{
23+
IDictionary<SmtSort, SmtSort> resolved = new Dictionary<SmtSort, SmtSort>();
24+
25+
var usf = new SmtSort.UniqueSortFactory();
26+
var tix1 = usf.Next();
27+
var val1 = usf.Next();
28+
var tix2 = usf.Next();
29+
30+
var t2 = new SmtArraysExTheory.ArraySort(tix1, val1);
31+
var t1 = new SmtArraysExTheory.ArraySort(tix2, t2);
32+
33+
34+
var pix1 = new SmtSort.GenericSort(new("Proc1"));
35+
var pvl1 = new SmtSort.GenericSort(new("PVal1"));
36+
var pix2 = new SmtSort.GenericSort(new("Proc2"));
37+
var p2 = new SmtArraysExTheory.ArraySort(pix1, pvl1);
38+
var p1 = new SmtArraysExTheory.ArraySort(pix2, p2);
39+
40+
Assert.True(SmtFunction.TraverseAndMatchTemplate(t1, p1, resolved));
41+
42+
Assert.Equal(5, resolved.Count);
43+
Assert.Equal(pix1, resolved[tix1]);
44+
Assert.Equal(pix2, resolved[tix2]);
45+
Assert.Equal(pvl1, resolved[val1]);
46+
Assert.Equal(p2, resolved[t2]);
47+
Assert.Equal(p1, resolved[t1]);
48+
}
49+
50+
/// <summary>
51+
/// Tests that single parametric sorts will be matched against sorts with higher arity.
52+
/// E.g., a template X1 can match against (Array Int Int) fine
53+
/// </summary>
54+
[Fact]
55+
public void SingleTemplatesMatchedAgainstParametric()
56+
{
57+
IDictionary<SmtSort, SmtSort> resolved = new Dictionary<SmtSort, SmtSort>();
58+
59+
var usf = new SmtSort.UniqueSortFactory();
60+
61+
var top1 = usf.Next();
62+
var subIx = usf.Next();
63+
var subVal = usf.Next();
64+
var top2 = new SmtArraysExTheory.ArraySort(subIx, subVal);
65+
66+
var pix1 = new SmtSort.GenericSort(new("Proc1"));
67+
var pvl1 = new SmtSort.GenericSort(new("PVal1"));
68+
var pix2 = new SmtSort.GenericSort(new("Proc2"));
69+
var p2 = new SmtArraysExTheory.ArraySort(pix1, pvl1);
70+
var p1 = new SmtArraysExTheory.ArraySort(pix2, p2);
71+
72+
Assert.True(SmtFunction.TraverseAndMatchTemplate(top1, p1, resolved));
73+
Assert.True(SmtFunction.TraverseAndMatchTemplate(top2, p1, resolved));
74+
75+
Assert.Equal(4, resolved.Count);
76+
Assert.Equal(p1, resolved[top1]);
77+
Assert.Equal(p1, resolved[top2]);
78+
Assert.Equal(pix2, resolved[subIx]);
79+
Assert.Equal(p2, resolved[subVal]);
80+
81+
}
82+
}
83+
}

Semgus-Lib/Model/Smt/SmtFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private bool TryResolveRank([NotNullWhen(true)] out SmtFunctionRank? rank, SmtFu
234234
/// <param name="concrete">Concrete sort to match against</param>
235235
/// <param name="resolvedParameters">Dictionary of resolved parameters</param>
236236
/// <returns>True if successfully resolves, false if not</returns>
237-
private static bool TraverseAndMatchTemplate(SmtSort template, SmtSort concrete, IDictionary<SmtSort, SmtSort> resolvedParameters)
237+
internal static bool TraverseAndMatchTemplate(SmtSort template, SmtSort concrete, IDictionary<SmtSort, SmtSort> resolvedParameters)
238238
{
239239
// Template can be a parameter, and concrete can be whatever
240240
if (template.IsSortParameter)

0 commit comments

Comments
 (0)