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', |
| 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 Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame^] | 36 | Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas', |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 37 | DebugProtoTest, |
| 38 | TestSerializer.Data; |
| 39 | |
| 40 | |
| 41 | |
| 42 | type |
| 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 Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame^] | 52 | procedure Test_Serializer_Deserializer; |
| 53 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 54 | public |
| 55 | constructor Create; |
| 56 | destructor Destroy; override; |
| 57 | |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame^] | 58 | procedure RunTests; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 59 | end; |
| 60 | |
| 61 | |
| 62 | |
| 63 | { TTestSerializer } |
| 64 | |
| 65 | constructor TTestSerializer.Create; |
| 66 | begin |
| 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); |
| 72 | end; |
| 73 | |
| 74 | |
| 75 | destructor TTestSerializer.Destroy; |
| 76 | begin |
| 77 | try |
| 78 | FreeAndNil( FProtocols); |
| 79 | finally |
| 80 | inherited Destroy; |
| 81 | end; |
| 82 | end; |
| 83 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 84 | type TMethod = (mt_Bytes, mt_Stream); |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame^] | 85 | |
| 86 | |
| 87 | procedure TTestSerializer.Test_Serializer_Deserializer; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 88 | var level3ooe, correct : IOneOfEach; |
| 89 | factory : IProtocolFactory; |
| 90 | bytes : TBytes; |
| 91 | stream : TFileStream; |
| 92 | i : Integer; |
| 93 | method : TMethod; |
| 94 | begin |
| 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; |
| 157 | end; |
| 158 | |
| 159 | |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame^] | 160 | procedure TTestSerializer.RunTests; |
| 161 | begin |
| 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; |
| 170 | end; |
| 171 | |
| 172 | |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 173 | class function TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory) : TBytes; |
| 174 | var serial : TSerializer; |
| 175 | begin |
| 176 | serial := TSerializer.Create( factory); |
| 177 | try |
| 178 | result := serial.Serialize( input); |
| 179 | finally |
| 180 | serial.Free; |
| 181 | end; |
| 182 | end; |
| 183 | |
| 184 | |
| 185 | class procedure TTestSerializer.Serialize(const input : IBase; const factory : IProtocolFactory; const aStream : TStream); |
| 186 | var serial : TSerializer; |
| 187 | begin |
| 188 | serial := TSerializer.Create( factory); |
| 189 | try |
| 190 | serial.Serialize( input, aStream); |
| 191 | finally |
| 192 | serial.Free; |
| 193 | end; |
| 194 | end; |
| 195 | |
| 196 | |
| 197 | class procedure TTestSerializer.Deserialize( const input : TBytes; const target : IBase; const factory : IProtocolFactory); |
| 198 | var serial : TDeserializer; |
| 199 | begin |
| 200 | serial := TDeserializer.Create( factory); |
| 201 | try |
| 202 | serial.Deserialize( input, target); |
| 203 | finally |
| 204 | serial.Free; |
| 205 | end; |
| 206 | end; |
| 207 | |
| 208 | class procedure TTestSerializer.Deserialize( const input : TStream; const target : IBase; const factory : IProtocolFactory); |
| 209 | var serial : TDeserializer; |
| 210 | begin |
| 211 | serial := TDeserializer.Create( factory); |
| 212 | try |
| 213 | serial.Deserialize( input, target); |
| 214 | finally |
| 215 | serial.Free; |
| 216 | end; |
| 217 | end; |
| 218 | |
| 219 | |
| 220 | var test : TTestSerializer; |
| 221 | begin |
| 222 | test := TTestSerializer.Create; |
| 223 | try |
Jens Geyer | 9bb4c11 | 2014-07-03 23:05:54 +0200 | [diff] [blame^] | 224 | test.RunTests; |
Roger Meier | 2b2c0b2 | 2012-09-12 20:09:02 +0000 | [diff] [blame] | 225 | finally |
| 226 | test.Free; |
| 227 | end; |
| 228 | end. |
| 229 | |