summaryrefslogtreecommitdiff
path: root/libgo/go/exp/eval/eval_test.go
blob: ff28cf1a9084a3ccfe3c9f4b1a12d8dc6d9b0e29 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package eval

import (
	"big"
	"flag"
	"fmt"
	"go/token"
	"log"
	"os"
	"reflect"
	"regexp"
	"testing"
)

// All tests are done using the same file set.
var fset = token.NewFileSet()

// Print each statement or expression before parsing it
var noisy = false

func init() { flag.BoolVar(&noisy, "noisy", false, "chatter during eval tests") }

/*
 * Generic statement/expression test framework
 */

type test []job

type job struct {
	code  string
	cerr  string
	rterr string
	val   Value
	noval bool
}

func runTests(t *testing.T, baseName string, tests []test) {
	for i, test := range tests {
		name := fmt.Sprintf("%s[%d]", baseName, i)
		test.run(t, name)
	}
}

func (a test) run(t *testing.T, name string) {
	w := newTestWorld()
	for _, j := range a {
		src := j.code + ";" // trailing semicolon to finish statement
		if noisy {
			println("code:", src)
		}

		code, err := w.Compile(fset, src)
		if err != nil {
			if j.cerr == "" {
				t.Errorf("%s: Compile %s: %v", name, src, err)
				break
			}
			if !match(t, err, j.cerr) {
				t.Errorf("%s: Compile %s = error %s; want %v", name, src, err, j.cerr)
				break
			}
			continue
		}
		if j.cerr != "" {
			t.Errorf("%s: Compile %s succeeded; want %s", name, src, j.cerr)
			break
		}

		val, err := code.Run()
		if err != nil {
			if j.rterr == "" {
				t.Errorf("%s: Run %s: %v", name, src, err)
				break
			}
			if !match(t, err, j.rterr) {
				t.Errorf("%s: Run %s = error %s; want %v", name, src, err, j.rterr)
				break
			}
			continue
		}
		if j.rterr != "" {
			t.Errorf("%s: Run %s succeeded; want %s", name, src, j.rterr)
			break
		}

		if !j.noval && !reflect.DeepEqual(val, j.val) {
			t.Errorf("%s: Run %s = %T(%v) want %T(%v)", name, src, val, val, j.val, j.val)
		}
	}
}

func match(t *testing.T, err os.Error, pat string) bool {
	ok, err1 := regexp.MatchString(pat, err.String())
	if err1 != nil {
		t.Fatalf("compile regexp %s: %v", pat, err1)
	}
	return ok
}


/*
 * Test constructors
 */

// Expression compile error
func CErr(expr string, cerr string) test { return test([]job{{code: expr, cerr: cerr}}) }

// Expression runtime error
func RErr(expr string, rterr string) test { return test([]job{{code: expr, rterr: rterr}}) }

// Expression value
func Val(expr string, val interface{}) test {
	return test([]job{{code: expr, val: toValue(val)}})
}

// Statement runs without error
func Run(stmts string) test { return test([]job{{code: stmts, noval: true}}) }

// Two statements without error.
// TODO(rsc): Should be possible with Run but the parser
// won't let us do both top-level and non-top-level statements.
func Run2(stmt1, stmt2 string) test {
	return test([]job{{code: stmt1, noval: true}, {code: stmt2, noval: true}})
}

// Statement runs and test one expression's value
func Val1(stmts string, expr1 string, val1 interface{}) test {
	return test([]job{
		{code: stmts, noval: true},
		{code: expr1, val: toValue(val1)},
	})
}

// Statement runs and test two expressions' values
func Val2(stmts string, expr1 string, val1 interface{}, expr2 string, val2 interface{}) test {
	return test([]job{
		{code: stmts, noval: true},
		{code: expr1, val: toValue(val1)},
		{code: expr2, val: toValue(val2)},
	})
}

/*
 * Value constructors
 */

type vstruct []interface{}

type varray []interface{}

type vslice struct {
	arr      varray
	len, cap int
}

func toValue(val interface{}) Value {
	switch val := val.(type) {
	case bool:
		r := boolV(val)
		return &r
	case uint8:
		r := uint8V(val)
		return &r
	case uint:
		r := uintV(val)
		return &r
	case int:
		r := intV(val)
		return &r
	case *big.Int:
		return &idealIntV{val}
	case float64:
		r := float64V(val)
		return &r
	case *big.Rat:
		return &idealFloatV{val}
	case string:
		r := stringV(val)
		return &r
	case vstruct:
		elems := make([]Value, len(val))
		for i, e := range val {
			elems[i] = toValue(e)
		}
		r := structV(elems)
		return &r
	case varray:
		elems := make([]Value, len(val))
		for i, e := range val {
			elems[i] = toValue(e)
		}
		r := arrayV(elems)
		return &r
	case vslice:
		return &sliceV{Slice{toValue(val.arr).(ArrayValue), int64(val.len), int64(val.cap)}}
	case Func:
		return &funcV{val}
	}
	log.Panicf("toValue(%T) not implemented", val)
	panic("unreachable")
}

/*
 * Default test scope
 */

type testFunc struct{}

func (*testFunc) NewFrame() *Frame { return &Frame{nil, make([]Value, 2)} }

func (*testFunc) Call(t *Thread) {
	n := t.f.Vars[0].(IntValue).Get(t)

	res := n + 1

	t.f.Vars[1].(IntValue).Set(t, res)
}

type oneTwoFunc struct{}

func (*oneTwoFunc) NewFrame() *Frame { return &Frame{nil, make([]Value, 2)} }

func (*oneTwoFunc) Call(t *Thread) {
	t.f.Vars[0].(IntValue).Set(t, 1)
	t.f.Vars[1].(IntValue).Set(t, 2)
}

type voidFunc struct{}

func (*voidFunc) NewFrame() *Frame { return &Frame{nil, []Value{}} }

func (*voidFunc) Call(t *Thread) {}

func newTestWorld() *World {
	w := NewWorld()

	def := func(name string, t Type, val interface{}) { w.DefineVar(name, t, toValue(val)) }

	w.DefineConst("c", IdealIntType, toValue(big.NewInt(1)))
	def("i", IntType, 1)
	def("i2", IntType, 2)
	def("u", UintType, uint(1))
	def("f", Float64Type, 1.0)
	def("s", StringType, "abc")
	def("t", NewStructType([]StructField{{"a", IntType, false}}), vstruct{1})
	def("ai", NewArrayType(2, IntType), varray{1, 2})
	def("aai", NewArrayType(2, NewArrayType(2, IntType)), varray{varray{1, 2}, varray{3, 4}})
	def("aai2", NewArrayType(2, NewArrayType(2, IntType)), varray{varray{5, 6}, varray{7, 8}})
	def("fn", NewFuncType([]Type{IntType}, false, []Type{IntType}), &testFunc{})
	def("oneTwo", NewFuncType([]Type{}, false, []Type{IntType, IntType}), &oneTwoFunc{})
	def("void", NewFuncType([]Type{}, false, []Type{}), &voidFunc{})
	def("sli", NewSliceType(IntType), vslice{varray{1, 2, 3}, 2, 3})

	return w
}