Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 1 | (* |
| 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 Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 20 | program TestSerializer; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 21 | |
| 22 | {$APPTYPE CONSOLE} |
| 23 | |
| 24 | uses |
| 25 | Classes, Windows, SysUtils, Generics.Collections, |
| 26 | Thrift in '..\..\src\Thrift.pas', |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 27 | Thrift.Exception in '..\..\src\Thrift.Exception.pas', |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 28 | Thrift.Socket in '..\..\src\Thrift.Socket.pas', |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 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', |
Jens Geyer | a715f70 | 2019-08-28 22:56:13 +0200 | [diff] [blame^] | 32 | Thrift.Protocol.Compact in '..\..\src\Thrift.Protocol.Compact.pas', |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 33 | Thrift.Collections in '..\..\src\Thrift.Collections.pas', |
| 34 | Thrift.Server in '..\..\src\Thrift.Server.pas', |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 35 | Thrift.Utils in '..\..\src\Thrift.Utils.pas', |
| 36 | Thrift.Serializer in '..\..\src\Thrift.Serializer.pas', |
| 37 | Thrift.Stream in '..\..\src\Thrift.Stream.pas', |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 38 | Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas', |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 39 | Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas', |
Jens Geyer | 92d8062 | 2018-05-02 22:28:44 +0200 | [diff] [blame] | 40 | System_, |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 41 | DebugProtoTest, |
| 42 | TestSerializer.Data; |
| 43 | |
| 44 | |
| 45 | |
| 46 | type |
| 47 | TTestSerializer = class //extends TestCase { |
Jens Geyer | a715f70 | 2019-08-28 22:56:13 +0200 | [diff] [blame^] | 48 | private type |
| 49 | TMethod = ( |
| 50 | mt_Bytes, |
| 51 | mt_Stream |
| 52 | ); |
| 53 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 54 | private |
| 55 | FProtocols : TList< IProtocolFactory>; |
| 56 | |
| 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; |
| 61 | |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 62 | procedure Test_Serializer_Deserializer; |
Jens Geyer | a715f70 | 2019-08-28 22:56:13 +0200 | [diff] [blame^] | 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); |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 65 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 66 | public |
| 67 | constructor Create; |
| 68 | destructor Destroy; override; |
| 69 | |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 70 | procedure RunTests; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 71 | end; |
| 72 | |
| 73 | |
| 74 | |
| 75 | { TTestSerializer } |
| 76 | |
| 77 | constructor TTestSerializer.Create; |
| 78 | begin |
| 79 | inherited Create; |
| 80 | FProtocols := TList< IProtocolFactory>.Create; |
| 81 | FProtocols.Add( TBinaryProtocolImpl.TFactory.Create); |
Jens Geyer | a715f70 | 2019-08-28 22:56:13 +0200 | [diff] [blame^] | 82 | FProtocols.Add( TCompactProtocolImpl.TFactory.Create); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 83 | FProtocols.Add( TJSONProtocolImpl.TFactory.Create); |
| 84 | end; |
| 85 | |
| 86 | |
| 87 | destructor TTestSerializer.Destroy; |
| 88 | begin |
| 89 | try |
| 90 | FreeAndNil( FProtocols); |
| 91 | finally |
| 92 | inherited Destroy; |
| 93 | end; |
| 94 | end; |
| 95 | |
Jens Geyer | a715f70 | 2019-08-28 22:56:13 +0200 | [diff] [blame^] | 96 | |
| 97 | procedure TTestSerializer.Test_OneOfEach( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream); |
| 98 | var tested, correct : IOneOfEach; |
| 99 | bytes : TBytes; |
| 100 | i : Integer; |
| 101 | begin |
| 102 | // write |
| 103 | tested := Fixtures.CreateOneOfEach; |
| 104 | case method of |
| 105 | mt_Bytes: bytes := Serialize( tested, factory); |
| 106 | mt_Stream: begin |
| 107 | stream.Size := 0; |
| 108 | Serialize( tested, factory, stream); |
| 109 | end |
| 110 | else |
| 111 | ASSERT( FALSE); |
| 112 | end; |
| 113 | |
| 114 | // init + read |
| 115 | tested := TOneOfEachImpl.Create; |
| 116 | case method of |
| 117 | mt_Bytes: Deserialize( bytes, tested, factory); |
| 118 | mt_Stream: begin |
| 119 | stream.Position := 0; |
| 120 | Deserialize( stream, tested, factory); |
| 121 | end |
| 122 | else |
| 123 | ASSERT( FALSE); |
| 124 | end; |
| 125 | |
| 126 | // check |
| 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); |
| 138 | |
| 139 | ASSERT( Length(tested.Base64) = Length(correct.Base64)); |
| 140 | ASSERT( CompareMem( @tested.Base64[0], @correct.Base64[0], Length(correct.Base64))); |
| 141 | |
| 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]); |
| 145 | |
| 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]); |
| 149 | |
| 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]); |
| 153 | end; |
| 154 | |
| 155 | |
| 156 | procedure TTestSerializer.Test_CompactStruct( const method : TMethod; const factory : IProtocolFactory; const stream : TFileStream); |
| 157 | var tested, correct : ICompactProtoTestStruct; |
| 158 | bytes : TBytes; |
| 159 | begin |
| 160 | // write |
| 161 | tested := Fixtures.CreateCompactProtoTestStruct; |
| 162 | case method of |
| 163 | mt_Bytes: bytes := Serialize( tested, factory); |
| 164 | mt_Stream: begin |
| 165 | stream.Size := 0; |
| 166 | Serialize( tested, factory, stream); |
| 167 | end |
| 168 | else |
| 169 | ASSERT( FALSE); |
| 170 | end; |
| 171 | |
| 172 | // init + read |
| 173 | correct := TCompactProtoTestStructImpl.Create; |
| 174 | case method of |
| 175 | mt_Bytes: Deserialize( bytes, tested, factory); |
| 176 | mt_Stream: begin |
| 177 | stream.Position := 0; |
| 178 | Deserialize( stream, tested, factory); |
| 179 | end |
| 180 | else |
| 181 | ASSERT( FALSE); |
| 182 | end; |
| 183 | |
| 184 | // check |
| 185 | correct := Fixtures.CreateCompactProtoTestStruct; |
| 186 | ASSERT( correct.Field500 = tested.Field500); |
| 187 | ASSERT( correct.Field5000 = tested.Field5000); |
| 188 | ASSERT( correct.Field20000 = tested.Field20000); |
| 189 | end; |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 190 | |
| 191 | |
| 192 | procedure TTestSerializer.Test_Serializer_Deserializer; |
Jens Geyer | a715f70 | 2019-08-28 22:56:13 +0200 | [diff] [blame^] | 193 | var factory : IProtocolFactory; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 194 | stream : TFileStream; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 195 | method : TMethod; |
| 196 | begin |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 197 | stream := TFileStream.Create( 'TestSerializer.dat', fmCreate); |
| 198 | try |
| 199 | |
| 200 | for method in [Low(TMethod)..High(TMethod)] do begin |
| 201 | for factory in FProtocols do begin |
| 202 | |
Jens Geyer | a715f70 | 2019-08-28 22:56:13 +0200 | [diff] [blame^] | 203 | Test_OneOfEach( method, factory, stream); |
| 204 | Test_CompactStruct( method, factory, stream); |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 205 | end; |
| 206 | end; |
| 207 | |
| 208 | finally |
| 209 | stream.Free; |
| 210 | end; |
| 211 | end; |
| 212 | |
| 213 | |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 214 | procedure TTestSerializer.RunTests; |
| 215 | begin |
| 216 | try |
| 217 | Test_Serializer_Deserializer; |
| 218 | except |
| 219 | on e:Exception do begin |
| 220 | Writeln( e.Message); |
| 221 | Write('Hit ENTER to close ... '); Readln; |
| 222 | end; |
| 223 | end; |
| 224 | end; |
| 225 | |
| 226 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 227 | class function TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes; |
| 228 | var serial : TSerializer; |
| 229 | begin |
| 230 | serial := TSerializer.Create( factory); |
| 231 | try |
| 232 | result := serial.Serialize( input); |
| 233 | finally |
| 234 | serial.Free; |
| 235 | end; |
| 236 | end; |
| 237 | |
| 238 | |
| 239 | class procedure TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream); |
| 240 | var serial : TSerializer; |
| 241 | begin |
| 242 | serial := TSerializer.Create( factory); |
| 243 | try |
| 244 | serial.Serialize( input, aStream); |
| 245 | finally |
| 246 | serial.Free; |
| 247 | end; |
| 248 | end; |
| 249 | |
| 250 | |
| 251 | class procedure TTestSerializer.Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory); |
| 252 | var serial : TDeserializer; |
| 253 | begin |
| 254 | serial := TDeserializer.Create( factory); |
| 255 | try |
| 256 | serial.Deserialize( input, target); |
| 257 | finally |
| 258 | serial.Free; |
| 259 | end; |
| 260 | end; |
| 261 | |
| 262 | class procedure TTestSerializer.Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory); |
| 263 | var serial : TDeserializer; |
| 264 | begin |
| 265 | serial := TDeserializer.Create( factory); |
| 266 | try |
| 267 | serial.Deserialize( input, target); |
| 268 | finally |
| 269 | serial.Free; |
| 270 | end; |
| 271 | end; |
| 272 | |
| 273 | |
| 274 | var test : TTestSerializer; |
| 275 | begin |
| 276 | test := TTestSerializer.Create; |
| 277 | try |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame] | 278 | test.RunTests; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 279 | finally |
| 280 | test.Free; |
| 281 | end; |
| 282 | end. |
| 283 | |