1 | // |
---|---|
2 | // RandomStreamTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "RandomStreamTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/RandomStream.h" |
15 | #include <vector> |
16 | #include <cmath> |
17 | |
18 | |
19 | using Poco::RandomInputStream; |
20 | |
21 | |
22 | RandomStreamTest::RandomStreamTest(const std::string& rName): CppUnit::TestCase(rName) |
23 | { |
24 | } |
25 | |
26 | |
27 | RandomStreamTest::~RandomStreamTest() |
28 | { |
29 | } |
30 | |
31 | |
32 | void RandomStreamTest::testStream() |
33 | { |
34 | RandomInputStream rnd; |
35 | |
36 | const int n = 16; |
37 | std::vector<int> d(n, 0); |
38 | for (int i = 0; i < 1000; ++i) |
39 | { |
40 | unsigned char c; |
41 | rnd >> c; |
42 | d[c & 0x0F]++; |
43 | d[(c >> 4) & 0x0F]++; |
44 | } |
45 | int sum = 0; |
46 | for (int k = 0; k < n; ++k) sum += d[k]; |
47 | int avg = sum/n; |
48 | int var = 0; |
49 | for (int k = 0; k < n; ++k) var += (d[k] - avg)*(d[k] - avg); |
50 | var /= n; |
51 | int sd = int(std::sqrt((double) var)); |
52 | |
53 | assertTrue (110 < avg && avg < 140); |
54 | assertTrue (sd < 20); |
55 | } |
56 | |
57 | |
58 | void RandomStreamTest::setUp() |
59 | { |
60 | } |
61 | |
62 | |
63 | void RandomStreamTest::tearDown() |
64 | { |
65 | } |
66 | |
67 | |
68 | CppUnit::Test* RandomStreamTest::suite() |
69 | { |
70 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RandomStreamTest"); |
71 | |
72 | CppUnit_addTest(pSuite, RandomStreamTest, testStream); |
73 | |
74 | return pSuite; |
75 | } |
76 |