File: testvarious.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (93 lines) | stat: -rw-r--r-- 1,858 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
unit TestVarious;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FpPascalBuilder, LazLogger, TestOutputLogger, fpcunit,
  testutils, testregistry;

type

  { TTestVarious }

  TTestVarious = class(TTestCase)
  published
    procedure TestQuoteString;
  end;

implementation

procedure TTestVarious.TestQuoteString;
const
  N: array [0..9] of string = (
    #0, #1, 'a', '1', '''',
    'ä', // correct utf8
    #193, #225#129, #225, #129  // broken
  );
  E: array [0..9] of string = (
    '#$00', '#$01', 'a', '1', '''''',
    'ä',
    '#$C1', '#$E1#$81', '#$E1', '#$81'  // broken
  );

  Q='''';
var
  c: array [1..5] of integer;
  l, i: Integer;
  s1, s2: String;
  InQuote: Boolean;
begin
  AssertEquals(Q+Q,       QuoteText(''));
  AssertEquals(Q+'a'+Q,      QuoteText('a'));
  AssertEquals(Q+Q+Q+Q,     QuoteText(Q));
  AssertEquals('#$09',       QuoteText(#9));

  for l := 1 to 5 do begin
    for i := 1 to l do c[i] := 0;
    while true do begin
      s1 := '';
      s2 := '';
      InQuote := False;
      for i := 1 to l do begin
        s1 := s1 + N[c[i]]; // input
        if (E[c[i]][1] <> '#') xor InQuote then begin
          s2 := s2 + Q;
          InQuote := not InQuote;
        end;
        s2 := s2 + E[c[i]]; // expect
      end;
      if InQuote then
        s2 := s2 + Q;

      AssertEquals(s2, QuoteText(s1));

      // iterate all variations for length 1 - 5
      i := 1;
      while i <= l do begin
        if c[i] < 9 then begin
          inc(c[i]);
          // avoid combining 2 broken into one correct char
          if (c[i] in [6,7,8]) and (i < l) and (c[i+1] = 9) then
            c[i] := 9;
          break;
        end else begin
          c[i] := 0;
          inc(i);
        end;
      end;
      if i > l then
        break;
    end;
  end;

end;



initialization

  RegisterTest(TTestVarious);
end.