Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test.py for Python3 #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions assignments/1_io_library/stud/test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
#!/usr/bin/python

from __future__ import print_function, division
try:
bytes('foo', 'utf-8')
except:
u8_bytes = lambda a: a
u8_str = lambda a: a
else:
u8_bytes = lambda a: bytes(a, 'utf-8')
u8_str = lambda a: str(a, 'utf-8')

import os
import subprocess
import sys
import re
import sys
from subprocess import CalledProcessError, Popen, PIPE
from termcolor import colored

try:
from termcolor import colored
except ImportError:
from termcolor_py2 import colored

#-------helpers---------------

Expand Down Expand Up @@ -45,23 +59,23 @@ def compile( fname, text ):
f.close()

if subprocess.call( ['nasm', '-f', 'elf64', fname + '.asm', '-o', fname+'.o'] ) == 0 and subprocess.call( ['ld', '-o' , fname, fname+'.o'] ) == 0:
print ' ', fname, ': compiled'
print(' ', fname, ': compiled')
return True
else:
print ' ', fname, ': failed to compile'
print(' ', fname, ': failed to compile')
return False


def launch( fname, seed = '' ):
output = ''
try:
p = Popen(['./'+fname], shell=None, stdin=PIPE, stdout=PIPE)
(output, err) = p.communicate(input=seed)
return (output, p.returncode)
(output, err) = p.communicate(input=u8_bytes(seed))
return (u8_str(output), p.returncode)
except CalledProcessError as exc:
return (exc.output, exc.returncode)
else:
return (output, 0)
return (u8_str(output), 0)



Expand All @@ -88,7 +102,7 @@ def perform(self, arg):
if res is None:
return False
(output, code) = res
print '"', arg,'" ->', res
print('"', arg,'" ->', res)
return self.checker( arg, output, code )

before_call="""
Expand Down Expand Up @@ -276,7 +290,7 @@ def perform(self, arg):
mov rax, 60
xor rdi, rdi
syscall""",
lambda i, o, r: first_or_empty(i) == o),
lambda i, o, r: i == o),

Test('read_word_length',
lambda v:"""
Expand All @@ -295,7 +309,7 @@ def perform(self, arg):
mov rax, 60
mov rdi, rdx
syscall""",
lambda i, o, r: len(first_or_empty(i)) == r or len(first_or_empty(i)) > 19),
lambda i, o, r: len(i) == r or len(i) > 19),

Test('read_word_too_long',
lambda v:"""
Expand All @@ -314,7 +328,7 @@ def perform(self, arg):
mov rdi, rax
mov rax, 60
syscall""",
lambda i, o, r: ( (not len(first_or_empty(i)) > 19) and r != 0 ) or r == 0 ),
lambda i, o, r: ( (not len(i) > 19) and r != 0 ) or r == 0 ),

Test('parse_uint',
lambda v: """section .data
Expand Down Expand Up @@ -396,15 +410,15 @@ def perform(self, arg):
err_too_long_msg: db "string is too long", 10, 0
section .data
arg1: db '""" + v + """', 0
arg2: times """ + str(len(v)/2) + """ db 66
arg2: times """ + str(len(v)//2) + """ db 66
section .text
%include "lib.inc"
global _start
_start:
""" + before_call + """
mov rdi, arg1
mov rsi, arg2
mov rdx, """ + str(len(v)/2 ) + """
mov rdx, """ + str(len(v)//2) + """
call string_copy
test rax, rax
jnz .good
Expand Down Expand Up @@ -440,7 +454,7 @@ def perform(self, arg):
'read_char'
: ['-1', '-1234asdasd5234121', '', ' ', '\t ', 'hey ya ye ya', 'hello world' ],
'read_word'
: ['-1'], # , '-1234asdasd5234121', '', ' ', '\t ', 'hey ya ye ya', 'hello world' ],
: ['-1', '-1234asdasd5234121', '', ' ', '\t ', 'hey ya ye ya', 'hello world' ],
'read_word_length'
: ['-1', '-1234asdasd5234121', '', ' ', '\t ', 'hey ya ye ya', 'hello world' ],
'read_word_too_long'
Expand All @@ -461,17 +475,17 @@ def perform(self, arg):
for arg in inputs[t.name]:
if not found_error:
try:
print ' testing', t.name,'on "'+ arg +'"'
print(' testing', t.name,'on "'+ arg +'"')
res = t.perform(arg)
if res:
print ' [', colored(' ok ', 'green'), ']'
print(' [', colored(' ok ', 'green'), ']')
else:
print '* [ ', colored('fail', 'red'), ']'
print('* [ ', colored('fail', 'red'), ']')
found_error = True
except:
print '* [ ', colored('fail', 'red'), '] with exception' , sys.exc_info()[0]
except KeyError:
print('* [ ', colored('fail', 'red'), '] with exception' , sys.exc_info()[0])
found_error = True
if found_error:
print 'Not all tests have been passed'
print('Not all tests have been passed')
else:
print colored( "Good work, all tests are passed", 'green')
print(colored( "Good work, all tests are passed", 'green'))