Source

ocaml-core / base / core / lib_test / core_int_test.ml

open Core.Std
open OUnit

let () = Random.self_init ()
let esc_test i = int_of_string (Int.to_string_hum i) = i

let bound = 2 lsl 10

let rand () =
  let rec aux acc cnt =
    if cnt = 0 then
      acc
    else
      let bit = if Random.bool () then 1 else 0 in
      aux (2*acc + bit) (cnt -1)
  in
  let aval = aux 1 (Random.int (Sys.word_size - 3)) in
  if Random.bool () then
    - aval
  else
    aval
(* Random with a distribution favouring small ones*)

let test =
  "core_int" >:::
    [ "to_string_hum" >::
        (fun () ->
           "random" @? (
             List.init ~f:(fun _ -> rand ()) 10_000
           |! List.for_all ~f:esc_test
           );
           "max_int" @? esc_test max_int;
           "min_int" @? esc_test min_int
        )
    ]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.