This commit is contained in:
Coffepots 2018-04-05 19:00:46 +01:00
commit 078f7abf22
16 changed files with 55 additions and 50 deletions

3
.gitignore vendored
View File

@ -1,3 +1,2 @@
nimcache/ nimcache/
runner build/
*_test

View File

@ -2,7 +2,7 @@ sudo: required
services: services:
- docker - docker
before_install: before_install:
- docker pull yglukhov/nim-base - docker pull statusteam/nim-base
script: script:
- docker run yglukhov/nim-base nim --version - docker run -v "$(pwd):/project" -w /project statusteam/nim-base nim --version
- docker run -v "$(pwd):/project" -w /project yglukhov/nim-base /bin/bash -c "nimble install -y && ./tests/build_test.sh && ./tests/test.sh" - docker run -v "$(pwd):/project" -w /project statusteam/nim-base sh -c "nimble refresh && nimble test"

View File

@ -7,10 +7,20 @@ description = "An Ethereum 2.0 Sharding Client for Resource-Restricted Devices
license = "Apache License 2.0" license = "Apache License 2.0"
skipDirs = @["tests"] skipDirs = @["tests"]
requires "nim >= 0.17.0", requires "nim >= 0.18.1",
"https://github.com/status-im/nim-keccak-tiny.git >= 0.1.0", "https://github.com/status-im/nim-keccak-tiny.git >= 0.1.0",
"https://github.com/alehander42/nim-rlp.git#fix-ordinal", "https://github.com/alehander42/nim-rlp#fix-ordinal", #TODO switching to the Status repo throws: "Error: undeclared identifier: 'Range'"
"https://github.com/status-im/nim-ttmath >= 0.5.0" "https://github.com/status-im/nim-ttmath#master"
proc test(name: string, lang = "cpp") =
if not dirExists "build":
mkDir "build"
if not dirExists "nimcache":
mkDir "nimcache"
--run
--nimcache: "nimcache"
switch("out", ("./build/" & name))
setCommand lang, "tests/" & name & ".nim"
task test, "Run tests":
test "all_tests"

View File

@ -8,10 +8,18 @@ type
Value* = ref object Value* = ref object
case kind*: ValueKind: case kind*: ValueKind:
of VInt: of VInt:
i*: Int256 Fi: array[32, byte] #Int256
of VBinary: of VBinary:
b*: seq[byte] b*: seq[byte]
# TODO: The Int256 value is stored as array[32, byte], and we bitcast it
# back and forth. This is a hacky workaround for the problem that clang
# doesn't let you store ttmath types inside nim variant types (unions). Things
# should get better when we switch to mpint.
proc i*(v: Value): Int256 {.inline.} =
cast[ptr Int256](unsafeAddr v.Fi)[]
proc `$`*(value: Value): string = proc `$`*(value: Value): string =
case value.kind: case value.kind:
of VInt: of VInt:
@ -19,23 +27,16 @@ proc `$`*(value: Value): string =
of VBinary: of VBinary:
&"Binary({value.b})" &"Binary({value.b})"
proc vint*(i: int): Value = proc toArr(i: Int256): array[32, byte] {.inline.} =
Value(kind: VInt, i: i.int256) cast[ptr array[32, byte]](unsafeAddr i)[]
proc vint*(i: Int256): Value = proc vint*(i: Int256): Value =
Value(kind: VInt, i: i) Value(kind: VInt, Fi: i.toArr)
proc vint*(i: int): Value {.inline.} = vint(i.int256)
proc vbinary*(b: string): Value = proc vbinary*(b: string): Value =
Value(kind: VBinary, b: b.mapIt(it.byte)) Value(kind: VBinary, b: b.mapIt(it.byte))
proc vbinary*(b: seq[byte]): Value = proc vbinary*(b: seq[byte]): Value =
Value(kind: VBinary, b: b) Value(kind: VBinary, b: b)
proc `==`*(a: Value, b: Value): bool =
if a.kind != b.kind:
return false
case a.kind:
of VInt:
a.i == b.i
of VBinary:
a.b == b.b

View File

@ -1,7 +1,3 @@
# tests # tests
TODO: more vm tests and fixtures! TODO: more vm tests and fixtures!
```bash
./tests/test.sh
```

5
tests/all_tests.nim Normal file
View File

@ -0,0 +1,5 @@
import ./test_code_stream,
./test_gas_meter,
./test_memory,
./test_stack

View File

@ -1,6 +0,0 @@
#!/bin/bash
nim cpp tests/code_stream_test.nim
nim cpp tests/gas_meter_test.nim
nim cpp tests/memory_test.nim
nim cpp tests/stack_test.nim

View File

@ -1 +0,0 @@
-p:"../src"

View File

@ -1,7 +0,0 @@
#!/bin/bash
./tests/code_stream_test
./tests/gas_meter_test
./tests/memory_test
./tests/stack_test

View File

@ -1,4 +1,5 @@
import unittest, strutils, sequtils, opcode_values, vm / code_stream import unittest, strutils, sequtils,
../src/opcode_values, ../src/vm/code_stream
suite "parse bytecode": suite "parse bytecode":
test "accepts bytes": test "accepts bytes":

View File

@ -1,4 +1,6 @@
import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, logging, vm / gas_meter, ttmath import unittest, macros, strformat, strutils, sequtils,
ttmath,
../src/[constants, opcode_values, errors, logging, vm/gas_meter]
# TODO: quicktest # TODO: quicktest
# PS: parametrize can be easily immitated, but still quicktests would be even more useful # PS: parametrize can be easily immitated, but still quicktests would be even more useful

View File

@ -1,4 +1,6 @@
import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, vm / memory, ttmath import unittest, macros, strformat, strutils, sequtils,
ttmath,
../src/[constants, opcode_values, errors, vm/memory]
proc memory32: Memory = proc memory32: Memory =
result = newMemory() result = newMemory()

View File

@ -1,4 +1,7 @@
import unittest, macros, strformat, strutils, sequtils, constants, opcode_values, errors, vm / [stack, value], ttmath, utils / [bytes, padding], utils_numeric import unittest, macros, strformat, strutils, sequtils,
ttmath,
../src/[constants, opcode_values, errors, utils_numeric, vm/stack, vm/value, utils/bytes, utils/padding]
template testPush(value: untyped, expected: untyped): untyped = template testPush(value: untyped, expected: untyped): untyped =
var stack = newStack() var stack = newStack()