~ubuntu-branches/ubuntu/saucy/openexr/saucy

« back to all changes in this revision

Viewing changes to ImathTest/testRandom.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adeodato Simó
  • Date: 2008-03-24 23:00:21 UTC
  • mfrom: (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080324230021-gnofz9mnvcj1xlv3
Tags: 1.6.1-3
Disable (hopefully temporarily) the test suite on arm and ia64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///////////////////////////////////////////////////////////////////////////
2
 
//
3
 
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4
 
// Digital Ltd. LLC
5
 
// 
6
 
// All rights reserved.
7
 
// 
8
 
// Redistribution and use in source and binary forms, with or without
9
 
// modification, are permitted provided that the following conditions are
10
 
// met:
11
 
// *       Redistributions of source code must retain the above copyright
12
 
// notice, this list of conditions and the following disclaimer.
13
 
// *       Redistributions in binary form must reproduce the above
14
 
// copyright notice, this list of conditions and the following disclaimer
15
 
// in the documentation and/or other materials provided with the
16
 
// distribution.
17
 
// *       Neither the name of Industrial Light & Magic nor the names of
18
 
// its contributors may be used to endorse or promote products derived
19
 
// from this software without specific prior written permission. 
20
 
// 
21
 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 
//
33
 
///////////////////////////////////////////////////////////////////////////
34
 
 
35
 
 
36
 
 
37
 
#include <testRandom.h>
38
 
#include <ImathRandom.h>
39
 
#include <ImathVec.h>
40
 
#include <ImathFun.h>
41
 
#include <iostream>
42
 
#include <iomanip>
43
 
#include <assert.h>
44
 
 
45
 
 
46
 
using namespace std;
47
 
using Imath::abs;
48
 
 
49
 
namespace {
50
 
 
51
 
 
52
 
template <class Rand>
53
 
void
54
 
testGenerator ()
55
 
{
56
 
    //
57
 
    // Test if the values, and the differences between
58
 
    // successive values, are evenly distributed.
59
 
    //
60
 
 
61
 
    const int N = 10;
62
 
    const int M = 100000;
63
 
 
64
 
    int values[N + 1];
65
 
    int diffs[2 * N + 3];
66
 
    int *v = &values[0];
67
 
    int *d = &diffs[N + 2];
68
 
 
69
 
    for (int i = 0; i <= N; ++i)
70
 
        v[i] = 0;
71
 
 
72
 
    for (int i = -N; i <= N; ++i)
73
 
        d[i] = 0;
74
 
 
75
 
    Rand rand (0);
76
 
    float previous = 0;
77
 
 
78
 
    for (int i = 0; i < M * N; ++i)
79
 
    {
80
 
        float r = rand.nextf (0.0, 1.0);
81
 
        float diff = r - previous;
82
 
        previous = r;
83
 
 
84
 
        v[int (r * N)] += 1;
85
 
        d[Imath::floor (diff * N + 0.5)] += 1;
86
 
    }
87
 
 
88
 
    cout << "  values" << endl;
89
 
 
90
 
    for (int i = 0; i < N; ++i)
91
 
    {
92
 
        // cout << setw (4) << i << ' ' << setw(6) << v[i] << ' ';
93
 
        assert (abs (v[i] - M) < 0.01 * M);
94
 
 
95
 
        // for (int j = 0; j < v[i] * 60 / M; ++j)
96
 
        //     cout << '*';
97
 
 
98
 
        // cout << endl;
99
 
    }
100
 
 
101
 
    assert (v[N] == 0);
102
 
 
103
 
    cout << "  differences between successive values" << endl;
104
 
 
105
 
    for (int i = -N; i <= N; ++i)
106
 
    {
107
 
        // cout << setw (4) << i << ' ' << setw (6) << d[i] << ' ';
108
 
        assert (abs ((N - abs (i)) * M / N - d[i]) < 0.05 * M);
109
 
 
110
 
        // for (int j = 0; j < d[i] * 60 / M; ++j)
111
 
        //     cout << '*';
112
 
 
113
 
        // cout << endl;
114
 
    }
115
 
}
116
 
 
117
 
 
118
 
template <class Rand>
119
 
void
120
 
testSolidSphere ()
121
 
{
122
 
    const int N = 10;
123
 
    const int M = 10000;
124
 
    int v[N + 1];
125
 
 
126
 
    for (int i = 0; i <= N; ++i)
127
 
        v[i] = 0;
128
 
 
129
 
    Rand rand (0);
130
 
 
131
 
    for (int i = 0; i < M * N; ++i)
132
 
    {
133
 
        Imath::V3f p = Imath::solidSphereRand<Imath::V3f> (rand);
134
 
        float l = p.length();
135
 
        v[Imath::floor (l * N)] += 1;
136
 
 
137
 
        assert (l < 1.00001);
138
 
    }
139
 
 
140
 
    for (int i = 0; i < N; ++i)
141
 
        assert (v[i] > 0);
142
 
}
143
 
 
144
 
 
145
 
template <class Rand>
146
 
void
147
 
testHollowSphere ()
148
 
{
149
 
    const int M = 100000;
150
 
    Rand rand (0);
151
 
 
152
 
    for (int i = 0; i < M; ++i)
153
 
    {
154
 
        Imath::V3f p = Imath::hollowSphereRand<Imath::V3f> (rand);
155
 
        float l = p.length();
156
 
 
157
 
        assert (abs (l - 1) < 0.00001);
158
 
    }
159
 
}
160
 
 
161
 
 
162
 
} // namespace
163
 
 
164
 
 
165
 
void
166
 
testRandom ()
167
 
{
168
 
    cout << "Testing random number generators" << endl;
169
 
 
170
 
    cout << "Rand32" << endl;
171
 
    testGenerator<Imath::Rand32>();
172
 
 
173
 
    cout << "Rand48" << endl;
174
 
    testGenerator<Imath::Rand32>();
175
 
 
176
 
    cout << "solidSphereRand()" << endl;
177
 
    testSolidSphere<Imath::Rand32>();
178
 
 
179
 
    cout << "hollowSphereRand()" << endl;
180
 
    testHollowSphere<Imath::Rand32>();
181
 
 
182
 
    cout << "ok\n" << endl;
183
 
}