-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathTypeInferencing.cs
More file actions
92 lines (77 loc) · 3.1 KB
/
Copy pathTypeInferencing.cs
File metadata and controls
92 lines (77 loc) · 3.1 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
using System;
using NUnit.Framework;
using kOS.Safe.Compilation;
using kOS.Safe.Compilation.IR;
using kOS.Safe.Encapsulation;
using kOS.Safe.Test.Execution;
using kOS.Safe.Utilities;
namespace kOS.Safe.Test.Compilation
{
[SetUpFixture]
public class StaticSetup
{
[SetUp]
public void Setup()
{
SafeHouse.Init(new Config(), new VersionInfo(0, 0, 0, 0), "", false, "./");
SafeHouse.Logger = new NoopLogger();
try
{
AssemblyWalkAttribute.Walk();
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine(e.StackTrace);
throw;
}
}
}
[TestFixture]
public class TypeInferencing
{
[Test]
public void StructureSuffixTest()
{
// Tests that the bare minimum works
Type structureType = typeof(Encapsulation.Structure);
Type result = TypeInferencer.GetTypeForSuffix(structureType, "TOSTRING");
Assert.AreEqual(typeof(StringValue), result);
Type integerType = typeof(ScalarIntValue);
result = TypeInferencer.GetTypeForSuffix(integerType, "istype");
Assert.AreEqual(typeof(BooleanValue), result);
}
[Test]
public void ListSuffixTest()
{
// Test that lists and enumerables work
Type listType = typeof(ListValue);
Type result = TypeInferencer.GetTypeForSuffix(listType, "ITERATOR");
Assert.AreEqual(typeof(Enumerator), result);
result = TypeInferencer.GetTypeForSuffix(listType, "COPY");
Assert.AreEqual(typeof(ListValue), result);
result = TypeInferencer.GetTypeForIndex(listType);
Assert.AreEqual(typeof(Encapsulation.Structure), result);
result = TypeInferencer.GetTypeForSuffix(listType, "CLEAR");
Assert.AreEqual(null, result);
// Test an abstract generic type
listType = typeof(EnumerableValue<Encapsulation.Structure, System.Collections.Generic.IEnumerable<Encapsulation.Structure>>);
result = TypeInferencer.GetTypeForSuffix(listType, "ITERATOR");
Assert.AreEqual(typeof(Enumerator), result);
}
[Test]
public void TestInstructionInferencing()
{
InterimConstantValue a = new InterimConstantValue(ScalarIntValue.One, 0, 0);
InterimConstantValue b = new InterimConstantValue(ScalarIntValue.Two, 0, 0);
IRBinaryOp add = new IRBinaryOp(null, new OpcodeMathAdd(), a, b);
Assert.AreEqual(typeof(ScalarValue), add.Type);
IRCall call = new IRCall(null, new OpcodeCall("sin"), b);
Assert.IsTrue(typeof(ScalarValue).IsAssignableFrom(call.Type));
IRCall print = new IRCall(null, new OpcodeCall("print"), a);
Assert.AreEqual(null, print.Type);
IRCall userCall = new IRCall(null, new OpcodeCall("$test*"), a, b);
Assert.AreEqual(typeof(Encapsulation.Structure), userCall.Type);
}
}
}