1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
(* $Id: test-base.ml,v 1.18 2006/08/13 21:23:08 yori Exp $ *)
(* Copyright 2002 Yamagata Yoriyuki *)
open Printf
open Blender
open Camomile
open UPervasives
module Random = Random4
(*
let random_pair () =
let a0 = Random.int 0x8000000 in
let b0 = Random.int 0x8000000 in
(min a0 b0, max a0 b0)
*)
(* Tests for UChar *)
let _ =
random_test ~desc:"char <-> uchar" ~log:"char_uchar"
~data:(fun _ -> Char.chr (Random.int 0x100))
~body:(fun c ->
expect_pass ~body:(fun () ->
let u = UChar.of_char c in
let c' = UChar.char_of u in
expect_equal c c'))
let _ =
random_test ~desc:"int <-> uchar" ~log:"int_uchar"
~data:(fun _ ->
match Random.int 3 with
| 0 -> Random.int 0xd800
| 1 -> 0xe000 + Random.int 8190
| 2 -> 0x10000 + Random.int 1048576
| _ -> assert false)
~body:(fun n ->
expect_pass ~body:(fun () ->
let u = UChar.chr n in
let n' = UChar.code u in
expect_equal n n'))
let _ =
random_test ~desc:"uint <-> uchar" ~log:"uint_uchar"
~data:(fun _ -> Random.int 0x8000000)
~body:(fun n ->
expect_pass ~body:(fun () ->
let u = UChar.chr_of_uint n in
let n' = UChar.uint_code u in
expect_equal n n'))
let _ =
test ~desc:"comparison" ~body:(fun () ->
expect_pass ~body:(fun () ->
let u1 = uchar_of_int 0x00 in
let u2 = uchar_of_int 0x300 in
let u3 = uchar_of_int 0x40000 in
let u4 = uchar_of_int 0x3ffffff in
let u5 = uchar_of_int 0x7ffffff in
expect_true ~msg:(lazy "u1 >= u2") (UChar.compare u1 u2 < -1);
expect_true ~msg:(lazy "u2 >= u3") (UChar.compare u2 u3 < -1);
expect_true ~msg:(lazy "u3 >= u4") (UChar.compare u3 u4 < -1);
expect_true ~msg:(lazy "u4 >= u5") (UChar.compare u4 u5 < -1)))
(* Tests for USet, UMap *)
module StdUSet = Set.Make (UChar)
module StdUMap = Map.Make (UChar)
let random_uchars size =
let r = ref StdUSet.empty in
for _ = 1 to size / 2 do
r := StdUSet.add (uchar_of_int (Random.int 0x10000)) !r
done;
for _ = 1 to size - (size / 2) do
r := StdUSet.add (uchar_of_int (Random.int 0x8000000)) !r
done;
!r
let uset_of_stdset us = StdUSet.fold USet.add us USet.empty
(*
let test_uset ~desc uset us =
StdUSet.iter (fun u ->
let n = int_of_uchar u in
expect_true
~msg:(lazy (sprintf "\\u%08x is missing in %s" n desc))
(USet.mem u uset);
let u = uchar_of_int (n + 1) in
expect_equal_app
~msg:(lazy (sprintf
"u in us1 is %b but u in uset1 is %b for u = \\u%08x in %s"
(StdUSet.mem u us)
(USet.mem u uset)
(n + 1)
desc))
(StdUSet.mem u) us
(USet.mem u) uset;
let u = uchar_of_int (n - 1) in
expect_equal_app
~msg:(lazy (sprintf
"u in us1 is %b but u in uset1 is %b for u = \\u%08x in %s"
(StdUSet.mem u us)
(USet.mem u uset)
(n - 1)
desc))
(StdUSet.mem u) us
(USet.mem u) uset)
us
*)
(*
let test_range ~desc uset =
let a, b = random_pair () in
let uset' = USet.add_range (uchar_of_int a) (uchar_of_int b) uset in
for i = a to b do
expect_true
~msg:(lazy (sprintf "\\u%08x is missing in %s" i desc))
(USet.mem (uchar_of_int i) uset')
done;
USet.iter
(fun u ->
let i = int_of_uchar u in
expect_true
~msg:(lazy (sprintf "\\u%08x is missing in %s" i desc))
(USet.mem u uset'))
uset;
USet.iter (fun u ->
let i = int_of_uchar u in
expect_true
~msg:(lazy (sprintf "\\u%08x is in %s but souldn't" i desc))
(USet.mem u uset || a <= i && i <= b))
uset'
*)
(* let _ = *)
(* random_test *)
(* ~desc:"USet" *)
(* ~log:"uset" *)
(* ~data:(fun size -> (random_uchars size, random_uchars size)) *)
(* ~body:(fun (us1, us2) -> expect_pass (fun () -> *)
(* let uset1 = uset_of_stdset us1 in *)
(* test_uset ~desc:"uset1" uset1 us1; *)
(* let uset2 = uset_of_stdset us2 in *)
(* test_uset ~desc:"uset2" uset2 us2; *)
(* test_uset ~desc:"union" *)
(* (USet.union uset1 uset2) *)
(* (StdUSet.union us1 us2); *)
(* test_uset ~desc:"inter" *)
(* (USet.inter uset1 uset2) *)
(* (StdUSet.inter us1 us2); *)
(* test_uset ~desc:"diff" *)
(* (USet.diff uset1 uset2) *)
(* (StdUSet.diff us1 us2); *)
(* test_range ~desc:"range" uset1)) *)
let random_umap size =
let r = ref StdUMap.empty in
for _ = 1 to size / 2 do
let v = 1.0 /. float_of_int (Random.int 10) in
r := StdUMap.add (uchar_of_int (Random.int 0x10000)) v !r
done;
for _ = 1 to size - (size / 2) do
let v = 1.0 /. float_of_int (Random.int 10) in
r := StdUMap.add (uchar_of_int (Random.int 0x8000000)) v !r
done;
!r
let umap_of_stdmap m = StdUMap.fold UMap.add m UMap.empty
let _ =
random_test ~desc:"UMep" ~log:"umap"
~data:(fun size -> random_umap size)
~body:(fun m ->
expect_pass ~body:(fun () ->
let umap = umap_of_stdmap m in
StdUMap.iter
(fun u _ ->
let n = int_of_uchar u in
expect_equal_app (StdUMap.find u) m (UMap.find u) umap;
let u = uchar_of_int (n + 1) in
expect_equal_app (StdUMap.find u) m (UMap.find u) umap;
let u = uchar_of_int (n - 1) in
expect_equal_app (StdUMap.find u) m (UMap.find u) umap)
m))
(* Interval Association List *)
(*
let rec assoc k al =
match al with
[] -> raise Not_found
| (k1, k2, v) :: r -> if k1 <= k && k <= k2 then v else assoc k r
let rec random_assoc size =
if size <= 0 then [] else
let a, b = random_pair () in
(a, b, Random.int 16) :: random_assoc (size - 1)
let umap_of_assoc al =
let al = List.rev al in
let rec f al umap =
match al with
[] -> umap
| (k1, k2, v) :: r ->
f r (UMap.add_range (UChar.chr k1) (UChar.chr k2) v umap)
in
f al UMap.empty
*)
(* let _ = *)
(* random_test *)
(* ~desc:"UMep interval" *)
(* ~log:"umap interval" *)
(* ~data:(fun size -> random_assoc size) *)
(* ~body:(fun al -> expect_pass (fun () -> *)
(* let umap = umap_of_assoc al in *)
(* for i = 0 to 0x8000000 do *)
(* expect_equal_app *)
(* (UMap.find (UChar.chr i)) umap *)
(* (assoc i) al *)
(* done)) *)
(* domain, set_to_map, map_to_set *)
(* let _ = *)
(* random_test *)
(* ~desc:"UMap <-> USet" *)
(* ~log:"UMap <-> USet" *)
(* ~data:(fun size -> random_assoc size) *)
(* ~body:(fun al -> expect_pass (fun () -> *)
(* let umap = umap_of_assoc al in *)
(* let dom = UMap.domain umap in *)
(* let umap' = UMap.set_to_map dom 1 in *)
(* let umap'' = UMap.map (fun _ -> 1) umap in *)
(* for i = 0 to 0x8000000 do *)
(* expect_equal_app *)
(* (UMap.find (UChar.chr i)) umap' *)
(* (UMap.find (UChar.chr i)) umap'' *)
(* done)) *)
(* Tests for UCharTbl *)
let test_tbl utbl uset exc =
USet.iter
(fun u ->
expect_true
~msg:(lazy (sprintf "\\u%08x is missing" (int_of_uchar u)))
(UCharTbl.Bool.get utbl u))
uset;
for _ = 0 to 100 do
let u = uchar_of_int (Random.int 0x8000000) in
expect_equal_app
~msg:
(lazy
(sprintf "%s for UCharTbl while %s for USet for \\u%0x08"
(if UCharTbl.Bool.get utbl u then "true" else "false")
(if USet.mem u uset then "true" else "false")
(int_of_uchar u)))
(UCharTbl.Bool.get utbl) u
(fun u -> USet.mem u uset)
u
done;
List.iter
(fun u ->
expect_true
~msg:(lazy (sprintf "\\u%08x is not removed" (int_of_uchar u)))
(not (UCharTbl.Bool.get utbl u)))
exc
let _ =
random_test ~desc:"UCharTbl.Bool" ~log:"uchartbl_bool"
~data:(fun size -> uset_of_stdset (random_uchars size))
~body:(fun uset ->
expect_pass ~body:(fun () ->
let utbl = UCharTbl.Bool.of_set uset in
test_tbl utbl uset []))
module FHash = struct
type t = float
let equal = ( = )
let hash = Hashtbl.hash
end
module Tbl = UCharTbl.Make (FHash)
let test_map tbl umap u =
let v1 = try UMap.find u umap with Not_found -> 0.0 in
let v2 = Tbl.get tbl u in
expect_equal ~msg:(lazy (sprintf "%f in map while %f in tbl" v1 v2)) v1 v2
let _ =
random_test ~desc:"UCharTbl" ~log:"uchartbl"
~data:(fun size -> umap_of_stdmap (random_umap size))
~body:(fun umap ->
expect_pass ~body:(fun () ->
let tbl = Tbl.of_map 0.0 umap in
UMap.iter
(fun u _ ->
test_map tbl umap u;
let n = int_of_uchar u in
let u = uchar_of_int (n + 1) in
test_map tbl umap u;
let u = uchar_of_int (n - 1) in
test_map tbl umap u)
umap))
(* Tests for UText *)
let _ =
random_test ~desc:"UText.of_string" ~log:"base_utext_of_string"
~data:(fun size ->
let s = Bytes.create size in
for i = 0 to size - 1 do
Bytes.set s i (Char.chr (Random.int 0x100))
done;
Bytes.to_string s)
~body:(fun s ->
expect_pass ~body:(fun () ->
let text = UText.of_string s in
expect_equal (String.length s) (UText.length text);
for i = 0 to String.length s - 1 do
let c1 = s.[i] in
let c2 = UChar.char_of (UText.get text i) in
expect_equal ~msg:(lazy (sprintf "location %d" i)) c1 c2
done))
let _ =
random_test ~desc:"UText.make" ~log:"base_utext_make"
~data:(fun size -> (Random.int 0x8000000, size))
~body:(fun (n, size) ->
expect_pass ~body:(fun () ->
let u = uchar_of_int n in
let text = UText.make size u in
for i = 0 to size - 1 do
let n' = int_of_uchar (UText.get text i) in
expect_equal
~msg:(lazy (sprintf "location %d: %04x %04x" i n n'))
n n'
done))
module UTextTest = UStorageTest.Make ((UText : UnicodeString.Type))
let _ = UTextTest.test ~desc:"UText test" ~log:"base_utext" ()
let _ =
random_test ~desc:"UText.ustring" ~log:"base_UText_ustring"
~data:(fun size ->
Array.init size (fun _ -> uchar_of_int (Random.int 0x8000000)))
~body:(fun a ->
expect_pass ~body:(fun () ->
(* tests for ustring *)
let s = UText.make (Array.length a) (uchar_of_int 0) in
expect_equal ~msg:(lazy "create") (UText.length s) (Array.length a);
for i = 0 to UText.length s - 1 do
UText.set s i a.(i)
done;
for i = 0 to UText.length s - 1 do
expect_equal
~msg:(lazy (sprintf "ustring set/get: location %d" i))
(UText.get s i) a.(i)
done;
let r = ref 0 in
UText.iter
(fun u ->
expect_equal
~msg:(lazy (sprintf "ustring iter: location %d" !r))
u a.(!r);
incr r)
s;
expect_equal
~msg:(lazy (sprintf "ustring iter: count %d" !r))
!r (UText.length s);
let cur = ref (UText.nth s 0) in
let r = ref 0 in
while not (UText.out_of_range s !cur) do
expect_equal
~msg:(lazy (sprintf "ustring cursor get: location %d" !r))
(UText.look s !cur) a.(!r);
cur := UText.next s !cur;
incr r
done;
expect_equal
~msg:(lazy (sprintf "ustring index: count %d" !r))
!r (UText.length s);
for _ = 0 to 100 do
let pos = Random.int (UText.length s) in
let cur = UText.nth s pos in
expect_equal
~msg:(lazy (sprintf "ustring nth: location %d" pos))
(UText.look s cur) a.(pos);
if pos = 0 then ()
else (
let cur' = UText.prev s cur in
expect_equal_app
~msg:(lazy (sprintf "ustring cursor next : location %d" pos))
(fun () -> UText.look s cur')
()
(fun i -> a.(i))
(pos - 1);
if pos = UText.length s - 1 then ()
else (
let cur' = UText.next s cur in
expect_equal_app
~msg:(lazy (sprintf "ustring cursor next : location %d" pos))
(fun () -> UText.look s cur')
()
(fun i -> a.(i))
(pos + 1)))
done))
let _ =
random_test ~desc:"UText.*" ~log:"base_utext"
~data:(fun size ->
Array.init size (fun _ -> uchar_of_int (Random.int 0x8000000)))
~body:(fun a ->
expect_pass ~body:(fun () ->
(* tests for ustring *)
let s = UText.make (Array.length a) (uchar_of_int 0) in
for i = 0 to UText.length s - 1 do
UText.set s i a.(i)
done;
let s' = UText.copy s in
for i = 0 to UText.length s - 1 do
expect_equal
~msg:(lazy (sprintf "copy: location %d" i))
(UText.get s i) (UText.get s' i)
done;
let pos = Random.int (UText.length s) in
let len = Random.int (UText.length s - pos + 1) in
let s' = UText.sub s pos len in
for i = 0 to len - 1 do
expect_equal
~msg:
(lazy
(sprintf "sub for ustring: location %d from %d of length %d" i
pos len))
(UText.get s (pos + i))
(UText.get s' i)
done;
let s' = UText.copy s in
let u = uchar_of_int (Random.int 0x8000000) in
let pos = Random.int (UText.length s) in
let len = Random.int (UText.length s - pos + 1) in
UText.fill s' pos len u;
for i = 0 to len - 1 do
expect_equal
~msg:
(lazy
(sprintf "fill: location %d from %d of length %d" i pos len))
(UText.get s' (pos + i))
u
done;
let len' = Random.int (UText.length s) + UText.length s in
let s' = UText.make len' (uchar_of_int 0) in
let pos = Random.int (UText.length s' - UText.length s + 1) in
UText.blit s 0 s' pos (UText.length s);
for i = 0 to UText.length s - 1 do
expect_equal
~msg:(lazy (sprintf "blit: location %d from %d" i pos))
(UText.get s' (pos + i))
(UText.get s i)
done;
let pos = Random.int (UText.length s) in
let s1 = UText.sub s 0 pos in
let s2 = UText.sub s pos (UText.length s - pos) in
let s' = UText.append s1 s2 in
for i = 0 to UText.length s - 1 do
expect_equal
~msg:(lazy (sprintf "append %d: location %d" pos i))
(UText.get s i) (UText.get s' i)
done;
(* tests for utext *)
let s = UText.utext_of_ustring s in
let pos = Random.int (UText.length s) in
let len = Random.int (UText.length s - pos + 1) in
let s' = UText.sub s pos len in
for i = 0 to len - 1 do
expect_equal
~msg:
(lazy
(sprintf "sub for ustring: location %d from %d of length %d" i
pos len))
(UText.get s (pos + i))
(UText.get s' i)
done;
let pos = Random.int (UText.length s) in
let s1 = UText.sub s 0 pos in
let s2 = UText.sub s pos (UText.length s - pos) in
let s' = UText.append s1 s2 in
for i = 0 to UText.length s - 1 do
expect_equal
~msg:(lazy (sprintf "append %d: location %d" pos i))
(UText.get s i) (UText.get s' i)
done))
(* Tests for XString *)
module XStringTest = UStorageTest.Make ((XString : UnicodeString.Type))
let _ = XStringTest.test ~desc:"XString" ~log:"base_xstring" ()
let _ =
random_test ~desc:"XString extra" ~log:"base_xstring_extra"
~data:(fun size ->
Array.init size (fun _ -> uchar_of_int (Random.int 0x8000000)))
~body:(fun a ->
expect_pass ~body:(fun () ->
let size = Array.length a in
(* tests for ustring *)
let s = XString.make 0 (uchar_of_int 0) in
for i = 0 to size - 1 do
XString.set s i a.(i)
done;
expect_equal
~msg:
(lazy (sprintf "xstring length: %d %d" (XString.length s) size))
(XString.length s) size;
for i = 0 to size - 1 do
expect_equal
~msg:(lazy (sprintf "xstring set/get: location %d" i))
(XString.get s i) a.(i)
done;
let s' = XString.copy s in
for i = 0 to size - 1 do
expect_equal
~msg:(lazy (sprintf "xstring copy: location %d" i))
(XString.get s i) (XString.get s' i)
done;
let pos = Random.int size in
let len = Random.int (size - pos + 1) in
let s' = XString.sub s pos len in
for i = 0 to len - 1 do
expect_equal
~msg:
(lazy
(sprintf "xstring sub: location %d from %d of length %d" i pos
len))
(XString.get s (pos + i))
(XString.get s' i)
done;
let s'' = XString.append (XString.sub s 0 pos) s' in
for i = 0 to pos + len - 1 do
expect_equal
~msg:
(lazy
(sprintf "xstring append: appended in %d, location %d" pos i))
(XString.get s'' i) (XString.get s i)
done;
let r = ref 0 in
XString.iter
(fun u ->
expect_equal
~msg:(lazy (sprintf "xstring iter: location %d" !r))
u a.(!r);
incr r)
s;
expect_equal ~msg:(lazy (sprintf "xstring iter: count %d" !r)) !r size))
(* Tests for SubText *)
module SubTextTest (Text : UnicodeString.Type) = struct
module SubText = SubText.Make (Text)
module Test1 = UStorageTest.Make (SubText)
let test ~desc ~log =
Test1.test ~desc:(desc ^ ": test1") ~log:(log ^ "_test1") ();
random_test ~desc:(desc ^ ": test2") ~log:(log ^ "_test2")
~data:(fun size ->
Text.init size (fun _ -> uchar_of_int (Random.int 0x8000000)))
~body:(fun t ->
expect_pass ~body:(fun () ->
let len = Text.length t in
let pos = Random.int len in
let len' = Random.int (len - pos) in
let cur = Text.nth t pos in
let cur' = Text.move t cur len' in
let sub = SubText.refer t cur cur' in
let r1 = ref cur in
let r2 = ref (SubText.nth sub 0) in
for i = 0 to len' - 1 do
expect_equal
~msg:(lazy (sprintf "sub: location %d" i))
(Text.look t !r1) (SubText.look sub !r2);
r1 := Text.next t !r1;
r2 := SubText.next sub !r2
done;
let t' = SubText.excerpt sub in
let r1 = ref cur in
let r2 = ref (Text.nth t' 0) in
for i = 0 to len' - 1 do
expect_equal
~msg:(lazy (sprintf "sub excerpt: location %d" i))
(Text.look t !r1) (Text.look t' !r2);
r1 := Text.next t !r1;
r2 := Text.next t' !r2
done))
end
module SubUText = SubTextTest (UText)
let _ = SubUText.test ~desc:"subutext" ~log:"base_subutext"
module SubXString = SubTextTest (XString)
let _ = SubXString.test ~desc:"subxstring" ~log:"base_subxstring"
module SubUTF8 = SubTextTest (UTF8)
let _ = SubUTF8.test ~desc:"subutf8" ~log:"base_subutf8"
let () = Blender.main ()
|