Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
3dfe0fea
"...git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "841504bb1a32321641ec19db3cc65376af9e2bd7"
Commit
3dfe0fea
authored
Nov 24, 2012
by
Davis King
Browse files
Added unit tests for uniform_random_hash() and gaussian_random_hash().
parent
36faa29b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
132 additions
and
0 deletions
+132
-0
dlib/test/rand.cpp
dlib/test/rand.cpp
+132
-0
No files found.
dlib/test/rand.cpp
View file @
3dfe0fea
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
#include <cmath>
#include <cmath>
#include <dlib/rand.h>
#include <dlib/rand.h>
#include <dlib/compress_stream.h>
#include <dlib/compress_stream.h>
#include <dlib/hash.h>
#include "tester.h"
#include "tester.h"
...
@@ -209,6 +210,7 @@ namespace
...
@@ -209,6 +210,7 @@ namespace
rand_type
&
rnd
rand_type
&
rnd
)
)
{
{
print_spinner
();
dlog
<<
LINFO
<<
"test normality"
;
dlog
<<
LINFO
<<
"test normality"
;
double
cnt1
=
0
;
// num <= -1.2
double
cnt1
=
0
;
// num <= -1.2
double
cnt2
=
0
;
// num <= -0.5
double
cnt2
=
0
;
// num <= -0.5
...
@@ -247,9 +249,137 @@ namespace
...
@@ -247,9 +249,137 @@ namespace
}
}
void
test_gaussian_random_hash
()
{
print_spinner
();
dlog
<<
LINFO
<<
"test_gaussian_random_hash()"
;
double
cnt1
=
0
;
// num <= -1.2
double
cnt2
=
0
;
// num <= -0.5
double
cnt3
=
0
;
// num <= 0
double
cnt4
=
0
;
// num <= 0.5
double
cnt5
=
0
;
// num <= 1.2
const
unsigned
long
total
=
1000000
;
for
(
unsigned
long
i
=
0
;
i
<
total
;
++
i
)
{
const
double
r
=
gaussian_random_hash
(
i
,
0
,
0
);
if
(
r
<=
-
1.2
)
cnt1
+=
1
;
if
(
r
<=
-
0.5
)
cnt2
+=
1
;
if
(
r
<=
0
)
cnt3
+=
1
;
if
(
r
<=
0.5
)
cnt4
+=
1
;
if
(
r
<=
1.2
)
cnt5
+=
1
;
}
for
(
unsigned
long
i
=
0
;
i
<
total
;
++
i
)
{
const
double
r
=
gaussian_random_hash
(
0
,
i
,
0
);
if
(
r
<=
-
1.2
)
cnt1
+=
1
;
if
(
r
<=
-
0.5
)
cnt2
+=
1
;
if
(
r
<=
0
)
cnt3
+=
1
;
if
(
r
<=
0.5
)
cnt4
+=
1
;
if
(
r
<=
1.2
)
cnt5
+=
1
;
}
for
(
unsigned
long
i
=
0
;
i
<
total
;
++
i
)
{
const
double
r
=
gaussian_random_hash
(
0
,
0
,
i
);
if
(
r
<=
-
1.2
)
cnt1
+=
1
;
if
(
r
<=
-
0.5
)
cnt2
+=
1
;
if
(
r
<=
0
)
cnt3
+=
1
;
if
(
r
<=
0.5
)
cnt4
+=
1
;
if
(
r
<=
1.2
)
cnt5
+=
1
;
}
cnt1
/=
total
*
3
;
cnt2
/=
total
*
3
;
cnt3
/=
total
*
3
;
cnt4
/=
total
*
3
;
cnt5
/=
total
*
3
;
dlog
<<
LINFO
<<
"cnt1: "
<<
cnt1
;
dlog
<<
LINFO
<<
"cnt2: "
<<
cnt2
;
dlog
<<
LINFO
<<
"cnt3: "
<<
cnt3
;
dlog
<<
LINFO
<<
"cnt4: "
<<
cnt4
;
dlog
<<
LINFO
<<
"cnt5: "
<<
cnt5
;
DLIB_TEST
(
std
::
abs
(
cnt1
-
0.11507
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt2
-
0.30854
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt3
-
0.5
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt4
-
0.69146
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt5
-
0.88493
)
<
0.001
);
}
void
test_uniform_random_hash
()
{
print_spinner
();
dlog
<<
LINFO
<<
"test_uniform_random_hash()"
;
double
cnt1
=
0
;
// num <= 0.2
double
cnt2
=
0
;
// num <= 0.4
double
cnt3
=
0
;
// num <= 0.6
double
cnt4
=
0
;
// num <= 0.8
double
cnt5
=
0
;
// num <= 1.0
double
min_val
=
10
;
double
max_val
=
0
;
const
unsigned
long
total
=
1000000
;
for
(
unsigned
long
i
=
0
;
i
<
total
;
++
i
)
{
const
double
r
=
uniform_random_hash
(
i
,
0
,
0
);
min_val
=
min
(
r
,
min_val
);
max_val
=
max
(
r
,
max_val
);
if
(
r
<=
0.2
)
cnt1
+=
1
;
if
(
r
<=
0.4
)
cnt2
+=
1
;
if
(
r
<=
0.6
)
cnt3
+=
1
;
if
(
r
<=
0.8
)
cnt4
+=
1
;
if
(
r
<=
1.0
)
cnt5
+=
1
;
}
for
(
unsigned
long
i
=
0
;
i
<
total
;
++
i
)
{
const
double
r
=
uniform_random_hash
(
0
,
i
,
0
);
min_val
=
min
(
r
,
min_val
);
max_val
=
max
(
r
,
max_val
);
if
(
r
<=
0.2
)
cnt1
+=
1
;
if
(
r
<=
0.4
)
cnt2
+=
1
;
if
(
r
<=
0.6
)
cnt3
+=
1
;
if
(
r
<=
0.8
)
cnt4
+=
1
;
if
(
r
<=
1.0
)
cnt5
+=
1
;
}
for
(
unsigned
long
i
=
0
;
i
<
total
;
++
i
)
{
const
double
r
=
uniform_random_hash
(
0
,
0
,
i
);
min_val
=
min
(
r
,
min_val
);
max_val
=
max
(
r
,
max_val
);
if
(
r
<=
0.2
)
cnt1
+=
1
;
if
(
r
<=
0.4
)
cnt2
+=
1
;
if
(
r
<=
0.6
)
cnt3
+=
1
;
if
(
r
<=
0.8
)
cnt4
+=
1
;
if
(
r
<=
1.0
)
cnt5
+=
1
;
}
cnt1
/=
total
*
3
;
cnt2
/=
total
*
3
;
cnt3
/=
total
*
3
;
cnt4
/=
total
*
3
;
cnt5
/=
total
*
3
;
dlog
<<
LINFO
<<
"cnt1: "
<<
cnt1
;
dlog
<<
LINFO
<<
"cnt2: "
<<
cnt2
;
dlog
<<
LINFO
<<
"cnt3: "
<<
cnt3
;
dlog
<<
LINFO
<<
"cnt4: "
<<
cnt4
;
dlog
<<
LINFO
<<
"cnt5: "
<<
cnt5
;
dlog
<<
LINFO
<<
"min_val: "
<<
min_val
;
dlog
<<
LINFO
<<
"max_val: "
<<
max_val
;
DLIB_TEST
(
std
::
abs
(
cnt1
-
0.2
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt2
-
0.4
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt3
-
0.6
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt4
-
0.8
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
cnt5
-
1.0
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
min_val
-
0.0
)
<
0.001
);
DLIB_TEST
(
std
::
abs
(
max_val
-
1.0
)
<
0.001
);
}
class
rand_tester
:
public
tester
class
rand_tester
:
public
tester
{
{
...
@@ -269,6 +399,8 @@ namespace
...
@@ -269,6 +399,8 @@ namespace
dlib
::
rand
rnd
;
dlib
::
rand
rnd
;
test_normal_numbers
(
rnd
);
test_normal_numbers
(
rnd
);
test_gaussian_random_hash
();
test_uniform_random_hash
();
}
}
}
a
;
}
a
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment