@@ -185,10 +185,11 @@ func Check2[U, V any](forall Seq2[U, V]) Seq2[U, V] {
185
185
return func (body func (U , V ) bool ) {
186
186
state := READY
187
187
forall (func (u U , v V ) bool {
188
- if state != READY {
189
- panic (fail [state ])
190
- }
188
+ tmp := state
191
189
state = PANIC
190
+ if tmp != READY {
191
+ panic (fail [tmp ])
192
+ }
192
193
ret := body (u , v )
193
194
if ret {
194
195
state = READY
@@ -208,10 +209,11 @@ func Check[U any](forall Seq[U]) Seq[U] {
208
209
return func (body func (U ) bool ) {
209
210
state := READY
210
211
forall (func (u U ) bool {
211
- if state != READY {
212
- panic (fail [state ])
213
- }
212
+ tmp := state
214
213
state = PANIC
214
+ if tmp != READY {
215
+ panic (fail [tmp ])
216
+ }
215
217
ret := body (u )
216
218
if ret {
217
219
state = READY
@@ -1122,12 +1124,12 @@ func TestPanickyIterator1(t *testing.T) {
1122
1124
} else {
1123
1125
t .Errorf ("Saw wrong panic '%v'" , r )
1124
1126
}
1127
+ if ! slices .Equal (expect , result ) {
1128
+ t .Errorf ("Expected %v, got %v" , expect , result )
1129
+ }
1125
1130
} else {
1126
1131
t .Errorf ("Wanted to see a failure, result was %v" , result )
1127
1132
}
1128
- if ! slices .Equal (expect , result ) {
1129
- t .Errorf ("Expected %v, got %v" , expect , result )
1130
- }
1131
1133
}()
1132
1134
for _ , z := range PanickyOfSliceIndex ([]int {1 , 2 , 3 , 4 }) {
1133
1135
result = append (result , z )
@@ -1172,12 +1174,12 @@ func TestPanickyIterator2(t *testing.T) {
1172
1174
} else {
1173
1175
t .Errorf ("Saw wrong panic '%v'" , r )
1174
1176
}
1177
+ if ! slices .Equal (expect , result ) {
1178
+ t .Errorf ("Expected %v, got %v" , expect , result )
1179
+ }
1175
1180
} else {
1176
1181
t .Errorf ("Wanted to see a failure, result was %v" , result )
1177
1182
}
1178
- if ! slices .Equal (expect , result ) {
1179
- t .Errorf ("Expected %v, got %v" , expect , result )
1180
- }
1181
1183
}()
1182
1184
for _ , x := range OfSliceIndex ([]int {100 , 200 }) {
1183
1185
result = append (result , x )
@@ -1207,11 +1209,11 @@ func TestPanickyIterator2Check(t *testing.T) {
1207
1209
} else {
1208
1210
t .Errorf ("Saw wrong panic '%v'" , r )
1209
1211
}
1212
+ if ! slices .Equal (expect , result ) {
1213
+ t .Errorf ("Expected %v, got %v" , expect , result )
1214
+ }
1210
1215
} else {
1211
- t .Errorf ("Wanted to see a failure, result was %v" , result )
1212
- }
1213
- if ! slices .Equal (expect , result ) {
1214
- t .Errorf ("Expected %v, got %v" , expect , result )
1216
+ t .Errorf ("Wanted to see a panic, result was %v" , result )
1215
1217
}
1216
1218
}()
1217
1219
for _ , x := range Check2 (OfSliceIndex ([]int {100 , 200 })) {
@@ -1234,13 +1236,19 @@ func TestPanickyIterator2Check(t *testing.T) {
1234
1236
1235
1237
func TestPanickyIterator3 (t * testing.T ) {
1236
1238
var result []int
1237
- var expect = []int {100 , 10 , 1 , 2 , 200 , 10 , 1 , 2 }
1239
+ var expect = []int {100 , 10 , 1 , 2 }
1238
1240
defer func () {
1239
1241
if r := recover (); r != nil {
1240
- t .Errorf ("Unexpected panic '%v'" , r )
1241
- }
1242
- if ! slices .Equal (expect , result ) {
1243
- t .Errorf ("Expected %v, got %v" , expect , result )
1242
+ if matchError (r , RERR_MISSING ) {
1243
+ t .Logf ("Saw expected panic '%v'" , r )
1244
+ } else {
1245
+ t .Errorf ("Saw wrong panic '%v'" , r )
1246
+ }
1247
+ if ! slices .Equal (expect , result ) {
1248
+ t .Errorf ("Expected %v, got %v" , expect , result )
1249
+ }
1250
+ } else {
1251
+ t .Errorf ("Wanted to see a panic, result was %v" , result )
1244
1252
}
1245
1253
}()
1246
1254
for _ , x := range OfSliceIndex ([]int {100 , 200 }) {
@@ -1262,13 +1270,19 @@ func TestPanickyIterator3(t *testing.T) {
1262
1270
}
1263
1271
func TestPanickyIterator3Check (t * testing.T ) {
1264
1272
var result []int
1265
- var expect = []int {100 , 10 , 1 , 2 , 200 , 10 , 1 , 2 }
1273
+ var expect = []int {100 , 10 , 1 , 2 }
1266
1274
defer func () {
1267
1275
if r := recover (); r != nil {
1268
- t .Errorf ("Unexpected panic '%v'" , r )
1269
- }
1270
- if ! slices .Equal (expect , result ) {
1271
- t .Errorf ("Expected %v, got %v" , expect , result )
1276
+ if matchError (r , CERR_MISSING ) {
1277
+ t .Logf ("Saw expected panic '%v'" , r )
1278
+ } else {
1279
+ t .Errorf ("Saw wrong panic '%v'" , r )
1280
+ }
1281
+ if ! slices .Equal (expect , result ) {
1282
+ t .Errorf ("Expected %v, got %v" , expect , result )
1283
+ }
1284
+ } else {
1285
+ t .Errorf ("Wanted to see a panic, result was %v" , result )
1272
1286
}
1273
1287
}()
1274
1288
for _ , x := range Check2 (OfSliceIndex ([]int {100 , 200 })) {
@@ -1298,9 +1312,11 @@ func TestPanickyIterator4(t *testing.T) {
1298
1312
} else {
1299
1313
t .Errorf ("Saw wrong panic '%v'" , r )
1300
1314
}
1301
- }
1302
- if ! slices .Equal (expect , result ) {
1303
- t .Errorf ("Expected %v, got %v" , expect , result )
1315
+ if ! slices .Equal (expect , result ) {
1316
+ t .Errorf ("Expected %v, got %v" , expect , result )
1317
+ }
1318
+ } else {
1319
+ t .Errorf ("Wanted to see a panic, result was %v" , result )
1304
1320
}
1305
1321
}()
1306
1322
for _ , x := range SwallowPanicOfSliceIndex ([]int {1 , 2 , 3 , 4 }) {
@@ -1321,9 +1337,11 @@ func TestPanickyIterator4Check(t *testing.T) {
1321
1337
} else {
1322
1338
t .Errorf ("Saw wrong panic '%v'" , r )
1323
1339
}
1324
- }
1325
- if ! slices .Equal (expect , result ) {
1326
- t .Errorf ("Expected %v, got %v" , expect , result )
1340
+ if ! slices .Equal (expect , result ) {
1341
+ t .Errorf ("Expected %v, got %v" , expect , result )
1342
+ }
1343
+ } else {
1344
+ t .Errorf ("Wanted to see a panic, result was %v" , result )
1327
1345
}
1328
1346
}()
1329
1347
for _ , x := range Check2 (SwallowPanicOfSliceIndex ([]int {1 , 2 , 3 , 4 })) {
@@ -1409,33 +1427,76 @@ X:
1409
1427
1410
1428
// TestVeryBad1 checks the behavior of an extremely poorly behaved iterator.
1411
1429
func TestVeryBad1 (t * testing.T ) {
1412
- result := veryBad ( []int {10 , 20 , 30 , 40 , 50 }) // odd length
1413
- expect := []int { 1 , 10 }
1430
+ expect := []int {} // assignment does not happen
1431
+ var result []int
1414
1432
1415
- if ! slices .Equal (expect , result ) {
1416
- t .Errorf ("Expected %v, got %v" , expect , result )
1433
+ defer func () {
1434
+ if r := recover (); r != nil {
1435
+ expectPanic (t , r , RERR_MISSING )
1436
+ if ! slices .Equal (expect , result ) {
1437
+ t .Errorf ("(Inner) Expected %v, got %v" , expect , result )
1438
+ }
1439
+ } else {
1440
+ t .Error ("Wanted to see a failure" )
1441
+ }
1442
+ }()
1443
+
1444
+ result = veryBad ([]int {10 , 20 , 30 , 40 , 50 }) // odd length
1445
+
1446
+ }
1447
+
1448
+ func expectPanic (t * testing.T , r any , s string ) {
1449
+ if matchError (r , s ) {
1450
+ t .Logf ("Saw expected panic '%v'" , r )
1451
+ } else {
1452
+ t .Errorf ("Saw wrong panic '%v'" , r )
1453
+ }
1454
+ }
1455
+
1456
+ func expectError (t * testing.T , err any , s string ) {
1457
+ if matchError (err , s ) {
1458
+ t .Logf ("Saw expected error '%v'" , err )
1459
+ } else {
1460
+ t .Errorf ("Saw wrong error '%v'" , err )
1417
1461
}
1418
1462
}
1419
1463
1420
1464
// TestVeryBad2 checks the behavior of an extremely poorly behaved iterator.
1421
1465
func TestVeryBad2 (t * testing.T ) {
1422
- result := veryBad ([]int {10 , 20 , 30 , 40 }) // even length
1423
- expect := []int {1 , 10 }
1466
+ result := []int {}
1467
+ expect := []int {}
1468
+
1469
+ defer func () {
1470
+ if r := recover (); r != nil {
1471
+ expectPanic (t , r , RERR_MISSING )
1472
+ if ! slices .Equal (expect , result ) {
1473
+ t .Errorf ("(Inner) Expected %v, got %v" , expect , result )
1474
+ }
1475
+ } else {
1476
+ t .Error ("Wanted to see a failure" )
1477
+ }
1478
+ }()
1479
+
1480
+ result = veryBad ([]int {10 , 20 , 30 , 40 }) // even length
1424
1481
1425
- if ! slices .Equal (expect , result ) {
1426
- t .Errorf ("Expected %v, got %v" , expect , result )
1427
- }
1428
1482
}
1429
1483
1430
1484
// TestVeryBadCheck checks the behavior of an extremely poorly behaved iterator,
1431
1485
// which also suppresses the exceptions from "Check"
1432
1486
func TestVeryBadCheck (t * testing.T ) {
1433
- result := veryBadCheck ([]int {10 , 20 , 30 , 40 }) // even length
1434
- expect := []int {1 , 10 }
1487
+ expect := []int {}
1488
+ var result []int
1489
+ defer func () {
1490
+ if r := recover (); r != nil {
1491
+ expectPanic (t , r , CERR_MISSING )
1492
+ }
1493
+ if ! slices .Equal (expect , result ) {
1494
+ t .Errorf ("Expected %v, got %v" , expect , result )
1495
+ }
1496
+ }()
1497
+
1498
+ result = veryBadCheck ([]int {10 , 20 , 30 , 40 }) // even length
1435
1499
1436
- if ! slices .Equal (expect , result ) {
1437
- t .Errorf ("Expected %v, got %v" , expect , result )
1438
- }
1439
1500
}
1440
1501
1441
1502
// TestOk is the nice version of the very bad iterator.
0 commit comments