Skip to content

Commit 2a93b89

Browse files
author
Simon Cruanes
committed
compat test for Netencoding.Html.encode/encode_utf8
a bit of special cases, a bit of proptest
1 parent 03f34b5 commit 2a93b89

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

test.ml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,54 @@ let () = test "Web.htmlencode resize" @@ fun () ->
542542
"&&&&&&&&&&&";
543543
()
544544

545+
let () = test "Netencoding.Html.encode_utf8 agrees with generic encoder" @@ fun () ->
546+
let reference =
547+
Netencoding.Html.encode ~in_enc:`Enc_utf8 ~out_enc:`Enc_utf8 ()
548+
in
549+
let state = Random.State.make [| 0x51a7; 0x8f8 |] in
550+
let unsafe_chars = "<>\"&\000\001\127" in
551+
let rec random_scalar () =
552+
let p = Random.State.int state 0x110000 in
553+
if (p >= 0xd800 && p < 0xe000) || p = 0xfffe || p = 0xffff then
554+
random_scalar ()
555+
else
556+
p
557+
in
558+
let random_codepoint () =
559+
match Random.State.int state 10 with
560+
| 0 | 1 ->
561+
Char.code
562+
unsafe_chars.[Random.State.int state (String.length unsafe_chars)]
563+
| 2 | 3 | 4 | 5 -> Random.State.int state 128
564+
| _ -> random_scalar ()
565+
in
566+
let check case codepoints =
567+
let input = Netconversion.ustring_of_uarray `Enc_utf8 codepoints in
568+
assert_equal
569+
~msg:(sprintf "generated UTF-8 case %d (%d code points)" case
570+
(Array.length codepoints))
571+
(reference input)
572+
(Netencoding.Html.encode_utf8 input)
573+
in
574+
check 0
575+
[| 0x0000; 0x0001; 0x0022; 0x0026; 0x003c; 0x003e; 0x007f; 0x0080;
576+
0x07ff; 0x0800; 0xd7ff; 0xe000; 0xfffd; 0x10000; 0x10ffff |];
577+
let fixed_lengths = [ 0; 1; 2; 15; 16; 31; 32; 127; 249; 250; 251; 1000 ] in
578+
List.iteri
579+
(fun i len -> check (i + 1) (Array.init len (fun _ -> random_codepoint ())))
580+
fixed_lengths;
581+
for i = 0 to 199 do
582+
let len =
583+
match Random.State.int state 4 with
584+
| 0 -> Random.State.int state 17
585+
| 1 -> Random.State.int state 257
586+
| 2 -> 249 + Random.State.int state 3
587+
| _ -> Random.State.int state 1025
588+
in
589+
check (i + 1 + List.length fixed_lengths)
590+
(Array.init len (fun _ -> random_codepoint ()))
591+
done
592+
545593
let () = test "Web.urldecode" @@ fun () ->
546594
assert_equal (Web.urldecode "Hello+G%C3%BCnter") "Hello Günter";
547595
()

0 commit comments

Comments
 (0)