Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180open Sql.Encoding
let encoding = Alcotest.testable pp equal
let hex s = String.concat "" (List.map (fun c -> Fmt.str "%02X" (Char.code c)) (String.to_seq s |> List.of_seq))
let unhex h = String.init (String.length h / 2) (fun i -> Char.chr (int_of_string ("0x" ^ String.sub h (2 * i) 2)))
(* The names PRAGMA encoding takes (pragma.c, encnames), in any case; the bare UTF-16 names are the machine's own byte order. *)let test_of_string () = let check name want = Alcotest.(check (option encoding)) name want (of_string name) in check "UTF8" (Some Utf8); check "utf-8" (Some Utf8); check "UTF-16le" (Some Utf16le); check "utf16LE" (Some Utf16le); check "UTF-16BE" (Some Utf16be); check "UTF16be" (Some Utf16be); check "UTF-16" (Some (if Sys.big_endian then Utf16be else Utf16le)); check "utf16" (Some (if Sys.big_endian then Utf16be else Utf16le)); check "UTF-32" None; Alcotest.(check (list string)) "the names PRAGMA encoding answers" [ "UTF-8"; "UTF-16le"; "UTF-16be" ] (List.map to_string [ Utf8; Utf16le; Utf16be ])
(* Header offset 56 (fileformat2.html sec.1.3.13): 1, 2 or 3, read by its low two bits with anything but 2 and 3 UTF-8. *)let test_code () = Alcotest.(check (list int)) "codes" [ 1; 2; 3 ] (List.map code [ Utf8; Utf16le; Utf16be ]); List.iter (fun (n, want) -> Alcotest.check encoding (Fmt.str "of_code %d" n) want (of_code n)) [ (0, Utf8); (1, Utf8); (2, Utf16le); (3, Utf16be); (6, Utf16le); (7, Utf16be); ]
(* UTF-8 to UTF-16, as sqlite3 3.54.1 converts a literal into a UTF-16 database: hex(CAST(s AS BLOB)) there, for [s] the UTF-8 bytes on the left. A lead byte takes every continuation byte after it, an overlong form, a stray 0xFF and a UTF-8 surrogate become U+FFFD, U+FFFE and U+FFFF are kept, a lone continuation byte is the character of its own value, and a character past U+10FFFF is written as the units its formula makes. *)let test_of_utf8 () = List.iter (fun (input, le, be) -> let s = unhex input in Alcotest.(check string) ("UTF-16le of " ^ input) le (hex (of_utf8 Utf16le s)); Alcotest.(check string) ("UTF-16be of " ^ input) be (hex (of_utf8 Utf16be s)); Alcotest.(check string) ("UTF-8 of " ^ input) input (hex (of_utf8 Utf8 s))) [ ( "FF41C080EDA080EFBFBEF4908080E282", "FDFF4100FDFFFDFFFEFF00D800DC8200", "FFFD0041FFFDFFFDFFFED800DC000082" ); ("F7BFBFBFBFBFBF", "BFDBFFDF", "DBBFDFFF"); ("EFBFBF", "FFFF", "FFFF"); ("80BF", "8000BF00", "008000BF"); ("F09F9880", "3DD800DE", "D83DDE00"); ("FE8080", "FDFF", "FFFD"); ("", "", ""); ]
(* UTF-16 to UTF-8, as sqlite3 3.54.1 hands out a UTF-16 database's text: CAST(x'..' AS TEXT) there, printed. A surrogate takes the unit after it as its pair whatever that unit is, a lone one at the end is the character it names, and an odd last byte is dropped. Whatever was read, the string read gives its bytes back (sqlite3 copies a value it never converted); the same text spelt anew converts as text. *)let test_to_utf8 () = let check e stored want = let s = unhex stored in let read = to_utf8 e s 0 (String.length s) in Alcotest.(check string) (Fmt.str "%a %s" pp e stored) want (hex read); Alcotest.(check string) (Fmt.str "%a %s written back" pp e stored) stored (hex (of_utf8 e read)) in check Utf16le "00D8" "EDA080"; check Utf16le "00D84100" "F0908181"; check Utf16le "00DC4100" "F0908181"; check Utf16le "41004200" "4142"; check Utf16le "4100FF" "41"; check Utf16le "610062" "61"; check Utf16le "3DD800DE" "F09F9880"; check Utf16le "00DC00D8" "F0908080"; check Utf16be "D800" "EDA080"; check Utf16be "D8000041" "F0908181"; check Utf16be "DC000041" "F0908181"; check Utf16be "D83DDE00" "F09F9880"; check Utf16be "006162" "61"; Alcotest.(check string) "the same text spelt anew converts" "00D841DC" (hex (of_utf8 Utf16le (Bytes.to_string (Bytes.of_string "\xf0\x90\x81\x81")))); Alcotest.(check string) "a slice" "B" (to_utf8 Utf16le "A\000B\000C\000" 2 2); Alcotest.check_raises "a slice outside its string" (Invalid_argument "Sql.Encoding.to_utf8: a slice outside its string") (fun () -> ignore (to_utf8 Utf16be "ab" 1 2 : string))
(* Every inexact read keeps its bytes for as long as its text lives, however many there are and however often the collector runs: a statement holds the texts of every row it rewrites before it writes one. *)let test_many_inexact_reads () = let stored i = Fmt.str "%c%c\x00\xd8A\x00" (Char.chr (i land 0xff)) (Char.chr (i lsr 8)) in let reads = Array.init 20_000 (fun i -> let s = stored i in to_utf8 Utf16le s 0 (String.length s)) in Gc.full_major (); Array.iteri (fun i read -> if of_utf8 Utf16le read <> stored i then Alcotest.failf "read %d written back as %s" i (hex (of_utf8 Utf16le read))) reads
let sign n = Int.compare n 0
(* BINARY compares the stored bytes (sqlite3 binCollFunc, memcmp), so a UTF-16le database sorts 'a' (61 00) after U+0100 (00 01), and UTF-16be a surrogate pair (D800 DC00) before U+E000, against UTF-8's order of both. *)let test_compare_text () = let a = "a" and a_macron = "\xc4\x80" in let e000 = "\xee\x80\x80" and u10000 = "\xf0\x90\x80\x80" in Alcotest.(check int) "UTF-8: a < U+0100" (-1) (sign (compare_text Utf8 a a_macron)); Alcotest.(check int) "UTF-16le: a > U+0100" 1 (sign (compare_text Utf16le a a_macron)); Alcotest.(check int) "UTF-16be: a < U+0100" (-1) (sign (compare_text Utf16be a a_macron)); Alcotest.(check int) "UTF-8: U+E000 < U+10000" (-1) (sign (compare_text Utf8 e000 u10000)); Alcotest.(check int) "UTF-16be: U+E000 > U+10000" 1 (sign (compare_text Utf16be e000 u10000)); Alcotest.(check int) "a prefix sorts first" (-1) (sign (compare_text Utf16le "ab" "abc"))
let suite = ( "encoding", [ Alcotest.test_case "of_string / to_string" `Quick test_of_string; Alcotest.test_case "code / of_code" `Quick test_code; Alcotest.test_case "of_utf8 as sqlite3 converts" `Quick test_of_utf8; Alcotest.test_case "to_utf8 as sqlite3 converts" `Quick test_to_utf8; Alcotest.test_case "every inexact read written back" `Quick test_many_inexact_reads; Alcotest.test_case "compare_text is the stored order" `Quick test_compare_text; ] )