blob: d692fabe37a7d86578f7bde8122a1a350b693397 [file] [log] [blame]
Nobuaki Sukegawa7c7d6792015-12-09 03:22:35 +09001--
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
20module BinarySpec where
21
22import Test.Hspec
23import Test.Hspec.QuickCheck (prop)
24
25import qualified Data.ByteString.Lazy as LBS
26import qualified Data.ByteString.Lazy.Char8 as C
27
28import Thrift.Types
29import Thrift.Transport
30import Thrift.Transport.Memory
31import Thrift.Protocol
32import Thrift.Protocol.Binary
33
34spec :: Spec
35spec = do
36 describe "BinaryProtocol" $ do
37 describe "double" $ do
38 it "writes in big endian order" $ do
39 let val = 2 ** 53
40 trans <- openMemoryBuffer
41 let proto = BinaryProtocol trans
42 writeVal proto (TDouble val)
43 bin <- tRead trans 8
44 (LBS.unpack bin) `shouldBe`[67, 64, 0, 0, 0, 0, 0, 0]
45
46 it "reads in big endian order" $ do
47 let bin = LBS.pack [67, 64, 0, 0, 0, 0, 0, 0]
48 trans <- openMemoryBuffer
49 let proto = BinaryProtocol trans
50 tWrite trans bin
51 val <- readVal proto T_DOUBLE
52 val `shouldBe` (TDouble $ 2 ** 53)
53
54 prop "round trip" $ \val -> do
55 trans <- openMemoryBuffer
56 let proto = BinaryProtocol trans
57 writeVal proto $ TDouble val
58 val2 <- readVal proto T_DOUBLE
59 val2 `shouldBe` (TDouble val)
60
61 describe "string" $ do
62 it "writes" $ do
63 let val = C.pack "aaa"
64 trans <- openMemoryBuffer
65 let proto = BinaryProtocol trans
66 writeVal proto (TString val)
67 bin <- tRead trans 7
68 (LBS.unpack bin) `shouldBe` [0, 0, 0, 3, 97, 97, 97]
Nobuaki Sukegawae68ccc22015-12-13 21:45:39 +090069
70 describe "binary" $ do
71 it "writes" $ do
72 trans <- openMemoryBuffer
73 let proto = BinaryProtocol trans
74 writeVal proto (TBinary $ LBS.pack [42, 43, 44])
75 bin <- tRead trans 100
76 (LBS.unpack bin) `shouldBe` [0, 0, 0, 3, 42, 43, 44]
77
78 it "reads" $ do
79 trans <- openMemoryBuffer
80 let proto = BinaryProtocol trans
81 tWrite trans $ LBS.pack [0, 0, 0, 3, 42, 43, 44]
82 val <- readVal proto (T_BINARY)
83 val `shouldBe` (TBinary $ LBS.pack [42, 43, 44])
84
85 prop "round trip" $ \val -> do
86 trans <- openMemoryBuffer
87 let proto = BinaryProtocol trans
88 writeVal proto (TBinary $ LBS.pack val)
89 val2 <- readVal proto (T_BINARY)
90 val2 `shouldBe` (TBinary $ LBS.pack val)
91