talign2.pp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. {$define haswidestring}
  13. {$else}
  14. {$ifndef ver70}
  15. {$define haswidestring}
  16. {$endif}
  17. {$endif}
  18. {$ifdef fpc}
  19. {$ifdef unix}
  20. uses
  21. cwstring;
  22. {$endif}
  23. {$endif}
  24. procedure test(b : boolean);
  25. begin
  26. if b then exit;
  27. WriteLn('Error in length/alignment!!');
  28. halt(1);
  29. end;
  30. var
  31. pt: pchar;
  32. const
  33. b: byte = 0; { lets just misalign the stuff }
  34. p : pchar = 'simple pchar stuff';
  35. ansistr : ansistring = 'simple ansistring';
  36. {$ifdef haswidestring}
  37. widestr : widestring = 'simple widestring';
  38. {$endif}
  39. shortstr :shortstring = 'simple shortstring';
  40. begin
  41. test(length(ansistr)=17);
  42. {$ifdef haswidestring}
  43. test(length(widestr)=17);
  44. {$endif}
  45. test(length(shortstr)=18);
  46. { verify if the address are correctly aligned! }
  47. pt:=@shortstr;
  48. test((ptruint(pt) mod sizeof(pointer))=0);
  49. pt:=p;
  50. test((ptruint(pt) mod sizeof(pointer))=0);
  51. pt:=pchar(ansistr);
  52. test((ptruint(pt) mod sizeof(pointer))=0);
  53. {$ifdef haswidestring}
  54. pt:=pchar(widestr);
  55. {$ifdef FPC_WINLIKEWIDESTRING}
  56. test((ptruint(pt) mod 4)=0);
  57. {$else FPC_WINLIKEWIDESTRING}
  58. test((ptruint(pt) mod sizeof(pointer))=0);
  59. {$endif FPC_WINLIKEWIDESTRING}
  60. {$endif}
  61. end.