| 1 |
(* Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc. *) |
| 2 |
(* This file is part of GNU Modula-2. |
| 3 |
|
| 4 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 5 |
the terms of the GNU General Public License as published by the Free |
| 6 |
Software Foundation; either version 2, or (at your option) any later |
| 7 |
version. |
| 8 |
|
| 9 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 |
for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 16 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) |
| 17 |
|
| 18 |
MODULE realconv ; |
| 19 |
|
| 20 |
FROM RealConversions IMPORT RealToString, StringToReal ; |
| 21 |
FROM libc IMPORT exit, printf ; |
| 22 |
FROM SYSTEM IMPORT ADR ; |
| 23 |
FROM StrLib IMPORT StrEqual ; |
| 24 |
|
| 25 |
|
| 26 |
PROCEDURE Assert (v: BOOLEAN; f: ARRAY OF CHAR; l: CARDINAL; e: ARRAY OF CHAR) ; |
| 27 |
BEGIN |
| 28 |
IF v |
| 29 |
THEN |
| 30 |
printf("successfully evaluated assertion (%s)\n", ADR(e)) |
| 31 |
ELSE |
| 32 |
printf("%s:%d assertion failed when evaluating %s\n", ADR(f), l, ADR(e)) ; |
| 33 |
res := 1 ; |
| 34 |
exit(res) |
| 35 |
END |
| 36 |
END Assert ; |
| 37 |
|
| 38 |
|
| 39 |
VAR |
| 40 |
d : REAL ; |
| 41 |
l : LONGREAL ; |
| 42 |
res: INTEGER ; |
| 43 |
a : ARRAY [0..100] OF CHAR ; |
| 44 |
ok : BOOLEAN ; |
| 45 |
BEGIN |
| 46 |
res := 0 ; |
| 47 |
RealToString(100.0, 10, 10, a, ok) ; |
| 48 |
printf ("RealToString generates string: %s\n", a) ; |
| 49 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 50 |
printf("value returned is '%s'\n", ADR(a)) ; |
| 51 |
Assert(StrEqual('100.000000', a), __FILE__, __LINE__, 'testing return value of "100.000000"') ; |
| 52 |
RealToString(100.0, -5, 12, a, ok) ; |
| 53 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 54 |
printf("value returned is '%s'\n", ADR(a)) ; |
| 55 |
Assert(StrEqual(' 1.00000E+2', a), __FILE__, __LINE__, 'testing return value of " 1.00000E+2"') ; |
| 56 |
|
| 57 |
RealToString(123.456789, 10, 10, a, ok) ; |
| 58 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 59 |
printf("value returned is '%s'\n", ADR(a)) ; |
| 60 |
Assert(StrEqual('123.456789', a), __FILE__, __LINE__, 'testing return value of "123.456789"') ; |
| 61 |
RealToString(123.456789, -5, 13, a, ok) ; |
| 62 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 63 |
printf("value returned is '%s'\n", ADR(a)) ; |
| 64 |
Assert(StrEqual(' 1.23456E+2', a), __FILE__, __LINE__, 'testing return value of " 1.23456E+2"') ; |
| 65 |
|
| 66 |
RealToString(123.456789, -2, 15, a, ok) ; |
| 67 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 68 |
printf("value returned is '%s'\n", ADR(a)) ; |
| 69 |
Assert(StrEqual(' 1.23E+2', a), __FILE__, __LINE__, 'testing return value of " 1.23E+2"') ; |
| 70 |
|
| 71 |
StringToReal(' 1234567.89E-4', d, ok) ; |
| 72 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 73 |
printf('value returned is %f\n', d) ; |
| 74 |
|
| 75 |
RealToString(3.14159268, -6, 13, a, ok) ; |
| 76 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 77 |
printf("value returned is '%s'\n", ADR(a)) ; |
| 78 |
Assert(StrEqual(' 3.141592E+0', a), __FILE__, __LINE__, 'testing return value of " 3.141592E+0"') ; |
| 79 |
|
| 80 |
RealToString(12345.6789, 5, 20, a, ok) ; |
| 81 |
Assert(ok, __FILE__, __LINE__, 'testing ok return BOOLEAN') ; |
| 82 |
printf("value returned is '%s'\n", ADR(a)) ; |
| 83 |
Assert(StrEqual(' 12345.67890', a), __FILE__, __LINE__, 'testing return value of " 12345.67890"') ; |
| 84 |
|
| 85 |
exit(res) |
| 86 |
END realconv. |