From a1ab71a8f784802e7170f6850e67ef6ea982ab4f Mon Sep 17 00:00:00 2001 From: Preetham Gujjula Date: Thu, 11 Apr 2024 18:05:25 -0700 Subject: [PATCH 1/2] Add test for fromInfinite --- chimera.cabal | 1 + test/Test.hs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/chimera.cabal b/chimera.cabal index 090c355..7a210aa 100644 --- a/chimera.cabal +++ b/chimera.cabal @@ -95,6 +95,7 @@ test-suite chimera-test build-depends: base >=4.5 && <5, chimera, + infinite-list, QuickCheck >=2.10 && <2.15, tasty <1.6, tasty-hunit <0.11, diff --git a/test/Test.hs b/test/Test.hs index cf0956b..10340e7 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -12,7 +12,9 @@ import Test.Tasty.QuickCheck as QC hiding ((.&.)) import Data.Bits import Data.Foldable import Data.Function (fix) +import qualified Data.List.Infinite as I import qualified Data.List as L +import qualified Data.List.NonEmpty as NE import qualified Data.Vector.Generic as G import Data.Chimera.ContinuousMapping @@ -158,6 +160,13 @@ chimeraTests = testGroup "Chimera" (if fromIntegral jx < length xs then xs !! fromIntegral jx else x) === Ch.index (Ch.fromListWithDef x xs :: UChimera Bool) jx + , QC.testProperty "fromInfinite" $ + \x xs ix -> + let jx = ix `mod` 65536 in + let ys = I.cycle (x NE.:| xs) in + (ys I.!! jx) === + Ch.index (Ch.fromInfinite ys :: UChimera Bool) jx + , QC.testProperty "fromVectorWithDef" $ \x xs ix -> let jx = ix `mod` 65536 in From cf0108c5938f3565e5bfd9be4a96ec32aec9d01c Mon Sep 17 00:00:00 2001 From: Preetham Gujjula Date: Thu, 11 Apr 2024 17:42:25 -0700 Subject: [PATCH 2/2] #40 Fix fromInfinite error In the implementation of fromInfinite, we pass an infinite list to Data.Primitive.Array.fromListN (bits + 1), but fromListN expects a list a list of length (bits + 1), which results in a runtime error. To fix this, we add a check in go to break recursion and to ensure that the list that is passed into fromListN has length (bits + 1). --- src/Data/Chimera/Internal.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Data/Chimera/Internal.hs b/src/Data/Chimera/Internal.hs index fea0ea3..8b9c372 100644 --- a/src/Data/Chimera/Internal.hs +++ b/src/Data/Chimera/Internal.hs @@ -572,7 +572,10 @@ fromInfinite = Chimera . fromListN (bits + 1) . go0 where go0 (x :< xs) = G.singleton x : go 0 xs - go k xs = G.fromListN kk ys : go (k + 1) zs + go k xs = + if k == bits + then [] + else G.fromListN kk ys : go (k + 1) zs where kk = 1 `shiftL` k (ys, zs) = Inf.splitAt kk xs