Debian Codegraph @

test/hs/Test/Ganeti/Utils/Statistics.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2
3 {-| Unit tests for Ganeti statistics utils.
4
5 -}
6
7 {-
8
9 Copyright (C) 2014 Google Inc.
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are
14 met:
15
16 1. Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 -}
36
37 module Test.Ganeti.Utils.Statistics (testUtils_Statistics) where
38
39 import Test.QuickCheck (Property, forAll, choose, vectorOf)
40
41 import Test.Ganeti.TestCommon
42 import Test.Ganeti.TestHelper
43
44 import Ganeti.Utils (stdDev)
45 import Ganeti.Utils.Statistics
46
47 -- | Test the update function for standard deviations against the naive
48 -- implementation.
49 prop_stddev_update :: Property
50 prop_stddev_update =
51 forAll (choose (0, 6) >>= flip vectorOf (choose (0, 1))) $ \xs ->
52 forAll (choose (0, 1)) $ \a ->
53 forAll (choose (0, 1)) $ \b ->
54 forAll (choose (1, 6) >>= flip vectorOf (choose (0, 1))) $ \ys ->
55 let original = xs ++ [a] ++ ys
56 modified = xs ++ [b] ++ ys
57 with_update =
58 getStatisticValue
59 $ updateStatistics (getStdDevStatistics $ map SimpleNumber original)
60 (SimpleNumber a, SimpleNumber b)
61 direct = stdDev modified
62 in counterexample ("Value computed by update " ++ show with_update
63 ++ " differs too much from correct value " ++ show direct)
64 (abs (with_update - direct) < 1e-10)
65
66 testSuite "Utils/Statistics"
67 [ 'prop_stddev_update
68 ]