kowainik / treap

:leaves: :deciduous_tree: :fallen_leaf: Efficient implementation of the implicit treap data structure
Mozilla Public License 2.0
63 stars 1 forks source link

More robust unit testing with both elements and measure #26

Closed chshersh closed 5 years ago

chshersh commented 5 years ago

Currently unit tests compare two treaps only by comparing elements via toList function. It would be more robust to test also the measure of the Treap. But for that we need more convenient util functions.

sphaso commented 5 years ago

Do you have any idea regarding this? would it be enough to map through the Treap, taking the measures and comparing the list of measures?

chshersh commented 5 years ago

@sphaso Sure, I have an idea. Currently unit tests for the update and construction functions look like this:

https://github.com/chshersh/treap/blob/8ff56e8dfeec056f15aa748aea487557e2b9f855/test/Test/Rand/Update.hs#L35-L36

What would be nice is to write the tests in the following form:

it "delete 0 removes first element" $
    Treap.delete 0 smallTreap `describedAs` [2..5] `with` 14

In order to make this work we need to add several custom functions to the Test.Common module, specifically:

with :: [a] -> m -> ([a], m)
with = (,)

describedAs :: Treap m a -> ([a], m) -> Expectation

(the constraints and fixities are to figure out)

sphaso commented 5 years ago

@chshersh great. I'm working on it on my fork. Do you think these types are good or should they be more generic?

with :: [Int] -> Int -> ([Int], Sum Int)
describedAs :: TestTreap -> ([Int], Sum Int) -> Bool
chshersh commented 5 years ago

@sphaso Almost correct. But instead of Bool, describedAs should have result type Expectation from the hspec library and implemented in terms of shouldBe

sphaso commented 5 years ago

@chshersh gotcha, I completely missed the Expectation type :) I opened a PR.