| 1 |
(* Copyright (C) 2009 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 sigfig ; |
| 19 |
|
| 20 |
FROM DynamicStrings IMPORT String, EqualArray, KillString, InitString, |
| 21 |
PushAllocation, PopAllocation ; |
| 22 |
|
| 23 |
FROM StringConvert IMPORT ToSigFig ; |
| 24 |
FROM StrIO IMPORT WriteString, WriteLn ; |
| 25 |
FROM NumberIO IMPORT WriteCard ; |
| 26 |
FROM FIO IMPORT StdOut, FlushBuffer ; |
| 27 |
FROM SFIO IMPORT WriteS ; |
| 28 |
FROM libc IMPORT exit ; |
| 29 |
|
| 30 |
TYPE |
| 31 |
tests = RECORD |
| 32 |
n : CARDINAL ; |
| 33 |
i, o: ARRAY [0..maxString] OF CHAR ; |
| 34 |
END ; |
| 35 |
sigfigArray = ARRAY [0..6] OF tests ; |
| 36 |
|
| 37 |
CONST |
| 38 |
maxString = 80 ; |
| 39 |
|
| 40 |
VAR |
| 41 |
j: CARDINAL ; |
| 42 |
t, |
| 43 |
s: String ; |
| 44 |
a: sigfigArray ; |
| 45 |
e: INTEGER ; |
| 46 |
BEGIN |
| 47 |
a := sigfigArray{tests{ 3, "12.3456789" , "12.3"}, |
| 48 |
tests{ 4, "12.3456789" , "12.35"}, |
| 49 |
tests{ 5, "12.3456789" , "12.346"}, |
| 50 |
tests{ 6, "12.3456789" , "12.3457"}, |
| 51 |
|
| 52 |
tests{ 3, "19.99" , "20.0"}, |
| 53 |
tests{ 3, "99.999" , "100"}, |
| 54 |
tests{ 3, "99.999" , "100"}} ; |
| 55 |
e := 0 ; |
| 56 |
FOR j := 0 TO HIGH(a) DO |
| 57 |
PushAllocation ; |
| 58 |
WITH a[j] DO |
| 59 |
t := InitString(i) ; |
| 60 |
s := ToSigFig(t, n) ; |
| 61 |
IF EqualArray(s, o) |
| 62 |
THEN |
| 63 |
WriteString(' passed ') |
| 64 |
ELSE |
| 65 |
WriteString('**failed**') |
| 66 |
END ; |
| 67 |
WriteString(' ToSigFig(') ; |
| 68 |
WriteString(i) ; WriteString(', ') ; WriteCard(n, 0) ; WriteString(') -> ') ; |
| 69 |
IF EqualArray(s, o) |
| 70 |
THEN |
| 71 |
WriteString(o) |
| 72 |
ELSE |
| 73 |
e := 1 ; (* failure code *) |
| 74 |
s := WriteS(StdOut, s) ; WriteString(' (it should be: ') ; |
| 75 |
WriteString(o) ; WriteString(')') |
| 76 |
END ; |
| 77 |
WriteLn ; |
| 78 |
s := KillString(s) |
| 79 |
END ; |
| 80 |
PopAllocation(TRUE) |
| 81 |
END ; |
| 82 |
WriteLn ; |
| 83 |
WriteString('The sigfig tests: ') ; |
| 84 |
IF e=0 |
| 85 |
THEN |
| 86 |
WriteString('passed') |
| 87 |
ELSE |
| 88 |
WriteString('failed') |
| 89 |
END ; |
| 90 |
WriteLn ; |
| 91 |
FlushBuffer(StdOut) ; |
| 92 |
exit(e) |
| 93 |
END sigfig. |