(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** Tests for {!Sql.Value_ops}: SQLite's value-level semantics -- storage-class ordering, three-valued logic, text conversion, aggregates and affinity. *) module V = Sql.Value_ops module Val = Catalog.Value let vstr : Val.t -> string = function | Null -> "NULL" | Int i -> Fmt.str "int:%Ld" i | Float f -> Fmt.str "float:%.6g" f | Text s -> "text:" ^ s | Blob b -> "blob:" ^ b let value = Alcotest.testable (Fmt.of_to_string vstr) (fun a b -> vstr a = vstr b) let sign n = compare n 0 let test_compare_storage_classes () = (* NULL < number < text < blob, numbers compared exactly across int/real. *) Alcotest.(check int) "NULL < int" (-1) (sign (V.compare_values Null (Int 1L))); Alcotest.(check int) "int < text" (-1) (sign (V.compare_values (Int 9L) (Text "a"))); Alcotest.(check int) "text < blob" (-1) (sign (V.compare_values (Text "z") (Blob "a"))); Alcotest.(check int) "int equals the same real" 0 (sign (V.compare_values (Int 5L) (Float 5.0))); Alcotest.(check int) "int ordering" (-1) (sign (V.compare_values (Int 1L) (Int 2L))); Alcotest.(check int) "3 < 3.5 across int and real" (-1) (sign (V.compare_values (Int 3L) (Float 3.5))); (* int64 max must sort BELOW the double it would round up to, not equal it *) Alcotest.(check int) "int64 max < 2^63 as a double" (-1) (sign (V.compare_values (Int Int64.max_int) (Float 9223372036854775808.0))) (* Collated comparison: BINARY by bytes, NOCASE folds ASCII case, RTRIM ignores trailing spaces; the collation is irrelevant to a non-text operand. The sequence is resolved before it gets here -- {!Sql.Private.Collate.resolve} reads the name, and refuses one nothing carries. *) let test_collation () = let seq = Sql.Private.Collate.resolve in let eq name a b = V.compare_values_coll (seq name) (Text a) (Text b) = 0 in Alcotest.(check bool) "NOCASE folds case" true (eq "NOCASE" "abc" "ABC"); Alcotest.(check bool) "BINARY keeps case" false (eq "BINARY" "abc" "ABC"); Alcotest.(check bool) "RTRIM ignores trailing spaces" true (eq "RTRIM" "a " "a"); Alcotest.(check bool) "RTRIM keeps leading spaces" false (eq "RTRIM" " a" "a"); Alcotest.(check int) "collation is irrelevant to ints" 0 (V.compare_values_coll (seq "NOCASE") (Int 1L) (Int 1L)); Alcotest.(check bool) "a name is matched without regard to case" true (eq "nocase" "abc" "ABC"); (* collation_key gives values equal under the collation a shared form *) Alcotest.(check value) "NOCASE key folds case" (Text "abc") (V.collation_key (seq "NOCASE") (Text "ABC")); Alcotest.(check value) "collation_key leaves a non-text value alone" (Int 7L) (V.collation_key (seq "NOCASE") (Int 7L)); Alcotest.check_raises "an unknown collation raises" (Catalog.Error (Prepare "no such collation sequence: BOGUS")) (fun () -> ignore (seq "BOGUS")) let test_text_of_value () = Alcotest.(check string) "int" "42" (V.text_of_value (Int 42L)); Alcotest.(check string) "null renders empty" "" (V.text_of_value Null); Alcotest.(check string) "real keeps a point" "1.5" (V.text_of_value (Float 1.5)); Alcotest.(check string) "a whole real keeps .0" "2.0" (V.text_of_value (Float 2.0)); Alcotest.(check string) "text is itself" "hi" (V.text_of_value (Text "hi")) let test_text_of_real_matches_sqlite3 () = let case expected f = Alcotest.(check string) (Fmt.str "real %h" f) expected (V.text_of_value (Float f)) in case "1.5" 1.5; case "2.0" 2.0; case "0.1" 0.1; case "100000000000000.0" 1e14; case "1000000000000000.0" 1e15; case "10000000000000000.0" 1e16; case "Inf" Float.infinity; case "-Inf" Float.neg_infinity; case "NaN" Float.nan (* A non-finite real is rendered by sqlite3's own printf, not C's: it spells them "Inf", "-Inf" and "NaN", where C's %g gives "inf" and "nan". Checked against sqlite3 3.54.0, whose CAST(1e999 AS TEXT) is "Inf" and whose sqlite3_mprintf("%!.15g", 0.0/0.0) -- the same call CAST goes through -- is "NaN". *) let test_text_of_value_non_finite () = Alcotest.(check string) "infinity" "Inf" (V.text_of_value (Float Float.infinity)); Alcotest.(check string) "negative infinity" "-Inf" (V.text_of_value (Float Float.neg_infinity)); Alcotest.(check string) "nan" "NaN" (V.text_of_value (Float Float.nan)) (* sqlite3 renders a real through its own printf as "%!.17g": vdbemem.c's sqlite3VdbeMemStringify formats with [db->nFpDigit] significant digits, which main.c sets to 17. Every vector below is the text sqlite3 3.54.0 prints for that value in list mode, taken from the shell. Seventeen digits, not fifteen: [123456789012345.6] is "123456789012345.59", and a value whose shortest round-tripping form needs 15, 16 or 17 digits keeps all seventeen -- sqlite3FpDecode's shortcut (util.c) only ever tries a prefix of the first fourteen, so "shortest that round-trips" is not the rule and neither is C's "%.15g". Nor is it a correctly rounded 17 digits. FpDecode decodes to 18 and rounds that to 17, so [1.0/3.0] -- 0.33333333333333331482... exactly -- comes out "...332": rounding to 18 gives ...315, whose trailing 5 then rounds the 17th digit up, where rounding the value straight to 17 would leave ...331. *) let test_text_of_value_seventeen_digits () = let c f expected = Alcotest.(check string) (Fmt.str "%h" f) expected (V.text_of_value (Float f)) in c 0.1 "0.1"; c 0.3 "0.3"; c 1.1 "1.1"; c 49.47 "49.47"; c (1.0 /. 3.0) "0.33333333333333332"; c (100.0 /. 3.0) "33.333333333333336"; c 123456789012345.6 "123456789012345.59"; c 3.14159265358979 "3.14159265358979"; (* shortest round-trips at 15, 16 and 17 significant digits: all keep 17 *) c 889.93094241196695 "889.93094241196695"; c 573.15681709364128 "573.15681709364128"; c 198.33563752922566 "198.33563752922566"; c 355.26683999526955 "355.26683999526955" (* The "!" flag of that format forces a decimal point into the mantissa (printf.c), so the text always reads back as a REAL. %g's own rule picks the exponential form when the decimal exponent is below -4 or above 16, and sqlite3FpDecode's shortcut is what keeps a whole real at one fractional digit rather than seventeen. Both zeros render "0.0": FpDecode gives a zero the '+' sign whatever its bit pattern. *) let test_text_of_value_decimal_point () = let c f expected = Alcotest.(check string) (Fmt.str "%h" f) expected (V.text_of_value (Float f)) in c 5.0 "5.0"; c 2.0 "2.0"; c 100.0 "100.0"; c (-2.5) "-2.5"; c 0.0 "0.0"; c (-0.0) "0.0"; c 1234567890123456.0 "1234567890123456.0"; (* the exponent 16 / -4 boundaries of %g *) c 1e16 "10000000000000000.0"; c 1e17 "1.0e+17"; c 1e-4 "0.0001"; c 1e-5 "1.0e-05"; c 2e-7 "2.0e-07"; c 1e20 "1.0e+20"; c 1e22 "1.0e+22"; c 9.9999999999999999e22 "1.0e+23" (* The extremes of the type: the largest and smallest normal doubles, and the subnormals below them, which carry fewer significant bits than the seventeen digits sqlite3 prints. *) let test_text_of_value_extremes () = let c f expected = Alcotest.(check string) (Fmt.str "%h" f) expected (V.text_of_value (Float f)) in c 1e300 "1.0e+300"; c 1e-300 "1.0e-300"; c 1e308 "1.0e+308"; c 1.7976931348623157e308 "1.7976931348623157e+308"; c 2.2250738585072014e-308 "2.2250738585072014e-308"; c 5e-324 "4.9406564584124654e-324"; c 1e-320 "9.9998886718268301e-321"; c 1e-310 "9.9999999999999695e-311"; c 3e-322 "3.0138004396316039e-322" (* Seventeen digits, but not seventeen digits of the true value. sqlite3 decodes to 18 significant digits with 128-bit fixed-point arithmetic that keeps only the high half of the product, so its 18th digit is biased low by up to 2^-64 relative and lands the other side of a rounding boundary from a correctly rounded decode. That flips the 17th digit printed, and where the 18th digit was a 5 it also drops a digit: 1.0/9.0 is 0.111111111111111104943... exactly, whose 18 correctly rounded digits end ...105 and round up to ...111, while sqlite3's end ...104 and round down to ...110, whose trailing zero is then stripped. The population is not exotic. Every vector below is an ordinary quotient or a value one ulp off a power of ten. sqlite3 3.54.0: SELECT 1.0/9.0, 72.0/7.0, 2.0/130.0, 100000.00000000001 -> 0.1111111111111111|10.285714285714286|0.015384615384615385| 100000.00000000001 *) let test_text_of_value_approximate_decode () = let c f expected = Alcotest.(check string) (Fmt.str "%h" f) expected (V.text_of_value (Float f)) in c (1.0 /. 9.0) "0.1111111111111111"; c (72.0 /. 7.0) "10.285714285714286"; c (2.0 /. 130.0) "0.015384615384615385"; c (1.0 /. 518.0) "0.0019305019305019305"; c (1.0 /. 730.0) "0.0013698630136986301"; c (sqrt 116.0) "10.770329614269007"; c 100000.00000000001 "100000.00000000001"; (* one ulp above a power of ten, where the 18th digit is a tie *) c 1.0000000000000001e-100 "1.0000000000000001e-100"; c 1.0000000000000001e-160 "1.0000000000000001e-160"; c 1.0000000000000002e-59 "1.0000000000000002e-59"; (* and a subnormal drawn from the same rounding boundary *) c 1.06827828201473845e-308 "1.0682782820147384e-308" (* Reading a numeric literal back is sqlite3AtoF, not a correctly rounded strtod: it accumulates significant digits into a 64-bit significand and stops once another digit would not fit, then converts that pair through sqlite3Fp10Convert2. Every digit past the nineteenth or so is dropped rather than rounded, so a literal spelled with twenty reads back one ulp below the nearest double -- the digit sqlite3 truncated. A correctly rounded reader picks the other neighbour, and the two then disagree about what a stored REAL is. sqlite3 3.54.0, as the IEEE 754 bit patterns of what it parses: 3.6697984035011495496e-104 -> 2a750aac738a6b56 2.7632421315504844217e-250 -> 0c1fa78cae33224f *) let test_leading_number_is_atof () = let bits what s expect = let f = match V.leading_number s with | Float f -> f | Int n -> Int64.to_float n | v -> Alcotest.failf "%s: %a is not a number" what Val.pp v in Alcotest.(check string) what expect (Fmt.str "%016Lx" (Int64.bits_of_float f)) in bits "twenty digits, small exponent" "3.6697984035011495496e-104" "2a750aac738a6b56"; bits "twenty digits, smaller exponent" "2.7632421315504844217e-250" "0c1fa78cae33224f"; (* nineteen digits still fit the significand, so nothing is dropped *) bits "nineteen digits" "1.797693134862315708e308" "7fefffffffffffff"; bits "thirty digits of an integer" "123456789012345678901234567890" "45f8ee90ff6c373e"; bits "leading zeros are not significant" "0.0000000000000000000000001" "3abef2d0f5da7dd9"; (* the ends of the type read as sqlite3 reads them *) bits "overflow is an infinity" "1e400" "7ff0000000000000"; bits "underflow is zero" "1e-400" "0000000000000000"; bits "the smallest subnormal" "5e-324" "0000000000000001" (* The one place a double becomes a value: sqlite3VdbeMemSetDouble stores a REAL only when the double is not a NaN, and leaves the cell NULL otherwise. An infinity is an ordinary value. sqlite3 3.54.0: SELECT typeof(1e400 * 1e400) -> real SELECT typeof(1e400 - 1e400) -> null *) let test_real_of_float () = let v = Alcotest.testable Val.pp Val.equal in Alcotest.check v "a finite double is REAL" (Float 1.5) (V.real 1.5); Alcotest.check v "+Inf is REAL" (Float Float.infinity) (V.real Float.infinity); Alcotest.check v "-Inf is REAL" (Float Float.neg_infinity) (V.real Float.neg_infinity); Alcotest.check v "a NaN is NULL" Null (V.real Float.nan); Alcotest.check v "Inf - Inf is NULL" Null (V.real (Float.infinity -. Float.infinity)); Alcotest.check v "0 * Inf is NULL" Null (V.real (0. *. Float.infinity)) (* A comparison under numeric affinity reads each text operand through [whole_number] at every row, as sqlite3 applies the affinity at every OP_Lt: TPC-H's [l_shipdate >= '1994-01-01'] on a NUMERIC column reads two dates a row. A text that spells no number is refused before anything is allocated, so a run of 2,000 readings allocates what a run of 1,000 does: a reading that builds the prefix it then refuses allocates per call. *) let test_whole_number_refuses_without_allocating () = let words calls = let before = Gc.minor_words () in for _ = 1 to calls do ignore (Sys.opaque_identity (V.whole_number (Sys.opaque_identity "1994-01-01"))) done; Gc.minor_words () -. before in Alcotest.check Alcotest.(option (testable Val.pp Val.equal)) "a date is no number" None (V.whole_number "1994-01-01"); Alcotest.(check (float 0.)) "1,000 more readings allocate nothing more" (words 1000) (words 2000) let tstr = function V.T -> "T" | V.F -> "F" | V.U -> "U" let tri = Alcotest.testable (Fmt.of_to_string tstr) (fun a b -> tstr a = tstr b) let test_three_valued_logic () = Alcotest.(check tri) "NULL is unknown" V.U (V.tri_of Null); Alcotest.(check tri) "zero is false" V.F (V.tri_of (Int 0L)); Alcotest.(check tri) "non-zero is true" V.T (V.tri_of (Int 5L)); Alcotest.(check tri) "true AND false is false" V.F (V.tri_and V.T V.F); Alcotest.(check tri) "true AND unknown is unknown" V.U (V.tri_and V.T V.U); Alcotest.(check tri) "false AND unknown is false" V.F (V.tri_and V.F V.U); Alcotest.(check tri) "false OR unknown is unknown" V.U (V.tri_or V.F V.U); Alcotest.(check tri) "true OR unknown is true" V.T (V.tri_or V.T V.U); Alcotest.(check tri) "not unknown is unknown" V.U (V.tri_not V.U); (* text is truthy by the number it leads with, not by being non-empty *) Alcotest.(check tri) "'5x' coerces to true" V.T (V.tri_of (Text "5x")); Alcotest.(check tri) "'abc' coerces to false" V.F (V.tri_of (Text "abc")); (* not_value is the same logic as a value: NULL stays NULL *) Alcotest.(check value) "NOT 0 is 1" (Int 1L) (V.not_value (Int 0L)); Alcotest.(check value) "NOT 5 is 0" (Int 0L) (V.not_value (Int 5L)); Alcotest.(check value) "NOT NULL is NULL" Null (V.not_value Null) let test_is_value () = Alcotest.(check value) "NULL IS NULL is 1" (Int 1L) (V.is_value Null Null); Alcotest.(check value) "NULL IS 1 is 0" (Int 0L) (V.is_value Null (Int 1L)); Alcotest.(check value) "1 IS 1 is 1" (Int 1L) (V.is_value (Int 1L) (Int 1L)) let test_numeric_agg () = Alcotest.(check value) "integer sum stays integer" (Int 6L) (V.numeric_agg "sum" [ Int 1L; Int 2L; Int 3L ]); Alcotest.(check value) "avg is real" (Float 2.0) (V.numeric_agg "avg" [ Int 1L; Int 3L ]); Alcotest.(check value) "a sum with a real is real" (Float 6.0) (V.numeric_agg "sum" [ Int 1L; Float 2.0; Int 3L ]); Alcotest.(check value) "max uses storage-class order" (Int 5L) (V.numeric_agg "max" [ Int 1L; Int 5L; Int 3L ]); Alcotest.(check value) "min uses storage-class order" (Int 1L) (V.numeric_agg "min" [ Int 3L; Int 1L; Int 2L ]); Alcotest.(check value) "empty sum is NULL" Null (V.numeric_agg "sum" []); Alcotest.(check value) "empty total is 0.0" (Float 0.0) (V.numeric_agg "total" []); (* an integer sum stays integer until it cannot, and then it raises *) Alcotest.check_raises "an int64 sum overflow raises" (Catalog.Error (Runtime "integer overflow")) (fun () -> ignore (V.numeric_agg "sum" [ Int Int64.max_int; Int 1L ])) let test_like_and_glob () = Alcotest.(check bool) "% matches a run" true (V.like_match "a%" "abc"); Alcotest.(check bool) "_ matches one char" true (V.like_match "a_c" "abc"); Alcotest.(check bool) "LIKE is case-insensitive (ASCII)" true (V.like_match "A%" "abc"); Alcotest.(check bool) "no match" false (V.like_match "a%" "xyz"); Alcotest.(check bool) "% is not anchored" true (V.like_match "%b_" "abc"); Alcotest.(check bool) "_ matches one char only" false (V.like_match "a_" "abc"); Alcotest.(check bool) "* matches a run" true (V.glob_match "a*" "abc"); Alcotest.(check bool) "? matches one char" true (V.glob_match "a?c" "abc"); Alcotest.(check bool) "GLOB is case-sensitive" false (V.glob_match "A*" "abc"); Alcotest.(check bool) "GLOB *.txt matches" true (V.glob_match "*.txt" "a.txt"); Alcotest.(check bool) "GLOB [a-c] class" true (V.glob_match "[a-c]" "b"); (* the value-level forms propagate NULL *) Alcotest.(check value) "LIKE NULL is NULL" Null (V.like_op (Text "abc") Null); Alcotest.(check value) "'a' || 'b' is 'ab'" (Text "ab") (V.concat_op (Text "a") (Text "b")) (* The two numeric coercions: arithmetic affinity (non-numeric text becomes 0) against comparison affinity (non-numeric text stays text, so it equals no number), plus the integer coercion the bitwise operators apply. *) let test_coercion () = Alcotest.(check value) "numeric_affinity of '12' is 12" (Int 12L) (V.numeric_affinity (Text "12")); Alcotest.(check value) "numeric_affinity of 'abc' is 0" (Int 0L) (V.numeric_affinity (Text "abc")); Alcotest.(check value) "num_coerce of '12' is 12" (Int 12L) (V.num_coerce (Text "12")); Alcotest.(check value) "num_coerce of 'abc' stays text" (Text "abc") (V.num_coerce (Text "abc")); Alcotest.(check bool) "'3.5' reads as real" true (V.is_real (Text "3.5")); Alcotest.(check bool) "'3' does not read as real" false (V.is_real (Text "3")); Alcotest.(check int64) "intify 3.9 truncates to 3" 3L (V.intify (Float 3.9)); Alcotest.(check int64) "intify '12x' reads 12" 12L (V.intify (Text "12x")) let test_affinity_and_cast () = let aff = function | `Integer -> "int" | `Text -> "text" | `Blob -> "blob" | `Real -> "real" | `Numeric -> "num" in Alcotest.(check string) "INT name is integer" "int" (aff (V.affinity_of_type "INTEGER")); Alcotest.(check string) "VARCHAR is text" "text" (aff (V.affinity_of_type "VARCHAR(10)")); Alcotest.(check string) "empty is blob" "blob" (aff (V.affinity_of_type "")); Alcotest.(check string) "REAL is real" "real" (aff (V.affinity_of_type "REAL")); Alcotest.(check string) "DECIMAL is numeric" "num" (aff (V.affinity_of_type "DECIMAL")); Alcotest.(check string) "BIGINT is integer" "int" (aff (V.affinity_of_type "BIGINT")); Alcotest.(check value) "CAST text to integer" (Int 42L) (V.cast "INTEGER" (Text "42")); Alcotest.(check value) "CAST int to text" (Text "42") (V.cast "TEXT" (Int 42L)); Alcotest.(check value) "CAST preserves NULL" Null (V.cast "INTEGER" Null); (* a CAST reads the prefix its target affinity accepts *) Alcotest.(check value) "CAST('12.9' AS INTEGER) reads the integer prefix" (Int 12L) (V.cast "INTEGER" (Text "12.9")); Alcotest.(check value) "CAST('3.5x' AS REAL) reads the numeric prefix" (Float 3.5) (V.cast "REAL" (Text "3.5x")); (* NUMERIC collapses an integral real to an integer *) Alcotest.(check value) "CAST('5.0' AS NUMERIC) collapses to an integer" (Int 5L) (V.cast "NUMERIC" (Text "5.0")); (* column write affinity numifies only well-formed text *) Alcotest.(check value) "apply_affinity INTEGER numifies '42'" (Int 42L) (V.apply_affinity `Integer (Text "42")); Alcotest.(check value) "apply_affinity INTEGER keeps non-numeric text" (Text "abc") (V.apply_affinity `Integer (Text "abc")) let test_intify_and_distinct () = Alcotest.(check (list value)) "distinct keeps first occurrences" [ Int 1L; Int 2L; Null ] (V.distinct_values [ Int 1L; Int 1L; Int 2L; Null; Null ]); (* an int and the equal real are one value, so only the first survives *) Alcotest.(check (list value)) "an int and the equal real dedup" [ Int 1L ] (V.distinct_values [ Int 1L; Float 1.0 ]) (* SQLite's value-level semantics: three-valued truth, storage affinity and CAST, the LIKE/GLOB pattern operators and the aggregate folds. Vectors are quoted from the SQLite documentation and cross-checked against sqlite3 3.54. *) module Doc = struct open Sql.Value_ops module V = Catalog.Value let vstr : V.t -> string = function | Null -> "NULL" | Int i -> Fmt.str "int:%Ld" i | Float f -> Fmt.str "float:%.17g" f | Text s -> "text:" ^ s | Blob b -> "blob:" ^ b let value = Alcotest.testable (Fmt.of_to_string vstr) (fun a b -> vstr a = vstr b) let tstr = function T -> "T" | F -> "F" | U -> "U" let tri = Alcotest.testable (Fmt.of_to_string tstr) (fun a b -> tstr a = tstr b) (* lang_expr.html sec.14 "Boolean Expressions": a value is cast to NUMERIC as a CAST expression would, a numeric zero is false, NULL stays NULL, everything else is true. The section's own worked list is the vector below. *) let test_truth_of_a_value () = let case t v = Alcotest.(check tri) (vstr v) t (tri_of v) in case U V.Null; case F (V.Float 0.0); case F (V.Int 0L); case F (V.Text "english"); case F (V.Text "0"); case T (V.Int 1L); case T (V.Float 1.0); case T (V.Float 0.1); case T (V.Float (-0.1)); case T (V.Text "1english"); (* the cast reads a prefix, so the empty string and a blob follow the same rule as any other text *) case F (V.Text ""); case F (V.Blob "english"); case T (V.Blob "1english") let test_three_valued_logic () = Alcotest.(check tri) "true AND false" F (tri_and T F); Alcotest.(check tri) "true AND unknown" U (tri_and T U); Alcotest.(check tri) "false AND unknown" F (tri_and F U); Alcotest.(check tri) "false OR unknown" U (tri_or F U); Alcotest.(check tri) "true OR unknown" T (tri_or T U); Alcotest.(check tri) "false OR false" F (tri_or F F); Alcotest.(check tri) "not unknown" U (tri_not U); Alcotest.(check value) "NOT 0 is 1" (V.Int 1L) (not_value (V.Int 0L)); Alcotest.(check value) "NOT NULL is NULL" V.Null (not_value V.Null) (* [a IS b] is NULL-safe and never answers NULL. *) let test_is_value () = Alcotest.(check value) "NULL IS NULL" (V.Int 1L) (is_value Null Null); Alcotest.(check value) "NULL IS 1" (V.Int 0L) (is_value Null (V.Int 1L)); Alcotest.(check value) "1 IS 1.0" (V.Int 1L) (is_value (V.Int 1L) (V.Float 1.)) (* lang_aggfunc.html: sum() of no non-NULL rows is NULL but total() is 0.0; total() is always real; sum() is integer only when every input is; sum() throws "integer overflow" when an all-integer fold leaves int64. min()/max() are the first/last value an ORDER BY on the column would return. *) let test_numeric_agg () = Alcotest.(check value) "integer sum stays integer" (V.Int 6L) (numeric_agg "sum" [ Int 1L; Int 2L; Int 3L ]); Alcotest.(check value) "a real input makes the sum real" (V.Float 6.0) (numeric_agg "sum" [ Int 1L; Float 2.0; Int 3L ]); Alcotest.(check value) "empty sum is NULL" V.Null (numeric_agg "sum" []); Alcotest.(check value) "empty total is 0.0" (V.Float 0.0) (numeric_agg "total" []); Alcotest.(check value) "total is real over integers" (V.Float 3.0) (numeric_agg "total" [ Int 1L; Int 2L ]); Alcotest.(check value) "avg is real over integers" (V.Float 2.0) (numeric_agg "avg" [ Int 1L; Int 3L ]); Alcotest.(check value) "empty avg is NULL" V.Null (numeric_agg "avg" []); (* ORDER BY order, so text sorts above every number *) Alcotest.(check value) "max is the last in ORDER BY" (V.Text "a") (numeric_agg "max" [ Int 9L; Text "a"; Float 2.5 ]); Alcotest.(check value) "min is the first in ORDER BY" (V.Float 2.5) (numeric_agg "min" [ Int 9L; Text "a"; Float 2.5 ]); Alcotest.(check value) "min of nothing is NULL" V.Null (numeric_agg "min" []); Alcotest.check_raises "int64 overflow is loud" (Catalog.Error (Runtime "integer overflow")) (fun () -> ignore (numeric_agg "sum" [ Int Int64.max_int; Int 1L ])); Alcotest.check_raises "an unknown fold is rejected" (Catalog.Error (Prepare "unknown aggregate median()")) (fun () -> ignore (numeric_agg "median" [ Int 1L ])) (* An overflowing fold answers Inf, but a fold whose running total goes NaN answers NULL: the finalizer hands its double to sqlite3_result_double, which stores no NaN (sqlite3VdbeMemSetDouble). Against sqlite3 3.54.0, over a column holding 1e400 and -1e400 (both stored as infinities): SELECT sum(v), typeof(sum(v)) -> |null SELECT avg(v), typeof(avg(v)) -> |null SELECT total(v), typeof(total(v)) -> |null and over a column holding 1e308 twice, where only the sum overflows: SELECT sum(v), typeof(sum(v)) -> Inf|real *) let test_agg_non_finite () = let inf = V.Float Float.infinity and ninf = V.Float Float.neg_infinity in Alcotest.(check value) "a sum that overflows is Inf" (V.Float Float.infinity) (numeric_agg "sum" [ Float 1e308; Float 1e308 ]); Alcotest.(check value) "a sum of +Inf and -Inf is NULL" V.Null (numeric_agg "sum" [ inf; ninf ]); Alcotest.(check value) "the matching total is NULL" V.Null (numeric_agg "total" [ inf; ninf ]); Alcotest.(check value) "the matching avg is NULL" V.Null (numeric_agg "avg" [ inf; ninf ]) (* lang_aggfunc.html: avg() reads "String and BLOB values that do not look like numbers" as 0, and "all of the constraints that apply to total() also apply to avg()", so sum() and total() read them the same way. sum() "is an integer value if all non-NULL inputs are integers. If any input to sum() is neither an integer nor a NULL, then sum() returns a floating point value which is an approximation of the mathematical sum." Answers cross-checked against sqlite3 3.54. *) let test_agg_over_non_numeric () = Alcotest.(check value) "a string that is not a number counts as zero" (V.Float 3.0) (numeric_agg "sum" [ Int 1L; Text "abc"; Int 2L ]); Alcotest.(check value) "a string contributes the number it leads with" (V.Float 9.0) (numeric_agg "sum" [ Int 4L; Text "5x" ]); Alcotest.(check value) "a blob does too" (V.Float 16.0) (numeric_agg "sum" [ Int 4L; Blob "12" ]); Alcotest.(check value) "a sum of nothing but strings is still real" (V.Float 5.0) (numeric_agg "sum" [ Text "abc"; Text "5x" ]); Alcotest.(check value) "total folds a string as zero too" (V.Float 3.0) (numeric_agg "total" [ Int 1L; Text "abc"; Int 2L ]); Alcotest.(check value) "avg divides by every non-NULL row" (V.Float 1.0) (numeric_agg "avg" [ Int 1L; Text "abc"; Int 2L ]); (* an overflow is raised only when every input is an integer *) Alcotest.(check value) "a string disarms the integer overflow" (V.Float 9.2233720368547758e18) (numeric_agg "sum" [ Int Int64.max_int; Int 1L; Text "abc" ]) (* lang_aggfunc.html leaves "all non-NULL inputs are integers" without saying whether an input is the storage class or the number it denotes, and the page never mentions storage classes at all. Its avg() paragraph settles that the family reads a string as a number ("String and BLOB values that do not look like numbers are interpreted as 0", and "all of the constraints that apply to total() also apply to avg()"), so a string that spells an integer is an integer input, and the page's other rule -- sum() "will throw an 'integer overflow' exception if all inputs are integers or NULL" -- is decided by the same test. That is the reading followed here, and the one sqlite3 3.54 implements ([sqlite3_value_numeric_type] applies numeric affinity first). Only a whole string counts: a fractional or exponent spelling is real, and a blob is never coerced. *) let test_sum_over_numeric_text () = Alcotest.(check value) "an integer spelled as text keeps the sum integer" (V.Int 7L) (numeric_agg "sum" [ Text "3"; Int 4L ]); Alcotest.(check value) "the whitespace around it does not change that" (V.Int 7L) (numeric_agg "sum" [ Text " 3 "; Int 4L ]); Alcotest.(check value) "a leading sign does not either" (V.Int 7L) (numeric_agg "sum" [ Text "+3"; Int 4L ]); Alcotest.(check value) "a fractional spelling makes it real" (V.Float 7.0) (numeric_agg "sum" [ Text "3.0"; Int 4L ]); Alcotest.(check value) "an exponent spelling does too" (V.Float 7.0) (numeric_agg "sum" [ Text "3e0"; Int 4L ]); Alcotest.(check value) "text that is not a number does too, folding as zero" (V.Float 4.0) (numeric_agg "sum" [ Text "0x10"; Int 4L ]); Alcotest.(check value) "an integer past int64 does too" (V.Float 1e20) (numeric_agg "sum" [ Text "99999999999999999999" ]); (* a blob is left alone, so it never counts as an integer input *) Alcotest.(check value) "a blob spelling an integer makes it real" (V.Float 16.0) (numeric_agg "sum" [ Blob "12"; Int 4L ]); (* the overflow rule reads "input is an integer" the same way *) Alcotest.check_raises "an all-integer fold overflows loudly" (Catalog.Error (Runtime "integer overflow")) (fun () -> ignore (numeric_agg "sum" [ Text "9223372036854775807"; Text "1" ])) (* func.c, kahanBabuskaNeumaierInit: the first real starts the compensated sum from the integer sum, an integer past 2^52 split into its multiple of 16384 as the total and the rest as the error. Adding the two parts instead holds the same value in another state, which the terms after it round otherwise. sqlite3 3.54.1: SELECT sum(x) FROM (SELECT 4503599627370497 AS x UNION ALL SELECT 0.001 UNION ALL SELECT -4503599627370497) -> 0.00099999999999988987 *) let test_sum_real_start () = Alcotest.(check value) "the integer sum splits as it starts the real sum" (V.Float 0.00099999999999988987) (numeric_agg "sum" [ Int 4503599627370497L; Float 0.001; Int (-4503599627370497L) ]) let test_distinct_values () = Alcotest.(check (list value)) "keeps the first occurrence of each" [ Int 1L; Int 2L; Null ] (distinct_values [ Int 1L; Int 1L; Int 2L; Null; Null ]); (* equality is by value, not by storage class, so 1 and 1.0 are one value *) Alcotest.(check (list value)) "an integer and the equal real are one value" [ Int 1L ] (distinct_values [ V.Int 1L; V.Float 1.0 ]) (* lang_expr.html sec.5: [%] matches any run, [_] one character, any other character matches itself or its ASCII case counterpart. The ESCAPE character makes the next one literal. *) let test_like () = let yes p s = Alcotest.(check bool) (Fmt.str "%s LIKE %s" s p) true (like_match p s) in let no p s = Alcotest.(check bool) (Fmt.str "%s NOT LIKE %s" s p) false (like_match p s) in yes "a%" "abc"; yes "%" ""; yes "a_c" "abc"; no "a_c" "ac"; yes "A%" "abc"; yes "a%" "ABC"; no "a%" "xyz"; (* only ASCII case folds: the section's own pair, ['a' LIKE 'A'] is true but ['ae' LIKE 'AE'] is false, spelled here in the UTF-8 sqlite3 stores *) yes "A" "a"; no "\xc3\x86" "\xc3\xa6"; yes "100%" "100 dollars"; Alcotest.(check bool) "escape makes %% literal" true (like_match ~escape:(Char.code '\\') "100\\%" "100%"); Alcotest.(check bool) "escaped %% no longer matches a run" false (like_match ~escape:(Char.code '\\') "100\\%" "100abc"); Alcotest.(check bool) "the escape character escapes itself" true (like_match ~escape:(Char.code '\\') "a\\\\b" "a\\b") (* lang_expr.html sec.5: GLOB uses Unix file globbing and is case sensitive. *) let test_glob () = let yes p s = Alcotest.(check bool) (Fmt.str "%s GLOB %s" s p) true (glob_match p s) in let no p s = Alcotest.(check bool) (Fmt.str "%s NOT GLOB %s" s p) false (glob_match p s) in yes "a*" "abc"; yes "a?c" "abc"; no "A*" "abc"; yes "[abc]z" "bz"; no "[abc]z" "dz"; yes "[a-c]z" "bz"; no "[^a-c]z" "bz"; yes "[^a-c]z" "dz"; (* a ']' first in the class is a literal member of it, and a '[' anywhere in one is literal too, so both of these classes are closed *) yes "a[]]b" "a]b"; yes "[]]" "]"; yes "[a[]b" "ab"; yes "a[-]b" "a-b"; (* a '[' with no closing ']' matches nothing at all, not a literal bracket: lang_expr.html sec.5 defers the class syntax to "the Unix file globbing syntax" without defining this case, and the reference matcher runs off the end of the pattern and answers no-match. Cross-checked against sqlite3 3.54. *) no "[abc" "[abc"; no "x[y" "x[y"; no "[" "["; no "[x" "x"; no "[]" "]"; no "*[" "x" let test_pattern_ops () = Alcotest.(check value) "a NULL operand makes LIKE NULL" V.Null (like_op Null (V.Text "a%")); Alcotest.(check value) "LIKE is 1 on a match" (V.Int 1L) (like_op (V.Text "abc") (V.Text "a%")); Alcotest.(check value) "GLOB is 0 on no match" (V.Int 0L) (glob_op (V.Text "abc") (V.Text "A*")); Alcotest.(check value) "|| is NULL if either side is" V.Null (concat_op (V.Text "a") Null); Alcotest.(check value) "|| joins the text forms" (V.Text "a1") (concat_op (V.Text "a") (V.Int 1L)); Alcotest.check_raises "an empty ESCAPE is rejected" (Catalog.Error (Runtime "ESCAPE expression must be a single character")) (fun () -> ignore (like_op ~escape:(V.Text "") (V.Text "a") (V.Text "a"))) (* datatype3.html sec.3.1: a declared type containing INT has INTEGER affinity, then CHAR/CLOB/TEXT, then BLOB (or no type at all), then REAL/FLOA/DOUB, else NUMERIC. The rules are tried in that order, so "CHARINT" is INTEGER. *) let test_affinity_of_type () = let aff = function | `Integer -> "INTEGER" | `Text -> "TEXT" | `Blob -> "BLOB" | `Real -> "REAL" | `Numeric -> "NUMERIC" in let case expect ty = Alcotest.(check string) ty expect (aff (affinity_of_type ty)) in case "INTEGER" "INT"; case "INTEGER" "BIGINT"; case "INTEGER" "UNSIGNED BIG INT"; case "TEXT" "VARCHAR(255)"; case "TEXT" "NATIVE CHARACTER(70)"; case "BLOB" ""; case "BLOB" "BLOB"; case "REAL" "DOUBLE PRECISION"; case "REAL" "FLOAT"; case "NUMERIC" "DECIMAL(10,5)"; case "NUMERIC" "BOOLEAN"; case "NUMERIC" "DATETIME"; (* INTEGER wins over TEXT because the INT rule is tried first *) case "INTEGER" "CHARINT" (* datatype3.html sec.3: a column's affinity is applied to a value on the way in. NUMERIC/INTEGER read well-formed numeric text as a number and store a whole real as an integer; TEXT renders a number; a BLOB and NULL never convert. *) let test_apply_affinity () = let case expect ty v = Alcotest.(check value) (Fmt.str "%s <- %s" ty (vstr v)) expect (apply_affinity (affinity_of_type ty) v) in case (V.Int 5L) "INTEGER" (V.Text "5"); case (V.Text "abc") "INTEGER" (V.Text "abc"); case (V.Int 2L) "NUMERIC" (V.Float 2.0); case (V.Float 1.5) "NUMERIC" (V.Float 1.5); case (V.Text "42") "TEXT" (V.Int 42L); case (V.Float 3.0) "REAL" (V.Int 3L); case (V.Blob "5") "INTEGER" (V.Blob "5"); case V.Null "INTEGER" V.Null; (* Storage affinity converts text only when the whole of it is a number by sqlite3's grammar, so what a column stores is what was written unless it really was numeric. Text with a tail already stayed text; a hex literal, a digit separator and the words for the non-finite values must too -- the last of those would otherwise put a NaN in the value model, which has none. sqlite3 3.54.0, over a table of an INT, a REAL and a NUMERIC column: '0x10' text|'0x10' text|'0x10' text|'0x10' '1_000' text|'1_000' text|'1_000' text|'1_000' 'nan' text|'nan' text|'nan' text|'nan' 'inf' text|'inf' text|'inf' text|'inf' '5x' text|'5x' text|'5x' text|'5x' '16' integer|16 real|16.0 integer|16 ' 16 ' integer|16 real|16.0 integer|16 *) List.iter (fun ty -> case (V.Text "0x10") ty (V.Text "0x10"); case (V.Text "1_000") ty (V.Text "1_000"); case (V.Text "nan") ty (V.Text "nan"); case (V.Text "inf") ty (V.Text "inf"); case (V.Text "5x") ty (V.Text "5x")) [ "INT"; "REAL"; "NUMERIC" ]; case (V.Int 16L) "INT" (V.Text " 16 "); case (V.Float 16.0) "REAL" (V.Text " 16 "); case (V.Int 16L) "NUMERIC" (V.Text " 16 ") (* lang_expr.html#castexpr: casting TEXT to INTEGER or REAL takes the longest prefix that reads as one and ignores the rest; NULL is preserved. *) let test_cast () = let case expect ty v = Alcotest.(check value) (Fmt.str "CAST(%s AS %s)" (vstr v) ty) expect (cast ty v) in case (V.Int 42L) "INTEGER" (V.Text "42"); case (V.Int 123L) "INTEGER" (V.Text "123abc"); case (V.Int 123L) "INTEGER" (V.Text "123e+5"); case (V.Int 0L) "INTEGER" (V.Text "abc"); case (V.Int 1L) "INTEGER" (V.Float 1.9); (* sqlite3 3.54.1: SELECT CAST(1e300 AS INTEGER), CAST(-1e300 AS INTEGER) answers 9223372036854775807|-9223372036854775808 *) case (V.Int Int64.max_int) "INTEGER" (V.Float 1e300); case (V.Int Int64.min_int) "INTEGER" (V.Float (-1e300)); case (V.Text "42") "TEXT" (V.Int 42L); case (V.Text "1.5") "TEXT" (V.Float 1.5); case (V.Blob "42") "BLOB" (V.Int 42L); case (V.Float 1.5) "REAL" (V.Text "1.5abc"); case (V.Int 2L) "NUMERIC" (V.Text "2"); case (V.Float 2.5) "NUMERIC" (V.Text "2.5"); case V.Null "INTEGER" V.Null; case V.Null "TEXT" V.Null (* The two numeric coercions differ on text that is not a number: arithmetic reads a leading prefix and falls back to 0, a comparison leaves it alone so it cannot spuriously equal a number. *) let test_numeric_coercions () = Alcotest.(check value) "arithmetic reads a prefix" (V.Int 5L) (numeric_affinity (V.Text "5x")); Alcotest.(check value) "arithmetic falls back to 0" (V.Int 0L) (numeric_affinity (V.Text "abc")); Alcotest.(check value) "comparison converts a clean number" (V.Int 5L) (num_coerce (V.Text "5")); Alcotest.(check value) "comparison leaves 5x as text" (V.Text "5x") (num_coerce (V.Text "5x")); Alcotest.(check int64) "intify truncates a real" 1L (intify (V.Float 1.9)); Alcotest.(check int64) "intify reads a prefix" 5L (intify (V.Text "5x")); Alcotest.(check int64) "intify of nothing is 0" 0L (intify (V.Text "abc")); (* sqlite3VdbeIntValue reads text through sqlite3Atoi64, its leading integer: sqlite3 3.54.1 answers 1|2|1|12|9223372036854775807 to SELECT '1e3' | 0, '2.5e1' | 0, x'31653321' | 0, ' 12.9abc' | 0, '99999999999999999999' | 0 *) Alcotest.(check int64) "intify '1e3' is 1" 1L (intify (V.Text "1e3")); Alcotest.(check int64) "intify '2.5e1' is 2" 2L (intify (V.Text "2.5e1")); Alcotest.(check int64) "a blob the same" 1L (intify (V.Blob "1e3!")); Alcotest.(check int64) "past the blanks" 12L (intify (V.Text " 12.9abc")); Alcotest.(check int64) "held at the int64 range" Int64.max_int (intify (V.Text "99999999999999999999")); (* So is a real, by doubleToInt64 (vdbemem.c): sqlite3 3.54.1 answers 9223372036854775807|-9223372036854775808|9223372036854775807 to SELECT 1e300 | 0, -1e300 | 0, 9.3e18 | 0 *) Alcotest.(check int64) "1e300" Int64.max_int (intify (V.Float 1e300)); Alcotest.(check int64) "-1e300" Int64.min_int (intify (V.Float (-1e300))); Alcotest.(check int64) "9.3e18" Int64.max_int (intify (V.Float 9.3e18)); Alcotest.(check bool) "a float is real" true (is_real (V.Float 1.0)); Alcotest.(check bool) "integer text is not" false (is_real (V.Text "1")); Alcotest.(check bool) "real text is" true (is_real (V.Text "1.5")); Alcotest.(check bool) "text spelling no number is not" false (is_real (V.Text "nan")) (* The numeric gate a math function puts its argument through: [sqlite3_value_numeric_type] (vdbeapi.c) applies numeric affinity and reports the storage class it lands on, and func.c's math1Func / math2Func / logFunc / ceilingFunc / signFunc all answer NULL -- by returning without setting a result -- for anything that is not then INTEGER or REAL. So NULL, a blob and text that does not spell a whole number have no answer, while text that does is read as the number. sqlite3 3.54.0: sqrt('4') 2.0 sqrt(' 4 ') 2.0 sqrt('4x') NULL sqrt(x'34') NULL sqrt(NULL) NULL sign('4') 1 ceil('4') 4 typeof integer ceil('4.2') 5.0 typeof real ceil(x'34') NULL typeof null *) let test_numeric_type () = let c what expected v = Alcotest.check (Alcotest.option value) what expected (numeric_type v) in c "an integer is itself" (Some (V.Int 4L)) (V.Int 4L); c "a real is itself" (Some (V.Float 4.5)) (V.Float 4.5); c "an infinity is a real" (Some (V.Float Float.infinity)) (V.Float Float.infinity); c "numeric text reads as an integer" (Some (V.Int 4L)) (V.Text "4"); c "with surrounding whitespace too" (Some (V.Int 4L)) (V.Text " 4 "); c "a fractional spelling reads as a real" (Some (V.Float 4.2)) (V.Text "4.2"); c "an exponent spelling reads as a real" (Some (V.Float 400.)) (V.Text "4e2"); c "text with a numeric prefix only is not numeric" None (V.Text "4x"); c "nor is non-numeric text" None (V.Text "abc"); c "nor is empty text" None (V.Text ""); c "a blob is never numeric" None (V.Blob "4"); c "and NULL is not" None V.Null (* Where sqlite3 stops reading a TEXT value at an embedded NUL. The value keeps every byte -- [sqlite3_result_text64] is given a length and the Mem carries it -- but [sqlite3_value_text] hands a function implementation a bare [const unsigned char *], a NUL-terminated C string with no length beside it. So a function that reads its argument as text sees only the bytes before the first NUL, while one that reads the same value as a blob, or compares it, sees them all. sqlite3 3.54.0 over [char(65,0,66)], the three bytes 41 00 42: length() 1 hex() 410042 quote() 'A' octet_length() 3 printf('%s') 41 CAST(.. AS BLOB) hex 410042 unicode(char(0,65)) NULL char(65,0,66) = 'A' 0 upper() hex 410042 instr(.., char(66)) 3 *) let test_c_text_of_value () = let c what expected v = Alcotest.(check string) what expected (c_text_of_value v) in c "a NUL cuts the text short" "A" (V.Text "A\000B"); c "a leading NUL leaves nothing" "" (V.Text "\000AB"); c "text with no NUL is itself" "AB" (V.Text "AB"); c "the bytes are not escaped, they are dropped" "A" (V.Text "A\000\000B"); c "a blob is read the same way" "A" (V.Blob "A\000B"); c "and a number's text has no NUL to stop at" "42" (V.Int 42L); c "nor has NULL's empty text" "" V.Null; (* the value itself is untouched: the full byte string is still what a comparison and a blob-reading function see *) Alcotest.(check string) "text_of_value keeps every byte" "A\000B" (text_of_value (V.Text "A\000B")); Alcotest.(check bool) "so the value does not equal its C-string prefix" false (compare_values (V.Text "A\000B") (V.Text "A") = 0); (* LIKE and GLOB read both operands through the accessor (likeFunc, func.c), so they match on the prefix the comparison above rejects. sqlite3 3.54.0: char(65,0,66) LIKE 'A' 1 char(65,0,66) GLOB 'A' 1 char(65,0,66) LIKE 'A%' 1 char(65,0,66) LIKE 'A_B' 0 *) Alcotest.(check value) "LIKE matches the C string" (V.Int 1L) (like_op (V.Text "A\000B") (V.Text "A")); Alcotest.(check value) "and so does GLOB" (V.Int 1L) (glob_op (V.Text "A\000B") (V.Text "A")); Alcotest.(check value) "the bytes past the NUL are not there to match" (V.Int 0L) (like_op (V.Text "A\000B") (V.Text "A_B")) (* Every text-to-number reading takes "the longest possible prefix of the value that can be interpreted as a real number" (lang_expr.html#castexpr), and sqlite3AtoF spells that grammar out: leading and trailing whitespace, an optional sign, digits, an optional [.] and an optional [e] exponent. Nothing else is a number -- no [inf], no [nan], no hexadecimal, no [_] between digits -- where OCaml's own [float_of_string] and [Int64.of_string] accept all four. Answers cross-checked against sqlite3 3.54. *) let test_text_that_is_not_a_number () = let cast_case expect ty v = Alcotest.(check value) (Fmt.str "CAST(%s AS %s)" (vstr v) ty) expect (cast ty v) in cast_case (V.Float 0.) "REAL" (V.Text "inf"); cast_case (V.Float 0.) "REAL" (V.Text "infinity"); cast_case (V.Float 0.) "REAL" (V.Text "nan"); cast_case (V.Float 0.) "REAL" (V.Text "0x10"); cast_case (V.Float 1.) "REAL" (V.Text "1_0"); cast_case (V.Int 0L) "NUMERIC" (V.Text "inf"); cast_case (V.Int 0L) "INTEGER" (V.Text "0x10"); cast_case (V.Int 1L) "INTEGER" (V.Text "1_0"); (* a real prefix out of double range is still a number, and reads as Inf *) cast_case (V.Float Float.infinity) "REAL" (V.Text "1e400"); (* a column stores what it cannot read as a number unchanged *) let aff_case expect ty v = Alcotest.(check value) (Fmt.str "%s <- %s" ty (vstr v)) expect (apply_affinity (affinity_of_type ty) v) in aff_case (V.Text "inf") "REAL" (V.Text "inf"); aff_case (V.Text "0x10") "INTEGER" (V.Text "0x10"); aff_case (V.Text "1_0") "NUMERIC" (V.Text "1_0"); (* a comparison converts an operand only when the whole of it is a number, ignoring the whitespace around it, and never converts a blob *) Alcotest.(check value) "comparison leaves inf as text" (V.Text "inf") (num_coerce (V.Text "inf")); Alcotest.(check value) "comparison leaves 0x10 as text" (V.Text "0x10") (num_coerce (V.Text "0x10")); Alcotest.(check value) "comparison leaves 1_0 as text" (V.Text "1_0") (num_coerce (V.Text "1_0")); Alcotest.(check value) "comparison trims the whitespace around a number" (V.Int 12L) (num_coerce (V.Text " 12 ")); Alcotest.(check value) "comparison never converts a blob" (V.Blob "12") (num_coerce (V.Blob "12")); (* [%] answers a real when either operand reads as one: a leading real prefix, or an integer prefix too wide for int64 *) Alcotest.(check bool) "inf is not a real" false (is_real (V.Text "inf")); Alcotest.(check bool) "5.5x is" true (is_real (V.Text "5.5x")); Alcotest.(check bool) "5x is not" false (is_real (V.Text "5x")); Alcotest.(check bool) "an integer past int64 is" true (is_real (V.Text "99999999999999999999")); Alcotest.(check bool) "a blob reads like the text" true (is_real (V.Blob "1.5")); Alcotest.(check tri) "WHERE 'inf' is false" F (tri_of (V.Text "inf")) (* A collation refines the order of two TEXT values and nothing else. The sequence arrives resolved -- a name no built-in carries is refused by whichever frontend read the [COLLATE], so no lookup happens here. *) let test_collated_comparison () = Alcotest.(check int) "NOCASE folds ASCII case" 0 (compare_values_coll Sql.Collation.Nocase (V.Text "ABC") (V.Text "abc")); Alcotest.(check bool) "BINARY does not" true (compare_values_coll Sql.Collation.Binary (V.Text "ABC") (V.Text "abc") < 0); Alcotest.(check int) "a collation is irrelevant to numbers" 0 (compare_values_coll Sql.Collation.Nocase (V.Int 1L) (V.Float 1.0)); Alcotest.(check value) "the NOCASE key folds case" (V.Text "abc") (collation_key Sql.Collation.Nocase (V.Text "ABC")); Alcotest.(check value) "a non-text key is itself" (V.Int 1L) (collation_key Sql.Collation.Nocase (V.Int 1L)); Alcotest.(check (option bool)) "and no built-in carries an unknown name" None (Option.map (fun _ -> true) (Sql.Collation.of_string "SPANISH")) let test_text_of_value () = Alcotest.(check string) "int" "42" (text_of_value (V.Int 42L)); Alcotest.(check string) "NULL is empty" "" (text_of_value V.Null); Alcotest.(check string) "a real keeps a point" "1.5" (text_of_value (V.Float 1.5)); Alcotest.(check string) "a whole real keeps one" "2.0" (text_of_value (V.Float 2.0)); Alcotest.(check string) "a blob is its bytes" "\x00\x01" (text_of_value (V.Blob "\x00\x01")); Alcotest.(check (float 0.)) "float_of_value on an int" 3.0 (float_of_value (V.Int 3L)); Alcotest.check_raises "float_of_value rejects text" (Invalid_argument "float_of_value") (fun () -> ignore (float_of_value (V.Text "3"))) end let suite = ( "value_ops", [ Alcotest.test_case "storage-class comparison" `Quick test_compare_storage_classes; Alcotest.test_case "text_of_value" `Quick test_text_of_value; Alcotest.test_case "real text matches sqlite3" `Quick test_text_of_real_matches_sqlite3; Alcotest.test_case "text_of_value (non-finite reals)" `Quick test_text_of_value_non_finite; Alcotest.test_case "text_of_value (seventeen digits)" `Quick test_text_of_value_seventeen_digits; Alcotest.test_case "text_of_value (decimal point)" `Quick test_text_of_value_decimal_point; Alcotest.test_case "text_of_value (extremes)" `Quick test_text_of_value_extremes; Alcotest.test_case "text_of_value (an approximate decode)" `Quick test_text_of_value_approximate_decode; Alcotest.test_case "leading_number reads as sqlite3AtoF" `Quick test_leading_number_is_atof; Alcotest.test_case "real (a NaN is not a value)" `Quick test_real_of_float; Alcotest.test_case "whole_number refuses without allocating" `Quick test_whole_number_refuses_without_allocating; Alcotest.test_case "three-valued logic" `Quick test_three_valued_logic; Alcotest.test_case "IS (NULL-safe equality)" `Quick test_is_value; Alcotest.test_case "numeric aggregates" `Quick test_numeric_agg; Alcotest.test_case "LIKE and GLOB" `Quick test_like_and_glob; Alcotest.test_case "numeric coercion" `Quick test_coercion; Alcotest.test_case "collation" `Quick test_collation; Alcotest.test_case "affinity and CAST" `Quick test_affinity_and_cast; Alcotest.test_case "distinct values" `Quick test_intify_and_distinct; Alcotest.test_case "truth of a value" `Quick Doc.test_truth_of_a_value; Alcotest.test_case "three-valued logic" `Quick Doc.test_three_valued_logic; Alcotest.test_case "IS (NULL-safe equality)" `Quick Doc.test_is_value; Alcotest.test_case "numeric aggregates" `Quick Doc.test_numeric_agg; Alcotest.test_case "aggregates over non-finite input" `Quick Doc.test_agg_non_finite; Alcotest.test_case "aggregates over non-numeric input" `Quick Doc.test_agg_over_non_numeric; Alcotest.test_case "sum over numeric text" `Quick Doc.test_sum_over_numeric_text; Alcotest.test_case "sum starting its real sum" `Quick Doc.test_sum_real_start; Alcotest.test_case "distinct values" `Quick Doc.test_distinct_values; Alcotest.test_case "LIKE" `Quick Doc.test_like; Alcotest.test_case "GLOB" `Quick Doc.test_glob; Alcotest.test_case "pattern and concat operators" `Quick Doc.test_pattern_ops; Alcotest.test_case "type-name affinity" `Quick Doc.test_affinity_of_type; Alcotest.test_case "column affinity" `Quick Doc.test_apply_affinity; Alcotest.test_case "CAST" `Quick Doc.test_cast; Alcotest.test_case "numeric coercions" `Quick Doc.test_numeric_coercions; Alcotest.test_case "text that is not a number" `Quick Doc.test_text_that_is_not_a_number; Alcotest.test_case "the numeric gate on a function argument" `Quick Doc.test_numeric_type; Alcotest.test_case "text read as a C string" `Quick Doc.test_c_text_of_value; Alcotest.test_case "collated comparison" `Quick Doc.test_collated_comparison; Alcotest.test_case "text conversion" `Quick Doc.test_text_of_value; ] )