From ae959ec4f14e7e8d65a17f5d9e918bde51992b01 Mon Sep 17 00:00:00 2001
From: Simon Marlow <marlowsd@gmail.com>
Date: Thu, 25 Nov 2010 12:17:17 +0000
Subject: [PATCH] Add a test for reliable encoding errors

---
 testsuite/tests/ghc-regress/lib/IO/all.T      |  2 ++
 .../ghc-regress/lib/IO/encodingerror001.hs    | 27 ++++++++++++++
 .../lib/IO/encodingerror001.stdout            | 36 +++++++++++++++++++
 3 files changed, 65 insertions(+)
 create mode 100644 testsuite/tests/ghc-regress/lib/IO/encodingerror001.hs
 create mode 100644 testsuite/tests/ghc-regress/lib/IO/encodingerror001.stdout

diff --git a/testsuite/tests/ghc-regress/lib/IO/all.T b/testsuite/tests/ghc-regress/lib/IO/all.T
index 79e5c0a04a4..2eea1e4dcd8 100644
--- a/testsuite/tests/ghc-regress/lib/IO/all.T
+++ b/testsuite/tests/ghc-regress/lib/IO/all.T
@@ -156,3 +156,5 @@ test('openTempFile001', normal, compile_and_run, [''])
 test('T4113', normal, compile_and_run, [''])
 
 test('T4144', normal, compile_and_run, [''])
+
+test('encodingerror001', normal, compile_and_run, [''])
diff --git a/testsuite/tests/ghc-regress/lib/IO/encodingerror001.hs b/testsuite/tests/ghc-regress/lib/IO/encodingerror001.hs
new file mode 100644
index 00000000000..327b490adb3
--- /dev/null
+++ b/testsuite/tests/ghc-regress/lib/IO/encodingerror001.hs
@@ -0,0 +1,27 @@
+import System.IO
+import System.IO.Error
+import Text.Printf
+import Control.Monad
+
+main = do
+  hSetEncoding stdout latin1
+  forM [NoBuffering,
+        LineBuffering,
+        BlockBuffering Nothing,
+        BlockBuffering (Just 3),
+        BlockBuffering (Just 9),
+        BlockBuffering (Just 32)] $ \b -> do
+     hSetBuffering stdout b
+     checkedPutStr "test 1\n"
+     checkedPutStr "Ä›\n" -- nothing gets written
+     checkedPutStr "test 2\n"
+     checkedPutStr "Hέllo\n" -- we should write at least the 'H'
+     checkedPutStr "test 3\n"
+     checkedPutStr "Hello αβγ\n" -- we should write at least the "Hello "
+
+checkedPutStr str = do
+  r <- try $ putStr str
+  case r of
+    Right _ -> return ()
+    Left  e -> printf "Caught %s while trying to write %s\n"
+                  (show e) (show str)
diff --git a/testsuite/tests/ghc-regress/lib/IO/encodingerror001.stdout b/testsuite/tests/ghc-regress/lib/IO/encodingerror001.stdout
new file mode 100644
index 00000000000..23718da4b2b
--- /dev/null
+++ b/testsuite/tests/ghc-regress/lib/IO/encodingerror001.stdout
@@ -0,0 +1,36 @@
+test 1
+Caught <stdout>: hPutChar: invalid argument (character is out of range for this encoding) while trying to write "\283\n"
+test 2
+HCaught <stdout>: hPutChar: invalid argument (character is out of range for this encoding) while trying to write "H\941llo\n"
+test 3
+Hello Caught <stdout>: hPutChar: invalid argument (character is out of range for this encoding) while trying to write "Hello \945\946\947\n"
+test 1
+Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "\283\n"
+test 2
+HCaught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "H\941llo\n"
+test 3
+Hello Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "Hello \945\946\947\n"
+test 1
+Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "\283\n"
+test 2
+HCaught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "H\941llo\n"
+test 3
+Hello Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "Hello \945\946\947\n"
+test 1
+Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "\283\n"
+test 2
+HCaught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "H\941llo\n"
+test 3
+Hello Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "Hello \945\946\947\n"
+test 1
+Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "\283\n"
+test 2
+HCaught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "H\941llo\n"
+test 3
+Hello Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "Hello \945\946\947\n"
+test 1
+Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "\283\n"
+test 2
+HCaught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "H\941llo\n"
+test 3
+Hello Caught <stdout>: commitBuffer: invalid argument (character is out of range for this encoding) while trying to write "Hello \945\946\947\n"
-- 
GitLab