~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/util/descriptive_stats_test.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *    Copyright (C) 2012 10gen Inc.
 
3
 *
 
4
 *    This program is free software: you can redistribute it and/or  modify
 
5
 *    it under the terms of the GNU Affero General Public License, version 3,
 
6
 *    as published by the Free Software Foundation.
 
7
 *
 
8
 *    This program is distributed in the hope that it will be useful,
 
9
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *    GNU Affero General Public License for more details.
 
12
 *
 
13
 *    You should have received a copy of the GNU Affero General Public License
 
14
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
/**
 
18
 * Tests for mongo/util/descriptive_stats.h
 
19
 */
 
20
 
 
21
#include <cstdlib>
 
22
#include <cmath>
 
23
#include <limits>
 
24
#include <string>
 
25
 
 
26
#include "mongo/unittest/unittest.h"
 
27
#include "mongo/util/descriptive_stats.h"
 
28
 
 
29
using namespace std;
 
30
 
 
31
namespace {
 
32
 
 
33
    const size_t NumQuantiles = 99;
 
34
 
 
35
    TEST(DistributionEstimators, TestNominalResults) {
 
36
        mongo::DistributionEstimators<NumQuantiles> d;
 
37
 
 
38
        for (int i = 0; i < 100000; i++) {
 
39
            d << double(i) / 100000;
 
40
        }
 
41
        ASSERT_TRUE(d.quantilesReady());
 
42
        for (size_t quant = 1; quant <= NumQuantiles; ++quant) {
 
43
            ASSERT_EQUALS(d.probability(quant), double(quant) / 100);
 
44
            ASSERT_APPROX_EQUAL(d.quantile(quant), double(quant) / 100, 0.05);
 
45
            double prob = double(quant) / 100;
 
46
            ASSERT_APPROX_EQUAL(d.icdf(prob), prob, 0.05);
 
47
        }
 
48
        ASSERT_APPROX_EQUAL(d.min(), 0.0, 0.05);
 
49
        ASSERT_APPROX_EQUAL(d.max(), 1.0, 0.05);
 
50
        ASSERT_APPROX_EQUAL(d.median(), 0.5, 0.05);
 
51
    }
 
52
 
 
53
    TEST(DistributionEstimators, TestAppendQuantilesToBSONArrayBuilder) {
 
54
        mongo::DistributionEstimators<NumQuantiles> d;
 
55
 
 
56
        for (int i = 0; i < 10000; i++) {
 
57
            d << static_cast<double>(i) / 10000;
 
58
        }
 
59
 
 
60
        mongo::BSONArrayBuilder arrayBuilder;
 
61
        d.appendQuantilesToBSONArrayBuilder(arrayBuilder);
 
62
        mongo::BSONArray arr = arrayBuilder.arr();
 
63
 
 
64
        for (size_t i = 0; i <= NumQuantiles + 1; i++) {
 
65
            ASSERT_EQUALS(arr[i].Number(), d.quantile(i));
 
66
        }
 
67
    }
 
68
 
 
69
    TEST(BasicEstimators, TestNominalResults) {
 
70
        mongo::BasicEstimators<unsigned int> d;
 
71
 
 
72
        unsigned int count = 0;
 
73
        // [50, 51, 52, ..., 99949, 99950]
 
74
        for (int i = 50; i <= 100000 - 50; i++) {
 
75
            d << unsigned(i);
 
76
            count++;
 
77
        }
 
78
        ASSERT_EQUALS(d.min(), 50u);
 
79
        ASSERT_EQUALS(d.max(), 100000u - 50u);
 
80
        ASSERT_APPROX_EQUAL(d.mean(), 100000 / 2, 1e-15);
 
81
        ASSERT_APPROX_EQUAL(d.stddev(), sqrt((static_cast<double>(count) * count - 1) / 12), 1e-15);
 
82
    }
 
83
 
 
84
    TEST(BasicEstimators, TestAppendBasicToBSONObjBuilder) {
 
85
        mongo::BasicEstimators<unsigned int> b;
 
86
 
 
87
        for (int i = 0; i < 10000; i++) {
 
88
            b << i;
 
89
        }
 
90
 
 
91
        mongo::BSONObjBuilder builder;
 
92
        b.appendBasicToBSONObjBuilder(builder);
 
93
        mongo::BSONObj obj = builder.obj();
 
94
 
 
95
        ASSERT_EQUALS(obj["count"].Number(), b.count());
 
96
        ASSERT_EQUALS(obj["mean"].Number(), b.mean());
 
97
        ASSERT_EQUALS(obj["stddev"].Number(), b.stddev());
 
98
        ASSERT_EQUALS(obj["min"].Number(), b.min());
 
99
        ASSERT_EQUALS(obj["max"].Number(), b.max());
 
100
    }
 
101
 
 
102
    TEST(SummaryEstimators, TestNominalResults) {
 
103
        mongo::SummaryEstimators<int, NumQuantiles> d;
 
104
 
 
105
        for (int a = -200; a <= 200; a++) {
 
106
            d << a;
 
107
        }
 
108
        ASSERT_TRUE(d.quantilesReady());
 
109
        for (size_t i = 0; i < d.numberOfQuantiles; i++) {
 
110
            ASSERT_APPROX_EQUAL(d.quantile(i), -200 + static_cast<int>(i) * 4, 1);
 
111
        }
 
112
        ASSERT_EQUALS(d.min(), -200);
 
113
        ASSERT_EQUALS(d.max(), 200);
 
114
        ASSERT_APPROX_EQUAL(d.mean(), 0, 1e-15);
 
115
        ASSERT_APPROX_EQUAL(d.icdf(.25), -100, 1e-15);
 
116
    }
 
117
 
 
118
    TEST(SummaryEstimators, TestStatisticSummaryToBSONObj) {
 
119
        mongo::SummaryEstimators<double, NumQuantiles> e;
 
120
 
 
121
        for (int i = 0; i < 10000; i++) {
 
122
            e << static_cast<double>(i) / 100;
 
123
        }
 
124
        verify(e.quantilesReady());
 
125
 
 
126
        mongo::BSONObj obj = e.statisticSummaryToBSONObj();
 
127
 
 
128
        ASSERT_EQUALS(obj["count"].Number(), e.count());
 
129
        ASSERT_EQUALS(obj["mean"].Number(), e.mean());
 
130
        ASSERT_EQUALS(obj["stddev"].Number(), e.stddev());
 
131
        ASSERT_EQUALS(obj["min"].Number(), e.min());
 
132
        ASSERT_EQUALS(obj["max"].Number(), e.max());
 
133
 
 
134
        mongo::BSONObj quantiles = obj["quantiles"].Obj();
 
135
        ASSERT_EQUALS(static_cast<size_t>(quantiles.nFields()), NumQuantiles);
 
136
        for (mongo::BSONObjIterator it = quantiles.begin(); it.more(); ++it) {
 
137
            ASSERT_EQUALS((*it).Number(), e.icdf(atof((*it).fieldName())));
 
138
        }
 
139
    }
 
140
 
 
141
}  // namespace
 
142