summaryrefslogtreecommitdiffstats
path: root/tests/jq.test
diff options
context:
space:
mode:
authoritchyny <itchyny@cybozu.co.jp>2023-07-31 12:48:14 +0900
committerNico Williams <nico@cryptonector.com>2023-07-31 15:39:13 -0500
commit0f80921268edcc3d502e6d84612ba71fed6b0947 (patch)
tree40da8d84c1235bd94b5018a3fd015a60a7cda44f /tests/jq.test
parent6716e23ae6d534db0d3f2af2d1610f17444fb5a9 (diff)
Fix constant folding of division and reminder with zero divisor
Previously constant folding of zero division (e.x. 1/0) produces a compile error. This was incorrectly implemented by checking if the division result is infinite, so produces wrong results compared to the query where no constant folding is processed (e.x. 1e308/0.1). This patch delays the operation when the divisor is zero. This makes the results more consistent, but changes the exit code on zero division from 3 to 5. Also 0/0 now produces the zero division error, not NaN. This patch also fixes the modulo operation. Previously constant folding logic does not take care of the % operator, but now it folds if the both dividend and divisor are safe numbers to cast to the integer type, and the divisor is not zero. This patch also fixes some code that relies on undefined cast behaviors in C. The modulo operation produces NaN if either the dividend or divisor is NaN.
Diffstat (limited to 'tests/jq.test')
-rw-r--r--tests/jq.test23
1 files changed, 15 insertions, 8 deletions
diff --git a/tests/jq.test b/tests/jq.test
index 55630a5a..73cf93d0 100644
--- a/tests/jq.test
+++ b/tests/jq.test
@@ -556,9 +556,13 @@ null
null
172
-[(infinite, -infinite) % (1, -1)]
+[(infinite, -infinite) % (1, -1, infinite)]
null
-[0,0,0,0]
+[0,0,0,0,0,-1]
+
+[nan % 1, 1 % nan | isnan]
+null
+[true,true]
1 + tonumber + ("10" | tonumber)
4
@@ -1664,18 +1668,21 @@ try (1/.) catch .
0
"number (1) and number (0) cannot be divided because the divisor is zero"
-0/0, (0 as $x | $x/0) | isnan
+try (1/0) catch .
0
-true
-true
+"number (1) and number (0) cannot be divided because the divisor is zero"
+
+try (0/0) catch .
+0
+"number (0) and number (0) cannot be divided because the divisor is zero"
try (1%.) catch .
0
"number (1) and number (0) cannot be divided (remainder) because the divisor is zero"
-%%FAIL
-1/0
-jq: error: Division by zero? at <top-level>, line 1:
+try (1%0) catch .
+0
+"number (1) and number (0) cannot be divided (remainder) because the divisor is zero"
# Basic numbers tests: integers, powers of two
[range(-52;52;1)] as $powers | [$powers[]|pow(2;.)|log2|round] == $powers