def test_nta_from_xml(): """Test NTA.from_xml and NTA.from_element.""" nta = NTA.from_xml(testcase_dir + "nta_xml_files/small_nta.xml") assert len(nta.templates) == 2 assert nta.declaration.text == "// Place global declarations here." assert ( nta.system.text[:-3] == "// Place template instantiations here.\nProcess1 = Test1();\nProcess2 = Test2();\n// List one or more processes to be composed into a system.\nsystem Process1, Process2;" ) assert len(nta.queries) == 1 nta = NTA.from_xml(testcase_dir + "nta_xml_files/big_nta.xml") assert len(nta.templates) == 4 assert nta.declaration.text == "// Place global declarations here.\nchan c1;" assert len(nta.queries) == 1
def test_constraint_cache_apply_single_patch(): """Test _apply_single_patch method.""" nta = NTA.from_xml(testcase_dir + "constraint_cache_xml_files/test01.xml") cc = nta.patch_cache # Update the threshold of the transition in the second template. # x <= 10 -> x <= 15 template = nta.templates[1] # Second template transition = template.graph._transitions[0] # First template constraints = transition.guard.constraints update = ConstraintUpdate(constraints[0], "15") # type: ignore cp = ConstraintPatch(template, update, obj_ref=transition) cc.cache(cp) lines = open(testcase_dir + "constraint_cache_xml_files/test01.xml").readlines() lines_subject_to_change = lines[:] cc._apply_single_patch(lines_subject_to_change, cc.patches[0]) # 56th line is changed. assert lines[:56] == lines_subject_to_change[:56] assert lines[57:] == lines_subject_to_change[57:] assert lines[56].replace("10", "15") == lines_subject_to_change[56]
def test_constraint_cache_init(): """Test ConstraintCache().""" nta = NTA.from_xml(testcase_dir + "constraint_cache_xml_files/test01.xml") cc = nta.patch_cache assert cc.nta == nta assert cc.patches == []
def test_nta_flush_changes_no_changes(): """Test NTA.flush_constraint_changes() with no changes.""" path = testcase_dir + "nta_xml_files/small_nta.xml" nta = NTA.from_xml(path) nta.flush_constraint_changes("/tmp/out.xml") with open(path) as inf, open("/tmp/out.xml") as outf: inlines, outlines = inf.readlines(), outf.readlines() for i in range(len(inlines)): assert _dec_check(inlines[i], outlines[i]) path = testcase_dir + "nta_xml_files/big_nta.xml" nta = NTA.from_xml(path) nta.flush_constraint_changes("/tmp/out.xml") with open(path) as inf, open("/tmp/out.xml") as outf: inlines, outlines = inf.readlines(), outf.readlines() for i in range(len(inlines)): assert _dec_check(inlines[i], outlines[i])
def test_nta_to_file(): """Test NTA.to_file.""" path = testcase_dir + "nta_xml_files/small_nta.xml" nta = NTA.from_xml(path) nta.to_file("/tmp/out.xml") with open(path) as inf, open("/tmp/out.xml") as outf: inlines, outlines = inf.readlines(), outf.readlines() for i in range(len(inlines)): assert _dec_check(inlines[i], outlines[i]) path = testcase_dir + "nta_xml_files/big_nta.xml" nta = NTA.from_xml(path) nta.to_file("/tmp/out.xml") with open(path) as inf, open("/tmp/out.xml") as outf: inlines, outlines = inf.readlines(), outf.readlines() for i in range(len(inlines)): assert _dec_check(inlines[i], outlines[i])
def test_nta_init(): """Test NTA().""" nta = NTA( declaration=Declaration("foo"), templates=[], system=SystemDeclaration("bar"), queries=[], context=Context.empty() ) assert nta.declaration.text == "foo" assert nta.system.text == "bar" assert nta.templates == [] assert nta.queries == [] assert nta._associated_file == "" assert nta._doctype == ""
def test_constraint_cache_cache(): """Test ConstraintCache.cache().""" nta = NTA.from_xml(testcase_dir + "constraint_cache_xml_files/test01.xml") cc = nta.patch_cache # Update the threshold of the transition in the second template. # x <= 10 -> x <= 15 template = nta.templates[1] # Second template transition = template.graph._transitions[0] # First template constraints = transition.guard.constraints update = ConstraintUpdate(constraints[0], 15) # type: ignore cp = ConstraintPatch(template, update, obj_ref=transition) cc.cache(cp) assert cc.patches == [cp]
def test_constraint_patch_init_location(): """Test ConstraintPatch().""" nta = NTA.from_xml(testcase_dir + "constraint_cache_xml_files/test01.xml") # Update the invariant of l0 in the first template. # x <= 10 -> x <= 15 template = nta.templates[0] # First template location = template.graph._named_locations["l0"] constraints = location.invariant.constraints update = ConstraintUpdate(constraints[0], 15) # type: ignore cp = ConstraintPatch(template, update, obj_ref=location) assert cp.template_ref == template assert cp.obj_ref == location assert cp.change == update
def test_constraint_patch_init_transition(): """Test ConstraintPatch().""" nta = NTA.from_xml(testcase_dir + "constraint_cache_xml_files/test01.xml") # Update the threshold of the transition in the second template. # x <= 10 -> x <= 15 template = nta.templates[1] # Second template transition = template.graph._transitions[0] # First transition constraints = transition.guard.constraints update = ConstraintUpdate(constraints[0], 15) # type: ignore cp = ConstraintPatch(template, update, obj_ref=transition) assert cp.template_ref == template assert cp.obj_ref == transition assert cp.change == update