blob: 1f5ae8bbbd535ad414623d97418dc14ac5105743 [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
25 Classes, Windows, SysUtils, Generics.Collections,
26 Thrift in '..\..\src\Thrift.pas',
Jens Geyer606f1ef2018-04-09 23:09:41 +020027 Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Jens Geyerbea9bbe2016-04-20 00:02:40 +020028 Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Roger Meier2b2c0b22012-09-12 20:09:02 +000029 Thrift.Transport in '..\..\src\Thrift.Transport.pas',
30 Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
31 Thrift.Protocol.JSON in '..\..\src\Thrift.Protocol.JSON.pas',
32 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
33 Thrift.Server in '..\..\src\Thrift.Server.pas',
Roger Meier2b2c0b22012-09-12 20:09:02 +000034 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
35 Thrift.Serializer in '..\..\src\Thrift.Serializer.pas',
36 Thrift.Stream in '..\..\src\Thrift.Stream.pas',
Jens Geyer02230912019-04-03 01:12:51 +020037 Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas',
Jens Geyer9bb4c112014-07-03 23:05:54 +020038 Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
Jens Geyer92d80622018-05-02 22:28:44 +020039 System_,
Roger Meier2b2c0b22012-09-12 20:09:02 +000040 DebugProtoTest,
41 TestSerializer.Data;
42
43
44
45type
46 TTestSerializer = class //extends TestCase {
47 private
48 FProtocols : TList< IProtocolFactory>;
49
50 class function Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes; overload;
51 class procedure Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream); overload;
52 class procedure Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory); overload;
53 class procedure Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory); overload;
54
Jens Geyer9bb4c112014-07-03 23:05:54 +020055 procedure Test_Serializer_Deserializer;
56
Roger Meier2b2c0b22012-09-12 20:09:02 +000057 public
58 constructor Create;
59 destructor Destroy; override;
60
Jens Geyer9bb4c112014-07-03 23:05:54 +020061 procedure RunTests;
Roger Meier2b2c0b22012-09-12 20:09:02 +000062 end;
63
64
65
66{ TTestSerializer }
67
68constructor TTestSerializer.Create;
69begin
70 inherited Create;
71 FProtocols := TList< IProtocolFactory>.Create;
72 FProtocols.Add( TBinaryProtocolImpl.TFactory.Create);
73 //FProtocols.Add( TCompactProtocolImpl.TFactory.Create);
74 FProtocols.Add( TJSONProtocolImpl.TFactory.Create);
75end;
76
77
78destructor TTestSerializer.Destroy;
79begin
80 try
81 FreeAndNil( FProtocols);
82 finally
83 inherited Destroy;
84 end;
85end;
86
Roger Meier2b2c0b22012-09-12 20:09:02 +000087type TMethod = (mt_Bytes, mt_Stream);
Jens Geyer9bb4c112014-07-03 23:05:54 +020088
89
90procedure TTestSerializer.Test_Serializer_Deserializer;
Roger Meier2b2c0b22012-09-12 20:09:02 +000091var level3ooe, correct : IOneOfEach;
92 factory : IProtocolFactory;
93 bytes : TBytes;
94 stream : TFileStream;
95 i : Integer;
96 method : TMethod;
97begin
98 correct := Fixtures.CreateOneOfEach;
99 stream := TFileStream.Create( 'TestSerializer.dat', fmCreate);
100 try
101
102 for method in [Low(TMethod)..High(TMethod)] do begin
103 for factory in FProtocols do begin
104
105 // write
106 level3ooe := Fixtures.CreateOneOfEach;
107 case method of
108 mt_Bytes: bytes := Serialize( level3ooe, factory);
109 mt_Stream: begin
110 stream.Size := 0;
111 Serialize( level3ooe, factory, stream);
112 end
113 else
114 ASSERT( FALSE);
115 end;
116
117 // init + read
118 level3ooe := TOneOfEachImpl.Create;
119 case method of
120 mt_Bytes: Deserialize( bytes, level3ooe, factory);
121 mt_Stream: begin
122 stream.Position := 0;
123 Deserialize( stream, level3ooe, factory);
124 end
125 else
126 ASSERT( FALSE);
127 end;
128
129
130 // check
131 ASSERT( level3ooe.Im_true = correct.Im_true);
132 ASSERT( level3ooe.Im_false = correct.Im_false);
133 ASSERT( level3ooe.A_bite = correct.A_bite);
134 ASSERT( level3ooe.Integer16 = correct.Integer16);
135 ASSERT( level3ooe.Integer32 = correct.Integer32);
136 ASSERT( level3ooe.Integer64 = correct.Integer64);
137 ASSERT( Abs( level3ooe.Double_precision - correct.Double_precision) < 1E-12);
138 ASSERT( level3ooe.Some_characters = correct.Some_characters);
139 ASSERT( level3ooe.Zomg_unicode = correct.Zomg_unicode);
140 ASSERT( level3ooe.What_who = correct.What_who);
141 ASSERT( level3ooe.Base64 = correct.Base64);
142
143 ASSERT( level3ooe.Byte_list.Count = correct.Byte_list.Count);
144 for i := 0 to level3ooe.Byte_list.Count-1
145 do ASSERT( level3ooe.Byte_list[i] = correct.Byte_list[i]);
146
147 ASSERT( level3ooe.I16_list.Count = correct.I16_list.Count);
148 for i := 0 to level3ooe.I16_list.Count-1
149 do ASSERT( level3ooe.I16_list[i] = correct.I16_list[i]);
150
151 ASSERT( level3ooe.I64_list.Count = correct.I64_list.Count);
152 for i := 0 to level3ooe.I64_list.Count-1
153 do ASSERT( level3ooe.I64_list[i] = correct.I64_list[i]);
154 end;
155 end;
156
157 finally
158 stream.Free;
159 end;
160end;
161
162
Jens Geyer9bb4c112014-07-03 23:05:54 +0200163procedure TTestSerializer.RunTests;
164begin
165 try
166 Test_Serializer_Deserializer;
167 except
168 on e:Exception do begin
169 Writeln( e.Message);
170 Write('Hit ENTER to close ... '); Readln;
171 end;
172 end;
173end;
174
175
Roger Meier2b2c0b22012-09-12 20:09:02 +0000176class function TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes;
177var serial : TSerializer;
178begin
179 serial := TSerializer.Create( factory);
180 try
181 result := serial.Serialize( input);
182 finally
183 serial.Free;
184 end;
185end;
186
187
188class procedure TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream);
189var serial : TSerializer;
190begin
191 serial := TSerializer.Create( factory);
192 try
193 serial.Serialize( input, aStream);
194 finally
195 serial.Free;
196 end;
197end;
198
199
200class procedure TTestSerializer.Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory);
201var serial : TDeserializer;
202begin
203 serial := TDeserializer.Create( factory);
204 try
205 serial.Deserialize( input, target);
206 finally
207 serial.Free;
208 end;
209end;
210
211class procedure TTestSerializer.Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory);
212var serial : TDeserializer;
213begin
214 serial := TDeserializer.Create( factory);
215 try
216 serial.Deserialize( input, target);
217 finally
218 serial.Free;
219 end;
220end;
221
222
223var test : TTestSerializer;
224begin
225 test := TTestSerializer.Create;
226 try
Jens Geyer9bb4c112014-07-03 23:05:54 +0200227 test.RunTests;
Roger Meier2b2c0b22012-09-12 20:09:02 +0000228 finally
229 test.Free;
230 end;
231end.
232