def test_get_all(self): io = IO('test.json') io.set([]) repo = EventRepository('test.json') content = repo.get_all() self.assertEqual(content, []) event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) content = repo.get_all() self.assertIs(type(content), list) self.assertEqual(len(content), 2) stored_event1 = content[0] stored_event2 = content[1] self.assertEqual(stored_event1.get_date(), '14/01/2015') self.assertEqual(stored_event1.get_time(), '14:00') self.assertEqual(stored_event1.get_description(), 'Colectiv') self.assertEqual(stored_event2.get_date(), '14/01/2015') self.assertEqual(stored_event2.get_time(), '14:00') self.assertEqual(stored_event2.get_description(), 'Colectiv')
def check(src1, src2): if not os.path.exists(src1): IO.writestr("! checker: {src} not found".format(src=src1)) return None if not os.path.exists(src2): IO.writestr("! checker: {src} not found".format(src=src2)) return None cmd = "checker\\displaychecker.exe {0} {1}".format(src1, src2) # print("> {cmd}".format(cmd=cmd)) res = os.popen(cmd) res_lines = res.readlines() res_lines = [line.strip() for line in res_lines] valid_lines = [] for l in res_lines: if len(l) != 0 and l[0] != '-': valid_lines.append(l) # print(l) res_lines = valid_lines # print(res_lines) if res_lines[-1].strip() == 'Accepted': return ('Accepted', 'Accepted') elif res_lines[-1].strip() == 'Wrong Answer': return ('Wrong Answer', "\n".join(res_lines[0:-1])) else: return None
def loadtest(self, testcase: Testcase): in_name = self.path + '/' + self.globconf['inputfilename'] hexname = testcase.path + '/' + testcase.hex if not os.path.exists(hexname): IO.writestr('! Runner.loadtest: Hex Machine Code Not Exist') return False shutil.copyfile(src=hexname, dst=in_name) return True
def get_all(self): operation = IO(self.__defaultfile) person_events = [] for person_event in operation.get(): person_events.append(PersonEvent(person_event['person_id'], person_event['event_id'], person_event['date'])) return person_events
def run(self, testcase, out): r = self.loadtest(testcase) if not r: IO.writestr('! Runner.run: load testcase error.') return False outpath = "{path}/out".format(path=self.path) if not os.path.exists(outpath): os.mkdir(outpath) ###### Code Here ###### return True
def insert(self, person_event): operation = IO(self.__defaultfile) try: person_events = operation.get() except ValueError: person_events = [] person_events.append(person_event.get_serialization()) operation.set(person_events)
def timetrim(src, dst): if not os.path.exists(src): IO.writestr("! timetrim: {src} not found.".format(src=src)) return False if not os.path.exists(os.path.dirname(dst)): os.mkdir(os.path.dirname(dst)) cmd = "checker\\timeprocess.exe < {0} > {1}".format(src, dst) r = os.system(cmd) if r != 0: IO.writestr("! timetrim: error running timeprocess.exe") return None
def judge(self): # check runner if self.runner_type == None: IO.writestr('! Judger: not supported simulation tool') if self.mode == 'standard': return self.stdmode() elif self.mode == 'pat': return self.patmode() else: IO.writestr('! Judger: invalid mode') return False
def run(self, testcase, out): r = super().run(testcase, out) if not r: return False # iverilog run vvp = (self.iv_path + os.sep if self.iv_path else "") + "vvp" r = os.system("cd {path} && {vvp} src_unzip/mips.vvp > out/{out}".format(path=self.path, vvp=vvp, out=out)) if r != 0: IO.writestr("! Error Occured on iVerilog Running") return False return True
def get(self, id): ''' Returns an event with provided ID from disk storage :param id: :return: Event instance ''' operation = IO(self.__datafile) for event in operation.get(): if event['id'] == id: return Event(event['date'], event['time'], event['description'], event['id']) raise ValueError('Event not found!')
def get_all(self): ''' Returns all events from disk storage :return: ''' operation = IO(self.__datafile) events = [] for event in operation.get(): instance = Event(event['date'], event['time'], event['description'], event['id']) events.append(instance) return events
def test_validation(self): if os.path.exists(IO.filepath('test.json')): os.remove(IO.filepath('test.json')) with self.assertRaisesRegex(AttributeError, 'Please provide a valid file name!'): io = IO() io = IO('test.json') self.assertTrue(os.path.exists(IO.filepath('test.json'))) os.remove(IO.filepath('test.json'))
def test_delete(self): io = IO('test.json') io.set([]) Person.set_class_id(10) person = Person('Vasile', 'Pop', 'Str. Calea Floresti, nr. 24') repo = PersonRepository('test.json') repo.insert(person) repo.delete(person) with self.assertRaisesRegex(ValueError, 'Person not found!'): person = repo.get(10)
def compile(self): if self.iv_path == None: IO.writestr("! Runner(iverilog).run: iverilog not found.") return False iverilog = (self.iv_path + os.sep if self.iv_path else "") + "iverilog" src_unzip = self.path + "/src_unzip" mips = " ".join(self.v_list) # print(self.v_list) r = os.system("cd {src} && {iverilog} -o mips.vvp tb.v {mips}".format(src=src_unzip, iverilog=iverilog,mips=mips)) if r != 0: IO.writestr("! Runner(iverilog).run: Error Occured on iVerilog Compiling") return False return True
def get_all(self): ''' Returns all persons from disk storage :return: ''' operation = IO(self.__defaultfile) people = [] for person in operation.get(): person_instance = Person(person['first_name'], person['last_name'], person['address'], person['id']) people.append(person_instance) return people
def delete(self, person): ''' Deletes a person from disk storage :param deleted_event: :return: ''' operation = IO(self.__defaultfile) people = operation.get() for per in people: if per['id'] == person.get_id(): people.remove(per) operation.set(people)
def delete(self, deleted_event): ''' Deletes an event from disk storage :param deleted_event: :return: ''' operation = IO(self.__datafile) events = operation.get() for event in events: if event['id'] == deleted_event.get_id(): events.remove(event) break operation.set(events)
def update(self, updated_event): ''' Updates an event into disk storage :param updated_event: :return: ''' operation = IO(self.__datafile) events = operation.get() for event in events: if event['id'] == updated_event.get_id(): events[events.index(event)] = updated_event.get_serialization() break operation.set(events)
def update(self, person): ''' Updates a person into disk storage :param updated_event: :return: ''' operation = IO(self.__defaultfile) people = operation.get() for per in people: if per['id'] == person.get_id(): people[people.index(per)] = person.get_serialization() operation.set(people)
def test_insert(self): repo = PersonEventRepository() person_event = PersonEvent(25, 61) repo.insert(person_event) person_events = repo.get_by_id(25, 61) self.assertEqual(len(person_events), 1) # Remove last appended object io = IO('person_event.json') updated_person_events = io.get() updated_person_events.pop() io.set(updated_person_events)
def test_insert(self): io = IO('test.json') io.set([]) event = Event('14/01/2015', '14:00', 'Colectiv') EventRepository('test.json').insert(event) content = io.get() self.assertIs(type(content), list) stored_event = content[0] self.assertEqual(stored_event['date'], '14/01/2015') self.assertEqual(stored_event['time'], '14:00') self.assertEqual(stored_event['description'], 'Colectiv')
def insert(self, person): ''' Add a person object into disk storage :param event: :return: None ''' operation = IO(self.__defaultfile) try: people = operation.get() except ValueError: people = [] people.append(person.get_serialization()) operation.set(people)
def insert(self, event): ''' Add an event object into disk storage :param event: :return: None ''' operation = IO(self.__datafile) try: data = operation.get() except ValueError: data = [] data.append(event.get_serialization()) operation.set(data)
def test_get_max_id(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') self.assertEqual(repo.get_max_id(), 0) content = repo.get_all() self.assertEqual(content, []) event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) content = repo.get_all() self.assertEqual(repo.get_max_id(), 2)
def test_insert(self): io = IO('test.json') io.set([]) Person.set_class_id(0) person = Person('Vasile', 'Pop', 'Str. Calea Floresti, nr. 24') repo = PersonRepository('test.json') repo.insert(person) people = io.get() person = people[0] self.assertEqual(len(people), 1) self.assertEqual(person['id'], 0) self.assertEqual(person['first_name'], 'Vasile') self.assertEqual(person['last_name'], 'Pop') self.assertEqual(person['address'], 'Str. Calea Floresti, nr. 24')
def get_by_id(self, person_id = None, event_id = None): operation = IO(self.__defaultfile) person_events = [] for person_event in operation.get(): if person_id is not None and event_id is not None: if person_event['person_id'] == person_id and person_event['event_id'] == event_id: return [PersonEvent(person_id, event_id, person_event['date'])] elif person_id is None: if person_event['event_id'] == event_id: person_events.append(PersonEvent(person_event['person_id'], event_id, person_event['date'])) else: if person_event['person_id'] == person_id: person_events.append(PersonEvent(person_id, person_event['event_id'], person_event['date'])) return person_events
def test_get(self): io = IO('test.json') io.set([]) Person.set_class_id(10) person = Person('Vasile', 'Pop', 'Str. Calea Floresti, nr. 24') repo = PersonRepository('test.json') repo.insert(person) person = repo.get(10) self.assertEqual(person.get_id(), 10) self.assertEqual(person.get_name(), 'Vasile Pop') self.assertEqual(person.get_address(), 'Str. Calea Floresti, nr. 24') with self.assertRaisesRegex(ValueError, 'Person not found!'): person = repo.get(0)
def run(self, testcase, out): r = super().run(testcase, out) if not r: return False tcl = "\n".join(self.tcl_run) tcl_name = 'run.do' with open(self.path + '/' + tcl_name, "w") as fp: fp.write(tcl) # modelsim compile and run vsim = (self.sim_path + '\\' if self.sim_path else '') + 'vsim.exe' cmd = 'cd {path} && \"{vsim}\" -c -do {tcl} > out/{out}'.format( path=self.path, vsim=vsim, tcl=tcl_name, out=out) r = os.system(cmd) if r != 0: IO.writestr('! Error Occured on ModelSim Running') return False return True
def test_set(self): file = open(IO.filepath('test.json'), 'w') file.close() io = IO('test.json') io.set('{"data":[{"a":1}]}') self.assertEqual(io.get(), '{"data":[{"a":1}]}') io.set(1) self.assertEqual(io.get(), 1)
def test_get_all(self): io = IO('test.json') io.set([]) Person.set_class_id(0) person = Person('Vasile', 'Pop', 'Str. Calea Floresti, nr. 24') repo = PersonRepository('test.json') repo.insert(person) people = repo.get_all() self.assertEqual(len(people), 1) person = people[0] self.assertEqual(person.get_id(), 0) self.assertEqual(person.get_name(), 'Vasile Pop') self.assertEqual(person.get_address(), 'Str. Calea Floresti, nr. 24')
def test_delete(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) event = repo.get(0) self.assertIs(type(event), Event) repo.delete(event) with self.assertRaisesRegex(ValueError, 'Event not found!'): updated_event = repo.get(0)
def test_update(self): io = IO('test.json') io.set([]) Person.set_class_id(10) person = Person('Vasile', 'Pop', 'Str. Calea Floresti, nr. 24') repo = PersonRepository('test.json') repo.insert(person) person = repo.get(10) person.update('Dan', 'Popescu', 'Calea Dorobantilor') repo.update(person) updated_person = repo.get(10) self.assertEqual(person.get_id(), 10) self.assertEqual(person.get_name(), 'Dan Popescu') self.assertEqual(person.get_address(), 'Calea Dorobantilor')
def test_get(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) content = repo.get(0) self.assertIs(type(content), Event) self.assertEqual(content.get_date(), '14/01/2015') self.assertEqual(content.get_time(), '14:00') self.assertEqual(content.get_description(), 'Colectiv') with self.assertRaisesRegex(ValueError, 'Event not found!'): content = repo.get(10)
def run(self, testcase, out): if self.batch_run == None: IO.writestr('! Runner(ise).run: Batch not configured.') return False if self.tcl == None: IO.writestr('! Runner(ise).run: Tcl not configured.') return False r = super().run(testcase, out) if not r: return False tcl = "\n".join(self.tcl) with open(self.path + '\\ise.tcl', "w") as fout: fout.write(tcl) # Run # r = os.system("{path}\\mips.exe -tclbatch {path}\\ise.tcl > {path}\\out\\{out}".format(path=self.path, out=out)) bat = "\r\n".join(self.batch_run).format(root=self.ise_root, bin=self.ise_bin, path=self.path, out=out) bat = "@echo off\r\n" + bat with open(self.path + "\\run.bat", "w") as fout: fout.write(bat) # Compile r = os.system("cd {path} && call run.bat".format(path=self.path)) if r != 0: IO.writestr('! Error Occured on ISE Running') return False return True
def compile(self): if self.sim_path == None: IO.writestr( '! Runner(modelsim).run: ModelSim Simulator not configured.') return False if self.tcl_compile == None: IO.writestr( '! Runner(modelsim).run: Modelsim Compile Tcl Batch not configured.' ) return False tcl = "\n".join(self.tcl_compile) for v in self.v_list: tcl += '\nvlog src_unzip/{v}'.format(v=v) tcl_name = 'compile.do' with open(self.path + '/' + tcl_name, "w") as fp: fp.write(tcl) vsim = (self.sim_path + '\\' if self.sim_path else '') + 'vsim.exe' cmd = 'cd {path} && \"{vsim}\" -c -do {tcl} >nul 2>nul'.format( path=self.path, vsim=vsim, tcl=tcl_name) r = os.system(cmd) if r != 0: IO.writestr('! Error Occured on ModelSim Compileing') return False return True
def test_update(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) event = repo.get(0) self.assertIs(type(event), Event) event.update('15/01/2015', '15:00', 'Untold festival') repo.update(event) updated_event = repo.get(0) self.assertEqual(updated_event.get_date(), '15/01/2015') self.assertEqual(updated_event.get_time(), '15:00') self.assertEqual(updated_event.get_description(), 'Untold festival')
def loadcode(self): if not os.path.exists(self.src): IO.writestr("! Runner.loadcode: Source Not Exist!") return False src_unzip = self.path + '/' + 'src_unzip' self.src_unzip = src_unzip if os.path.exists(src_unzip): shutil.rmtree(src_unzip) os.mkdir(self.path + '/' + 'src_unzip') try: zip = zipfile.ZipFile(self.src) zip.extractall(src_unzip) zip.close() except: IO.writestr("! Runner.loadcode: Error occured on extracting zip") return False self.v_list = [] self._addIncProtect(src_unzip, '') # copy testbench tb = self.globconf['testbench'] shutil.copyfile(src=tb, dst=src_unzip + '/tb.v') return True
def compile(self): if self.ise_root == None: IO.writestr('! Runner(ise).compile: ISE Path not configured.') return False if self.batch_compile == None: IO.writestr('! Runner(ise).compile: Batch not configured.') return False src_list = " ".join(["src_unzip\\" + v for v in self.v_list]) bat = "\r\n".join(self.batch_compile).format(root=self.ise_root, bin=self.ise_bin, path=self.path, src=src_list) bat = "@echo off\r\n" + bat with open(self.path + "\\compile.bat", "w") as fout: fout.write(bat) # Compile r = os.system("cd {path} && call compile.bat".format(path=self.path)) if r != 0: IO.writestr('! Error Occured on ISE Compiling') return False return True
def test_read(self): file = open(IO.filepath('test.json'), 'w') file.write('1,2,3,,5') file.close() io = IO('test.json') with self.assertRaisesRegex(ValueError, 'The content of test.json is not valid!'): io.get() file = open(IO.filepath('test.json'), 'w') file.write('invalid json') file.close() io = IO('test.json') with self.assertRaisesRegex(ValueError, 'The content of test.json is not valid!'): io.get() file = open(IO.filepath('test.json'), 'w') file.write('[]') file.close() self.assertEqual(io.get(), []) os.remove(IO.filepath('test.json'))
def test_initialization(self): io = IO('test.json') io.set([]) repository = PersonRepository('test.json') self.assertIsInstance(repository, PersonRepository)
def patmode(self): IO.writestr('Judger Identifier: {id}'.format( id=Config.getValue('configs/global.json', 'identifier'))) IO.writestr('Simulation Tool: {sim}'.format(sim=self.task['tool'])) IO.writestr(' - Pat Mode - ') runner1 = self.runner_type(self.task['src1'], self.path) r = runner1.compile() if not r: IO.writestr('src1 Compile Error') return runner2 = self.runner_type(self.task['src2'], self.path) r = runner2.compile() if not r: IO.writestr('src2 Compile Error') return for test in self.testcaseSet: outstr = 'Test Case #<{name}>: '.format(name=test.name) r = runner1.run(test, 'out1.txt') if not r: outstr += "{res} \nComment: {comment}".format( res='Runtime Error', comment='src1 Runtime Error') r = runner2.run(test, 'out2.txt') if not r: outstr += "{res} \nComment: {comment}".format( res='Runtime Error', comment='src2 Runtime Error') Checker.timetrim(self.path + '/out/out1.txt', self.path + '/out/out1_t.txt') Checker.timetrim(self.path + '/out/out2.txt', self.path + '/out/out2_t.txt') res = Checker.check(self.path + '/out/out1_t.txt', self.path + '/out/out2_t.txt') if res == None: outstr += 'Checker Error' else: outstr += "{res} \nComment: {comment}".format(res=res[0], comment=res[1]) IO.writestr(outstr)
def stdmode(self): IO.writestr('Judger Identifier: {id}'.format( id=Config.getValue('configs/global.json', 'identifier'))) IO.writestr('Simulation Tool: {sim}'.format(sim=self.task['tool'])) IO.writestr(' - Standard Mode - ') runner = self.runner_type(self.task['src'], self.path) r = runner.compile() if not r: IO.writestr('Compile Error') return for test in self.testcaseSet: if not test.display: IO.writestr( '# Test Case #<{name}>: Omitted\nComment: Standard Answer not ready.' .format(name=test.name)) continue outstr = 'Test Case #<{name}>: '.format(name=test.name) r = runner.run(test, 'out.txt') if not r: outstr += "{res} \nComment: {comment}".format( res='Runtime Error', comment='Runtime Error') IO.writestr(outstr) break Checker.timetrim(test.path + '/' + test.display, self.path + '/out/std_t.txt') Checker.timetrim(self.path + '/out/out.txt', self.path + '/out/out_t.txt') # res = Checker.check(test.path + '/' + test.display, self.path + '/out/out.txt') res = Checker.check(self.path + '/out/std_t.txt', self.path + '/out/out_t.txt') outstr = 'Test Case #<{name}>: '.format(name=test.name) if res == None: outstr += 'Checker Error' else: outstr += "{res} \nComment: {comment}".format(res=res[0], comment=res[1]) IO.writestr(outstr)