File: ProgressTrackertest.hs

package info (click to toggle)
missingh 1.6.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 688 kB
  • sloc: haskell: 5,472; makefile: 2
file content (132 lines) | stat: -rw-r--r-- 4,256 bytes parent folder | download | duplicates (2)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{-
Copyright (C) 2006-2011 John Goerzen <jgoerzen@complete.org>

All rights reserved.

For license and copyright information, see the file LICENSE
-}

module ProgressTrackertest(tests) where
import Data.Progress.Tracker
import Test.HUnit
import Control.Concurrent.MVar

setup =
    do timem <- newMVar 0
       let timesource = readMVar timem
       po <- newProgress' (ProgressStatus 0 100 0 "" timesource) []
       return (po, timem)

settime timem newval = swapMVar timem newval >> return ()

test_incrP =
    do (po, timem) <- setup
       incrP po 5
       withStatus po $ \s ->
                  do assertEqual "completedUnits" 5 (completedUnits s)
                     assertEqual "totalUnits" 100 (totalUnits s)
       incrP po 95
       withStatus po $ \s ->
                  do assertEqual "completedUnits" 100 (completedUnits s)
                     assertEqual "totalUnits" 100 (totalUnits s)
       incrP po 5
       withStatus po $ \s ->
                  do assertEqual "completedUnits" 105 (completedUnits s)
                     assertEqual "totalUnits" 105 (totalUnits s)
       incrP' po 5
       withStatus po $ \s ->
                  do assertEqual "completedUnits" 110 (completedUnits s)
                     assertEqual "totalUnits" 105 (totalUnits s)
       incrTotal po 10
       withStatus po $ \s ->
                  do 110 @=? completedUnits s
                     115 @=? totalUnits s

test_setP =
    do (po, timem) <- setup
       setP po 5
       withStatus po $ \s ->
           do 5 @=? completedUnits s
              100 @=? totalUnits s
       setP po 100
       withStatus po $ \s ->
           do 100 @=? completedUnits s
              100 @=? totalUnits s
       setP po 105
       withStatus po $ \s ->
           do 105 @=? completedUnits s
              105 @=? totalUnits s
       setP' po 110
       withStatus po $ \s ->
           do 110 @=? completedUnits s
              105 @=? totalUnits s
       setTotal po 115
       withStatus po $ \s ->
           do 110 @=? completedUnits s
              115 @=? totalUnits s

test_speed =
    do (po, timem) <- setup
       getSpeed po >>= assertEqual "initial speed" 0
       getETR po >>= assertEqual "initial ETR" 0
       getETA po >>= assertEqual "initial ETA" 0

       incrP po 10
       getSpeed po >>= assertEqual "speed after incr" 0
       getETR po >>= assertEqual "ETR after incr" 0
       getETA po >>= assertEqual "ETA after incr" 0

       settime timem 5
       getSpeed po >>= assertEqual "first speed" 2.0
       getETR po >>= assertEqual "first ETR" 45
       getETA po >>= assertEqual "first ETA" 50

       incrP po 90
       getSpeed po >>= assertEqual "speed 2" 20.0
       getETR po >>= assertEqual "etr 2" 0
       getETA po >>= assertEqual "eta 2" 5

       settime timem 400
       setP po 90
       getSpeed po >>= assertEqual "speed 3" 0.225
       getETR po >>= assertEqual "etr 2" 44
       getETA po >>= assertEqual "eta 2" 444

test_callback =
    do (po, _) <- setup
       mcounter <- newMVar (0::Int)
       mcounter1 <- newMVar (0::Int)
       mcounter2 <- newMVar (0::Int)
       (po2, _) <- setup
       (po3, _) <- setup

       addCallback po (minc mcounter)
       addParent po po2
       incrP po 5
       readMVar mcounter >>= assertEqual "cb1" 1
       withStatus po (\x -> 5 @=? completedUnits x)
       withStatus po2 (\x -> do 5 @=? completedUnits x
                                200 @=? totalUnits x)

       addCallback po2 (minc mcounter2)
       incrP po 100
       readMVar mcounter2 >>= (\x -> assertBool "cb2" (0 /= x))
       withStatus po2 (\x -> do 105 @=? completedUnits x
                                205 @=? totalUnits x)

       incrP' po 5
       withStatus po2 (\x -> do 110 @=? completedUnits x
                                205 @=? totalUnits x)

       finishP po
       withStatus po2 (\x -> do 110 @=? completedUnits x
                                210 @=? totalUnits x)


    where minc mv _ _ = modifyMVar_ mv (\x -> return $ x + 1)

tests = TestList [TestLabel "incrP" (TestCase test_incrP),
                  TestLabel "setP" (TestCase test_setP),
                  TestLabel "speed" (TestCase test_speed),
                  TestLabel "test_callback" (TestCase test_callback)]