sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | #!sqlite3 |
| 2 | # |
| 3 | # 2026-03-01 |
| 4 | # |
| 5 | # The author disclaims copyright to this source code. In place of |
| 6 | # a legal notice, here is a blessing: |
| 7 | # |
| 8 | # May you do good and not evil. |
| 9 | # May you find forgiveness for yourself and forgive others. |
| 10 | # May you share freely, never taking more than you give. |
| 11 | # |
| 12 | #*********************************************************************** |
| 13 | # |
| 14 | # Floating-point <-> text conversions |
| 15 | # |
| 16 | # FAILURES IN THIS SCRIPT ARE NOT NECESSARILY THE FAULT OF SQLITE. |
| 17 | # |
| 18 | # Some of the tests below use the system strtod() function as |
| 19 | # an oracle of truth. These tests assume that the system strtod() |
| 20 | # is always correct. That is the case for Win11, Macs, most Linux |
| 21 | # boxes and so forth. But it possible to find a machine for which |
| 22 | # is not true. (One example, is Macs from around 2005.) On such |
| 23 | # machines, some of these tests might fail. |
| 24 | # |
| 25 | # So, in other words, a failure in any of the tests below does not |
| 26 | # necessarily mean that SQLite is wrong. It might mean that the |
| 27 | # strtod() function in the standard library of the machine on which |
| 28 | # the test is running is wrong. |
| 29 | # |
| 30 | |
| 31 | # Verify that binary64 -> text -> binary64 conversions round-trip |
| 32 | # successfully for 98,256 different edge-case binary64 values. The |
| 33 | # query result is all cases that do not round-trip without change, |
| 34 | # and so the query result should be an empty set. |
| 35 | # |
| 36 | .open :memory: |
| 37 | .testcase 100 |
| 38 | .mode list |
| 39 | WITH |
| 40 | i1(i) AS (VALUES(0) UNION ALL SELECT i+1 FROM i1 WHERE i<15), |
| 41 | i2(j) AS (VALUES(0) UNION ALL SELECT j+1 FROM i2 WHERE j<0x7fe), |
| 42 | i3(k) AS (VALUES(0x0000000000000000), |
| 43 | (0x000ffffffffffff0), |
| 44 | (0x0008080808080800)), |
| 45 | fpint(n) AS (SELECT (j<<52)+i+k FROM i2, i1, i3), |
| 46 | fp(n,r) AS (SELECT n, ieee754_from_int(n) FROM fpint) |
| 47 | SELECT n, r FROM fp WHERE r<>(0.0 + (r||'')); |
| 48 | .check '' |
| 49 | |
| 50 | # Another batch of 106,444 edge cases: All postiive floating point |
| 51 | # values that have only a single bit set in the mantissa part of the |
| 52 | # number. |
| 53 | # |
| 54 | .testcase 110 |
| 55 | WITH |
| 56 | i1(i) AS (VALUES(0) UNION ALL SELECT i+1 FROM i1 WHERE i<51), |
| 57 | i2(j) AS (VALUES(0) UNION ALL SELECT j+1 FROM i2 WHERE j<0x7fe), |
| 58 | fpint(n) AS (SELECT (j<<52)+(1<<i) FROM i2, i1), |
| 59 | fp(n,r) AS (SELECT n, ieee754_from_int(n) FROM fpint) |
| 60 | SELECT n, r FROM fp WHERE r<>(0.0 + (r||'')); |
| 61 | .check '' |
| 62 | |
| 63 | # Verify that text -> binary64 conversions agree with system strtod(). |
| 64 | # for 98,256 different edge-cases. |
| 65 | # |
| 66 | .testcase 200 |
| 67 | .mode list |
| 68 | WITH |
| 69 | i1(i) AS (VALUES(0) UNION ALL SELECT i+1 FROM i1 WHERE i<15), |
| 70 | i2(j) AS (VALUES(0) UNION ALL SELECT j+1 FROM i2 WHERE j<0x7fe), |
| 71 | i3(k) AS (VALUES(0x0000000000000000), |
| 72 | (0x000ffffffffffff0), |
| 73 | (0x0008080808080800)), |
| 74 | fpint(n) AS (SELECT (j<<52)+i+k FROM i2, i1, i3), |
| 75 | fp(r) AS (SELECT ieee754_from_int(n) FROM fpint) |
| 76 | SELECT r FROM fp WHERE r<>strtod(r||''); |
| 77 | .check '' |
| 78 | |
| 79 | |
| 80 | # Another batch of 106,444 edge cases: All postiive floating point |
| 81 | # values that have only a single bit set in the mantissa part of the |
| 82 | # number. |
| 83 | # |
| 84 | .testcase 210 |
| 85 | WITH |
| 86 | i1(i) AS (VALUES(0) UNION ALL SELECT i+1 FROM i1 WHERE i<51), |
| 87 | i2(j) AS (VALUES(0) UNION ALL SELECT j+1 FROM i2 WHERE j<0x7fe), |
| 88 | fpint(n) AS (SELECT (j<<52)+(1<<i) FROM i2, i1), |
| 89 | fp(r) AS (SELECT ieee754_from_int(n) FROM fpint) |
| 90 | SELECT r FROM fp WHERE r<>strtod(r||''); |
| 91 | .check '' |
| 92 | |
| 93 | # Comparing SQLite's text-to-double conversion against strtod() |
| 94 | # for 200,000 random floating-point literals. |
| 95 | # |
| 96 | .param set $N 50_000 |
| 97 | .testcase 300 |
| 98 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 99 | VALUES(1,'1234.5789') |
| 100 | UNION ALL |
| 101 | SELECT n+1, format('%.*s.%.*se%+d', |
| 102 | (n%3)+1, |
| 103 | random()%1000, |
| 104 | abs(random()%16)+1, |
| 105 | abs(random()), |
| 106 | random()%308) |
| 107 | FROM fp WHERE n<$N |
| 108 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 109 | .check '' |
| 110 | .testcase 301 |
| 111 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 112 | VALUES(1,'1234.5789') |
| 113 | UNION ALL |
| 114 | SELECT n+1, format('%.*s.%.*s', |
| 115 | (n%3)+1, |
| 116 | random()%1000, |
| 117 | abs(random()%16)+1, |
| 118 | abs(random())) |
| 119 | FROM fp WHERE n<$N |
| 120 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 121 | .check '' |
| 122 | .testcase 302 |
| 123 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 124 | VALUES(1,'1234.5789') |
| 125 | UNION ALL |
| 126 | SELECT n+1, format('%.*s.%.*s', |
| 127 | abs(random()%7)+1, |
| 128 | random(), |
| 129 | abs(random()%10)+1, |
| 130 | abs(random())) |
| 131 | FROM fp WHERE n<$N |
| 132 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 133 | .check '' |
| 134 | .testcase 303 |
| 135 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 136 | VALUES(1,'1234.5789') |
| 137 | UNION ALL |
| 138 | SELECT n+1, format('%de%+03d', |
| 139 | random(), |
| 140 | random()%308) |
| 141 | FROM fp WHERE n<$N |
| 142 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 143 | .check '' |
| 144 | |
| 145 | # Another 500,000 comparisions between SQLite and strtod() from completely |
| 146 | # random floating point literals. |
| 147 | # |
| 148 | .testcase 310 |
| 149 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 150 | SELECT 1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 151 | UNION ALL |
| 152 | SELECT n+1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 153 | FROM fp WHERE n<$N*2 |
| 154 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 155 | .check '' |
| 156 | .testcase 311 |
| 157 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 158 | SELECT 1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 159 | UNION ALL |
| 160 | SELECT n+1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 161 | FROM fp WHERE n<$N*2 |
| 162 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 163 | .check '' |
| 164 | .testcase 312 |
| 165 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 166 | SELECT 1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 167 | UNION ALL |
| 168 | SELECT n+1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 169 | FROM fp WHERE n<$N*2 |
| 170 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 171 | .check '' |
| 172 | .testcase 313 |
| 173 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 174 | SELECT 1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 175 | UNION ALL |
| 176 | SELECT n+1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 177 | FROM fp WHERE n<$N*2 |
| 178 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 179 | .check '' |
| 180 | .testcase 314 |
| 181 | WITH RECURSIVE fp(n,x) AS MATERIALIZED ( |
| 182 | SELECT 1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 183 | UNION ALL |
| 184 | SELECT n+1, format('%+.*g',abs(random()%18), ieee754_from_int(random())) |
| 185 | FROM fp WHERE n<$N*2 |
| 186 | ) SELECT x FROM fp WHERE (x+0.0)<>strtod(x); |
| 187 | .check '' |