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 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) 59 60 describe "binary" $ do 61 it "writes" $ do 62 trans <- openMemoryBuffer 63 let proto = CompactProtocol trans 64 writeVal proto (TBinary $ LBS.pack [42, 43, 44]) 65 bin <- tRead trans 100 66 (LBS.unpack bin) `shouldBe` [3, 42, 43, 44] 67 68 it "reads" $ do 69 trans <- openMemoryBuffer 70 let proto = CompactProtocol trans 71 tWrite trans $ LBS.pack [3, 42, 43, 44] 72 val <- readVal proto (T_BINARY) 73 val `shouldBe` (TBinary $ LBS.pack [42, 43, 44]) 74 75 prop "round trip" $ \val -> do 76 trans <- openMemoryBuffer 77 let proto = CompactProtocol trans 78 writeVal proto (TBinary $ LBS.pack val) 79 val2 <- readVal proto (T_BINARY) 80 val2 `shouldBe` (TBinary $ LBS.pack val) 81 82