packages feed

array-list-0.1.0.0: tests/Main.hs

{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE OverloadedLists #-}

import Data.Array
import Data.Array.IsList
import Test.Hspec

main :: IO ()
main = hspec do
  describe "IsList (Array Int e)" do
    it "[1,2,3]" $
      ([1, 2, 3] :: Array Int Int)
        `shouldBe` array (0, 2) [(0, 1), (1, 2), (2, 3)]
    it "[\"one\",\"two\",\"three\"]" $
      (["one", "two", "three"] :: Array Int String)
        `shouldBe` array (0, 2) [(0, "one"), (1, "two"), (2, "three")]
    it "toList $ fromList [1,2,3]" $
      toList ([1, 2, 3] :: Array Int Int)
        `shouldBe` [1, 2, 3]
    it "toList $ fromList [\"one\",\"two\",\"three\"]" $
      toList (["one", "two", "three"] :: Array Int String)
        `shouldBe` ["one", "two", "three"]
  describe "IsList (Array (Int,Int) e)" do
    it "[[1, 2], [3, 4], [5, 6]]" $
      ( [ [1, 2],
          [3, 4],
          [5, 6]
        ] ::
          Array (Int, Int) Int
      )
        `shouldBe` array
          ((0, 0), (2, 1))
          [ ((0, 0), 1),
            ((0, 1), 2),
            ((1, 0), 3),
            ((1, 1), 4),
            ((2, 0), 5),
            ((2, 1), 6)
          ]
    it "toList . fromList == id" $
      toList
        ( [ [1, 2],
            [3, 4],
            [5, 6]
          ] ::
            Array (Int, Int) Int
        )
        `shouldBe` [[1, 2], [3, 4], [5, 6]]
  describe "IsList (Array (Int,Int,Int) e)" do
    it "3D-array" $
      ( [ [[1, 2, 3], [4, 5, 6]],
          [[7, 8, 9], [10, 11, 12]],
          [[13, 14, 15], [16, 17, 18]]
        ] ::
          Array (Int, Int, Int) Int
      )
        `shouldBe` array
          ((0, 0, 0), (2, 1, 2))
          [ ((0, 0, 0), 1),
            ((0, 0, 1), 2),
            ((0, 0, 2), 3),
            ((0, 1, 0), 4),
            ((0, 1, 1), 5),
            ((0, 1, 2), 6),
            ((1, 0, 0), 7),
            ((1, 0, 1), 8),
            ((1, 0, 2), 9),
            ((1, 1, 0), 10),
            ((1, 1, 1), 11),
            ((1, 1, 2), 12),
            ((2, 0, 0), 13),
            ((2, 0, 1), 14),
            ((2, 0, 2), 15),
            ((2, 1, 0), 16),
            ((2, 1, 1), 17),
            ((2, 1, 2), 18)
          ]
    it "toList . fromList == id" $
      toList
        ( [ [[1, 2, 3], [4, 5, 6]],
            [[7, 8, 9], [10, 11, 12]],
            [[13, 14, 15], [16, 17, 18]]
          ] ::
            Array (Int, Int, Int) Int
        )
        `shouldBe` [ [[1, 2, 3], [4, 5, 6]],
                     [[7, 8, 9], [10, 11, 12]],
                     [[13, 14, 15], [16, 17, 18]]
                   ]
  describe "IsList (Array (Int,Int,Int,Int) e)" do
    it "4D-array" $
      ( [ [ [[0, 1], [2, 3]],
            [[4, 5], [6, 7]]
          ],
          [ [[8, 9], [10, 11]],
            [[12, 13], [14, 15]]
          ]
        ] ::
          Array (Int, Int, Int, Int) Int
      )
        `shouldBe` array
          ((0, 0, 0, 0), (1, 1, 1, 1))
          [ ((0, 0, 0, 0), 0),
            ((0, 0, 0, 1), 1),
            ((0, 0, 1, 0), 2),
            ((0, 0, 1, 1), 3),
            ((0, 1, 0, 0), 4),
            ((0, 1, 0, 1), 5),
            ((0, 1, 1, 0), 6),
            ((0, 1, 1, 1), 7),
            ((1, 0, 0, 0), 8),
            ((1, 0, 0, 1), 9),
            ((1, 0, 1, 0), 10),
            ((1, 0, 1, 1), 11),
            ((1, 1, 0, 0), 12),
            ((1, 1, 0, 1), 13),
            ((1, 1, 1, 0), 14),
            ((1, 1, 1, 1), 15)
          ]
    it "toList . fromList == id" $
      toList
        ( [ [ [[0, 1], [2, 3]],
              [[4, 5], [6, 7]]
            ],
            [ [[8, 9], [10, 11]],
              [[12, 13], [14, 15]]
            ]
          ] ::
            Array (Int, Int, Int, Int) Int
        )
        `shouldBe` [ [ [[0, 1], [2, 3]],
                       [[4, 5], [6, 7]]
                     ],
                     [ [[8, 9], [10, 11]],
                       [[12, 13], [14, 15]]
                     ]
                   ]
  describe "IsList (Array (Int,Int,Int,Int,Int) e)" do
    it "5D-array" $
      ( [ [ [[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
            [[[8, 9], [10, 11]], [[12, 13], [14, 15]]]
          ],
          [ [[[16, 17], [18, 19]], [[20, 21], [22, 23]]],
            [[[24, 25], [26, 27]], [[28, 29], [30, 31]]]
          ]
        ] ::
          Array (Int, Int, Int, Int, Int) Int
      )
        `shouldBe` array
          ((0, 0, 0, 0, 0), (1, 1, 1, 1, 1))
          [ ((0, 0, 0, 0, 0), 0),
            ((0, 0, 0, 0, 1), 1),
            ((0, 0, 0, 1, 0), 2),
            ((0, 0, 0, 1, 1), 3),
            ((0, 0, 1, 0, 0), 4),
            ((0, 0, 1, 0, 1), 5),
            ((0, 0, 1, 1, 0), 6),
            ((0, 0, 1, 1, 1), 7),
            ((0, 1, 0, 0, 0), 8),
            ((0, 1, 0, 0, 1), 9),
            ((0, 1, 0, 1, 0), 10),
            ((0, 1, 0, 1, 1), 11),
            ((0, 1, 1, 0, 0), 12),
            ((0, 1, 1, 0, 1), 13),
            ((0, 1, 1, 1, 0), 14),
            ((0, 1, 1, 1, 1), 15),
            ((1, 0, 0, 0, 0), 16),
            ((1, 0, 0, 0, 1), 17),
            ((1, 0, 0, 1, 0), 18),
            ((1, 0, 0, 1, 1), 19),
            ((1, 0, 1, 0, 0), 20),
            ((1, 0, 1, 0, 1), 21),
            ((1, 0, 1, 1, 0), 22),
            ((1, 0, 1, 1, 1), 23),
            ((1, 1, 0, 0, 0), 24),
            ((1, 1, 0, 0, 1), 25),
            ((1, 1, 0, 1, 0), 26),
            ((1, 1, 0, 1, 1), 27),
            ((1, 1, 1, 0, 0), 28),
            ((1, 1, 1, 0, 1), 29),
            ((1, 1, 1, 1, 0), 30),
            ((1, 1, 1, 1, 1), 31)
          ]
    it "toList . fromList == id" $
      toList
        ( [ [ [[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
              [[[8, 9], [10, 11]], [[12, 13], [14, 15]]]
            ],
            [ [[[16, 17], [18, 19]], [[20, 21], [22, 23]]],
              [[[24, 25], [26, 27]], [[28, 29], [30, 31]]]
            ]
          ] ::
            Array (Int, Int, Int, Int, Int) Int
        )
        `shouldBe` [ [ [[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
                       [[[8, 9], [10, 11]], [[12, 13], [14, 15]]]
                     ],
                     [ [[[16, 17], [18, 19]], [[20, 21], [22, 23]]],
                       [[[24, 25], [26, 27]], [[28, 29], [30, 31]]]
                     ]
                   ]