summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/tbsx1.pp
blob: 98aaf3c7e4fd211b0d7579961ebd5033e50860e0 (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
{$mode objfpc}

program testbitscan;

function test_byte: boolean;
var
  x8,f,r: byte;
  i: integer;
begin
  for i:=0 to 7 do
  begin
    x8:=1 shl i;
    f:=BsfByte(x8);
    if (f<>i) then
    begin
      writeln('BsfByte(',x8,') returned ',f,', should be ',i);
      exit(false);
    end;
    r:=BsrByte(x8);
    if r<>i then
    begin
      writeln('BsrByte(',x8,') returned ',f,', should be ',i);
      exit(false);
    end;
  end;
  result:=true;
end;

function test_word: boolean;
var
  x16: word;
  i,f,r: integer;
begin
  for i:=0 to 15 do
  begin
    x16:=1 shl i;
    f:=BsfWord(x16);
    if (f<>i) then
    begin
      writeln('BsfWord(',x16,') returned ',f,', should be ',i);
      exit(false);
    end;
    r:=BsrWord(x16);
    if r<>i then
    begin
      writeln('BsrWord(',x16,') returned ',f,', should be ',i);
      exit(false);
    end;
  end;
  result:=true;
end;

function test_dword: boolean;
var
  x32: cardinal;
  i,f,r: integer;
begin
  for i:=0 to 31 do
  begin
    x32:=1 shl i;
    f:=BsfDWord(x32);
    if (f<>i) then
    begin
      writeln('BsfDWord(',x32,') returned ',f,', should be ',i);
      exit(false);
    end;
    r:=BsrDWord(x32);
    if r<>i then
    begin
      writeln('BsrDWord(',x32,') returned ',f,', should be ',i);
      exit(false);
    end;
  end;
  result:=true;
end;

function test_qword: boolean;
var
  x64: qword;
  i, f, r: integer;
begin
  for i:=0 to 63 do
  begin
    x64:=uint64(1) shl i;
    f:=BsfQWord(x64);
    if f<>i then begin
      writeln('BsfQWord(',x64,') returned ',f,', should be ',i);
      exit(false);
    end;
    r:=BsrQWord(x64);
    if r<>i then begin
      writeln('BsrQWord(',x64,') returned ',r,', should be ',i);
      exit(false);
    end;
  end;
  result:=true;
end;

begin
  if test_byte then writeln('passed') else halt(1);
  if test_word then writeln('passed') else halt(1);
  if test_dword then writeln('passed') else halt(1);
  if test_qword then writeln('passed') else halt(1);
end.