DRY file loading
This commit is contained in:
parent
ac0637c413
commit
6ff956394a
|
@ -3,9 +3,7 @@ package tests
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -87,9 +85,9 @@ type btTransaction struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunBlockTest(filepath string) error {
|
func RunBlockTest(filepath string) error {
|
||||||
bt, err := LoadBlockTests(filepath)
|
bt, err := loadBlockTests(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// map skipped tests to boolean set
|
// map skipped tests to boolean set
|
||||||
|
@ -158,22 +156,6 @@ func testEthConfig() *eth.Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadBlockTests loads a block test JSON file.
|
|
||||||
func LoadBlockTests(file string) (map[string]*BlockTest, error) {
|
|
||||||
bt := make(map[string]*btJSON)
|
|
||||||
if err := LoadJSON(file, &bt); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
out := make(map[string]*BlockTest)
|
|
||||||
for name, in := range bt {
|
|
||||||
var err error
|
|
||||||
if out[name], err = convertTest(in); err != nil {
|
|
||||||
return out, fmt.Errorf("bad test %q: %v", name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// InsertPreState populates the given database with the genesis
|
// InsertPreState populates the given database with the genesis
|
||||||
// accounts defined by the test.
|
// accounts defined by the test.
|
||||||
func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, error) {
|
func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, error) {
|
||||||
|
@ -467,34 +449,20 @@ func mustConvertUint(in string, base int) uint64 {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadJSON reads the given file and unmarshals its content.
|
func loadBlockTests(file string) (map[string]*BlockTest, error) {
|
||||||
func LoadJSON(file string, val interface{}) error {
|
bt := make(map[string]*btJSON)
|
||||||
content, err := ioutil.ReadFile(file)
|
if err := readTestFile(file, &bt); err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(content, val); err != nil {
|
|
||||||
if syntaxerr, ok := err.(*json.SyntaxError); ok {
|
|
||||||
line := findLine(content, syntaxerr.Offset)
|
|
||||||
return fmt.Errorf("JSON syntax error at %v:%v: %v", file, line, err)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("JSON unmarshal error in %v: %v", file, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// findLine returns the line number for the given offset into data.
|
out := make(map[string]*BlockTest)
|
||||||
func findLine(data []byte, offset int64) (line int) {
|
for name, in := range bt {
|
||||||
line = 1
|
var err error
|
||||||
for i, r := range string(data) {
|
if out[name], err = convertTest(in); err != nil {
|
||||||
if int64(i) >= offset {
|
return out, fmt.Errorf("bad test %q: %v", name, err)
|
||||||
return
|
|
||||||
}
|
|
||||||
if r == '\n' {
|
|
||||||
line++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nothing to see here, please move along...
|
// Nothing to see here, please move along...
|
||||||
|
|
|
@ -2,6 +2,7 @@ package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -24,14 +25,35 @@ var (
|
||||||
|
|
||||||
func readJSON(reader io.Reader, value interface{}) error {
|
func readJSON(reader io.Reader, value interface{}) error {
|
||||||
data, err := ioutil.ReadAll(reader)
|
data, err := ioutil.ReadAll(reader)
|
||||||
err = json.Unmarshal(data, &value)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("Error reading JSON file", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(data, &value); err != nil {
|
||||||
|
if syntaxerr, ok := err.(*json.SyntaxError); ok {
|
||||||
|
line := findLine(data, syntaxerr.Offset)
|
||||||
|
return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("JSON unmarshal error: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateHttpTests(uri string, value interface{}) error {
|
// findLine returns the line number for the given offset into data.
|
||||||
|
func findLine(data []byte, offset int64) (line int) {
|
||||||
|
line = 1
|
||||||
|
for i, r := range string(data) {
|
||||||
|
if int64(i) >= offset {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r == '\n' {
|
||||||
|
line++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func readHttpFile(uri string, value interface{}) error {
|
||||||
resp, err := http.Get(uri)
|
resp, err := http.Get(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -45,7 +67,7 @@ func CreateHttpTests(uri string, value interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFileTests(fn string, value interface{}) error {
|
func readTestFile(fn string, value interface{}) error {
|
||||||
file, err := os.Open(fn)
|
file, err := os.Open(fn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -54,7 +76,7 @@ func CreateFileTests(fn string, value interface{}) error {
|
||||||
|
|
||||||
err = readJSON(file, value)
|
err = readJSON(file, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("%s in file %s", err.Error(), fn)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func RunStateTest(p string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := make(map[string]VmTest)
|
tests := make(map[string]VmTest)
|
||||||
CreateFileTests(p, &tests)
|
readTestFile(p, &tests)
|
||||||
|
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
if skipTest[name] {
|
if skipTest[name] {
|
||||||
|
@ -61,16 +61,10 @@ func RunStateTest(p string) error {
|
||||||
ret, logs, _, _ = RunState(statedb, env, test.Transaction)
|
ret, logs, _, _ = RunState(statedb, env, test.Transaction)
|
||||||
|
|
||||||
// // Compare expected and actual return
|
// // Compare expected and actual return
|
||||||
// switch name {
|
|
||||||
// // the memory required for these tests (4294967297 bytes) would take too much time.
|
|
||||||
// // on 19 May 2015 decided to skip these tests their output.
|
|
||||||
// case "mload32bitBound_return", "mload32bitBound_return2":
|
|
||||||
// default:
|
|
||||||
rexp := common.FromHex(test.Out)
|
rexp := common.FromHex(test.Out)
|
||||||
if bytes.Compare(rexp, ret) != 0 {
|
if bytes.Compare(rexp, ret) != 0 {
|
||||||
return fmt.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
|
return fmt.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
|
||||||
}
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// check post state
|
// check post state
|
||||||
for addr, account := range test.Post {
|
for addr, account := range test.Post {
|
||||||
|
|
|
@ -37,7 +37,7 @@ func RunTransactionTests(file string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
bt := make(map[string]TransactionTest)
|
bt := make(map[string]TransactionTest)
|
||||||
if err := LoadJSON(file, &bt); err != nil {
|
if err := readTestFile(file, &bt); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ func RunVmTest(p string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := make(map[string]VmTest)
|
tests := make(map[string]VmTest)
|
||||||
err := CreateFileTests(p, &tests)
|
err := readTestFile(p, &tests)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue