Commit d8aeb62d authored by Michael Hanus 's avatar Michael Hanus
Browse files

Format tests extended

parent 6e9f049f
{-# OPTIONS_CYMAKE -F --pgmF=currypp --optF=foreigncode #-}
------------------------------------------------------------------------------
--- This program contains some tests for integrated code to format strings.
--- This program contains some examples for integrated code to support
--- string formatting.
---
--- The format specification C specification for `printf` formatting.
--- This specification may be found at
--- The format specification follows the C specification for `printf`
--- formatting whch may be found at
--- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>
------------------------------------------------------------------------------
......@@ -13,30 +14,39 @@ import Test.EasyCheck
-- Format a string and an integer:
ex1 :: String -> Int -> String
ex1 name age = ``format "Hello %s. Age %i!",name,age''
ex1 name age = ``format "Hello %s, you are %i years old.",name,age''
test_ex1 = (ex1 "World" 42) -=- "Hello World. Age 42!"
testEx1 = (ex1 "Mike" 42) -=- "Hello Mike, you are 42 years old."
-- Various integer formats:
test_intsigned = ``format "%+i",42'' -=- "+42"
-- Format two integers:
ex2 :: Int -> Int -> String
ex2 n1 n2 = ``format "%+.5d and % 10.4i",n1,n2''
test_int5 = ``format "%.5i",42'' -=- "00042"
testEx2 = (ex2 12345 34) -=- "+12345 and 0034"
test_intsigned5 = ``format "%+.5d",42'' -=- "+00042"
testInt5 = ``format "%.5i",42'' -=- "00042"
test_intfixedsigned5 = ``format "%+10.5i",42'' -=- " +00042"
testIntSigned5 = ``format "%+.5d",42'' -=- "+00042"
testIntFixedSigned5 = ``format "%+10.5i",42'' -=- " +00042"
-- Format a charater:
ex3 :: Char -> String
ex3 c = ``format "This is a char: %c",c''
testEx3 = (ex3 'a') -=- "This is a char: a"
-- Integer and character formatting:
test_intfixedsignedchar = ``format "%+5d%c",42,c'' -=- " +42%"
testIntFixedSignedChar = ``format "%+5d%c",42,c'' -=- " +42%"
where
c = '%'
-- Format a string with a given width and maximal length:
test_stringlength =
``format "This is a string: %08.4s",s'' -=- "This is a string: Hell"
where
s = "Hello!"
ex4 :: String -> String
ex4 s = ``format "This is a string: %010.4s!",s''
testEx4 = (ex4 "Hello") -=- "This is a string: Hell!"
-- Format with passing expressions:
ex8 :: Int -> Int -> String
ex8 n1 n2 = ``format "The sum of %+.5d and %+5i is %+6i.\n",n1,n2,n1+n2''
......@@ -44,11 +54,17 @@ ex8 n1 n2 = ``format "The sum of %+.5d and %+5i is %+6i.\n",n1,n2,n1+n2''
test_intexp = (ex8 42 2143) -=- "The sum of +00042 and +2143 is +2185.\n"
-- Format a float with a given width and precision:
test_floatfixprec3 = ``format "%+8.3f",f'' -=- " +3.142"
testFloatFixPrec3 = ``format "%+8.3f",f'' -=- " +3.142"
where
f = 3.14159
-- Format a float with an exponent:
test_floatexp = ``format "% .4E",f'' -=- " 3.1416E+02"
testFloatExp = ``format "% .4E",f'' -=- " 3.1416E+02"
where
f = 314.159
\ No newline at end of file
f = 314.159
-- Format the sum of two integers:
ex7 :: Int -> Int -> String
ex7 n1 n2 = ``format "The sum of %+.5d and %+5i is %+6i",n1,n2,n1+n2''
testEx7 = (ex7 1234 890) -=- "The sum of +01234 and +890 is +2124"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment