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