public void TestHaltEvent() { var test = @" namespace Foo { machine M { start state S { on halt goto S2; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventGotoState(typeof(Microsoft.PSharp.Halt), typeof(S2))] class S : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestAsyncEntryDeclaration() { var test = @" namespace Foo { machine M { start state S { async entry{ await Task.Delay(42); } } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_S_on_entry_action_async))] class S : MachineState { } protected async System.Threading.Tasks.Task psharp_S_on_entry_action_async() { await Task.Delay(42); } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestDeferEventDeclarationQualifiedComplex() { var test = @" namespace Foo { machine M { start state S { defer e1<int>, halt, default, Foo.e2<Bar.e3>; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [DeferEvents(typeof(e1<int>), typeof(Microsoft.PSharp.Halt), typeof(Microsoft.PSharp.Default), typeof(Foo.e2<Bar.e3>))] class S : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestWildcardEventDefer() { var test = @" namespace Foo { machine M { start state S { defer *; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [DeferEvents(typeof(Microsoft.PSharp.WildCardEvent))] class S : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestStateDeclaration() { var test = @" namespace Foo { machine M { start state S1 { } state S2 { } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] class S1 : MachineState { } class S2 : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestOnComplexGenericEventGotoStateDeclarationWithBody() { var test = @" namespace Foo { machine M { start state S1 { on e<List<Tuple<bool, object>>, Dictionary<string, float>> goto S2 with {} } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventGotoState(typeof(e<List<Tuple<bool, object>>, Dictionary<string, float>>), typeof(S2), nameof(psharp_S1_e_type_0_action))] class S1 : MachineState { } protected void psharp_S1_e_type_0_action() {} } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestOnComplexGenericEventDoActionDeclaration() { var test = @" using System.Collection.Generic; namespace Foo { machine M { start state S1 { on e<List<Tuple<bool, object>>, Dictionary<string, float>> do Bar; } } }"; var expected = @" using Microsoft.PSharp; using System.Collection.Generic; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventDoAction(typeof(e<List<Tuple<bool, object>>, Dictionary<string, float>>), nameof(Bar))] class S1 : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestOnSimpleGenericEventGotoStateDeclaration() { var test = @" namespace Foo { machine M { start state S1 { on e<int> goto S2; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventGotoState(typeof(e<int>), typeof(S2))] class S1 : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestEntryDeclaration() { var test = @" namespace Foo { machine M { start state S { entry{} } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_S_on_entry_action))] class S : MachineState { } protected void psharp_S_on_entry_action() {} } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestDeferEventDeclaration2() { var test = @" namespace Foo { machine M { start state S { defer e1,e2; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [DeferEvents(typeof(e1), typeof(e2))] class S : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestMachineOnEventDoActionDeclaration() { var test = @" namespace Foo { machine M { group G { start state S1 { on e do Bar; } } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { class G : StateGroup { [Microsoft.PSharp.Start] [OnEventDoAction(typeof(e), nameof(Bar))] public class S1 : MachineState { } } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestOnEventGotoStateAndDoActionDeclaration() { var test = @" namespace Foo { machine M { start state S1 { on e1 goto S2; on e2 do Bar; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventGotoState(typeof(e1), typeof(S2))] [OnEventDoAction(typeof(e2), nameof(Bar))] class S1 : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestMonitorStateGroupDeclaration() { var test = @" namespace Foo { monitor M { start state S1 { } group G { state S2 { } } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Monitor { [Microsoft.PSharp.Start] class S1 : MonitorState { } class G : StateGroup { public class S2 : MonitorState { } } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestOnEventDoActionDeclarationWithBody() { var test = @" namespace Foo { machine M { start state S1 { on e do {} } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventDoAction(typeof(e), nameof(psharp_S1_e_action))] class S1 : MachineState { } protected void psharp_S1_e_action() {} } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestVoidMethodDeclaration2() { var test = @" namespace Foo { machine M { start state S { } void Bar() { } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] class S : MachineState { } void Bar() { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestIgnoreEventDeclaration() { var test = @" namespace Foo { machine M { start state S { ignore e; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [IgnoreEvents(typeof(e))] class S : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestMachineFieldDeclaration() { var test = @" namespace Foo { machine M { machine N; start state S { } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { MachineId N; [Microsoft.PSharp.Start] class S : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestOnEventDoActionDeclarationWithAsyncBody() { var test = @" namespace Foo { machine M { start state S1 { on e do async { await Task.Delay(42); } } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventDoAction(typeof(e), nameof(psharp_S1_e_action_async))] class S1 : MachineState { } protected async System.Threading.Tasks.Task psharp_S1_e_action_async() { await Task.Delay(42); } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestOnQualifiedEventDoActionDeclaration() { var test = @" namespace Foo { machine M { start state S1 { on Foo.e do Bar; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventDoAction(typeof(Foo.e), nameof(Bar))] class S1 : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestWildcardEventAction() { var test = @" namespace Foo { machine M { start state S { on *,e1.e2 goto S2; on * push S2; } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEventGotoState(typeof(Microsoft.PSharp.WildCardEvent), typeof(S2))] [OnEventGotoState(typeof(e1.e2), typeof(S2))] [OnEventPushState(typeof(Microsoft.PSharp.WildCardEvent), typeof(S2))] class S : MachineState { } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestMachineNestedGroups() { var test = @" namespace Foo { machine M { group G1 { start state S1 { entry { jump(G3.S2); } } group G3 { state S2 { entry { jump(S1); } } state S3 { entry { jump(S2); } } } } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { class G1 : StateGroup { public class G3 : StateGroup { [OnEntry(nameof(psharp_G1_G3_S2_on_entry_action))] public class S2 : MachineState { } [OnEntry(nameof(psharp_G1_G3_S3_on_entry_action))] public class S3 : MachineState { } } [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_G1_S1_on_entry_action))] public class S1 : MachineState { } } protected void psharp_G1_S1_on_entry_action() { this.Goto(typeof(G1.G3.S2)); } protected void psharp_G1_G3_S2_on_entry_action() { this.Goto(typeof(G1.S1)); } protected void psharp_G1_G3_S3_on_entry_action() { this.Goto(typeof(G1.G3.S2)); } } } "; LanguageTestUtilities.AssertRewritten(expected, test); }
public void NoPayload_InMachine() { var test = @" namespace Foo { machine m1 { extern event E1x; internal event E1; internal event E2 : E1; internal event E2x : E1x; start state Init {} } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class m1 : Machine { internal class E1 : Event { public E1() : base() { } } internal class E2 : E1 { public E2() : base() { base.SetCardinalityConstraints(-1, -1); } } internal class E2x : E1x { public E2x() : base() { base.SetCardinalityConstraints(-1, -1); } } [Microsoft.PSharp.Start] class Init : MachineState { } } } "; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestCreateNamedMachineStatementWithDoublePayload() { var test = @" namespace Foo { public event e1 (k:int, s:string); machine M { machine Target; start state S { entry { string s = ""hello""; create(M, ""NamedMachine"", e1, 10, s); } } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { public class e1 : Event { public int k; public string s; public e1(int k, string s) : base() { this.k = k; this.s = s; } } class M : Machine { MachineId Target; [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_S_on_entry_action))] class S : MachineState { } protected void psharp_S_on_entry_action() { string s = ""hello""; this.CreateMachine(typeof(M),""NamedMachine"",new e1(10, s)); } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestEventRaiseStatement() { var test = @" namespace Foo { public event e1; machine M { start state S1 { entry { raise(e1); } on e1 goto S2; } state S2 { } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { public class e1 : Event { public e1() : base() { } } class M : Machine { [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_S1_on_entry_action))] [OnEventGotoState(typeof(e1), typeof(S2))] class S1 : MachineState { } class S2 : MachineState { } protected void psharp_S1_on_entry_action() { this.Raise(new e1()); } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestUsingDeclaration() { var test = @" using System.Text;"; var expected = @" using Microsoft.PSharp; using System.Text; "; LanguageTestUtilities.AssertRewritten(expected, test); }
public void PayloadOnBoth() { var test = @" namespace Foo { extern event E1x (a:string); internal event E1 (a:string); internal event E2 : E1 (b:int); internal event E2x : E1x (b:int); }"; var expected = @" using Microsoft.PSharp; namespace Foo { internal class E1 : Event { public string a; public E1(string a) : base() { this.a = a; } } internal class E2 : E1 { public int b; public E2(string a, int b) : base(a) { this.b = b; base.SetCardinalityConstraints(-1, -1); } } internal class E2x : E1x { public int b; public E2x(string a, int b) : base(a) { this.b = b; base.SetCardinalityConstraints(-1, -1); } } } "; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestCreateStatementWithSinglePayload() { var test = @" namespace Foo { public event e1 (k:int); machine M { machine Target; start state S { entry { create(M, e1, 10); } } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { public class e1 : Event { public int k; public e1(int k) : base() { this.k = k; } } class M : Machine { MachineId Target; [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_S_on_entry_action))] class S : MachineState { } protected void psharp_S_on_entry_action() { this.CreateMachine(typeof(M),new e1(10)); } } } "; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestPushStateStatementWithPSharpAPI2() { var test = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_S1_on_entry_action))] class S1 : MachineState { } class S2 : MachineState { } protected void psharp_S1_on_entry_action() { this.Push<S2>(); } } }"; var expected = @" using Microsoft.PSharp; namespace Foo { class M : Machine { [Microsoft.PSharp.Start] [OnEntry(nameof(psharp_S1_on_entry_action))] class S1 : MachineState { } class S2 : MachineState { } protected void psharp_S1_on_entry_action() { this.Push<S2>(); } } }"; // Note: original formatting is preserved for CSharp rewriting. LanguageTestUtilities.AssertRewritten(expected, test, isPSharpProgram: false); }
public void TestNamespaceDeclarationCompact() { var test = @" namespace Foo{}"; var expected = @" using Microsoft.PSharp; namespace Foo { }"; LanguageTestUtilities.AssertRewritten(expected, test); }
public void TestGenericEventDeclarationWithPayload() { var test = @" namespace Foo { internal event e1<T, K> (m:K, n:T); internal event e2 (m:string); public event e3 (n:int); }"; var expected = @" using Microsoft.PSharp; namespace Foo { internal class e1<T,K> : Event { public K m; public T n; public e1(K m, T n) : base() { this.m = m; this.n = n; } } internal class e2 : Event { public string m; public e2(string m) : base() { this.m = m; } } public class e3 : Event { public int n; public e3(int n) : base() { this.n = n; } } }"; LanguageTestUtilities.AssertRewritten(expected, test); }