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