module PointTest exposing (specs)

import Point exposing (Point, downsample, subseq)
import Pos exposing (Pos)
import Test exposing (..)
import Expect exposing (Expectation)

newPoint i = Point
             (1731437067 + (toFloat i)*1.2)
             (Pos 52 1 (Just 10))
             (Just (round(sin((toFloat i)/10)*10))) (Just i) Nothing


points n = List.map newPoint (List.range 0 n)

maybe default val =
    case val of
        Just v -> v
        Nothing -> default

specs: Test
specs =
    describe "Point"
        [ describe "downsample"
              [ test "it returns no more points than requested" <|
                    \_ ->
                    let orig = (points 10)
                        sampled = Point.downsample 5 orig
                    in Expect.equal 5 (List.length sampled)
              , test "it drops points which are too close together" <|
                  \_ ->
                      let orig = List.map newPoint [ 0, 1, 2, 3, 4, 10, 500, 1000 ]
                          sampled = Point.downsample 10 orig
                          idx p = maybe 0 p.power
                      in Expect.equalLists [0, 500, 1000] (List.map idx sampled)
              ]
        , describe "subseq"
            [ test "chooses only points in the time range" <|
                  \_ ->
                  let orig = (points 50)
                      s = 1731437087
                      d = 40
                      subs = subseq orig s d
                  in Expect.equal True
                      (List.all (\ p -> p.time >= s && p.time <= s + d) subs)
            , test "is empty if no points" <|
                  \_ ->
                      Expect.equalLists [] (subseq [] 10 10)
            , test "is empty if no time" <|
                  \_ ->
                      Expect.equalLists (subseq (points 50) 1731437067 0) []
            ]
        ]
