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