瀏覽代碼

Add tests for bug report 41460

Pierre Muller 1 月之前
父節點
當前提交
23398c9e2f
共有 5 個文件被更改,包括 199 次插入0 次删除
  1. 28 0
      tests/webtbf/tw41460b.pp
  2. 51 0
      tests/webtbs/tw41460.pp
  3. 32 0
      tests/webtbs/tw41460a.pp
  4. 39 0
      tests/webtbs/tw41460c.pp
  5. 49 0
      tests/webtbs/tw41460d.pp

+ 28 - 0
tests/webtbf/tw41460b.pp

@@ -0,0 +1,28 @@
+{ %FAIL }
+
+program calculate_offset;
+
+{$T+}
+{ In T+ mode, i.e. type pointer mode,
+  the difference between two pointers of the different types
+  should generate an error }
+
+type
+   TRecord = record
+     First: word;
+     Second: qword;
+   end;
+   PRecord = ^TRecord;
+
+var
+   res: PtrInt;
+   Rec : TRecord;
+begin
+   Res := @(PRecord(@Rec)^.Second) - @(PRecord(@Rec)^.First);
+   writeln('Offset of Second field inside TRecord is ',Res);
+   if (Res<2) then
+     begin
+       writeln('Offset of second field is smaller than size of first field');
+       halt(1);
+     end;
+end.

+ 51 - 0
tests/webtbs/tw41460.pp

@@ -0,0 +1,51 @@
+program calculate_offset;
+
+{$T-}
+
+type
+   TRecord = record
+     First: word;
+     Second: word;
+   end;
+   PRecord = ^TRecord;
+
+var
+   res, res2, res3 : PtrInt;
+   Rec : TRecord;
+
+begin
+{$ifndef SKIP_CONST_POINTER}
+   Res := @(PRecord(nil)^.Second) - @(PRecord(nil)^.First);
+   writeln('Offset of Second field inside TRecord is ',Res);
+   if (Res<2) then
+     begin
+       writeln('Offset of second field is smaller than size of first field');
+       halt(1);
+     end;
+{$else SKIP_CONST_POINTER}
+   Res:=2;
+{$endif SKIP_CONST_POINTER}
+   Res2 := @(PRecord(@Rec)^.Second) - @(PRecord(@Rec)^.First);
+   writeln('Offset of Second field inside TRecord is ',Res2);
+   if (Res2<2) then
+     begin
+       writeln('Offset of second field is smaller than size of first field');
+       halt(2);
+     end;
+   if (Res<>Res2) then
+     begin
+       writeln('Inconsistent results for Offset of second field between constant and non-constant pointers');
+       halt(3);
+     end;
+   Res3 := @(Rec.Second) - @(Rec.First);
+   if (Res3<2) then
+     begin
+       writeln('Offset of second field is smaller than size of first field');
+       halt(4);
+     end;
+   if (Res<>Res3) then
+     begin
+       writeln('Inconsistent results for Offset of second field between constant and non-constant pointers');
+       halt(5);
+     end;
+end.

+ 32 - 0
tests/webtbs/tw41460a.pp

@@ -0,0 +1,32 @@
+program calculate_offset;
+
+{$T+}
+{ In T+ mode, i.e. type pointer mode,
+  the difference between two pointers of the same type
+  is divided by the byte size of that type }
+type
+   TRecord = record
+     First: word;
+     Second: word;
+   end;
+   PRecord = ^TRecord;
+
+var
+   res, res_nil : PtrInt;
+   Rec : TRecord;
+begin
+   Res := @(PRecord(@Rec)^.Second) - @(PRecord(@Rec)^.First);
+   writeln('Number of word fields between Second and First is ',Res);
+   if (Res<>1) then
+     begin
+       writeln('Error in typed pointer arithmetics');
+       halt(1);
+     end;
+   Res_nil := @(PRecord(nil)^.Second) - @(PRecord(nil)^.First);
+   writeln('Number of word fields between Second and First is ',Res_nil);
+   if (Res_nil<>1) then
+     begin
+       writeln('Error in constant typed pointer arithmetics');
+       halt(2);
+     end;
+end.

+ 39 - 0
tests/webtbs/tw41460c.pp

@@ -0,0 +1,39 @@
+
+program calculate_offset;
+
+{$T-}
+{ In T- mode, i.e. not in typed pointer mode,
+  the difference between two pointers of the same type
+  is divided by number of bytes between the two pointers }
+
+type
+   TRecord = record
+     First: word;
+     Second: qword;
+   end;
+   PRecord = ^TRecord;
+
+var
+   res,res2 : PtrInt;
+   Rec : TRecord;
+begin
+   Res := @(PRecord(@Rec)^.Second) - @(PRecord(@Rec)^.First);
+   writeln('Offset of Second field inside TRecord is ',Res);
+   if (Res<2) then
+     begin
+       writeln('Offset of second field is smaller than size of first field');
+       halt(1);
+     end;
+   Res2 := @(PRecord(nil)^.Second) - @(PRecord(nil)^.First);
+   writeln('Offset of Second field inside TRecord  using constant pointers is ',Res2);
+   if (Res2<2) then
+     begin
+       writeln('Offset of second field is smaller than size of first field');
+       halt(2);
+     end;
+   if (Res2<>Res) then
+     begin
+       writeln('Inconsistent result for offset of second field');
+       halt(3);
+     end;
+end.

+ 49 - 0
tests/webtbs/tw41460d.pp

@@ -0,0 +1,49 @@
+program calculate_offset;
+
+{$T-}
+
+type
+   TRecord = record
+     First: byte;
+     Second: byte;
+     Third: byte;
+     Fourth: byte;
+   end;
+   PRecord = ^TRecord;
+
+var
+   res, res2, res3 : PtrInt;
+   Rec : TRecord;
+
+begin
+   Res := @(PRecord(nil)^.Fourth) - @(PRecord(nil)^.First);
+   writeln('Offset of Fourth field inside TRecord is ',Res);
+   if (Res<3) then
+     begin
+       writeln('Offset of Fourth field is smaller than size of first field');
+       halt(1);
+     end;
+   Res2 := @(PRecord(@Rec)^.Fourth) - @(PRecord(@Rec)^.First);
+   writeln('Offset of Fourth field inside TRecord is ',Res2);
+   if (Res2<3) then
+     begin
+       writeln('Offset of Fourth field is smaller than size of first field');
+       halt(2);
+     end;
+   if (Res<>Res2) then
+     begin
+       writeln('Inconsistent results for Offset of Fourth field between constant and non-constant pointers');
+       halt(3);
+     end;
+   Res3 := @(Rec.Fourth) - @(Rec.First);
+   if (Res3<3) then
+     begin
+       writeln('Offset of Fourth field is smaller than size of first field');
+       halt(4);
+     end;
+   if (Res<>Res3) then
+     begin
+       writeln('Inconsistent results for Offset of Fourth field between constant and non-constant pointers');
+       halt(5);
+     end;
+end.