| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- { t48_strcmp: string comparison, all six operators, string VARIABLES.
- String subscripts s[i] are TChar, so s[i] = c is a char compare; this
- pins string-vs-string, which is cmpstr (TPSRC2) / vcompare (TPSRC9).
- Each block weights the six operators 1,2,4,8,16,32 for =,<>,<,<=,>,>= so
- the printed sum is a SIGNATURE of which ones held:
- 41 equal (1+8+32) 14 left < right (2+4+8)
- 50 left > right (2+16+32)
- A plain +1 per operator cannot tell < from >: both come to 3, and so does
- every other unequal pair, because [a<b]+[a<=b]+[a>b]+[a>=b] is always 2.
- That is not a hypothetical - it is how the first version of this fixture
- reported 3,3,3,3 while every ordering comparison was inverted. }
- program t48_strcmp (input, output);
- var
- a, b : string[8];
- n : integer;
- begin
- a := 'abc';
- b := 'abc';
- n := 0;
- if a = b then n := n + 1;
- if a <> b then n := n + 2;
- if a < b then n := n + 4;
- if a <= b then n := n + 8;
- if a > b then n := n + 16;
- if a >= b then n := n + 32;
- writeln(n); { 41: =, <=, >= }
- b := 'abd'; { same length, differs at byte 3 }
- n := 0;
- if a = b then n := n + 1;
- if a <> b then n := n + 2;
- if a < b then n := n + 4;
- if a <= b then n := n + 8;
- if a > b then n := n + 16;
- if a >= b then n := n + 32;
- writeln(n); { 14: 'abc' < 'abd' }
- b := 'ab'; { b is a strict PREFIX of a }
- n := 0;
- if a = b then n := n + 1;
- if a <> b then n := n + 2;
- if a < b then n := n + 4;
- if a <= b then n := n + 8;
- if a > b then n := n + 16;
- if a >= b then n := n + 32;
- writeln(n); { 50: longer means greater }
- a := 'ab'; { a is a strict PREFIX of b }
- b := 'abc';
- n := 0;
- if a = b then n := n + 1;
- if a <> b then n := n + 2;
- if a < b then n := n + 4;
- if a <= b then n := n + 8;
- if a > b then n := n + 16;
- if a >= b then n := n + 32;
- writeln(n); { 14: shorter means less }
- b := ''; { the empty string: min(lenL,lenR) = 0,
- the JCXZ arm of cmpstr's loop }
- n := 0;
- if a = b then n := n + 1;
- if a <> b then n := n + 2;
- if a < b then n := n + 4;
- if a <= b then n := n + 8;
- if a > b then n := n + 16;
- if a >= b then n := n + 32;
- writeln(n); { 50: 'ab' > '' }
- b := 'abc'; { all six as VALUES, not branches }
- writeln(a = b, ' ', a <> b, ' ', a < b, ' ', a <= b, ' ',
- a > b, ' ', a >= b)
- end.
|