Nobuaki Sukegawa | 7c7d679 | 2015-12-09 03:22:35 +0900 | [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 | |
| 20 | module CompactSpec where |
| 21 | |
| 22 | import Test.Hspec |
| 23 | import Test.Hspec.QuickCheck (prop) |
| 24 | |
| 25 | import qualified Data.ByteString.Lazy as LBS |
| 26 | |
| 27 | import Thrift.Types |
| 28 | import Thrift.Transport |
| 29 | import Thrift.Transport.Memory |
| 30 | import Thrift.Protocol |
| 31 | import Thrift.Protocol.Compact |
| 32 | |
| 33 | spec :: Spec |
| 34 | spec = 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) |
Nobuaki Sukegawa | e68ccc2 | 2015-12-13 21:45:39 +0900 | [diff] [blame] | 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 | |