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