| 1 |
(* Copyright (C) 2016, 2017 |
| 2 |
Free Software Foundation, Inc. *) |
| 3 |
(* This file is part of GNU Modula-2. |
| 4 |
|
| 5 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free |
| 7 |
Software Foundation; either version 2, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License along |
| 16 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 17 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) |
| 18 |
|
| 19 |
MODULE modulus2 ; |
| 20 |
|
| 21 |
FROM libc IMPORT exit, printf ; |
| 22 |
FROM SYSTEM IMPORT ADR ; |
| 23 |
|
| 24 |
VAR |
| 25 |
res: INTEGER ; |
| 26 |
|
| 27 |
|
| 28 |
PROCEDURE Assert (i, v: INTEGER; f: ARRAY OF CHAR; l: CARDINAL; e: ARRAY OF CHAR) ; |
| 29 |
VAR |
| 30 |
r: INTEGER ; |
| 31 |
BEGIN |
| 32 |
IF i=v |
| 33 |
THEN |
| 34 |
r := printf("successfully evaluated %s = %d\n", ADR(e), i) |
| 35 |
ELSE |
| 36 |
r := printf("%s:%d assertion failed when evaluating %s as %d whereas it should be %d\n", ADR(f), l, ADR(e), i, v) ; |
| 37 |
res := 1 |
| 38 |
END |
| 39 |
END Assert ; |
| 40 |
|
| 41 |
|
| 42 |
(* |
| 43 |
Consistency - run the PIM4 P28 consistency test for DIV and MOD. |
| 44 |
*) |
| 45 |
|
| 46 |
PROCEDURE Consistency (t, q, r, left, right: INTEGER) ; |
| 47 |
BEGIN |
| 48 |
printf ("Test %d\n", t) ; |
| 49 |
printf ("======\n") ; |
| 50 |
IF (left = q * right + r) AND (r >= 0) |
| 51 |
THEN |
| 52 |
printf (" satisfies PIM4 consistency test\n") |
| 53 |
ELSE |
| 54 |
printf (" fails PIM4 consistency test") ; |
| 55 |
IF r<0 |
| 56 |
THEN |
| 57 |
printf (" (the remainder must be >= 0) (not %d)", r) |
| 58 |
END ; |
| 59 |
printf ("\n") ; |
| 60 |
|
| 61 |
printf (" q = %d, r = %d, left = %d, right = %d\n", q, r, left, right) ; |
| 62 |
res := 1 |
| 63 |
END |
| 64 |
END Consistency ; |
| 65 |
|
| 66 |
|
| 67 |
VAR |
| 68 |
q, r: INTEGER ; |
| 69 |
BEGIN |
| 70 |
res := 0 ; |
| 71 |
|
| 72 |
(* see P29 of PIM4 and using the examples in ISO M2 P201 |
| 73 |
and the GM2 documentation. *) |
| 74 |
|
| 75 |
(* test 1: 31 and 10 *) |
| 76 |
|
| 77 |
q := 31 DIV 10 ; |
| 78 |
Assert (q, 3, __FILE__, __LINE__, "31 DIV 10") ; |
| 79 |
r := 31 MOD 10 ; |
| 80 |
Assert (r, 1, __FILE__, __LINE__, "31 MOD 10") ; |
| 81 |
Consistency (1, q, r, 31, 10) ; |
| 82 |
|
| 83 |
(* test 2: -31 and 10 *) |
| 84 |
|
| 85 |
q := (-31) DIV 10 ; |
| 86 |
Assert (q, -4, __FILE__, __LINE__, "(-31) DIV 10") ; |
| 87 |
r := (-31) MOD 10 ; |
| 88 |
Assert (r, 9, __FILE__, __LINE__, "(-31) MOD 10") ; |
| 89 |
Consistency (2, q, r, -31, 10) ; |
| 90 |
|
| 91 |
(* test 3: 31 and -10 *) |
| 92 |
|
| 93 |
q := 31 DIV (-10) ; |
| 94 |
Assert (q, -3, __FILE__, __LINE__, "31 DIV (-10)") ; |
| 95 |
r := 31 MOD (-10) ; |
| 96 |
Assert (r, 1, __FILE__, __LINE__, "31 MOD (-10)") ; |
| 97 |
Consistency (3, q, r, 31, -10) ; |
| 98 |
|
| 99 |
(* test 4: -31 and -10 *) |
| 100 |
|
| 101 |
q := (-31) DIV (-10) ; |
| 102 |
Assert (q, 4, __FILE__, __LINE__, "(-31) DIV (-10)") ; |
| 103 |
r := (-31) MOD (-10) ; |
| 104 |
Assert (r, 9, __FILE__, __LINE__, "(-31) MOD (-10)") ; |
| 105 |
Consistency (4, q, r, -31, -10) ; |
| 106 |
|
| 107 |
exit(res) |
| 108 |
END modulus2. |