Skip to content

Commit

Permalink
vm: start of test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw committed May 15, 2015
1 parent 0273331 commit 1305651
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
94 changes: 94 additions & 0 deletions vm/tests/ops.py
@@ -0,0 +1,94 @@
#!/usr/bin/env python3.4

_2 = 2
_10 = 10
_11 = 11
_100 = 100
a=[0,1,2,3,4]

# Unary Ops
assert +_2 == 2
assert -_2 == -2
assert not _2 is False
assert ~_2 == -3

# Binary Ops
assert _2**_10 == 1024
assert _2*_10 == 20
assert _10//_2 == 5
assert _11//_2 == 5
assert _10/_2 == 5.0
assert _11/_2 == 5.5
assert _10 % _2 == 0
assert _11 % _2 == 1
assert _10 + _2 == 12
assert _10 - _2 == 8
assert a[1] == 1
assert a[4] == 4
assert _2 << _10 == 2048
assert _100 >> 2 == 25
assert _10 & _2 == 2
assert _100 | _2 == 102
assert _10 ^ _2 == 8

# Inplace Ops
a = _2
a **= _10
assert a == 1024
a = _2
a *= _10
assert a == 20
a = _10
a //= _2
assert a == 5
a = _11
a //= _2
assert a == 5
a = _10
a /= _2
assert a == 5.0
a = _11
a /= _2
assert a == 5.5
a = _10
a %= _2
assert a == 0
a = _11
a %= _2
assert a == 1
a = _10
a += _2
assert a == 12
a = _10
a -= _2
assert a == 8
a = _2
a <<= _10
assert a == 2048
a = _100
a >>= 2
assert a == 25
a = _10
a &= _2
assert a == 2
a = _100
a |= _2
assert a == 102
a = _10
a ^= _2
assert a == 8

# Comparison
assert _2 < _10
assert _2 <= _10
assert _2 <= _2
assert _2 == _2
assert _2 != _10
assert _10 > _2
assert _10 >= _2
assert _2 >= _2
# FIXME in
# FIXME not in
assert True is True
assert True is not False
# FIXME EXC_MATCH
62 changes: 62 additions & 0 deletions vm/vm_test.go
@@ -0,0 +1,62 @@
package vm_test

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"

_ "github.com/ncw/gpython/builtin"
"github.com/ncw/gpython/compile"
"github.com/ncw/gpython/py"
"github.com/ncw/gpython/vm"
)

const testDir = "tests"

// Run the code in str
func run(t *testing.T, prog string) {
f, err := os.Open(prog)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer f.Close()

str, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("ReadAll failed: %v", err)
}

obj, err := compile.Compile(string(str), prog, "exec", 0, true)
if err != nil {
t.Fatalf("Compile failed: %v", err)
}

code := obj.(*py.Code)
module := py.NewModule("__main__", "", nil, nil)
module.Globals["__file__"] = py.String(prog)

res, err := vm.Run(module.Globals, module.Globals, code, nil)
if err != nil {
py.TracebackDump(err)
t.Fatalf("Run failed: %v", err)
}

fmt.Printf("Return = %v\n", res)
}

func TestVm(t *testing.T) {
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Fatalf("ReadDir failed: %v", err)
}
for _, f := range files {
name := path.Join(testDir, f.Name())
if strings.HasSuffix(name, ".py") {
t.Logf("Running %q", name)
run(t, name)
}
}
}

0 comments on commit 1305651

Please sign in to comment.