packages feed

treemap-2.0.0.20161218: Data/TreeMap/Strict/Test.hs

{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
module Strict.Test where

import Data.Function (($), id, const)
import Data.Int (Int)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Map.Strict as Map
import Data.Monoid ((<>))
import qualified Data.Strict.Maybe as Strict
import Prelude (Integer, undefined)
import Test.Tasty
import Test.Tasty.HUnit

import qualified Data.TreeMap.Strict as TreeMap

tests :: TestTree
tests = testGroup "Strict"
 [ testGroup "insert"
	 [ testCase "[] 0" $
			TreeMap.insert const ((0::Int):|[]) () TreeMap.empty
		 @?= (TreeMap.TreeMap $ Map.fromList [ (0::Int, TreeMap.leaf ()) ])
	 , testCase "[] 0/1" $
			TreeMap.insert const ((0::Int):|[1]) () TreeMap.empty
		 @?=
			(TreeMap.TreeMap $
			Map.fromList
			 [ (0::Int, TreeMap.Node
				 { TreeMap.node_value = Strict.Nothing
				 , TreeMap.node_size = 1
				 , TreeMap.node_descendants =
					TreeMap.singleton ((1::Int):|[]) ()
				 })
			 ])
	 ]
 , testGroup "map_by_depth_first"
	 [ testCase "[0, 0/1, 0/1/2, 1, 1/2/3]" $
			TreeMap.map_by_depth_first
			 (\descendants value ->
				Map.foldl'
				 (\acc v -> (<>) acc $
					Strict.fromMaybe undefined $
					TreeMap.node_value v
				 )
				 (Strict.fromMaybe [] value)
				 (TreeMap.nodes descendants)
			 )
			(TreeMap.from_List const
			 [ ((0::Integer):|[], [0::Integer])
			 , (0:|[1], [0,1])
			 , (0:|[1,2], [0,1,2])
			 , (1:|[], [1])
			 , (1:|[2,3], [1,2,3])
			 ])
		 @?=
			TreeMap.from_List const
			 [ ((0::Integer):|[], [0,0,1,0,1,2])
			 , (0:|[1], [0,1,0,1,2])
			 , (0:|[1,2], [0,1,2])
			 , (1:|[], [1,1,2,3])
			 , (1:|[2], [1,2,3])
			 , (1:|[2,3], [1,2,3])
			 ]
	 , testCase "[0/0]" $
			TreeMap.map_by_depth_first
			 (\descendants value ->
				Map.foldl'
				 (\acc v -> (<>) acc $
					Strict.fromMaybe undefined $
					TreeMap.node_value v
				 )
				 (Strict.fromMaybe [] value)
				 (TreeMap.nodes descendants)
			 )
			(TreeMap.from_List const
			 [ ((0::Integer):|[0], [0::Integer,0])
			 ])
		 @?=
			TreeMap.from_List const
			 [ ((0::Integer):|[], [0,0])
			 , (0:|[0], [0,0])
			 ]
	 ]
 , testGroup "flatten"
	 [ testCase "[0, 0/1, 0/1/2]" $
			TreeMap.flatten id
			(TreeMap.from_List const
			 [ ((0::Integer):|[], ())
			 , (0:|[1], ())
			 , (0:|[1,2], ())
			 ])
		 @?=
			Map.fromList
			 [ ((0::Integer):|[], ())
			 , (0:|[1], ())
			 , (0:|[1,2], ())
			 ]
	 , testCase "[1, 1/2, 1/22, 1/2/3, 1/2/33, 11, 11/2, 11/2/3, 11/2/33]" $
			TreeMap.flatten id
			(TreeMap.from_List const
			 [ ((1::Integer):|[], ())
			 , (1:|[2], ())
			 , (1:|[22], ())
			 , (1:|[2,3], ())
			 , (1:|[2,33], ())
			 , (11:|[], ())
			 , (11:|[2], ())
			 , (11:|[2,3], ())
			 , (11:|[2,33], ())
			 ])
		 @?=
			Map.fromList
			 [ ((1::Integer):|[], ())
			 , (1:|[2], ())
			 , (1:|[22], ())
			 , (1:|[2,3], ())
			 , (1:|[2,33], ())
			 , (11:|[], ())
			 , (11:|[2], ())
			 , (11:|[2,3], ())
			 , (11:|[2,33], ())
			 ]
	 ]
 , testGroup "find_along"
	 [ testCase "0/1/2/3 [0, 0/1, 0/1/2, 0/1/2/3]" $
			TreeMap.find_along
			 (0:|[1,2,3])
			(TreeMap.from_List const
			 [ ((0::Integer):|[], [0])
			 , (0:|[1], [0,1])
			 , (0:|[1,2], [0,1,2])
			 , (0:|[1,2,3], [0,1,2,3])
			 ])
		 @?=
			[ [0::Integer]
			, [0,1]
			, [0,1,2]
			, [0,1,2,3]
			]
	 , testCase "0/1/2/3 [0, 0/1]" $
			TreeMap.find_along
			 (0:|[1,2,3])
			(TreeMap.from_List const
			 [ ((0::Integer):|[], [0])
			 , (0:|[1], [0,1])
			 ])
		 @?=
			[ [0::Integer]
			, [0,1]
			]
	 ]
 ]