-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathunit1.pas
269 lines (221 loc) · 5.66 KB
/
unit1.pas
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls, PairSplitter, PythonEngine, lcl.PythonGUIInputOutput;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
btnTestIntegers: TButton;
cbTestIntegers: TCheckBox;
Memo1: TMemo;
Memo2: TMemo;
PairSplitter1: TPairSplitter;
PairSplitterSide1: TPairSplitterSide;
PairSplitterSide2: TPairSplitterSide;
Panel1: TPanel;
Panel2: TPanel;
PythonEngine1: TPythonEngine;
PythonGUIInputOutput1: TPythonGUIInputOutput;
procedure btnTestIntegersClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
fVerbose : Boolean;
procedure Log( const AText : String );
procedure RunSelectedTests;
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
Uses
VarPyth;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
PythonEngine1.ExecStrings( Memo2.Lines );
end;
procedure TForm1.btnTestIntegersClick(Sender: TObject);
var
a, b, c : Variant;
big : Int64;
begin
// initialize the operands
a := VarPythonCreate(2);
Assert(VarIsPython(a));
Assert(VarIsPythonNumber(a));
Assert(VarIsPythonInteger(a));
Assert(Integer(a) = 2);
b := VarPythonCreate(3);
Assert(VarIsPython(b));
Assert(VarIsPythonNumber(b));
Assert(VarIsPythonInteger(b));
Assert(Integer(b) = 3);
// arithmetic operations
//----------------------
// addition
c := a + b;
// check result of operation
Assert( Integer(c) = 5 );
// check that operation did not change the content of operands.
Assert(Integer(a) = 2);
Assert(Integer(b) = 3);
// now with a litteral
c := a + b + 1;
Assert( Integer(c) = 6 );
c := a + 1 + b;
Assert( Integer(c) = 6 );
c := 1 + a + b;
Assert( Integer(c) = 6 );
// substraction
c := b - a;
Assert( Integer(c) = 1 );
// now with a litteral
c := b - a - 1;
Assert( Integer(c) = 0 );
c := b - 1 - a;
Assert( Integer(c) = 0 );
c := 1 - b - a;
Assert( Integer(c) = -4 );
// multiplication
c := a * b;
Assert( Integer(c) = 6 );
// now with a litteral
c := a * b * 2;
Assert( Integer(c) = 12 );
c := a * 2 * b;
Assert( Integer(c) = 12 );
c := 2 * a * b;
Assert( Integer(c) = 12 );
// integer division
c := b div a;
Assert( Integer(c) = 1 );
// division: in Python a division between 2 integers is the same as the integer division
c := b / a;
Assert( c = 1.5 );
Assert( Integer(c) = 2 );
// modulus
c := b mod a;
Assert( Integer(c) = 1 );
c := BuiltinModule.divmod(b, a); // this returns a tuple whose first item is the result of the division,
// and second item the modulo.
if VarIsPythonSequence(c) and (c.Length = 2) then
begin
Assert(Integer(c.GetItem(0)) = 1); // division
Assert(Integer(c.GetItem(1)) = 1); // modulo
end;
// power
c := BuiltinModule.pow(a, b);
Assert(c = 8);
// negation
c := -a;
Assert( Integer(c) = -2 );
// logical operations
//------------------
// inverse
c := not a; // in python it would be: c = ~2
Assert( Integer(c) = -3 );
// shift left (<<)
c := a shl b;
Assert( Integer(c) = 16 );
c := a shl 1;
Assert( Integer(c) = 4 );
// shift right (>>)
c := a shl b;
c := c shr b;
Assert( Integer(c) = Integer(a) );
c := b shr 1;
Assert( Integer(c) = 1 );
// and
c := a and (a*5);
Assert( Integer(c) = Integer(a) );
c := a and 6;
Assert( Integer(c) = Integer(a) );
// or
c := a or b;
Assert( Integer(c) = 3 );
c := a or 3;
Assert( Integer(c) = 3 );
// xor
c := a xor b;
Assert( Integer(c) = 1 );
c := a xor 3;
Assert( Integer(c) = 1 );
// comparisons
//------------
// equal
c := a = b;
Assert(c = False);
c := a = a;
Assert(c = True);
Assert( a = 2);
// not equal
c := a <> b;
Assert(c = True);
Assert( not (c = b) );
c := a <> a;
Assert(c = False);
Assert( a = 2);
// greater than
c := a > b; Assert(c = False);
c := b > a; Assert(c = True);
Assert( a > 1);
// greater or equal than
c := a >= b; Assert(c = False);
c := b >= a; Assert(c = True);
c := a >= a; Assert(c = True);
Assert( a >= 2 );
// less than
c := a < b; Assert(c = True);
c := b < a; Assert(c = False);
Assert( a < 6);
// less or equal than
c := a <= b; Assert(c = True);
c := b <= a; Assert(c = False);
c := a <= a; Assert(c = True);
Assert( a <= 2);
// parenthesis
c := a * ((a * b) div b);
Assert( c = a*2 );
// copy
c := a;
Assert( c = a);
Assert( VarIsSame(c, a) ); // checks if 2 variants share the same Python object.
// test long long (Int64)
big := Int64(MaxInt)*4;
b := VarPythonCreate(big);
Assert( b = big );
Assert( b <> big+1 );
Assert( b > MaxInt );
Assert( MaxInt < b );
Assert( b+1 = big+1 );
Assert( b*2 = big*2 );
Assert( b div 2 = big div 2 );
c := VarPythonCreate(True);
Assert(VarIsBool(c));
Assert(VarIsTrue(c));
c := VarPythonCreate(False);
Assert(VarIsBool(c));
Assert(not VarIsTrue(c));
// Done!
Log('Integer test was Ok.');end;
procedure TForm1.FormCreate(Sender: TObject);
begin
fVerbose := True;
end;
procedure TForm1.Log(const AText: String);
begin
if fVerbose then
Memo1.Lines.Add(AText);
end;
procedure TForm1.RunSelectedTests;
begin
end;
initialization
{$I unit1.lrs}
end.