2012-10-17 13:09:16 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
|
|
|
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2006 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. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2006-01-20 02:14:47 -08:00
|
|
|
open Testing;;
|
|
|
|
|
|
|
|
open Printf;;
|
|
|
|
|
2006-01-24 03:14:22 -08:00
|
|
|
(* Padding floating point numbers.
|
|
|
|
Testing * width specifications. *)
|
2006-01-20 02:14:47 -08:00
|
|
|
let test0 () =
|
|
|
|
sprintf "%.0f" 1.0 = "1" &&
|
|
|
|
sprintf "%.0f." 1.7 = "2." &&
|
|
|
|
sprintf "%.1f." 1.0 = "1.0." &&
|
|
|
|
sprintf "%0.1f." 12.0 = "12.0." &&
|
|
|
|
sprintf "%3.1f." 12.0 = "12.0." &&
|
|
|
|
sprintf "%5.1f." 12.0 = " 12.0." &&
|
|
|
|
sprintf "%10.1f." 12.0 = " 12.0." &&
|
|
|
|
sprintf "%010.1f." 12.0 = "00000012.0." &&
|
|
|
|
sprintf "% 10.1f." 12.0 = " 12.0." &&
|
|
|
|
sprintf "%+10.1f." 12.0 = " +12.0." &&
|
|
|
|
sprintf "%+10.1f." (-12.0) = " -12.0." &&
|
|
|
|
|
|
|
|
sprintf "%010.5f." 12.0 = "0012.00000." &&
|
|
|
|
sprintf "%010.0f." 12.0 = "0000000012." &&
|
|
|
|
sprintf "% 10.0f." 12.0 = " 12." &&
|
|
|
|
|
|
|
|
sprintf "%0.1f." 12.0 = "12.0." &&
|
|
|
|
sprintf "%10.1f." 1.001 = " 1.0." &&
|
|
|
|
sprintf "%05.1f." 1.001 = "001.0."
|
|
|
|
;;
|
|
|
|
|
|
|
|
test (test0 ());;
|
|
|
|
|
|
|
|
(* Padding integers (cf bug 3955).
|
|
|
|
Testing * width specifications. *)
|
|
|
|
let test1 () =
|
2006-01-24 03:14:22 -08:00
|
|
|
sprintf "%d\n" 1 = "1\n" &&
|
|
|
|
sprintf "%05d\n" 1 = "00001\n" &&
|
|
|
|
sprintf "%*d\n" 5 1 = " 1\n" &&
|
|
|
|
sprintf "%0*d\n" 5 1 = "00001\n";;
|
2006-01-20 02:14:47 -08:00
|
|
|
|
|
|
|
test (test1 ());;
|
2006-01-24 03:14:22 -08:00
|
|
|
|
2008-09-09 01:11:40 -07:00
|
|
|
(* FIXME: when positional specification will be OK. *)
|
|
|
|
let test2 () = true
|
|
|
|
(* sprintf "%1$d\n" 5 1 = " 1\n" &&
|
|
|
|
sprintf "%01$d\n" 5 1 = "00001\n" *);;
|
2012-07-30 11:04:46 -07:00
|
|
|
|
2006-01-24 03:14:22 -08:00
|
|
|
test (test2 ());;
|
|
|
|
|
|
|
|
(* Testing meta format string printing. *)
|
|
|
|
let test3 () =
|
|
|
|
sprintf "%{toto %s titi.\n%}" "Bonjour %s." = "%s" &&
|
|
|
|
sprintf "%{%d%s%}" "kk%dkk%s\n" = "%i%s";;
|
|
|
|
test (test3 ());;
|
|
|
|
|
|
|
|
(* Testing meta format string arguments. *)
|
|
|
|
let test4 () =
|
|
|
|
sprintf "%(%s%)" "Bonjour %s" "toto" = "Bonjour toto" &&
|
|
|
|
sprintf "%(%s%)" "Bonjour %s." "vous" = "Bonjour vous." &&
|
|
|
|
sprintf "%(%s%)" "Hello %s." "you" = "Hello you."
|
|
|
|
;;
|
|
|
|
|
|
|
|
test (test4 ());;
|
|
|
|
|
|
|
|
let test5 () =
|
|
|
|
sprintf "%(toto %s titi.\n%)"
|
|
|
|
"Bonjour %s." "vous" = "Bonjour vous." &&
|
|
|
|
sprintf "%(toto %s titi.\n%).\n"
|
|
|
|
"Bonjour %s" "toto" = "Bonjour toto.\n" &&
|
|
|
|
sprintf "%(toto %s titi.\n%)%s\n"
|
2012-10-17 13:09:16 -07:00
|
|
|
"Bonjour %s." "toto" " Ca va?" = "Bonjour toto. Ca va?\n"
|
2006-01-24 03:14:22 -08:00
|
|
|
;;
|
|
|
|
|
|
|
|
test (test5 ());;
|