<commit_before>{-# LANGUAGE ScopedTypeVariables, PartialTypeSignatures #-}
module Main where

import Streaming.Prelude
import Prelude hiding (filter, map, drop)
import Test.HUnit
import System.Exit
import Data.Function ((&))
import Data.Functor.Identity

main :: IO ()
main = do
  results <- runTestTT tests
  if (errors results + failures results == 0)
    then
      exitWith ExitSuccess
    else
      exitWith (ExitFailure 1)

tests = TestList [TestLabel "basic stream arithmetics" stream_basic]

stream_basic = 
  let xs :: [Integer] = concat . (toList_ :: _ ⊸ Identity [Integer]) $ 
                     each [1..100]
                   & filter even
                   & map (*2)
                   & drop 10
                   & filter (\x -> x `mod` 2 == 0)
  in TestCase $ do
    assertEqual "Length correct, " 40 (Prelude.length xs)
    assertEqual "Sum is correct, " 4880 (Prelude.sum xs)<commit_msg>Extend test file (broken atm)<commit_after>{-# LANGUAGE ScopedTypeVariables, PartialTypeSignatures #-}
{-# LANGUAGE RebindableSyntax #-}
module Main where

import Streaming.Prelude
import Prelude hiding (filter, map, drop)
import qualified Control.Monad.LMonad as L
import Test.HUnit
import System.IO
import Control.Monad.Trans
import System.Exit
import Data.Function ((&))
import Data.Functor.Identity

main :: IO ()
main = do
  results <- runTestTT tests
  case errors results + failures results == 0 of
    True  -> exitSuccess
    False -> exitWith $ ExitFailure 1

tests = TestList [TestLabel "basic stream arithmetics" stream_basic]

-- Basic arithmetics on streamed integers
stream_basic = 
  let xs :: [Integer] = concat . (toList_ :: _ ⊸ Identity [Integer]) $ 
                     each [1..100]
                   & filter even
                   & map (*2)
                   & drop 10
                   & filter (\x -> x `mod` 2 == 0)
  in TestCase $ do
    assertEqual "Length correct, " 40 (Prelude.length xs)
    assertEqual "Sum is correct, " 4880 (Prelude.sum xs)


-- Safe implementation of dangerous file interactions, as
-- explained here:
-- https://m0ar.github.io/safe-streaming/2017/06/19/linear-types-101.html
stream_file = do
  Right (a, s') <- next lineStream
  Right (b, s'') <- next lineStream
  Left () <- next lineStream
  return ()
  where
    lineStream :: Stream (LOf (IO String)) IO ()
    lineStream = lift $ openFile "stream_test_file" ReadMode L.>>= \h ->
      yield $ hGetLine h L.>>
      yield $ hGetLine h L.>>
      lift $ hClose h
{'fraction_non_alphanumeric': 0.10856453558504221, 'fraction_numerical': 0.018496180136710897, 'mean_word_length': 2.574712643678161, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1489992', 'n_tokens_mistral': 816, 'n_tokens_neox': 742, 'n_words': 248}