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