test_format.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
---------------------------> Sather 1.1 source file <--------------------------
-- test_format.sa: Formatting a string a la printf
-- Author: Benedict A. Gomes <gomes@icsi.berkeley.edu>
-- Author: Holger Klawitter <holger@icsi.berkeley.edu>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: test_format.sa,v 1.13 1996/08/30 21:59:33 borisv Exp $
-- For documentation see:
-- http://www.icsi.berkeley.edu/~holger/Sather/format.html
-- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
-- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
-- LICENSE contained in the file: Sather/Doc/License of the
-- Sather distribution. The license is also available from ICSI,
-- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
class TEST_FMT
class TEST_FMT
is
include TEST;
main is
class_name("FMT");
protect
test( "int",
#FMT("*%+3d+%-3d=%04d%%", 3, 4, 7),
"* +3+4 =0007%" );
test( "escaping",
#FMT( "%% %< <> %> %d", 3, 4 ), "% < 3 > 4" );
test( "flt and str",
#FMT("%10s is less than %8.3f", "(%)", 3.14159),
" (%) is less than 3.142" );
test( "char and fltd",
#FMT("%5c foo %g", 'x', 1.234d),
" x foo 1.234" );
test( "arg mixing",
#FMT("%2$d %1$d", 3, 4 ),
"4 3" );
test( "arg mixing args implicit order",
#FMT("%2$d %d %1$d %d", 1, 2, 3),
"2 3 1 2" );
FMT_ERROR::raise_exceptions := true;
raise_test( "wrong types error",
bind(FMT::create("%s",7)),
"INT needs \"diouxX\" but got 's'." );
raise_test( "end of format error I",
bind(FMT::create("%==")),
"Unexpected end of format in \"%==\"." );
raise_test( "end of format error II",
bind(FMT::create("+%")),
"Unexpected end of format in \"+%\"." );
raise_test( "bad argument number I",
bind(FMT::create("%1$d %3$d",1,2)),
"Illegal argument number '3'." );
raise_test( "bad argument number II",
bind(FMT::create("%2$d %d %d",1,2,3)),
"Illegal argument number '4'." );
test( "INT",
#FMT( "<###>+<^##>=<#^#>", 1, 2, 3 ),
" 1+2 = 3 " );
test( "FLT precision exhausing",
#FMT( "<###.##>=<####>=<1:+>*<3:#.##########>",
3.1551, 3.22222, 4.0/3.0 ),
" 3.16= 3=+3.1551*1.3333334000" );
test( "rounding fixed point",
#FMT( "<#.#>$<#.##>$<#.###>$<#.####>$<#.##>",
0.9995, 0.9995, 0.9996, 0.9995, 0.096 ),
"1.0$1.00$1.000$0.9995$0.10" );
test( "INT precision",
#FMT( "<###.##>$<.##>$<+###.>$<-###.>$<-.##>",
7, 8, 9, -10, -11 ),
" 7 $8 $ +9 $ -10 $-11 " );
raise_test( "using anchor and precision in INT",
bind(FMT::create("<^##.##>",7)),
"Decimal char and `^' cannot be used together."
);
test( "FLTD",
#FMT( "<-#.#######################>",
3.1234567890123456789012d ),
" 3.12345678901234570000000" );
test( "anchors",
#FMT( "<###^##>*<###^##>*<###^##>*<###^##>*<###^##>*<##^##>",
12,123,1234,12345,123456,12 ),
" 12 * 123 * 1234 * 12345*123456* 12 " );
test( "bases",
#FMT( "<hex####>*<bin####>*<oct####>", 140, 12, 130 ),
" 8C*1100* 202" );
-- test( "CPX",
-- #FMT( "<##.#> @<polar##.#;#.###> @<##.###;>",
-- #CPX(1.0,1.0),#CPX(0.0,1.0),#CPX(11.0,2.222) ),
-- " 1.0+ 1.0i @ 1.0*e^1.571i @11.000+2.222i" );
test( "string",
#FMT( "<>$<^##>$<F.###>$<F*##^##>", "xx", "yy", "yy", "zzz" ),
"xx$yy $.yy$*zzz*" );
test( "bool and char",
#FMT( "<#####^#>$<F^##>", false, '%' ),
" false$&%&&" );
test( "decimal dots",
#FMT( "<#> <.> <,> <#.> <#,>", 1.0, 2.0, 3.0, 4.0, 5.0 ),
"1 2.0 3,0 4. 5," );
FMT_ERROR::raise_exceptions := false; -- this is the default !
test( "returning string exceptions",
#FMT( "<> <^.> <>", 7,8,9 ),
"7 Decimal char and `^' cannot be used together. 9");
when $STR then
#ERR + "[" + exception.str + "]\n";
test("Happens never","true","false");
else
raise exception
end;
finish;
end; -- main
end; -- TEST_FMT