blob: 22708b432999fca2c29269773770b1bee3e6b236 [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 CompactSpec where
21
22import Test.Hspec
23import Test.Hspec.QuickCheck (prop)
24
25import qualified Data.ByteString.Lazy as LBS
26
27import Thrift.Types
28import Thrift.Transport
29import Thrift.Transport.Memory
30import Thrift.Protocol
31import Thrift.Protocol.Compact
32
33spec :: Spec
34spec = do
35 describe "CompactProtocol" $ do
36 describe "double" $ do
37 it "writes in little endian order" $ do
38 let val = 2 ** 53
39 trans <- openMemoryBuffer
40 let proto = CompactProtocol trans
41 writeVal proto (TDouble val)
42 bin <- tReadAll trans 8
43 (LBS.unpack bin) `shouldBe`[0, 0, 0, 0, 0, 0, 64, 67]
44
45 it "reads in little endian order" $ do
46 let bin = LBS.pack [0, 0, 0, 0, 0, 0, 64, 67]
47 trans <- openMemoryBuffer
48 let proto = CompactProtocol trans
49 tWrite trans bin
50 val <- readVal proto T_DOUBLE
51 val `shouldBe` (TDouble $ 2 ** 53)
52
53 prop "round trip" $ \val -> do
54 trans <- openMemoryBuffer
55 let proto = CompactProtocol trans
56 writeVal proto $ TDouble val
57 val2 <- readVal proto T_DOUBLE
58 val2 `shouldBe` (TDouble val)