Skip to content

Commit 09c199a

Browse files
committed
Tests: Add some test for utf8/utf16 conversion
1 parent eabc645 commit 09c199a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

lib/tests/dune.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@
9999
(preprocess
100100
(pps ppx_js_internal ppx_expect)))
101101

102+
(library
103+
;; lib/tests/test_string.ml
104+
(name test_string_75)
105+
(enabled_if true)
106+
(modules test_string)
107+
(libraries js_of_ocaml unix)
108+
(inline_tests (modes js wasm))
109+
(preprocess
110+
(pps ppx_js_internal ppx_expect)))
111+
102112
(library
103113
;; lib/tests/test_sys.ml
104114
(name test_sys_75)

lib/tests/test_string.ml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
(* Js_of_ocaml tests
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
* Copyright (C) 2025 Hugo Heuzard
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
*)
19+
20+
open Js_of_ocaml
21+
22+
let%expect_test "test utf8-utf16 conversions" =
23+
let min = Uchar.to_int Uchar.min in
24+
let max = Uchar.to_int Uchar.max in
25+
let utf8 = Buffer.create (max * 4) in
26+
let utf16 = Buffer.create (max * 4) in
27+
let l = ref [] in
28+
for i = max downto min do
29+
if Uchar.is_valid i then l := Uchar.of_int i :: !l
30+
done;
31+
List.iter
32+
(fun u ->
33+
Buffer.add_utf_16be_uchar utf16 u;
34+
Buffer.add_utf_8_uchar utf8 u)
35+
!l;
36+
let utf8 = Buffer.contents utf8 in
37+
let utf16 = Buffer.contents utf16 in
38+
39+
let utf16' = Js.string utf8 in
40+
let rec loop i =
41+
if i / 2 >= utf16'##.length
42+
then assert (i = String.length utf16)
43+
else
44+
let u_js =
45+
utf16'##codePointAt (i / 2)
46+
|> Js.Optdef.to_option
47+
|> Option.get
48+
|> Js.to_float
49+
|> int_of_float
50+
in
51+
let u_ml, len =
52+
let d = String.get_utf_16be_uchar utf16 i in
53+
assert (Uchar.utf_decode_is_valid d);
54+
let len = Uchar.utf_decode_length d in
55+
Uchar.to_int (Uchar.utf_decode_uchar d), len
56+
in
57+
if u_js = u_ml
58+
then loop (i + len)
59+
else (
60+
Printf.eprintf "%x <> %x\n" u_js u_ml;
61+
Printf.eprintf "string differ at %x\n" i;
62+
assert false)
63+
in
64+
loop 0;
65+
let utf8' = Js.to_string utf16' in
66+
for i = 0 to String.length utf8 - 1 do
67+
if Char.equal (String.get utf8 i) (String.get utf8' i)
68+
then ()
69+
else (
70+
Printf.eprintf "%C <> %C\n" (String.get utf8 i) (String.get utf8' i);
71+
Printf.eprintf "string differ at %d\n" i;
72+
())
73+
done;
74+
[%expect {||}]

0 commit comments

Comments
 (0)