MCPcopy Create free account
hub / github.com/chain/txvm / compareStacks

Function compareStacks

protocol/txvm/simple_ops_test.go:1261–1305  ·  view source on GitHub ↗

a is the stack returned by the test, b is the expected stack.

(t *testing.T, a, b stack)

Source from the content-addressed store, hash-verified

1259
1260// a is the stack returned by the test, b is the expected stack.
1261func compareStacks(t *testing.T, a, b stack) {
1262 if a.Len() != b.Len() {
1263 t.Fatalf("Stack length mismatch. Got %v, wanted %v", a, b)
1264 }
1265
1266 for i, aItem := range a {
1267 switch aa := aItem.(type) {
1268 case Int:
1269 bb, ok := b[i].(Int)
1270 checkTypeOk(t, ok, i, a[i], b[i])
1271 if aa != bb {
1272 t.Fatalf("Stack mismatch of Ints at location %d. Got %v, wanted %v", i, a[i], b[i])
1273 }
1274 case Bytes:
1275 bb, ok := b[i].(Bytes)
1276 checkTypeOk(t, ok, i, a[i], b[i])
1277 for j := 0; j < len(aa); j++ {
1278 if aa[j] != bb[j] {
1279 t.Fatalf("Stack mismatch of Bytes at location %d. Got %v, wanted %v", i, a[i], b[i])
1280 }
1281 }
1282 case Tuple:
1283 bb, ok := b[i].(Tuple)
1284 checkTypeOk(t, ok, i, a[i], b[i])
1285 for j := 0; j < len(aa); j++ {
1286 compareStacks(t, stack{aa[j]}, stack{bb[j]})
1287 }
1288 case *value:
1289 bb, ok := b[i].(*value)
1290 checkTypeOk(t, ok, i, a[i], b[i])
1291 if bb.amount != aa.amount || !bytes.Equal(bb.assetID, aa.assetID) || !bytes.Equal(bb.anchor, aa.anchor) {
1292 t.Fatalf("Mismatch of fields in value. Got %v, wanted %v", aa, bb)
1293 }
1294 case *contract:
1295 bb, ok := b[i].(*contract)
1296 checkTypeOk(t, ok, i, a[i], b[i])
1297 if bb.typecode != aa.typecode || !bytes.Equal(bb.seed, aa.seed) || !bytes.Equal(bb.program, aa.program) {
1298 t.Fatalf("Mismatch of fields in contract. Got %v, wanted %v", aa, bb)
1299 }
1300 compareStacks(t, aa.stack, bb.stack)
1301 default:
1302 t.Fatalf("Stack item at location %d is of an unrecognized type. Got %v, wanted %v", i, a[i], b[i])
1303 }
1304 }
1305}
1306
1307func checkTypeOk(t *testing.T, ok bool, i int, a, b Item) {
1308 if !ok {

Callers 2

TestSimpleOpcodesFunction · 0.85
TestComplexOpcodesFunction · 0.85

Calls 3

checkTypeOkFunction · 0.85
EqualMethod · 0.80
LenMethod · 0.45

Tested by

no test coverage detected