-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBitwiseTest.Mod
More file actions
453 lines (356 loc) · 13.6 KB
/
BitwiseTest.Mod
File metadata and controls
453 lines (356 loc) · 13.6 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
(** BitwiseTest.Mod - Tests for Bitwise.Mod.
Copyright (C) 2025
Released under The 3-Clause BSD License.
*)
MODULE BitwiseTest;
IMPORT Bitwise, Tests;
VAR
ts: Tests.TestSet;
PROCEDURE TestAnd*(): BOOLEAN;
VAR a, b, result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic AND test *)
a := 0FFH; b := 0AAH;
result := Bitwise.And(a, b);
Tests.ExpectedInt(0AAH, result, "Bitwise.And basic test failed", test);
(* Edge cases *)
result := Bitwise.And(0, 0FFFFFFFFH);
Tests.ExpectedInt(0, result, "Bitwise.And with zero failed", test);
result := Bitwise.And(0FFFFFFFFH, 0FFFFFFFFH);
Tests.ExpectedInt(0FFFFFFFFH, result, "Bitwise.And with all bits set failed", test);
result := Bitwise.And(0FFFFFFFFH, 0);
Tests.ExpectedInt(0, result, "Bitwise.And all bits with zero failed", test);
(* Negative numbers *)
result := Bitwise.And(-1, 0FFH);
Tests.ExpectedInt(0FFH, result, "Bitwise.And with negative number failed", test);
RETURN test
END TestAnd;
PROCEDURE TestAnd8*(): BOOLEAN;
VAR a, b, result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic AND test *)
a := 0FFH; b := 0AAH;
result := Bitwise.And8(a, b);
Tests.ExpectedInt(0AAH, result, "Bitwise.And8 basic test failed", test);
(* Edge cases *)
result := Bitwise.And8(0, 0FFH);
Tests.ExpectedInt(0, result, "Bitwise.And8 with zero failed", test);
result := Bitwise.And8(0FFH, 0FFH);
Tests.ExpectedInt(0FFH, result, "Bitwise.And8 with all bits set failed", test);
result := Bitwise.And8(0F0H, 00FH);
Tests.ExpectedInt(0, result, "Bitwise.And8 non-overlapping bits failed", test);
RETURN test
END TestAnd8;
PROCEDURE TestOr*(): BOOLEAN;
VAR a, b, result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic OR test *)
a := 0FFH; b := 0AAH;
result := Bitwise.Or(a, b);
Tests.ExpectedInt(0FFH, result, "Bitwise.Or basic test failed", test);
(* Edge cases *)
result := Bitwise.Or(0, 0);
Tests.ExpectedInt(0, result, "Bitwise.Or with zeros failed", test);
result := Bitwise.Or(0F0H, 00FH);
Tests.ExpectedInt(0FFH, result, "Bitwise.Or complementary bits failed", test);
result := Bitwise.Or(0FFFFFFFFH, 0);
Tests.ExpectedInt(0FFFFFFFFH, result, "Bitwise.Or all bits with zero failed", test);
RETURN test
END TestOr;
PROCEDURE TestOr8*(): BOOLEAN;
VAR a, b, result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic OR test *)
a := 0F0H; b := 00FH;
result := Bitwise.Or8(a, b);
Tests.ExpectedInt(0FFH, result, "Bitwise.Or8 basic test failed", test);
(* Edge cases *)
result := Bitwise.Or8(0, 0);
Tests.ExpectedInt(0, result, "Bitwise.Or8 with zeros failed", test);
result := Bitwise.Or8(0AAH, 055H);
Tests.ExpectedInt(0FFH, result, "Bitwise.Or8 alternating bits failed", test);
RETURN test
END TestOr8;
PROCEDURE TestXor*(): BOOLEAN;
VAR a, b, result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic XOR test *)
a := 0FFH; b := 0AAH;
result := Bitwise.Xor(a, b);
Tests.ExpectedInt(055H, result, "Bitwise.Xor basic test failed", test);
(* Identity tests *)
result := Bitwise.Xor(0AAH, 0AAH);
Tests.ExpectedInt(0, result, "Bitwise.Xor identity test failed", test);
result := Bitwise.Xor(0, 0AAH);
Tests.ExpectedInt(0AAH, result, "Bitwise.Xor with zero failed", test);
(* Complementary bits *)
result := Bitwise.Xor(0F0H, 00FH);
Tests.ExpectedInt(0FFH, result, "Bitwise.Xor complementary bits failed", test);
RETURN test
END TestXor;
PROCEDURE TestXor8*(): BOOLEAN;
VAR a, b, result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic XOR test *)
a := 0FFH; b := 0AAH;
result := Bitwise.Xor8(a, b);
Tests.ExpectedInt(055H, result, "Bitwise.Xor8 basic test failed", test);
(* Identity tests *)
result := Bitwise.Xor8(0AAH, 0AAH);
Tests.ExpectedInt(0, result, "Bitwise.Xor8 identity test failed", test);
result := Bitwise.Xor8(0, 0AAH);
Tests.ExpectedInt(0AAH, result, "Bitwise.Xor8 with zero failed", test);
RETURN test
END TestXor8;
PROCEDURE TestNot*(): BOOLEAN;
VAR a, result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic NOT test *)
a := 0H;
result := Bitwise.Not(a);
Tests.ExpectedInt(0FFFFFFFFH, result, "Bitwise.Not zero failed", test);
(* All bits set *)
result := Bitwise.Not(0FFFFFFFFH);
Tests.ExpectedInt(0, result, "Bitwise.Not all bits failed", test);
(* Pattern test *)
result := Bitwise.Not(0AAH);
Tests.ExpectedInt(0FFFFFF55H, result, "Bitwise.Not pattern failed", test);
RETURN test
END TestNot;
PROCEDURE TestNot8*(): BOOLEAN;
VAR a, result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic NOT test *)
a := 00H;
result := Bitwise.Not8(a);
Tests.ExpectedInt(0FFH, result, "Bitwise.Not8 zero failed", test);
(* All bits set *)
result := Bitwise.Not8(0FFH);
Tests.ExpectedInt(0, result, "Bitwise.Not8 all bits failed", test);
(* Pattern test *)
result := Bitwise.Not8(0AAH);
Tests.ExpectedInt(055H, result, "Bitwise.Not8 pattern failed", test);
RETURN test
END TestNot8;
PROCEDURE TestShiftLeft*(): BOOLEAN;
VAR result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic shift left *)
result := Bitwise.ShiftLeft(0FFFFH, 1);
Tests.ExpectedInt(01FFFEH, result, "Bitwise.ShiftLeft basic failed", test);
(* Zero shift *)
result := Bitwise.ShiftLeft(0FFFFH, 0);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.ShiftLeft zero shift failed", test);
(* Large shift *)
result := Bitwise.ShiftLeft(0FFFFH, 32);
Tests.ExpectedInt(0, result, "Bitwise.ShiftLeft large shift failed", test);
(* Negative shift *)
result := Bitwise.ShiftLeft(0FFFFH, -1);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.ShiftLeft negative shift failed", test);
(* Shift by 1 bit *)
result := Bitwise.ShiftLeft(1, 1);
Tests.ExpectedInt(2, result, "Bitwise.ShiftLeft single bit failed", test);
RETURN test
END TestShiftLeft;
PROCEDURE TestShiftLeft8*(): BOOLEAN;
VAR result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic shift left *)
result := Bitwise.ShiftLeft8(0FH, 1);
Tests.ExpectedInt(01EH, result, "Bitwise.ShiftLeft8 basic failed", test);
(* Zero shift *)
result := Bitwise.ShiftLeft8(0FFH, 0);
Tests.ExpectedInt(0FFH, result, "Bitwise.ShiftLeft8 zero shift failed", test);
(* Large shift *)
result := Bitwise.ShiftLeft8(0FFH, 8);
Tests.ExpectedInt(0, result, "Bitwise.ShiftLeft8 large shift failed", test);
(* Overflow test *)
result := Bitwise.ShiftLeft8(0FFH, 1);
Tests.ExpectedInt(0FEH, result, "Bitwise.ShiftLeft8 overflow failed", test);
RETURN test
END TestShiftLeft8;
PROCEDURE TestShiftRight*(): BOOLEAN;
VAR result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic shift right *)
result := Bitwise.ShiftRight(0FFFFH, 1);
Tests.ExpectedInt(07FFFH, result, "Bitwise.ShiftRight basic failed", test);
(* Zero shift *)
result := Bitwise.ShiftRight(0FFFFH, 0);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.ShiftRight zero shift failed", test);
(* Large shift with positive number *)
result := Bitwise.ShiftRight(0FFFFH, 32);
Tests.ExpectedInt(0, result, "Bitwise.ShiftRight large shift positive failed", test);
(* Large shift with negative number *)
result := Bitwise.ShiftRight(-1, 32);
Tests.ExpectedInt(-1, result, "Bitwise.ShiftRight large shift negative failed", test);
(* Negative number shift *)
result := Bitwise.ShiftRight(-8, 1);
Tests.ExpectedInt(-4, result, "Bitwise.ShiftRight negative number failed", test);
RETURN test
END TestShiftRight;
PROCEDURE TestLogicalShiftRight*(): BOOLEAN;
VAR a, result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic logical shift right - positive number *)
result := Bitwise.LogicalShiftRight(0FFFFH, 1);
Tests.ExpectedInt(07FFFH, result, "Bitwise.LogicalShiftRight basic positive failed", test);
(* Logical shift of negative number should fill with zeros *)
result := Bitwise.LogicalShiftRight(-1, 1);
Tests.ExpectedInt(7FFFFFFFH, result, "Bitwise.LogicalShiftRight negative number failed", test);
(* Compare with arithmetic shift to verify difference *)
a := -8; (* 0xFFFFFFF8 *)
result := Bitwise.LogicalShiftRight(a, 1);
Tests.ExpectedInt(7FFFFFFCH, result, "Bitwise.LogicalShiftRight vs arithmetic failed", test);
(* Zero shift *)
result := Bitwise.LogicalShiftRight(0FFFFH, 0);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.LogicalShiftRight zero shift failed", test);
(* Large shift *)
result := Bitwise.LogicalShiftRight(0FFFFFFFFH, 32);
Tests.ExpectedInt(0, result, "Bitwise.LogicalShiftRight large shift failed", test);
(* High bit set *)
result := Bitwise.LogicalShiftRight(80000000H, 1);
Tests.ExpectedInt(40000000H, result, "Bitwise.LogicalShiftRight high bit failed", test);
RETURN test
END TestLogicalShiftRight;
PROCEDURE TestShiftRight8*(): BOOLEAN;
VAR result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic shift right *)
result := Bitwise.ShiftRight8(0FEH, 1);
Tests.ExpectedInt(07FH, result, "Bitwise.ShiftRight8 basic failed", test);
(* Zero shift *)
result := Bitwise.ShiftRight8(0FFH, 0);
Tests.ExpectedInt(0FFH, result, "Bitwise.ShiftRight8 zero shift failed", test);
(* Large shift *)
result := Bitwise.ShiftRight8(0FFH, 8);
Tests.ExpectedInt(0, result, "Bitwise.ShiftRight8 large shift failed", test);
(* Single bit *)
result := Bitwise.ShiftRight8(2, 1);
Tests.ExpectedInt(1, result, "Bitwise.ShiftRight8 single bit failed", test);
RETURN test
END TestShiftRight8;
PROCEDURE TestRotateLeft*(): BOOLEAN;
VAR result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic rotate left *)
result := Bitwise.RotateLeft(0FFFFH, 1);
Tests.ExpectedInt(01FFFEH, result, "Bitwise.RotateLeft basic failed", test);
(* Zero rotation *)
result := Bitwise.RotateLeft(0FFFFH, 0);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.RotateLeft zero rotation failed", test);
(* Full rotation *)
result := Bitwise.RotateLeft(0FFFFH, 32);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.RotateLeft full rotation failed", test);
(* High bit rotation *)
result := Bitwise.RotateLeft(080000000H, 1);
Tests.ExpectedInt(1, result, "Bitwise.RotateLeft high bit failed", test);
RETURN test
END TestRotateLeft;
PROCEDURE TestRotateLeft8*(): BOOLEAN;
VAR result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic rotate left *)
result := Bitwise.RotateLeft8(0FH, 1);
Tests.ExpectedInt(01EH, result, "Bitwise.RotateLeft8 basic failed", test);
(* Zero rotation *)
result := Bitwise.RotateLeft8(0FFH, 0);
Tests.ExpectedInt(0FFH, result, "Bitwise.RotateLeft8 zero rotation failed", test);
(* Full rotation *)
result := Bitwise.RotateLeft8(0FFH, 8);
Tests.ExpectedInt(0FFH, result, "Bitwise.RotateLeft8 full rotation failed", test);
(* High bit rotation *)
result := Bitwise.RotateLeft8(080H, 1);
Tests.ExpectedInt(1, result, "Bitwise.RotateLeft8 high bit failed", test);
RETURN test
END TestRotateLeft8;
PROCEDURE TestRotateRight*(): BOOLEAN;
VAR result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic rotate right *)
result := Bitwise.RotateRight(0FFFFH, 1);
Tests.ExpectedInt(080007FFFH, result, "Bitwise.RotateRight basic failed", test);
(* Zero rotation *)
result := Bitwise.RotateRight(0FFFFH, 0);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.RotateRight zero rotation failed", test);
(* Full rotation *)
result := Bitwise.RotateRight(0FFFFH, 32);
Tests.ExpectedInt(0FFFFH, result, "Bitwise.RotateRight full rotation failed", test);
(* Low bit rotation *)
result := Bitwise.RotateRight(1, 1);
Tests.ExpectedInt(080000000H, result, "Bitwise.RotateRight low bit failed", test);
RETURN test
END TestRotateRight;
PROCEDURE TestRotateRight8*(): BOOLEAN;
VAR result: BYTE; test: BOOLEAN;
BEGIN
test := TRUE;
(* Basic rotate right *)
result := Bitwise.RotateRight8(0F0H, 1);
Tests.ExpectedInt(078H, result, "Bitwise.RotateRight8 basic failed", test);
(* Zero rotation *)
result := Bitwise.RotateRight8(0FFH, 0);
Tests.ExpectedInt(0FFH, result, "Bitwise.RotateRight8 zero rotation failed", test);
(* Full rotation *)
result := Bitwise.RotateRight8(0FFH, 8);
Tests.ExpectedInt(0FFH, result, "Bitwise.RotateRight8 full rotation failed", test);
(* Low bit rotation *)
result := Bitwise.RotateRight8(1, 1);
Tests.ExpectedInt(080H, result, "Bitwise.RotateRight8 low bit failed", test);
RETURN test
END TestRotateRight8;
PROCEDURE TestCombinedOperations*(): BOOLEAN;
VAR a, b, result: INTEGER; test: BOOLEAN;
BEGIN
test := TRUE;
(* Test De Morgan's law: NOT(a AND b) = (NOT a) OR (NOT b) *)
a := 0AAH; b := 055H;
result := Bitwise.Not(Bitwise.And(a, b));
Tests.ExpectedInt(Bitwise.Or(Bitwise.Not(a), Bitwise.Not(b)), result, "De Morgan's law failed", test);
(* Test XOR identity: a XOR b XOR b = a *)
result := Bitwise.Xor(Bitwise.Xor(a, b), b);
Tests.ExpectedInt(a, result, "XOR identity failed", test);
(* Test shift equivalence: left shift by n = multiply by 2^n (for small values) *)
a := 5;
result := Bitwise.ShiftLeft(a, 2);
Tests.ExpectedInt(20, result, "Shift left equivalence failed", test);
RETURN test
END TestCombinedOperations;
BEGIN
Tests.Init(ts, "Bitwise Tests");
Tests.Add(ts, TestAnd);
Tests.Add(ts, TestAnd8);
Tests.Add(ts, TestOr);
Tests.Add(ts, TestOr8);
Tests.Add(ts, TestXor);
Tests.Add(ts, TestXor8);
Tests.Add(ts, TestNot);
Tests.Add(ts, TestNot8);
Tests.Add(ts, TestShiftLeft);
Tests.Add(ts, TestShiftLeft8);
Tests.Add(ts, TestShiftRight);
Tests.Add(ts, TestLogicalShiftRight);
Tests.Add(ts, TestShiftRight8);
Tests.Add(ts, TestRotateLeft);
Tests.Add(ts, TestRotateLeft8);
Tests.Add(ts, TestRotateRight);
Tests.Add(ts, TestRotateRight8);
Tests.Add(ts, TestCombinedOperations);
ASSERT(Tests.Run(ts))
END BitwiseTest.