blob: 39752cf70957658efa8632e1ce7615a2c1bce699 [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
Jens Geyer9bb4c112014-07-03 23:05:54 +020020program TestSerializer;
Roger Meier2b2c0b22012-09-12 20:09:02 +000021
22{$APPTYPE CONSOLE}
23
24uses
Jens Geyerfad7fd32019-11-09 23:24:52 +010025 Classes,
26 Windows,
27 SysUtils,
28 Generics.Collections,
Roger Meier2b2c0b22012-09-12 20:09:02 +000029 Thrift in '..\..\src\Thrift.pas',
Jens Geyer606f1ef2018-04-09 23:09:41 +020030 Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Jens Geyerbea9bbe2016-04-20 00:02:40 +020031 Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Roger Meier2b2c0b22012-09-12 20:09:02 +000032 Thrift.Transport in '..\..\src\Thrift.Transport.pas',
33 Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
34 Thrift.Protocol.JSON in '..\..\src\Thrift.Protocol.JSON.pas',
Jens Geyera715f702019-08-28 22:56:13 +020035 Thrift.Protocol.Compact in '..\..\src\Thrift.Protocol.Compact.pas',
Roger Meier2b2c0b22012-09-12 20:09:02 +000036 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
37 Thrift.Server in '..\..\src\Thrift.Server.pas',
Roger Meier2b2c0b22012-09-12 20:09:02 +000038 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
39 Thrift.Serializer in '..\..\src\Thrift.Serializer.pas',
40 Thrift.Stream in '..\..\src\Thrift.Stream.pas',
Jens Geyer02230912019-04-03 01:12:51 +020041 Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas',
Jens Geyer9bb4c112014-07-03 23:05:54 +020042 Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
Jens Geyer92d80622018-05-02 22:28:44 +020043 System_,
Roger Meier2b2c0b22012-09-12 20:09:02 +000044 DebugProtoTest,
45 TestSerializer.Data;
46
Roger Meier2b2c0b22012-09-12 20:09:02 +000047type
48 TTestSerializer = class //extends TestCase {
Jens Geyera715f702019-08-28 22:56:13 +020049 private type
50 TMethod = (
51 mt_Bytes,
52 mt_Stream
53 );
54
Roger Meier2b2c0b22012-09-12 20:09:02 +000055 private
56 FProtocols : TList< IProtocolFactory>;
57
58 class function Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes; overload;
59 class procedure Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream); overload;
60 class procedure Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory); overload;
61 class procedure Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory); overload;
62
Jens Geyer9bb4c112014-07-03 23:05:54 +020063 procedure Test_Serializer_Deserializer;
Jens Geyera715f702019-08-28 22:56:13 +020064 procedure Test_OneOfEach( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
65 procedure Test_CompactStruct( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
Jens Geyer9bb4c112014-07-03 23:05:54 +020066
Roger Meier2b2c0b22012-09-12 20:09:02 +000067 public
68 constructor Create;
69 destructor Destroy; override;
70
Jens Geyer9bb4c112014-07-03 23:05:54 +020071 procedure RunTests;
Roger Meier2b2c0b22012-09-12 20:09:02 +000072 end;
73
74
75
76{ TTestSerializer }
77
78constructor TTestSerializer.Create;
79begin
80 inherited Create;
81 FProtocols := TList< IProtocolFactory>.Create;
82 FProtocols.Add( TBinaryProtocolImpl.TFactory.Create);
Jens Geyera715f702019-08-28 22:56:13 +020083 FProtocols.Add( TCompactProtocolImpl.TFactory.Create);
Roger Meier2b2c0b22012-09-12 20:09:02 +000084 FProtocols.Add( TJSONProtocolImpl.TFactory.Create);
85end;
86
87
88destructor TTestSerializer.Destroy;
89begin
90 try
91 FreeAndNil( FProtocols);
92 finally
93 inherited Destroy;
94 end;
95end;
96
Jens Geyera715f702019-08-28 22:56:13 +020097
98procedure TTestSerializer.Test_OneOfEach( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
99var tested, correct : IOneOfEach;
100 bytes : TBytes;
101 i : Integer;
102begin
103 // write
104 tested := Fixtures.CreateOneOfEach;
105 case method of
106 mt_Bytes: bytes := Serialize( tested, factory);
107 mt_Stream: begin
108 stream.Size := 0;
109 Serialize( tested, factory, stream);
110 end
111 else
112 ASSERT( FALSE);
113 end;
114
115 // init + read
116 tested := TOneOfEachImpl.Create;
117 case method of
118 mt_Bytes: Deserialize( bytes, tested, factory);
119 mt_Stream: begin
120 stream.Position := 0;
121 Deserialize( stream, tested, factory);
122 end
123 else
124 ASSERT( FALSE);
125 end;
126
127 // check
128 correct := Fixtures.CreateOneOfEach;
129 ASSERT( tested.Im_true = correct.Im_true);
130 ASSERT( tested.Im_false = correct.Im_false);
131 ASSERT( tested.A_bite = correct.A_bite);
132 ASSERT( tested.Integer16 = correct.Integer16);
133 ASSERT( tested.Integer32 = correct.Integer32);
134 ASSERT( tested.Integer64 = correct.Integer64);
135 ASSERT( Abs( tested.Double_precision - correct.Double_precision) < 1E-12);
136 ASSERT( tested.Some_characters = correct.Some_characters);
137 ASSERT( tested.Zomg_unicode = correct.Zomg_unicode);
138 ASSERT( tested.What_who = correct.What_who);
139
140 ASSERT( Length(tested.Base64) = Length(correct.Base64));
141 ASSERT( CompareMem( @tested.Base64[0], @correct.Base64[0], Length(correct.Base64)));
142
143 ASSERT( tested.Byte_list.Count = correct.Byte_list.Count);
144 for i := 0 to tested.Byte_list.Count-1
145 do ASSERT( tested.Byte_list[i] = correct.Byte_list[i]);
146
147 ASSERT( tested.I16_list.Count = correct.I16_list.Count);
148 for i := 0 to tested.I16_list.Count-1
149 do ASSERT( tested.I16_list[i] = correct.I16_list[i]);
150
151 ASSERT( tested.I64_list.Count = correct.I64_list.Count);
152 for i := 0 to tested.I64_list.Count-1
153 do ASSERT( tested.I64_list[i] = correct.I64_list[i]);
154end;
155
156
157procedure TTestSerializer.Test_CompactStruct( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
158var tested, correct : ICompactProtoTestStruct;
159 bytes : TBytes;
160begin
161 // write
162 tested := Fixtures.CreateCompactProtoTestStruct;
163 case method of
164 mt_Bytes: bytes := Serialize( tested, factory);
165 mt_Stream: begin
166 stream.Size := 0;
167 Serialize( tested, factory, stream);
168 end
169 else
170 ASSERT( FALSE);
171 end;
172
173 // init + read
174 correct := TCompactProtoTestStructImpl.Create;
175 case method of
176 mt_Bytes: Deserialize( bytes, tested, factory);
177 mt_Stream: begin
178 stream.Position := 0;
179 Deserialize( stream, tested, factory);
180 end
181 else
182 ASSERT( FALSE);
183 end;
184
185 // check
186 correct := Fixtures.CreateCompactProtoTestStruct;
187 ASSERT( correct.Field500 = tested.Field500);
188 ASSERT( correct.Field5000 = tested.Field5000);
189 ASSERT( correct.Field20000 = tested.Field20000);
190end;
Jens Geyer9bb4c112014-07-03 23:05:54 +0200191
192
193procedure TTestSerializer.Test_Serializer_Deserializer;
Jens Geyera715f702019-08-28 22:56:13 +0200194var factory : IProtocolFactory;
Roger Meier2b2c0b22012-09-12 20:09:02 +0000195 stream : TFileStream;
Roger Meier2b2c0b22012-09-12 20:09:02 +0000196 method : TMethod;
197begin
Roger Meier2b2c0b22012-09-12 20:09:02 +0000198 stream := TFileStream.Create( 'TestSerializer.dat', fmCreate);
199 try
200
201 for method in [Low(TMethod)..High(TMethod)] do begin
202 for factory in FProtocols do begin
203
Jens Geyera715f702019-08-28 22:56:13 +0200204 Test_OneOfEach( method, factory, stream);
205 Test_CompactStruct( method, factory, stream);
Roger Meier2b2c0b22012-09-12 20:09:02 +0000206 end;
207 end;
208
209 finally
210 stream.Free;
211 end;
212end;
213
214
Jens Geyer9bb4c112014-07-03 23:05:54 +0200215procedure TTestSerializer.RunTests;
216begin
217 try
218 Test_Serializer_Deserializer;
219 except
220 on e:Exception do begin
221 Writeln( e.Message);
222 Write('Hit ENTER to close ... '); Readln;
223 end;
224 end;
225end;
226
227
Roger Meier2b2c0b22012-09-12 20:09:02 +0000228class function TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes;
229var serial : TSerializer;
230begin
231 serial := TSerializer.Create( factory);
232 try
233 result := serial.Serialize( input);
234 finally
235 serial.Free;
236 end;
237end;
238
239
240class procedure TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream);
241var serial : TSerializer;
242begin
243 serial := TSerializer.Create( factory);
244 try
245 serial.Serialize( input, aStream);
246 finally
247 serial.Free;
248 end;
249end;
250
251
252class procedure TTestSerializer.Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory);
253var serial : TDeserializer;
254begin
255 serial := TDeserializer.Create( factory);
256 try
257 serial.Deserialize( input, target);
258 finally
259 serial.Free;
260 end;
261end;
262
263class procedure TTestSerializer.Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory);
264var serial : TDeserializer;
265begin
266 serial := TDeserializer.Create( factory);
267 try
268 serial.Deserialize( input, target);
269 finally
270 serial.Free;
271 end;
272end;
273
274
275var test : TTestSerializer;
276begin
277 test := TTestSerializer.Create;
278 try
Jens Geyer9bb4c112014-07-03 23:05:54 +0200279 test.RunTests;
Roger Meier2b2c0b22012-09-12 20:09:02 +0000280 finally
281 test.Free;
282 end;
283end.
284