Skip to content

Commit f2ea8b7

Browse files
author
qz
committed
char type allows only 7bit values, tests for char encode/decode fixed
1 parent 96f9f1e commit f2ea8b7

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Database/PostgreSQL/Protocol/Codecs/Encoders.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ bytea = putByteString
4646

4747
{-# INLINE char #-}
4848
char :: Char -> Encode
49-
char = putWord8 . fromIntegral . ord
49+
char c
50+
| ord(c) >= 128 = error "Character code must be below 128"
51+
| otherwise = (putWord8 . fromIntegral . ord) c
5052

5153
{-# INLINE date #-}
5254
date :: Day -> Encode

tests/Codecs/QuickCheck.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,18 @@ testCodecsEncodeDecode :: TestTree
9595
testCodecsEncodeDecode = testGroup "Codecs property 'encode . decode = id'"
9696
[ mkCodecTest "bool" PGT.bool PE.bool PD.bool
9797
, mkCodecTest "bytea" PGT.bytea PE.bytea PD.bytea
98-
, mkCodecTest "char" PGT.char PE.char PD.char
98+
, mkCodecTest "char" PGT.char (PE.char . unAsciiChar)
99+
(fmap AsciiChar <$> PD.char)
99100
, mkCodecTest "date" PGT.date PE.date PD.date
100101
, mkCodecTest "float4" PGT.float4 PE.float4 PD.float4
101102
, mkCodecTest "float8" PGT.float8 PE.float8 PD.float8
102103
, mkCodecTest "int2" PGT.int2 PE.int2 PD.int2
103104
, mkCodecTest "int4" PGT.int4 PE.int4 PD.int4
104105
, mkCodecTest "int8" PGT.int8 PE.int8 PD.int8
105106
, mkCodecTest "interval" PGT.interval PE.interval PD.interval
106-
, mkCodecTest "json" PGT.json (PE.bsJsonText . unJsonString )
107+
, mkCodecTest "json" PGT.json (PE.bsJsonText . unJsonString)
107108
(fmap JsonString <$> PD.bsJsonText)
108-
, mkCodecTest "jsonb" PGT.jsonb (PE.bsJsonBytes .unJsonString)
109+
, mkCodecTest "jsonb" PGT.jsonb (PE.bsJsonBytes . unJsonString)
109110
(fmap JsonString <$> PD.bsJsonBytes)
110111
, mkCodecTest "numeric" PGT.numeric PE.numeric PD.numeric
111112
, mkCodecTest "text" PGT.text PE.bsText PD.bsText
@@ -145,6 +146,12 @@ testCodecsEncodePrint = testGroup
145146
-- Orphan instances
146147
--
147148

149+
newtype AsciiChar = AsciiChar { unAsciiChar :: Char }
150+
deriving (Show, Eq)
151+
152+
instance Arbitrary AsciiChar where
153+
arbitrary = AsciiChar <$> choose ('\0', '\127')
154+
148155
-- Helper to generate valid json strings
149156
newtype JsonString = JsonString { unJsonString :: B.ByteString }
150157
deriving (Show, Eq, IsString)

0 commit comments

Comments
 (0)