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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 program TestSerializer;
25 Classes, Windows, SysUtils, Generics.Collections,
26 Thrift in '..\..\src\Thrift.pas',
27 Thrift.Exception in '..\..\src\Thrift.Exception.pas',
28 Thrift.Socket in '..\..\src\Thrift.Socket.pas',
29 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.Protocol.Compact in '..\..\src\Thrift.Protocol.Compact.pas',
33 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
34 Thrift.Server in '..\..\src\Thrift.Server.pas',
35 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
36 Thrift.Serializer in '..\..\src\Thrift.Serializer.pas',
37 Thrift.Stream in '..\..\src\Thrift.Stream.pas',
38 Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas',
39 Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
47 TTestSerializer = class //extends TestCase {
55 FProtocols : TList< IProtocolFactory>;
57 class function Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes; overload;
58 class procedure Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream); overload;
59 class procedure Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory); overload;
60 class procedure Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory); overload;
62 procedure Test_Serializer_Deserializer;
63 procedure Test_OneOfEach( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
64 procedure Test_CompactStruct( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
68 destructor Destroy; override;
77 constructor TTestSerializer.Create;
80 FProtocols := TList< IProtocolFactory>.Create;
81 FProtocols.Add( TBinaryProtocolImpl.TFactory.Create);
82 FProtocols.Add( TCompactProtocolImpl.TFactory.Create);
83 FProtocols.Add( TJSONProtocolImpl.TFactory.Create);
87 destructor TTestSerializer.Destroy;
90 FreeAndNil( FProtocols);
97 procedure TTestSerializer.Test_OneOfEach( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
98 var tested, correct : IOneOfEach;
103 tested := Fixtures.CreateOneOfEach;
105 mt_Bytes: bytes := Serialize( tested, factory);
108 Serialize( tested, factory, stream);
115 tested := TOneOfEachImpl.Create;
117 mt_Bytes: Deserialize( bytes, tested, factory);
119 stream.Position := 0;
120 Deserialize( stream, tested, factory);
127 correct := Fixtures.CreateOneOfEach;
128 ASSERT( tested.Im_true = correct.Im_true);
129 ASSERT( tested.Im_false = correct.Im_false);
130 ASSERT( tested.A_bite = correct.A_bite);
131 ASSERT( tested.Integer16 = correct.Integer16);
132 ASSERT( tested.Integer32 = correct.Integer32);
133 ASSERT( tested.Integer64 = correct.Integer64);
134 ASSERT( Abs( tested.Double_precision - correct.Double_precision) < 1E-12);
135 ASSERT( tested.Some_characters = correct.Some_characters);
136 ASSERT( tested.Zomg_unicode = correct.Zomg_unicode);
137 ASSERT( tested.What_who = correct.What_who);
139 ASSERT( Length(tested.Base64) = Length(correct.Base64));
140 ASSERT( CompareMem( @tested.Base64[0], @correct.Base64[0], Length(correct.Base64)));
142 ASSERT( tested.Byte_list.Count = correct.Byte_list.Count);
143 for i := 0 to tested.Byte_list.Count-1
144 do ASSERT( tested.Byte_list[i] = correct.Byte_list[i]);
146 ASSERT( tested.I16_list.Count = correct.I16_list.Count);
147 for i := 0 to tested.I16_list.Count-1
148 do ASSERT( tested.I16_list[i] = correct.I16_list[i]);
150 ASSERT( tested.I64_list.Count = correct.I64_list.Count);
151 for i := 0 to tested.I64_list.Count-1
152 do ASSERT( tested.I64_list[i] = correct.I64_list[i]);
156 procedure TTestSerializer.Test_CompactStruct( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream);
157 var tested, correct : ICompactProtoTestStruct;
161 tested := Fixtures.CreateCompactProtoTestStruct;
163 mt_Bytes: bytes := Serialize( tested, factory);
166 Serialize( tested, factory, stream);
173 correct := TCompactProtoTestStructImpl.Create;
175 mt_Bytes: Deserialize( bytes, tested, factory);
177 stream.Position := 0;
178 Deserialize( stream, tested, factory);
185 correct := Fixtures.CreateCompactProtoTestStruct;
186 ASSERT( correct.Field500 = tested.Field500);
187 ASSERT( correct.Field5000 = tested.Field5000);
188 ASSERT( correct.Field20000 = tested.Field20000);
192 procedure TTestSerializer.Test_Serializer_Deserializer;
193 var factory : IProtocolFactory;
194 stream : TFileStream;
197 stream := TFileStream.Create( 'TestSerializer.dat', fmCreate);
200 for method in [Low(TMethod)..High(TMethod)] do begin
201 for factory in FProtocols do begin
203 Test_OneOfEach( method, factory, stream);
204 Test_CompactStruct( method, factory, stream);
214 procedure TTestSerializer.RunTests;
217 Test_Serializer_Deserializer;
219 on e:Exception do begin
221 Write('Hit ENTER to close ... '); Readln;
227 class function TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes;
228 var serial : TSerializer;
230 serial := TSerializer.Create( factory);
232 result := serial.Serialize( input);
239 class procedure TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream);
240 var serial : TSerializer;
242 serial := TSerializer.Create( factory);
244 serial.Serialize( input, aStream);
251 class procedure TTestSerializer.Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory);
252 var serial : TDeserializer;
254 serial := TDeserializer.Create( factory);
256 serial.Deserialize( input, target);
262 class procedure TTestSerializer.Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory);
263 var serial : TDeserializer;
265 serial := TDeserializer.Create( factory);
267 serial.Deserialize( input, target);
274 var test : TTestSerializer;
276 test := TTestSerializer.Create;