From 03b6dd07dab518625e011fa1a79b1071822d3131 Mon Sep 17 00:00:00 2001 From: Preetham Gujjula Date: Wed, 8 May 2024 00:23:10 -0700 Subject: [PATCH] Add tests for applyMergeOn --- apply-merge.cabal | 1 + test/Main.hs | 2 ++ test/Test/Data/List/ApplyMerge.hs | 52 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 test/Test/Data/List/ApplyMerge.hs diff --git a/apply-merge.cabal b/apply-merge.cabal index 18d5bd7..c1f6c4e 100644 --- a/apply-merge.cabal +++ b/apply-merge.cabal @@ -61,6 +61,7 @@ test-suite apply-merge-tests Test.ApplyMerge.IntMap Test.ApplyMerge.IntSet Test.Data.DoublyLinkedList.STRef + Test.Data.List.ApplyMerge Test.Data.PQueue.Prio.Min.Mutable hs-source-dirs: src diff --git a/test/Main.hs b/test/Main.hs index b3c65ee..e8abb93 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -8,6 +8,7 @@ import Test.ApplyMerge.IntMap qualified (tests) import Test.ApplyMerge.IntSet qualified (tests) import Test.Data.DoublyLinkedList.STRef qualified (tests) import Test.Data.PQueue.Prio.Min.Mutable qualified (tests) +import Test.Data.List.ApplyMerge qualified (tests) import Test.Tasty (TestTree, defaultMain, testGroup) main :: IO () @@ -20,6 +21,7 @@ tests = [ Test.ApplyMerge.DoublyLinkedList.tests, Test.ApplyMerge.IntMap.tests, Test.ApplyMerge.IntSet.tests, + Test.Data.List.ApplyMerge.tests, Test.Data.DoublyLinkedList.STRef.tests, Test.Data.PQueue.Prio.Min.Mutable.tests ] diff --git a/test/Test/Data/List/ApplyMerge.hs b/test/Test/Data/List/ApplyMerge.hs new file mode 100644 index 0000000..bf14e92 --- /dev/null +++ b/test/Test/Data/List/ApplyMerge.hs @@ -0,0 +1,52 @@ +-- SPDX-FileCopyrightText: Copyright Preetham Gujjula +-- SPDX-License-Identifier: BSD-3-Clause +module Test.Data.List.ApplyMerge (tests) where + +import Data.Complex (Complex ((:+))) +import Data.List (sortOn) +import Data.List.ApplyMerge (applyMergeOn) +import Data.Ratio ((%)) +import Test.ApplyMerge.Common (basicTest, blockTest, maxTest, skewedTest) +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit (testCase, (@?=)) + +tests :: TestTree +tests = + testGroup + "Data.List.ApplyMerge.applyMergeOn" + [ basicTest (applyMergeOn id), + skewedTest (applyMergeOn id), + blockTest (applyMergeOn id), + maxTest (applyMergeOn id), + gaussianIntegerTest + ] + +gaussianIntegerTest :: TestTree +gaussianIntegerTest = + testCase "gaussian integers x + yi, with 0 <= x <= y, ordered by norm" $ do + let actual = + take 100 $ + applyMergeOn + (\x -> (norm x, slope x)) + (\x k -> x :+ (x + k)) + [0 ..] + [0 ..] + expected = + take 100 $ + filter (\(x :+ y) -> x <= y) $ + sortOn + (\x -> (norm x, slope x)) + (liftA2 (:+) [0 .. 100] [0 .. 100]) + in actual @?= expected + +norm :: Complex Integer -> Integer +norm (a :+ b) = a * a + b * b + +data Slope = Finite Rational | Infinity + deriving (Eq, Show, Ord) + +slope :: Complex Integer -> Slope +slope (x :+ y) = + if x == 0 + then Infinity + else Finite (y % x)