Skip to content

Commit 5faf26e

Browse files
committed
Add test cases to validate the correctness of logical-or operations
This commit adds more test cases to validate the correctness of logical-or operations after fixing the potential issues in the previous implementation.
1 parent 16fc3ba commit 5faf26e

File tree

1 file changed

+257
-0
lines changed

1 file changed

+257
-0
lines changed

tests/driver.sh

+257
Original file line numberDiff line numberDiff line change
@@ -1272,4 +1272,261 @@ int main()
12721272
return b1 + b2 + b3 + b4;
12731273
}
12741274
EOF
1275+
1276+
# Logical-or: simplest case
1277+
expr 1 "41 || 20"
1278+
1279+
# Logical-or: control flow
1280+
1281+
ans="0 20
1282+
0"
1283+
1284+
try_output 0 "$ans" << EOF
1285+
int main()
1286+
{
1287+
int a = 0;
1288+
int b = 20;
1289+
1290+
if (a || b)
1291+
printf("%d %d\n", a, b);
1292+
1293+
b = 0;
1294+
1295+
if (a || b)
1296+
printf("%d %d\n", a, b);
1297+
else
1298+
printf("0\n");
1299+
1300+
return 0;
1301+
}
1302+
EOF
1303+
1304+
# Logical-or: for loop
1305+
ans="a--
1306+
a--
1307+
b--
1308+
b--
1309+
b--
1310+
b--
1311+
b--
1312+
b--
1313+
b--
1314+
b--
1315+
0 0 45"
1316+
1317+
try_output 0 "$ans" << EOF
1318+
int main()
1319+
{
1320+
int a = 2, b = 8, c = 0;
1321+
for (int i = 0; a || b; i++) {
1322+
if (a) {
1323+
c += i;
1324+
a--;
1325+
printf("a--\n");
1326+
continue;
1327+
}
1328+
if (b) {
1329+
c += i;
1330+
b--;
1331+
printf("b--\n");
1332+
continue;
1333+
}
1334+
}
1335+
printf("%d %d %d\n", a, b, c);
1336+
1337+
return 0;
1338+
}
1339+
EOF
1340+
1341+
# Logical-or: while loop
1342+
ans="a -= 2
1343+
a -= 2
1344+
b -= 3
1345+
b -= 3
1346+
b -= 3
1347+
-1 0 13"
1348+
1349+
try_output 0 "$ans" << EOF
1350+
int main()
1351+
{
1352+
int a = 3, b = 9, c = 0;
1353+
while (a > 0 || b > 0) {
1354+
if (a > 0) {
1355+
c += 2;
1356+
a -= 2;
1357+
printf("a -= 2\n");
1358+
continue;
1359+
}
1360+
if (b > 0) {
1361+
c += 3;
1362+
b -= 3;
1363+
printf("b -= 3\n");
1364+
continue;
1365+
}
1366+
}
1367+
printf("%d %d %d\n", a, b, c);
1368+
1369+
return 0;
1370+
}
1371+
EOF
1372+
1373+
# Logical-or: do-while loop
1374+
ans="do: a -= 2
1375+
do: a -= 2
1376+
do: a -= 2
1377+
do: b -= 5
1378+
do: b -= 5
1379+
do: b -= 5
1380+
do: b -= 5
1381+
-1 -4 -26"
1382+
1383+
try_output 0 "$ans" << EOF
1384+
int main()
1385+
{
1386+
int a = 5, b = 16, c = 0;
1387+
do {
1388+
printf("do: ");
1389+
if (a > 0) {
1390+
c -= 2;
1391+
a -= 2;
1392+
printf("a -= 2\n");
1393+
} else if (b > 0) {
1394+
c -= 5;
1395+
b -= 5;
1396+
printf("b -= 5\n");
1397+
}
1398+
} while (a > 0 || b > 0);
1399+
printf("%d %d %d\n", a, b, c);
1400+
1401+
return 0;
1402+
}
1403+
EOF
1404+
1405+
# Logical-or: test the short-circuit principle
1406+
ans="10 > 0
1407+
10 0
1408+
20 > 0
1409+
0 20
1410+
get 0"
1411+
1412+
try_output 0 "$ans" << EOF
1413+
int func(int x)
1414+
{
1415+
if (x > 0)
1416+
printf("%d > 0\n", x);
1417+
return x;
1418+
}
1419+
1420+
int main()
1421+
{
1422+
int a = 10, b = 0, c = 20, d = -100;
1423+
if (func(a) || func(b))
1424+
printf("%d %d\n", a, b);
1425+
1426+
if (func(b) || func(c))
1427+
printf("%d %d\n", b, c);
1428+
1429+
if (func(d + 100) || func(b))
1430+
printf("%d %d\n", b, c);
1431+
else
1432+
printf("get 0\n");
1433+
1434+
1435+
return 0;
1436+
}
1437+
EOF
1438+
1439+
# Logical-or and logical-and: More complex use cases
1440+
ans="0
1441+
1
1442+
1
1443+
1
1444+
1
1445+
1
1446+
1
1447+
1
1448+
0
1449+
1
1450+
1
1451+
1
1452+
1
1453+
1
1454+
0
1455+
1
1456+
0
1457+
func(10): 10 > 0
1458+
func(20): 20 > 0
1459+
func(0): 0 <= 0
1460+
0
1461+
func(10): 10 > 0
1462+
0
1463+
func(10): 10 > 0
1464+
0
1465+
func(0): 0 <= 0
1466+
func(10): 10 > 0
1467+
func(-100): -100 <= 0
1468+
1
1469+
func(0): 0 <= 0
1470+
func(0): 0 <= 0
1471+
0
1472+
func(0): 0 <= 0
1473+
func(0): 0 <= 0
1474+
0
1475+
func(0): 0 <= 0
1476+
func(10): 10 > 0
1477+
func(-100): -100 <= 0
1478+
1
1479+
func(10): 10 > 0
1480+
func(-100): -100 <= 0
1481+
1
1482+
func(10): 10 > 0
1483+
func(-100): -100 <= 0
1484+
1"
1485+
1486+
try_output 0 "$ans" << EOF
1487+
int func(int x)
1488+
{
1489+
if (x > 0)
1490+
printf("func(%d): %d > 0\n", x, x);
1491+
else
1492+
printf("func(%d): %d <= 0\n", x, x);
1493+
return x;
1494+
}
1495+
1496+
int main()
1497+
{
1498+
int a = 10, b = 20, c = 0, d = -100;
1499+
printf("%d\n", a && b && c && d);
1500+
printf("%d\n", a || b && c && d);
1501+
printf("%d\n", a && b || c && d);
1502+
printf("%d\n", a && b && c || d);
1503+
printf("%d\n", a || b || c && d);
1504+
printf("%d\n", a || b && c || d);
1505+
printf("%d\n", a && b || c || d);
1506+
printf("%d\n", a || b || c || d);
1507+
1508+
printf("%d\n", (a || b) && c && d);
1509+
printf("%d\n", a && (b || c) && d);
1510+
printf("%d\n", a && b && (c || d));
1511+
printf("%d\n", (a || b || c) && d);
1512+
printf("%d\n", (a || b) && (c || d));
1513+
printf("%d\n", a && (b || c || d));
1514+
printf("%d\n", a * 0 && (b || c || d));
1515+
printf("%d\n", a * 2 && (b || c || d));
1516+
printf("%d\n", a && (b * 0 || c || d * 0));
1517+
1518+
printf("%d\n", func(a) && func(b) && func(c));
1519+
printf("%d\n", func(a) - a && func(b) && func(c));
1520+
printf("%d\n", func(a) - a && func(b) && func(c) + 1);
1521+
printf("%d\n", func(c) || func(a) && func(d));
1522+
printf("%d\n", func(c) || func(c) && func(d));
1523+
printf("%d\n", (func(c) || func(c)) && func(d + 100));
1524+
printf("%d\n", func(c) || func(a) && func(d));
1525+
printf("%d\n", func(a) && (func(d) || func(c)));
1526+
printf("%d\n", func(a) * 2 && (func(d) || func(c)));
1527+
1528+
return 0;
1529+
}
1530+
EOF
1531+
12751532
echo OK

0 commit comments

Comments
 (0)