1 (* Copyright (C) 2016, 2017
2 Free Software Foundation, Inc. *)
3 (* This file is part of GNU Modula-2.
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
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
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. *)
21 FROM libc IMPORT exit, printf ;
22 FROM SYSTEM IMPORT ADR ;
28 PROCEDURE Assert (i, v: INTEGER; f: ARRAY OF CHAR; l: CARDINAL; e: ARRAY OF CHAR) ;
34 r := printf("successfully evaluated %s = %d\n", ADR(e), i)
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) ;
43 Consistency - run the PIM4 P28 consistency test for DIV and MOD.
46 PROCEDURE Consistency (t, q, r, left, right: INTEGER) ;
48 printf ("Test %d\n", t) ;
50 IF (left = q * right + r) AND (r >= 0)
52 printf (" satisfies PIM4 consistency test\n")
54 printf (" fails PIM4 consistency test") ;
57 printf (" (the remainder must be >= 0) (not %d)", r)
61 printf (" q = %d, r = %d, left = %d, right = %d\n", q, r, left, right) ;
72 (* see P29 of PIM4 and using the examples in ISO M2 P201
73 and the GM2 documentation. *)
75 (* test 1: 31 and 10 *)
78 Assert (q, 3, __FILE__, __LINE__, "31 DIV 10") ;
80 Assert (r, 1, __FILE__, __LINE__, "31 MOD 10") ;
81 Consistency (1, q, r, 31, 10) ;
83 (* test 2: -31 and 10 *)
86 Assert (q, -4, __FILE__, __LINE__, "(-31) DIV 10") ;
88 Assert (r, 9, __FILE__, __LINE__, "(-31) MOD 10") ;
89 Consistency (2, q, r, -31, 10) ;
91 (* test 3: 31 and -10 *)
94 Assert (q, -3, __FILE__, __LINE__, "31 DIV (-10)") ;
96 Assert (r, 1, __FILE__, __LINE__, "31 MOD (-10)") ;
97 Consistency (3, q, r, 31, -10) ;
99 (* test 4: -31 and -10 *)
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) ;