func TestConvertMainModuleToTemplate(t *testing.T) { mod := intMainModule{ Vars: []intVar{ {"ch", "HandshakeChannel0"}, {"__pid0_ch", "HandshakeChannel0Proxy(ch)"}, {"proc1", "__pid0_ProcA(__pid0_ch)"}, }, } expected := []tmplModule{ { Name: "main", Args: []string{}, Vars: []tmplVar{ {"ch", "HandshakeChannel0"}, {"__pid0_ch", "HandshakeChannel0Proxy(ch)"}, {"proc1", "__pid0_ProcA(__pid0_ch)"}, }, }, } err, tmplMods := convertMainModuleToTemplate(mod) if err != nil { t.Fatalf("Unexpected error: %s", err) } expectPP := pp.PP(expected) actualPP := pp.PP(tmplMods) if expectPP != actualPP { t.Errorf("Unmatched\n%s\n", diff.Diff(expectPP, actualPP)) } }
func parseType(t *testing.T, src string, expect interface{}) { s := new(Scanner) s.Init([]rune("proc A() { var a "+src+"; }"), 0) definitions := Parse(s) if len(definitions) != 1 { t.Errorf("Expect %q to be parsed", src) return } if procDef, isInitBlock := definitions[0].(ProcDefinition); isInitBlock { if len(procDef.Statements) != 1 { t.Errorf("Expect %q to be parsed in ProcDefinition", src) return } if stmt, isVarDecl := procDef.Statements[0].(VarDeclStatement); isVarDecl { if !reflect.DeepEqual(stmt.Type, expect) { t.Errorf("\nExpected %s\nGot %s", pp.PP(expect), pp.PP(stmt.Type)) return } } else { t.Errorf("Expect %q to be parsed in ProcDefinition", src) return } } else { t.Errorf("Expect %q to be parsed in ProcDefinition", src) return } }
func ExamplePP() { val := []interface{}{"Hello, World!", 42} fmt.Println(pp.PP(val)) fmt.Println(pp.PP([]byte("Hello, World!"))) // Output: // [ // "Hello, World!", // 42, // ] // [ // 72, // 101, // 108, // 108, // 111, // 44, // 32, // 87, // 111, // 114, // 108, // 100, // 33, // ] }
func parse(t *testing.T, src string, expect interface{}) { s := new(Scanner) s.Init([]rune(src), 0) definitions := Parse(s) expectPP := pp.PP(expect) actualPP := pp.PP(definitions) if expectPP != actualPP { t.Errorf("\nExpected %s\nGot %s", expectPP, actualPP) } }
func TestConvertHandshakeChannelToTemplate(t *testing.T) { mod := intHandshakeChannel{ Name: "HandshakeChannel0", ValueType: []string{"boolean"}, ZeroValue: []string{"FALSE"}, } expected := []tmplModule{ { Name: "HandshakeChannel0", Args: []string{"running_pid", "filleds", "receiveds", "values_0"}, Vars: []tmplVar{ {"filled", "boolean"}, {"received", "boolean"}, {"value_0", "boolean"}, }, Assigns: []tmplAssign{ {"init(filled)", "FALSE"}, {"next(filled)", "filleds[running_pid]"}, {"init(received)", "FALSE"}, {"next(received)", "receiveds[running_pid]"}, {"init(value_0)", "FALSE"}, {"next(value_0)", "values_0[running_pid]"}, }, }, { Name: "HandshakeChannel0Proxy", Args: []string{"ch"}, Vars: []tmplVar{ {"next_filled", "boolean"}, {"next_received", "boolean"}, {"next_value_0", "boolean"}, }, Defs: []tmplAssign{ {"filled", "ch.filled"}, {"received", "ch.received"}, {"value_0", "ch.value_0"}, }, }, } err, tmplMods := convertHandshakeChannelToTemplate(mod) if err != nil { t.Fatalf("Unexpected error: %s", err) } expectPP := pp.PP(expected) actualPP := pp.PP(tmplMods) if expectPP != actualPP { t.Errorf("Unmatched\nExpected %s\nGot %s", expectPP, actualPP) } }
func TestConvertMainModuleToTemplate(t *testing.T) { mod := intMainModule{ Vars: []intVar{ {"ch", "HandshakeChannel0(running_pid, ch_filled, ch_received, ch_value_0)"}, {"__pid0_ch", "HandshakeChannel0Proxy(ch)"}, {"proc1", "__pid0_ProcA(running_pid, 0, __pid0_ch)"}, {"running_pid", "{0}"}, }, Assigns: []intAssign{ {"running_pid", "{0}"}, }, Defs: []intAssign{ {"ch_filled", "[__pid0_ch.next_filled]"}, {"ch_received", "[__pid0_ch.next_received]"}, {"ch_value_0", "[__pid0_ch.next_value_0]"}, }, } expected := []tmplModule{ { Name: "main", Args: []string{}, Vars: []tmplVar{ {"ch", "HandshakeChannel0(running_pid, ch_filled, ch_received, ch_value_0)"}, {"__pid0_ch", "HandshakeChannel0Proxy(ch)"}, {"proc1", "__pid0_ProcA(running_pid, 0, __pid0_ch)"}, {"running_pid", "{0}"}, }, Assigns: []tmplAssign{ {"running_pid", "{0}"}, }, Defs: []tmplAssign{ {"ch_filled", "[__pid0_ch.next_filled]"}, {"ch_received", "[__pid0_ch.next_received]"}, {"ch_value_0", "[__pid0_ch.next_value_0]"}, }, }, } err, tmplMods := convertMainModuleToTemplate(mod) if err != nil { t.Fatalf("Unexpected error: %s", err) } expectPP := pp.PP(expected) actualPP := pp.PP(tmplMods) if expectPP != actualPP { t.Errorf("Unmatched\nExpected %s\nGot %s", expectPP, actualPP) } }
func parseInBlock(t *testing.T, src string, expect interface{}) { s := new(Scanner) s.Init([]rune("proc A() { "+src+" }"), 0) definitions := Parse(s) if len(definitions) != 1 { t.Errorf("Expect %q to be parsed", src) return } if procDef, isProcDef := definitions[0].(ProcDefinition); isProcDef { if len(procDef.Statements) != 1 { t.Errorf("Expect %q to be parsed in ProcDefinition", src) return } if !reflect.DeepEqual(procDef.Statements[0], expect) { t.Errorf("\nExpected %s\nGot %s", pp.PP(expect), pp.PP(procDef.Statements[0])) return } } else { t.Errorf("Expect %q to be parsed in ProcDefinition", src) return } }
func TestConvertHandshakeChannelToTemplate(t *testing.T) { mod := intHandshakeChannel{ Name: "HandshakeChannel0", ValueType: []string{"boolean"}, ZeroValue: []string{"FALSE"}, } expected := []tmplModule{ { Name: "HandshakeChannel0", Args: []string{}, Vars: []tmplVar{ {"filled", "boolean"}, {"received", "boolean"}, {"value_0", "boolean"}, }, Assigns: []tmplAssign{ {"init(filled)", "FALSE"}, {"init(received)", "FALSE"}, {"init(value_0)", "FALSE"}, }, }, { Name: "HandshakeChannel0Proxy", Args: []string{"ch"}, Vars: []tmplVar{ {"send_filled", "boolean"}, {"send_leaving", "boolean"}, {"recv_received", "boolean"}, {"send_value_0", "boolean"}, }, Defs: []tmplAssign{ {"ready", "ch.filled"}, {"received", "ch.received"}, {"value_0", "ch.value_0"}, }, Assigns: []tmplAssign{ {"next(ch.filled)", strings.Join([]string{ "case", " send_filled : TRUE;", " send_leaving : FALSE;", " TRUE : ch.filled;", "esac", }, "\n")}, {"next(ch.received)", strings.Join([]string{ "case", " send_filled : FALSE;", " send_leaving : FALSE;", " recv_received : TRUE;", " TRUE : ch.received;", "esac", }, "\n")}, {"next(ch.value_0)", strings.Join([]string{ "case", " send_filled : send_value_0;", " TRUE : ch.value_0;", "esac", }, "\n")}, }, }, } err, tmplMods := convertHandshakeChannelToTemplate(mod) if err != nil { t.Fatalf("Unexpected error: %s", err) } expectPP := pp.PP(expected) actualPP := pp.PP(tmplMods) if expectPP != actualPP { t.Errorf("Unmatched\n%s\n", diff.Diff(expectPP, actualPP)) } }
func TestConvertProcModuleToTemplate(t *testing.T) { mod := intProcModule{ Name: "__pid0_ProcA", Args: []string{"ch0"}, Vars: []intVar{ {"b", "0..8"}, }, InitState: intState("state0"), Trans: []intTransition{ { FromState: "state0", NextState: "state1", Condition: "", }, { FromState: "state1", NextState: "state2", Condition: "!ch0.ready", Actions: []intAssign{ {"ch0.send_filled", "TRUE"}, {"ch0.send_value_0", "TRUE"}, }, }, }, Defaults: map[string]string{ "ch0.send_filled": "FALSE", "ch0.recv_received": "FALSE", "ch0.send_value_0": "ch0.value_0", }, Defs: []intAssign{}, } expected := []tmplModule{ { Name: "__pid0_ProcA", Args: []string{"ch0"}, Vars: []tmplVar{ {"state", "{state0, state1, state2}"}, {"transition", "{notrans, trans0, trans1}"}, {"b", "0..8"}, }, Trans: []string{ "transition = trans0 -> (TRUE)", "transition = trans1 -> (!ch0.ready)", }, Assigns: []tmplAssign{ {"transition", strings.Join([]string{ "case", " state = state0 & ((TRUE)) : {trans0};", " state = state1 & ((!ch0.ready)) : {trans1};", " TRUE : notrans;", "esac", }, "\n")}, {"init(state)", "state0"}, {"next(state)", strings.Join([]string{ "case", " transition = trans0 : state1;", " transition = trans1 : state2;", " TRUE : state;", "esac", }, "\n")}, {"ch0.send_filled", strings.Join([]string{ "case", " transition = trans1 : TRUE;", " TRUE : FALSE;", "esac", }, "\n")}, {"ch0.recv_received", strings.Join([]string{ "case", " TRUE : FALSE;", "esac", }, "\n")}, {"ch0.send_value_0", strings.Join([]string{ "case", " transition = trans1 : TRUE;", " TRUE : ch0.value_0;", "esac", }, "\n")}, }, Justice: "running", }, } err, tmplMods := convertProcModuleToTemplate(mod) if err != nil { t.Fatalf("Unexpected error: %s", err) } expectPP := pp.PP(expected) actualPP := pp.PP(tmplMods) if expectPP != actualPP { t.Errorf("Unmatched\n%s\n", diff.Diff(expectPP, actualPP)) } }
func TestConvertASTToIntModuleForSend(t *testing.T) { return // FIXME: sendWithTagSource := ` fault send(ch channel { bool }) @omission { // do nothing } proc SendProc(ch channel { bool }) { send(ch, true) @omission } init { ch: channel { bool }, sp: SendProc(ch), } ` expected := []intModule{ intHandshakeChannel{ Name: "HandshakeChannel0", ValueType: []string{"boolean"}, ZeroValue: []string{"FALSE"}, }, intProcModule{ Name: "__pid0_SendProc", Args: []string{"__orig_ch"}, Vars: []intVar{ {"ch", "HandshakeChannel0Proxy(__orig_ch)"}, }, InitState: intState("state0"), Trans: []intTransition{ { FromState: "state0", NextState: "state2", Condition: "", Actions: []intAssign(nil), }, { FromState: "state2", NextState: "state3", Condition: "!(ch.ready)", Actions: []intAssign{ {"ch.send_filled", "TRUE"}, {"ch.send_value_0", "TRUE"}, }, }, { FromState: "state3", NextState: "state4", Condition: "(ch.ready) & (ch.received)", Actions: []intAssign{ {"ch.send_leaving", "TRUE"}, }, }, { FromState: "state4", NextState: "state1", Condition: "", Actions: []intAssign(nil), }, { FromState: "state0", NextState: "state5", Condition: "", Actions: []intAssign(nil), }, { FromState: "state5", NextState: "state6", Condition: "!(ch.ready)", Actions: []intAssign{ {"ch.send_filled", "TRUE"}, {"ch.send_value_0", "TRUE"}, }, }, { FromState: "state6", NextState: "state7", Condition: "(ch.ready) & (ch.received)", Actions: []intAssign{ {"ch.send_leaving", "TRUE"}, }, }, { FromState: "state7", NextState: "state1", Condition: "", Actions: []intAssign(nil), }, }, Defaults: map[string]string{ "ch.send_leaving": "FALSE", "ch.send_filled": "FALSE", "ch.recv_received": "FALSE", "ch.send_value_0": "ch.value_0", }, Defs: []intAssign(nil), }, intMainModule{ Vars: []intVar{ {"ch", "HandshakeChannel0"}, {"sp", "process __pid0_SendProc(ch)"}, }, }, } scanner := new(parsing.Scanner) scanner.Init([]rune(sendWithTagSource), 0) defs := parsing.Parse(scanner) err, intMods := astToIr1(defs) if err != nil { t.Fatalf("Unexpected error: %s", err) } actualPP := pp.PP(intMods) expectPP := pp.PP(expected) if actualPP != expectPP { t.Errorf("Unmatched\n%s\n", diff.Diff(expectPP, actualPP)) } }
func TestConvertASTToIntModule(t *testing.T) { return // FIXME: pending because it fails defs := []Def{ ProcDef{ Name: "ProcA", Parameters: []Parameter{ { Name: "ch0", Type: HandshakeChannelType{ Elems: []Type{NamedType{"bool"}}, }, }, }, Stmts: []Stmt{ VarDeclStmt{ Name: "b", Type: NamedType{"int"}, }, SendStmt{ Channel: IdentifierExpr{Pos{}, "ch0"}, Args: []Expr{ TrueExpr{Pos{}}, }, }, }, }, InitBlock{ Vars: []InitVar{ ChannelVar{ Name: "ch", Type: HandshakeChannelType{ Elems: []Type{NamedType{"bool"}}, }, }, InstanceVar{ Name: "proc1", ProcDefName: "ProcA", Args: []Expr{ IdentifierExpr{Pos{}, "ch"}, }, }, }, }, } expected := []intModule{ intHandshakeChannel{ Name: "HandshakeChannel0", ValueType: []string{"boolean"}, ZeroValue: []string{"FALSE"}, }, intProcModule{ Name: "__pid0_ProcA", Args: []string{"__orig_ch0"}, Vars: []intVar{ {"ch0", "HandshakeChannel0Proxy(__orig_ch0)"}, {"b", "0..1"}, }, InitState: intState("state0"), Trans: []intTransition{ { FromState: "state0", NextState: "state1", Condition: "", }, { FromState: "state1", NextState: "state2", Condition: "!(ch0.ready)", Actions: []intAssign{ {"ch0.send_filled", "TRUE"}, {"ch0.send_value_0", "TRUE"}, }, }, { FromState: "state2", NextState: "state3", Condition: "(ch0.ready) & (ch0.received)", Actions: []intAssign{ {"ch0.send_leaving", "TRUE"}, }, }, }, Defaults: map[string]string{ "ch0.send_leaving": "FALSE", "ch0.send_filled": "FALSE", "ch0.recv_received": "FALSE", "ch0.send_value_0": "ch0.value_0", "next(b)": "b", }, Defs: []intAssign{}, }, intMainModule{ Vars: []intVar{ {"ch", "HandshakeChannel0"}, {"proc1", "process __pid0_ProcA(ch)"}, }, }, } err, intMods := astToIr1(defs) if err != nil { t.Fatalf("Unexpected error: %s", err) } expectPP := pp.PP(expected) actualPP := pp.PP(intMods) if expectPP != actualPP { t.Errorf("Unmatched\n%s\n", diff.Diff(expectPP, actualPP)) } }
func TestConvertASTToIntModule(t *testing.T) { defs := []Definition{ ProcDefinition{ Name: "ProcA", Parameters: []Parameter{ { Name: "ch0", Type: HandshakeChannelType{ Elems: []Type{NamedType{"bool"}}, }, }, }, Statements: []Statement{ VarDeclStatement{ Name: "b", Type: NamedType{"int"}, }, SendStatement{ Channel: IdentifierExpression{Pos{}, "ch0"}, Args: []Expression{ TrueExpression{Pos{}}, }, }, }, }, InitBlock{ Vars: []InitVar{ ChannelVar{ Name: "ch", Type: HandshakeChannelType{ Elems: []Type{NamedType{"bool"}}, }, }, InstanceVar{ Name: "proc1", ProcDefName: "ProcA", Args: []Expression{ IdentifierExpression{Pos{}, "ch"}, }, }, }, }, } expected := []intModule{ intHandshakeChannel{ Name: "HandshakeChannel0", ValueType: []string{"boolean"}, ZeroValue: []string{"FALSE"}, }, intProcModule{ Name: "__pid0_ProcA", Args: []string{"running_pid", "pid", "ch0"}, Vars: []intVar{ {"b", "0..8"}, }, InitState: intState("state0"), Trans: []intTransition{ { FromState: "state0", NextState: "state1", Condition: "", }, { FromState: "state1", NextState: "state2", Condition: "!(ch0.filled)", Actions: []intAssign{ {"ch0.next_filled", "TRUE"}, {"ch0.next_received", "FALSE"}, {"ch0.next_value_0", "TRUE"}, }, }, { FromState: "state2", NextState: "state3", Condition: "(ch0.filled) & (ch0.received)", Actions: []intAssign{ {"ch0.next_filled", "FALSE"}, }, }, }, Defaults: map[string]string{ "ch0.next_filled": "ch0.filled", "ch0.next_received": "ch0.received", "ch0.next_value_0": "ch0.value_0", "next(b)": "b", }, Defs: []intAssign{}, }, intMainModule{ Vars: []intVar{ {"ch", "HandshakeChannel0(running_pid, ch_filled, ch_received, ch_value_0)"}, {"__pid0_ch", "HandshakeChannel0Proxy(ch)"}, {"proc1", "__pid0_ProcA(running_pid, 0, __pid0_ch)"}, {"running_pid", "{0}"}, }, Assigns: []intAssign{ {"running_pid", "{0}"}, }, Defs: []intAssign{ {"ch_filled", "[__pid0_ch.next_filled]"}, {"ch_received", "[__pid0_ch.next_received]"}, {"ch_value_0", "[__pid0_ch.next_value_0]"}, }, }, } err, intMods := convertASTToIntModule(defs) if err != nil { t.Fatalf("Unexpected error: %s", err) } expectPP := pp.PP(expected) actualPP := pp.PP(intMods) if expectPP != actualPP { t.Errorf("Unmatched\nExpected %s\nGot %s", expectPP, actualPP) } }