t48_strcmp.pas 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. { t48_strcmp: string comparison, all six operators, string VARIABLES.
  2. String subscripts s[i] are TChar, so s[i] = c is a char compare; this
  3. pins string-vs-string, which is cmpstr (TPSRC2) / vcompare (TPSRC9).
  4. Each block weights the six operators 1,2,4,8,16,32 for =,<>,<,<=,>,>= so
  5. the printed sum is a SIGNATURE of which ones held:
  6. 41 equal (1+8+32) 14 left < right (2+4+8)
  7. 50 left > right (2+16+32)
  8. A plain +1 per operator cannot tell < from >: both come to 3, and so does
  9. every other unequal pair, because [a<b]+[a<=b]+[a>b]+[a>=b] is always 2.
  10. That is not a hypothetical - it is how the first version of this fixture
  11. reported 3,3,3,3 while every ordering comparison was inverted. }
  12. program t48_strcmp (input, output);
  13. var
  14. a, b : string[8];
  15. n : integer;
  16. begin
  17. a := 'abc';
  18. b := 'abc';
  19. n := 0;
  20. if a = b then n := n + 1;
  21. if a <> b then n := n + 2;
  22. if a < b then n := n + 4;
  23. if a <= b then n := n + 8;
  24. if a > b then n := n + 16;
  25. if a >= b then n := n + 32;
  26. writeln(n); { 41: =, <=, >= }
  27. b := 'abd'; { same length, differs at byte 3 }
  28. n := 0;
  29. if a = b then n := n + 1;
  30. if a <> b then n := n + 2;
  31. if a < b then n := n + 4;
  32. if a <= b then n := n + 8;
  33. if a > b then n := n + 16;
  34. if a >= b then n := n + 32;
  35. writeln(n); { 14: 'abc' < 'abd' }
  36. b := 'ab'; { b is a strict PREFIX of a }
  37. n := 0;
  38. if a = b then n := n + 1;
  39. if a <> b then n := n + 2;
  40. if a < b then n := n + 4;
  41. if a <= b then n := n + 8;
  42. if a > b then n := n + 16;
  43. if a >= b then n := n + 32;
  44. writeln(n); { 50: longer means greater }
  45. a := 'ab'; { a is a strict PREFIX of b }
  46. b := 'abc';
  47. n := 0;
  48. if a = b then n := n + 1;
  49. if a <> b then n := n + 2;
  50. if a < b then n := n + 4;
  51. if a <= b then n := n + 8;
  52. if a > b then n := n + 16;
  53. if a >= b then n := n + 32;
  54. writeln(n); { 14: shorter means less }
  55. b := ''; { the empty string: min(lenL,lenR) = 0,
  56. the JCXZ arm of cmpstr's loop }
  57. n := 0;
  58. if a = b then n := n + 1;
  59. if a <> b then n := n + 2;
  60. if a < b then n := n + 4;
  61. if a <= b then n := n + 8;
  62. if a > b then n := n + 16;
  63. if a >= b then n := n + 32;
  64. writeln(n); { 50: 'ab' > '' }
  65. b := 'abc'; { all six as VALUES, not branches }
  66. writeln(a = b, ' ', a <> b, ' ', a < b, ' ', a <= b, ' ',
  67. a > b, ' ', a >= b)
  68. end.