summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/tbs/tb0162.pp
blob: e698fe365cfc258bb78169c5ebce25a9138dcbe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
{ Old file: tbs0193.pp }
{ overflow checking for 8 and 16 bit operations wrong }

{$mode objfpc}

uses sysutils;

procedure doerror(l: longint);
begin
  writeln('error near ',l);
  halt(1);
end;

{$R-}
{$Q+}
var i: integer;
    b: byte;
    l: longint;
    c: cardinal;
    n: int64;
    q: qword;
begin
  i := 32767;
  i := i + 15;
  b := 255;
  b := b + 18;
  b := 255;
  b := b * 8;
  b := 255;
  b := b * 17;

{ 64 bit cpus do all calculations in 64 bit so longint and cardinal can't overflow }
{$ifndef CPU64}
  l := high(longint);
  try
    l := l+1;
    doerror(1);
  except
    on eintoverflow do
      ;
    else
      doerror(2);
  end;

  l := low(longint);
  try
    l := l-1;
    doerror(3);
  except
    on eintoverflow do
      ;
    else
      doerror(4);
  end;

  l := low(longint);
  try
    l := l*2;
    doerror(5);
  except
    on eintoverflow do
      ;
    else
      doerror(6);
  end;

  l := high(longint) div 2;
  try
    l := l*3;
    doerror(7);
  except
    on eintoverflow do
      ;
    else
      doerror(8);
  end;

  c := high(cardinal);
  try
    c := c+1;
    doerror(11);
  except
    on eintoverflow do
      ;
    else
      doerror(12);
  end;

  c := high(cardinal) div 2;
  try
    c := c*3;
    doerror(13);
  except
    on eintoverflow do
      ;
    else
      doerror(14);
  end;

  c := high(cardinal);
  try
    c := c*high(cardinal);
    doerror(15);
  except
    on eintoverflow do
      ;
    else
      doerror(16);
  end;

{$endif CPU64}

{$ifdef fpc}

  n := high(int64);
  try
    n := n+1;
    doerror(17);
  except
    on eintoverflow do
      ;
    else
      doerror(18);
  end;

  n := low(int64);
  try
    n := n-1;
    doerror(19);
  except
    on eintoverflow do
      ;
    else
      doerror(20);
  end;

  n := 0;
  try
    n := n-1;
  except
    on eintoverflow do
      doerror(39);
  end;


  n := low(int64);
  try
    n := n*2;
    doerror(21);
  except
    on eintoverflow do
      ;
    else
      doerror(22);
  end;

  n := high(int64) div 2;
  try
    n := n*3;
    doerror(23);
  except
    on eintoverflow do
      ;
    else
      doerror(24);
  end;


  q := 0;
  try
    q := q-1;
    doerror(25);
  except
    on eintoverflow do
      ;
    else
      doerror(26);
  end;


  q := qword(high(qword));
  try
    q := q+1;
    doerror(27);
  except
    on eintoverflow do
      ;
    else
      doerror(28);
  end;

  q := qword(high(qword)) div qword(2);
  try
    q := q*qword(3);
    doerror(29);
  except
    on eintoverflow do
      ;
    else
      doerror(30);
  end;

  q := high(qword);
  try
    q := q*high(qword);
    doerror(31);
  except
    on eintoverflow do
      ;
    else
      doerror(32);
  end;

{$endif fpc}

End.