<commit_before>-- this is test string used in the 'spec' --- don't touch me!
module System.Torrent.FileMapSpec (spec) where

import Control.Monad.Loops
import Data.List as L
import Data.ByteString.Lazy as BL
import System.Directory
import System.FilePath
import System.IO.Unsafe
import Test.Hspec

import Data.Torrent.Layout
import System.Torrent.FileMap as FM


layout :: FileLayout FileSize
layout =
  [ (dir </> "a", 2)
  , (dir </> "b", 3)
  , (dir </> "c", 2)
  ]
  where
    dir = unsafePerformIO $ getTemporaryDirectory

spec :: Spec
spec = do
  describe "FileMap" $ do
    it "creates new files" $ do
      m <- mmapFiles ReadWriteEx layout
      unmapFiles m

      (doesFileExist . fst) `allM` layout
        `shouldReturn` True

    it "have specified size" $ do
      m <- mmapFiles ReadOnly layout
      FM.size m `shouldBe` L.sum (L.map snd layout)
      unmapFiles m

    it "read from files" $ do
      let thisFile = [("tests/System/Torrent/FileMapSpec.hs", 15)]
      m <- mmapFiles ReadOnly thisFile
      readBytes 3 15 m `shouldReturn` "this is test"
      unmapFiles m

    it "writes to files" $ do
      m <- mmapFiles ReadWriteEx layout
      writeBytes 0 "a"   m
      readBytes  0 1 m `shouldReturn` "a"
      writeBytes 1 "ab"  m
      readBytes  1 2 m `shouldReturn` "ab"
      writeBytes 3 "b"   m
      readBytes  3 1 m `shouldReturn` "b"
      writeBytes 4 "bc"  m
      readBytes  4 2 m `shouldReturn` "bc"
      writeBytes 6 "c"   m
      readBytes  6 1 m `shouldReturn` "c"
      readBytes  0 7 m `shouldReturn` "aabbbcc"
      unmapFiles m

      BL.readFile (fst (layout !! 0)) `shouldReturn` "aa"
      BL.readFile (fst (layout !! 1)) `shouldReturn` "bbb"
      BL.readFile (fst (layout !! 2)) `shouldReturn` "cc"

    let max_page_size = 4 * 1024 * 1024
    let long_bs = BL.replicate (fromIntegral max_page_size) 0

    it "no buffer underflow errors" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes (1 - max_page_size) long_bs m
      unmapFiles m

    it "no buffer overflow errors" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes 5 long_bs m
      unmapFiles m

    it "ignore underflow reads" $ do
      m <- mmapFiles ReadOnly layout
      readBytes (-1) 1  m `shouldReturn` ""
      readBytes (-5) 12 m `shouldReturn` ""
      unmapFiles m

    it "ignore underflow writes" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes 0 "aa" m
      unmapFiles m

      m <- mmapFiles ReadWrite layout
      writeBytes (-1) "hhh" m
      unmapFiles m
      BL.readFile (fst (layout !! 0)) `shouldReturn` "aa"

    it "crop overflow reads" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes 5 "cc" m
      unmapFiles m

      m <- mmapFiles ReadOnly layout
      readBytes 5 10 m `shouldReturn` "cc"
      unmapFiles m

    it "crop overflow writes" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes 5 "ddddddddd" m
      unmapFiles m
      BL.readFile (fst (layout !! 2)) `shouldReturn` "dd"

    it "isomorphic to lazy bytestring" $ do
      m <- mmapFiles ReadOnly layout
      fromLazyByteString (toLazyByteString m) `shouldBe` m
      unmapFiles m<commit_msg>Group FileMap tests by function<commit_after>-- this is test string used in the 'spec' --- don't touch me!
module System.Torrent.FileMapSpec (spec) where

import Control.Monad.Loops
import Data.List as L
import Data.ByteString.Lazy as BL
import System.Directory
import System.FilePath
import System.IO.Unsafe
import Test.Hspec

import Data.Torrent.Layout
import System.Torrent.FileMap as FM


layout :: FileLayout FileSize
layout =
  [ (dir </> "a", 2)
  , (dir </> "b", 3)
  , (dir </> "c", 2)
  ]
  where
    dir = unsafePerformIO $ getTemporaryDirectory

spec :: Spec
spec = do
  describe "mmapFiles" $ do
    it "creates new files" $ do
      m <- mmapFiles ReadWriteEx layout
      unmapFiles m

      (doesFileExist . fst) `allM` layout
        `shouldReturn` True

  describe "size" $ do
    it "is equal to the layout size" $ do
      m <- mmapFiles ReadOnly layout
      FM.size m `shouldBe` L.sum (L.map snd layout)
      unmapFiles m

  describe "readBytes" $ do
    it "read from files" $ do
      let thisFile = [("tests/System/Torrent/FileMapSpec.hs", 15)]
      m <- mmapFiles ReadOnly thisFile
      readBytes 3 15 m `shouldReturn` "this is test"
      unmapFiles m

    it "ignore underflow reads" $ do
      m <- mmapFiles ReadOnly layout
      readBytes (-1) 1  m `shouldReturn` ""
      readBytes (-5) 12 m `shouldReturn` ""
      unmapFiles m

    it "crop overflow reads" $ do
      _m <- mmapFiles ReadWrite layout
      writeBytes 5 "cc" _m
      unmapFiles _m

      m <- mmapFiles ReadOnly layout
      readBytes 5 10 m `shouldReturn` "cc"
      unmapFiles m

  describe "writeBytes" $ do
    it "writes to files" $ do
      m <- mmapFiles ReadWriteEx layout
      writeBytes 0 "a"   m
      readBytes  0 1 m `shouldReturn` "a"
      writeBytes 1 "ab"  m
      readBytes  1 2 m `shouldReturn` "ab"
      writeBytes 3 "b"   m
      readBytes  3 1 m `shouldReturn` "b"
      writeBytes 4 "bc"  m
      readBytes  4 2 m `shouldReturn` "bc"
      writeBytes 6 "c"   m
      readBytes  6 1 m `shouldReturn` "c"
      readBytes  0 7 m `shouldReturn` "aabbbcc"
      unmapFiles m

      BL.readFile (fst (layout !! 0)) `shouldReturn` "aa"
      BL.readFile (fst (layout !! 1)) `shouldReturn` "bbb"
      BL.readFile (fst (layout !! 2)) `shouldReturn` "cc"

    let max_page_size = 4 * 1024 * 1024
    let long_bs = BL.replicate (fromIntegral max_page_size) 0

    it "no buffer underflow errors" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes (1 - max_page_size) long_bs m
      unmapFiles m

    it "no buffer overflow errors" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes 5 long_bs m
      unmapFiles m

    it "ignore underflow writes" $ do
      _m <- mmapFiles ReadWrite layout
      writeBytes 0 "aa" _m
      unmapFiles _m

      m <- mmapFiles ReadWrite layout
      writeBytes (-1) "hhh" m
      unmapFiles m
      BL.readFile (fst (layout !! 0)) `shouldReturn` "aa"

    it "crop overflow writes" $ do
      m <- mmapFiles ReadWrite layout
      writeBytes 5 "ddddddddd" m
      unmapFiles m
      BL.readFile (fst (layout !! 2)) `shouldReturn` "dd"

  describe "from/to lazy bytestring" $ do
    it "isomorphic to lazy bytestring" $ do
      m <- mmapFiles ReadOnly layout
      fromLazyByteString (toLazyByteString m) `shouldBe` m
      unmapFiles m
{'fraction_non_alphanumeric': 0.09182409414679467, 'fraction_numerical': 0.01672344379064726, 'mean_word_length': 2.437466737626397, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1063485', 'n_tokens_mistral': 2260, 'n_tokens_neox': 2111, 'n_words': 756}