summaryrefslogtreecommitdiff
path: root/check.py
blob: 70bef58eed0775bbae95ca3af607227c76c1d476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python

import os, sys, subprocess, difflib, json

interpreter = None
tests = []

for arg in sys.argv[1:]:
  if arg.startswith('--interpreter='):
    interpreter = arg.split('=')[1]
    print '[ using wasm interpreter at "%s" ]' % interpreter
    assert os.path.exists(interpreter), 'interpreter not found'
  else:
    tests.append(arg)

def fail(actual, expected):
  raise Exception("incorrect output, diff:\n\n%s" % (
    ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(expected.split('\n'), actual.split('\n'), fromfile='expected', tofile='actual')])
  ))

if not interpreter:
  print '[ no wasm interpreter provided, you should pass one as --interpreter=path/to/interpreter ]'

print '[ checking asm2wasm testcases... ]\n'

if len(tests) == 0:
  tests = sorted(os.listdir('test'))

for asm in tests:
  if asm.endswith('.asm.js'):
    print '..', asm
    wasm = asm.replace('.asm.js', '.wast')
    actual, err = subprocess.Popen([os.path.join('bin', 'asm2wasm'), os.path.join('test', asm)], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    assert err == '', 'bad err:' + err

    # verify output
    if not os.path.exists(os.path.join('test', wasm)):
      print actual
      raise Exception('output .wast file does not exist')
    expected = open(os.path.join('test', wasm)).read()
    if actual != expected:
      fail(actual, expected)

    # verify in wasm
    if interpreter:
      proc = subprocess.Popen([interpreter, os.path.join('test', wasm)], stderr=subprocess.PIPE)
      out, err = proc.communicate()
      if proc.returncode != 0:
        try: # to parse the error
          reported = err.split(':')[1]
          start, end = reported.split('-')
          start_line, start_col = map(int, start.split('.'))
          lines = expected.split('\n')
          print
          print '='*80
          print lines[start_line-1]
          print (' '*(start_col-1)) + '^'
          print (' '*(start_col-1)) + '|'
          print '='*80
          print err
        except Exception, e:
          raise Exception('wasm interpreter error: ' + err) # failed to pretty-print
        raise Exception('wasm interpreter error')

print '\n[ checking wasm-shell testcases... ]\n'

for t in tests:
  if t.endswith('.wast'):
    print '..', t
    t = os.path.join('test', t)
    actual, err = subprocess.Popen([os.path.join('bin', 'wasm-shell'), t], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    assert err == '', 'bad err:' + err

    expected = open(t).read()
    if actual != expected:
      fail(actual, expected)

print '\n[ checking wasm.js polyfill testcases... (need both emcc and nodejs in your path) ]\n'

for c in tests:
  if c.endswith(('.c', '.cpp')):
    print '..', c
    base = c.replace('.cpp', '').replace('.c', '')
    post = base + '.post.js'
    try:
      post = open(os.path.join('test', post)).read()
    except:
      post = None
    expected = open(os.path.join('test', base + '.txt')).read()
    emcc = os.path.join('test', base + '.emcc')
    extra = []
    if os.path.exists(emcc):
      extra = json.loads(open(emcc).read())
    if os.path.exists('a.normal.js'): os.unlink('a.normal.js')
    for opts in [[], ['-O1'], ['-O3', '--profiling']]:
      command = ['./emcc_to_wasm.js.sh', os.path.join('test', c)] + opts + extra
      subprocess.check_call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      print '....' + ' '.join(command)
      if post:
        open('a.normal.js', 'a').write(post)
        open('a.wasm.js', 'a').write(post)
      else:
        print '     (no post)'
      for which in ['normal', 'wasm']:
        print '......', which
        try:
          args = json.loads(open(os.path.join('test', base + '.args')).read())
        except:
          args = []
          print '     (no args)'
        proc = subprocess.Popen(['nodejs', 'a.' + which + '.js'] + args, stdout=subprocess.PIPE)
        out, err = proc.communicate()
        assert proc.returncode == 0
        if out.strip() != expected.strip():
          fail(out, expected)

print '\n[ success! ]'