<commit_before>(*************************************************************************)
(*                                                                       *)
(*                                 OCaml                                 *)
(*                                                                       *)
(*            <NAME>, projet Cristal, INRIA Rocquencourt            *)
(*                                                                       *)
(*   Copyright 2002 Institut National de Recherche en Informatique et    *)
(*   en Automatique.  All rights reserved.  This file is distributed     *)
(*   under the terms of the Q Public License version 1.0.                *)
(*                                                                       *)
(*************************************************************************)

(*

A testbed file for the module Scanf.

*)

open Testing;;

open Scanf;;

(* The ``continuation'' that returns the scanned value. *)
let id x = x;;

(* Testing space scanning. *)
let test0 () =
  (sscanf "" "" id) 1 +
  (sscanf "" " " id) 2 +
  (sscanf " " " " id) 3 +
  (sscanf "\t" " " id) 4 +
  (sscanf "\n" " " id) 5 +
  (sscanf "\n\t 6" " %d" id)
;;

test (test0 () = 21)
;;

(* Testing integer scanning %i and %d. *)
let test1 () =
  sscanf "1" "%d" id +
  sscanf " 2" " %d" id +
  sscanf " -2" " %d" id +
  sscanf " +2" " %d" id +
  sscanf " 2a " " %da" id
;;

test (test1 () = 5)
;;

let test2 () =
  sscanf "123" "%2i" id +
  sscanf "245" "%d" id +
  sscanf " 2a " " %1da" id
;;

test (test2 () = 259)
;;

let test3 () =
  sscanf "0xff" "%3i" id +
  sscanf "0XEF" "%3i" id +
  sscanf "x=-245" " x = %d" id +
  sscanf " 2a " " %1da" id
;;

test (test3 () = -214)
;;

(* Testing float scanning. *)
(* f style. *)
let test4 () =
  bscanf (Scanning.from_string "1")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string "-1")
    "%f" (fun b0 -> b0 = -1.0) &&
  bscanf (Scanning.from_string "+1")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string "1.")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string ".1")
    "%f" (fun b0 -> b0 = 0.1) &&
  bscanf (Scanning.from_string "-.1")
    "%f" (fun b0 -> b0 = -0.1) &&
  bscanf (Scanning.from_string "+.1")
    "%f" (fun b0 -> b0 = 0.1) &&
  bscanf (Scanning.from_string "+1.")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string "-1.")
    "%f" (fun b0 -> b0 = -1.0) &&
  bscanf (Scanning.from_string "0 1. 1.3")
    "%f %f %f" (fun b0 b1 b2 -> b0 = 0.0 && b1 = 1.0 && b2 = 1.3) &&
  bscanf (Scanning.from_string "0.113")
    "%4f" (fun b0 -> b0 = 0.11) &&
  bscanf (Scanning.from_string "0.113")
    "%5f" (fun b0 -> b0 = 0.113) &&
  bscanf (Scanning.from_string "000.113")
    "%15f" (fun b0 -> b0 = 0.113) &&
  bscanf (Scanning.from_string "+000.113")
    "%15f" (fun b0 -> b0 = 0.113) &&
  bscanf (Scanning.from_string "-000.113")
    "%15f" (fun b0 -> b0 = -0.113)
;;
test (test4 ())
;;

let same_float x y =
  let is_nan z = (z <> z) in
  if is_nan x then is_nan y
  else Int64.bits_of_float y = Int64.bits_of_float x
;;

(* e style. *)
let test5 () =
  bscanf (Scanning.from_string "1e1")
    "%e" (fun b -> b = 10.0) &&
  bscanf (Scanning.from_string "1e+1")
    "%e" (fun b -> b = 10.0) &&
  bscanf (Scanning.from_string "10e-1")
    "%e" (fun b -> b = 1.0) &&
  bscanf (Scanning.from_string "10.e-1")
    "%e" (fun b -> b = 1.0) &&
  bscanf (Scanning.from_string "1e1 1.e+1 1.3e-1")
    "%e %e %e" (fun b1 b2 b3 -> b1 = 10.0 && b2 = b1 && b3 = 0.13) &&

(* g style. *)
  bscanf (Scanning.from_string "1 1.1 0e+1 1.3e-1")
    "%g %g %g %g"
    (fun b1 b2 b3 b4 ->
     b1 = 1.0 && b2 = 1.1 && b3 = 0.0 && b4 = 0.13)
  &&
(* F style *)
  bscanf (Scanning.from_string "1.5 1.5e0 15e-1 0x1.8 0X1.8")
    "%F %F %f %F %F"
    (fun b1 b2 b3 b4 b5 -> b1 = b2 && b2 = b3 && b3 = b4 && b4 = b5)
  &&
(* h style *)
  begin
    let roundtrip x =
      bscanf (Printf.ksprintf Scanning.from_string "%h" x) "%h" (same_float x)
    in
    roundtrip (+0.) &&
    roundtrip (-0.) &&
    roundtrip (+1.) &&
    roundtrip (-1.) &&
    roundtrip (+1024.) &&
    roundtrip (-1024.) &&
    roundtrip 0X123.456 &&
    roundtrip 0X123456789ABCDE. &&
    roundtrip epsilon_float &&
    (* "nan" parsing fails on Windows? *)
    (Sys.win32 || roundtrip nan) &&
    roundtrip infinity &&
    roundtrip neg_infinity &&
    roundtrip (4. *. atan 1.) &&
    true
  end
  &&

  (* H style *)
  begin
    let roundtrip x =
      bscanf (Printf.ksprintf Scanning.from_string "%H" x) "%H" (same_float x)
    in
    roundtrip (+0.) &&
    roundtrip (-0.) &&
    roundtrip (+1.) &&
    roundtrip (-1.) &&
    roundtrip (+1024.) &&
    roundtrip (-1024.) &&
    roundtrip 0X123.456 &&
    roundtrip 0X123456789ABCDE. &&
    roundtrip epsilon_float &&
    (* "nan" parsing fails on Windows? *)
    (Sys.win32 || roundtrip nan) &&
    roundtrip infinity &&
    roundtrip neg_infinity &&
    roundtrip (4. *. atan 1.) &&
    true
  end
;;

test (test5 ())
;;

(* Testing boolean scanning. *)
let test6 () =
  bscanf (Scanning.from_string "truetrue") "%B%B"
         (fun b1 b2 -> (b1, b2) = (true, true))  &&
  bscanf (Scanning.from_string "truefalse") "%B%B"
         (fun b1 b2 -> (b1, b2) = (true, false)) &&
  bscanf (Scanning.from_string "falsetrue") "%B%B"
         (fun b1 b2 -> (b1, b2) = (false, true)) &&
  bscanf (Scanning.from_string "falsefalse") "%B%B"
         (fun b1 b2 -> (b1, b2) = (false, false)) &&
  bscanf (Scanning.from_string "true false") "%B %B"
         (fun b1 b2 -> (b1, b2) = (true, false))
;;

test (test6 ())
;;

(* Testing char scanning. *)

let test7 () =
  bscanf (Scanning.from_string "'a' '\n' '\t' '\000' '\032'")
         "%C %C %C %C %C"
    (fun c1 c2 c3 c4 c5 ->
       c1 = 'a' && c2 = '\n' && c3 = '\t' && c4 = '\000' && c5 = '\032') &&<commit_msg>float parsing: also disable {+,-}infinity testing on Windows<commit_after>(*************************************************************************)
(*                                                                       *)
(*                                 OCaml                                 *)
(*                                                                       *)
(*            <NAME>, projet Cristal, INRIA Rocquencourt            *)
(*                                                                       *)
(*   Copyright 2002 Institut National de Recherche en Informatique et    *)
(*   en Automatique.  All rights reserved.  This file is distributed     *)
(*   under the terms of the Q Public License version 1.0.                *)
(*                                                                       *)
(*************************************************************************)

(*

A testbed file for the module Scanf.

*)

open Testing;;

open Scanf;;

(* The ``continuation'' that returns the scanned value. *)
let id x = x;;

(* Testing space scanning. *)
let test0 () =
  (sscanf "" "" id) 1 +
  (sscanf "" " " id) 2 +
  (sscanf " " " " id) 3 +
  (sscanf "\t" " " id) 4 +
  (sscanf "\n" " " id) 5 +
  (sscanf "\n\t 6" " %d" id)
;;

test (test0 () = 21)
;;

(* Testing integer scanning %i and %d. *)
let test1 () =
  sscanf "1" "%d" id +
  sscanf " 2" " %d" id +
  sscanf " -2" " %d" id +
  sscanf " +2" " %d" id +
  sscanf " 2a " " %da" id
;;

test (test1 () = 5)
;;

let test2 () =
  sscanf "123" "%2i" id +
  sscanf "245" "%d" id +
  sscanf " 2a " " %1da" id
;;

test (test2 () = 259)
;;

let test3 () =
  sscanf "0xff" "%3i" id +
  sscanf "0XEF" "%3i" id +
  sscanf "x=-245" " x = %d" id +
  sscanf " 2a " " %1da" id
;;

test (test3 () = -214)
;;

(* Testing float scanning. *)
(* f style. *)
let test4 () =
  bscanf (Scanning.from_string "1")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string "-1")
    "%f" (fun b0 -> b0 = -1.0) &&
  bscanf (Scanning.from_string "+1")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string "1.")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string ".1")
    "%f" (fun b0 -> b0 = 0.1) &&
  bscanf (Scanning.from_string "-.1")
    "%f" (fun b0 -> b0 = -0.1) &&
  bscanf (Scanning.from_string "+.1")
    "%f" (fun b0 -> b0 = 0.1) &&
  bscanf (Scanning.from_string "+1.")
    "%f" (fun b0 -> b0 = 1.0) &&
  bscanf (Scanning.from_string "-1.")
    "%f" (fun b0 -> b0 = -1.0) &&
  bscanf (Scanning.from_string "0 1. 1.3")
    "%f %f %f" (fun b0 b1 b2 -> b0 = 0.0 && b1 = 1.0 && b2 = 1.3) &&
  bscanf (Scanning.from_string "0.113")
    "%4f" (fun b0 -> b0 = 0.11) &&
  bscanf (Scanning.from_string "0.113")
    "%5f" (fun b0 -> b0 = 0.113) &&
  bscanf (Scanning.from_string "000.113")
    "%15f" (fun b0 -> b0 = 0.113) &&
  bscanf (Scanning.from_string "+000.113")
    "%15f" (fun b0 -> b0 = 0.113) &&
  bscanf (Scanning.from_string "-000.113")
    "%15f" (fun b0 -> b0 = -0.113)
;;
test (test4 ())
;;

let same_float x y =
  let is_nan z = (z <> z) in
  if is_nan x then is_nan y
  else Int64.bits_of_float y = Int64.bits_of_float x
;;

(* e style. *)
let test5 () =
  bscanf (Scanning.from_string "1e1")
    "%e" (fun b -> b = 10.0) &&
  bscanf (Scanning.from_string "1e+1")
    "%e" (fun b -> b = 10.0) &&
  bscanf (Scanning.from_string "10e-1")
    "%e" (fun b -> b = 1.0) &&
  bscanf (Scanning.from_string "10.e-1")
    "%e" (fun b -> b = 1.0) &&
  bscanf (Scanning.from_string "1e1 1.e+1 1.3e-1")
    "%e %e %e" (fun b1 b2 b3 -> b1 = 10.0 && b2 = b1 && b3 = 0.13) &&

(* g style. *)
  bscanf (Scanning.from_string "1 1.1 0e+1 1.3e-1")
    "%g %g %g %g"
    (fun b1 b2 b3 b4 ->
     b1 = 1.0 && b2 = 1.1 && b3 = 0.0 && b4 = 0.13)
  &&
(* F style *)
  bscanf (Scanning.from_string "1.5 1.5e0 15e-1 0x1.8 0X1.8")
    "%F %F %f %F %F"
    (fun b1 b2 b3 b4 b5 -> b1 = b2 && b2 = b3 && b3 = b4 && b4 = b5)
  &&
(* h style *)
  begin
    let roundtrip x =
      bscanf (Printf.ksprintf Scanning.from_string "%h" x) "%h" (same_float x)
    in
    roundtrip (+0.) &&
    roundtrip (-0.) &&
    roundtrip (+1.) &&
    roundtrip (-1.) &&
    roundtrip (+1024.) &&
    roundtrip (-1024.) &&
    roundtrip 0X123.456 &&
    roundtrip 0X123456789ABCDE. &&
    roundtrip epsilon_float &&
    roundtrip (4. *. atan 1.) &&
    (Sys.win32 ||
     (* nan/infinity parsing fails on Windows? *)
     (roundtrip nan &&
      roundtrip infinity &&
      roundtrip neg_infinity)) &&
    true
  end
  &&

  (* H style *)
  begin
    let roundtrip x =
      bscanf (Printf.ksprintf Scanning.from_string "%H" x) "%H" (same_float x)
    in
    roundtrip (+0.) &&
    roundtrip (-0.) &&
    roundtrip (+1.) &&
    roundtrip (-1.) &&
    roundtrip (+1024.) &&
    roundtrip (-1024.) &&
    roundtrip 0X123.456 &&
    roundtrip 0X123456789ABCDE. &&
    roundtrip epsilon_float &&
    roundtrip (4. *. atan 1.) &&
    (Sys.win32 ||
     (* nan/infinity parsing fails on Windows? *)
     (roundtrip nan &&
      roundtrip infinity &&
      roundtrip neg_infinity)) &&
    true
  end
;;

test (test5 ())
;;

(* Testing boolean scanning. *)
let test6 () =
  bscanf (Scanning.from_string "truetrue") "%B%B"
         (fun b1 b2 -> (b1, b2) = (true, true))  &&
  bscanf (Scanning.from_string "truefalse") "%B%B"
         (fun b1 b2 -> (b1, b2) = (true, false)) &&
  bscanf (Scanning.from_string "falsetrue") "%B%B"
         (fun b1 b2 -> (b1, b2) = (false, true)) &&
  bscanf (Scanning.from_string "falsefalse") "%B%B"
         (fun b1 b2 -> (b1, b2) = (false, false)) &&
  bscanf (Scanning.from_string "true false") "%B %B"
         (fun b1 b2 -> (b1, b2) = (true, false))
;;

test (test6 ())
;;

(* Testing char scanning. *)

let test7 () =
  bscanf (Scanning.from_string "'a' '\n' '\t' '\000' '\032'")
         "%C %C %C %C %C"
    (fun c1 c2 c3 c4 c5 ->
       c1 = 'a' && c2 = '\n' && c3 = '\t' && c4 = '\000' && c5 = '\032') &&
{'fraction_non_alphanumeric': 0.21599931699820712, 'fraction_numerical': 0.06608042346111158, 'mean_word_length': 1.9951419074405523, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5268736', 'n_tokens_mistral': 5071, 'n_tokens_neox': 4688, 'n_words': 1257}