blob: 60e55c1ca32c14a95aeaf079bcaedfa036a38acd [file] [log] [blame]
Roger Meier2b2c0b22012-09-12 20:09:02 +00001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20program skiptest_version1;
21
22{$APPTYPE CONSOLE}
23
24uses
25 Classes, Windows, SysUtils, Generics.Collections,
26 Thrift in '..\..\src\Thrift.pas',
27 Thrift.Transport in '..\..\src\Thrift.Transport.pas',
28 Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
29 Thrift.Protocol.JSON in '..\..\src\Thrift.Protocol.JSON.pas',
30 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
31 Thrift.Server in '..\..\src\Thrift.Server.pas',
32 Thrift.Console in '..\..\src\Thrift.Console.pas',
33 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
34 Thrift.Serializer in '..\..\src\Thrift.Serializer.pas',
35 Thrift.Stream in '..\..\src\Thrift.Stream.pas',
36 DebugProtoTest,
37 TestSerializer.Data;
38
39
40
41type
42 TTestSerializer = class //extends TestCase {
43 private
44 FProtocols : TList< IProtocolFactory>;
45
46 class function Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes; overload;
47 class procedure Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream); overload;
48 class procedure Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory); overload;
49 class procedure Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory); overload;
50
51 public
52 constructor Create;
53 destructor Destroy; override;
54
55 procedure TestDeserialize;
56 end;
57
58
59
60{ TTestSerializer }
61
62constructor TTestSerializer.Create;
63begin
64 inherited Create;
65 FProtocols := TList< IProtocolFactory>.Create;
66 FProtocols.Add( TBinaryProtocolImpl.TFactory.Create);
67 //FProtocols.Add( TCompactProtocolImpl.TFactory.Create);
68 FProtocols.Add( TJSONProtocolImpl.TFactory.Create);
69end;
70
71
72destructor TTestSerializer.Destroy;
73begin
74 try
75 FreeAndNil( FProtocols);
76 finally
77 inherited Destroy;
78 end;
79end;
80
81
82procedure TTestSerializer.TestDeserialize;
83type TMethod = (mt_Bytes, mt_Stream);
84var level3ooe, correct : IOneOfEach;
85 factory : IProtocolFactory;
86 bytes : TBytes;
87 stream : TFileStream;
88 i : Integer;
89 method : TMethod;
90begin
91 correct := Fixtures.CreateOneOfEach;
92 stream := TFileStream.Create( 'TestSerializer.dat', fmCreate);
93 try
94
95 for method in [Low(TMethod)..High(TMethod)] do begin
96 for factory in FProtocols do begin
97
98 // write
99 level3ooe := Fixtures.CreateOneOfEach;
100 case method of
101 mt_Bytes: bytes := Serialize( level3ooe, factory);
102 mt_Stream: begin
103 stream.Size := 0;
104 Serialize( level3ooe, factory, stream);
105 end
106 else
107 ASSERT( FALSE);
108 end;
109
110 // init + read
111 level3ooe := TOneOfEachImpl.Create;
112 case method of
113 mt_Bytes: Deserialize( bytes, level3ooe, factory);
114 mt_Stream: begin
115 stream.Position := 0;
116 Deserialize( stream, level3ooe, factory);
117 end
118 else
119 ASSERT( FALSE);
120 end;
121
122
123 // check
124 ASSERT( level3ooe.Im_true = correct.Im_true);
125 ASSERT( level3ooe.Im_false = correct.Im_false);
126 ASSERT( level3ooe.A_bite = correct.A_bite);
127 ASSERT( level3ooe.Integer16 = correct.Integer16);
128 ASSERT( level3ooe.Integer32 = correct.Integer32);
129 ASSERT( level3ooe.Integer64 = correct.Integer64);
130 ASSERT( Abs( level3ooe.Double_precision - correct.Double_precision) < 1E-12);
131 ASSERT( level3ooe.Some_characters = correct.Some_characters);
132 ASSERT( level3ooe.Zomg_unicode = correct.Zomg_unicode);
133 ASSERT( level3ooe.What_who = correct.What_who);
134 ASSERT( level3ooe.Base64 = correct.Base64);
135
136 ASSERT( level3ooe.Byte_list.Count = correct.Byte_list.Count);
137 for i := 0 to level3ooe.Byte_list.Count-1
138 do ASSERT( level3ooe.Byte_list[i] = correct.Byte_list[i]);
139
140 ASSERT( level3ooe.I16_list.Count = correct.I16_list.Count);
141 for i := 0 to level3ooe.I16_list.Count-1
142 do ASSERT( level3ooe.I16_list[i] = correct.I16_list[i]);
143
144 ASSERT( level3ooe.I64_list.Count = correct.I64_list.Count);
145 for i := 0 to level3ooe.I64_list.Count-1
146 do ASSERT( level3ooe.I64_list[i] = correct.I64_list[i]);
147 end;
148 end;
149
150 finally
151 stream.Free;
152 end;
153end;
154
155
156class function TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes;
157var serial : TSerializer;
158begin
159 serial := TSerializer.Create( factory);
160 try
161 result := serial.Serialize( input);
162 finally
163 serial.Free;
164 end;
165end;
166
167
168class procedure TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream);
169var serial : TSerializer;
170begin
171 serial := TSerializer.Create( factory);
172 try
173 serial.Serialize( input, aStream);
174 finally
175 serial.Free;
176 end;
177end;
178
179
180class procedure TTestSerializer.Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory);
181var serial : TDeserializer;
182begin
183 serial := TDeserializer.Create( factory);
184 try
185 serial.Deserialize( input, target);
186 finally
187 serial.Free;
188 end;
189end;
190
191class procedure TTestSerializer.Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory);
192var serial : TDeserializer;
193begin
194 serial := TDeserializer.Create( factory);
195 try
196 serial.Deserialize( input, target);
197 finally
198 serial.Free;
199 end;
200end;
201
202
203var test : TTestSerializer;
204begin
205 test := TTestSerializer.Create;
206 try
207 test.TestDeserialize;
208 finally
209 test.Free;
210 end;
211end.
212