talign2.pp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. { %VERSION=1.1 }
  2. {%OPT=-Og}
  3. { This verifies if the strings are
  4. correctly aligned, normally the generated assembler
  5. should be verified manually.
  6. I consider this test as flawed, or is there a reason, why a
  7. shortstring should be aligned to pointer boundaries? (FK)
  8. }
  9. program talign2;
  10. {$ifdef fpc}
  11. {$mode objfpc}
  12. {$ifndef ver1_0}
  13. {$define haswidestring}
  14. {$endif}
  15. {$else}
  16. {$ifndef ver70}
  17. {$define haswidestring}
  18. {$endif}
  19. {$endif}
  20. procedure test(b : boolean);
  21. begin
  22. if b then exit;
  23. WriteLn('Error in length/alignment!!');
  24. halt(1);
  25. end;
  26. var
  27. pt: pchar;
  28. const
  29. b: byte = 0; { lets just misalign the stuff }
  30. p : pchar = 'simple pchar stuff';
  31. ansistr : ansistring = 'simple ansistring';
  32. {$ifdef haswidestring}
  33. widestr : widestring = 'simple widestring';
  34. {$endif}
  35. shortstr :shortstring = 'simple shortstring';
  36. begin
  37. test(length(ansistr)=17);
  38. {$ifdef haswidestring}
  39. test(length(widestr)=17);
  40. {$endif}
  41. test(length(shortstr)=18);
  42. { verify if the address are correctly aligned! }
  43. pt:=@shortstr;
  44. test((ptruint(pt) mod sizeof(pointer))=0);
  45. pt:=p;
  46. test((ptruint(pt) mod sizeof(pointer))=0);
  47. pt:=pchar(ansistr);
  48. test((ptruint(pt) mod sizeof(pointer))=0);
  49. {$ifdef haswidestring}
  50. pt:=pchar(widestr);
  51. {$ifdef FPC_WINLIKEWIDESTRING}
  52. test((ptruint(pt) mod 4)=0);
  53. {$else FPC_WINLIKEWIDESTRING}
  54. test((ptruint(pt) mod sizeof(pointer))=0);
  55. {$endif FPC_WINLIKEWIDESTRING}
  56. {$endif}
  57. end.