Skip to content

Commit 0774ab6

Browse files
ENH: update backend tests (#552)
* ENH: update backend tests
1 parent 76fbbc7 commit 0774ab6

File tree

3 files changed

+164
-103
lines changed

3 files changed

+164
-103
lines changed

dpnp/backend/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ enable_testing()
4444
include_directories(${GTEST_INCLUDE_DIR})
4545
link_directories(${GTEST_LIB_DIR})
4646

47-
add_executable(dpnpc_tests test_random.cpp)
47+
add_executable(dpnpc_tests test_main.cpp test_random.cpp)
4848
target_link_libraries(dpnpc_tests GTest::GTest GTest::Main pthread dpnp_backend_c)

dpnp/backend/tests/test_main.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2016-2020, Intel Corporation
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
// - Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// - Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
// THE POSSIBILITY OF SUCH DAMAGE.
24+
//*****************************************************************************
25+
26+
#include "gtest/gtest.h"
27+
28+
#include <dpnp_iface.hpp>
29+
30+
// TODO add namespace
31+
// will added for test_commons
32+
class DPNPCTestEnvironment : public testing::Environment
33+
{
34+
public:
35+
void SetUp() override
36+
{
37+
// TODO update print
38+
std::cout << "starting new env" << std::endl << std::endl;
39+
}
40+
void TearDown() override
41+
{
42+
}
43+
};
44+
45+
int RunAllTests(DPNPCTestEnvironment* env)
46+
{
47+
// testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
48+
(void)env;
49+
50+
// It returns 0 if all tests are successful, or 1 otherwise.
51+
return RUN_ALL_TESTS();
52+
}
53+
54+
int main(int argc, char** argv)
55+
{
56+
::testing::InitGoogleTest(&argc, argv);
57+
58+
// currently using global queue
59+
60+
// It returns 0 if all tests are successful, or 1 otherwise.
61+
return RUN_ALL_TESTS();
62+
}

dpnp/backend/tests/test_random.cpp

Lines changed: 101 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,60 @@
2626
#include <dpnp_iface.hpp>
2727
#include <dpnp_iface_fptr.hpp>
2828

29+
#include <iostream>
30+
2931
#include <math.h>
3032
#include <vector>
3133

3234
#include "gtest/gtest.h"
3335

36+
// TODO
37+
// * data management will be redesigned: allocation as chars (and casting on teste suits)
38+
// * this class will be generlized
39+
class RandomTestCase : public ::testing::Test
40+
{
41+
public:
42+
static void SetUpTestCase()
43+
{
44+
_get_device_mem();
45+
}
46+
47+
static void TearDownTestCase()
48+
{
49+
dpnp_memory_free_c(result1);
50+
result1 = result2 = nullptr;
51+
}
52+
53+
void SetUp() override
54+
{
55+
for (size_t i = 0; i < size; ++i)
56+
{
57+
result1[i] = 1;
58+
result2[i] = 0;
59+
}
60+
}
61+
62+
void TearDown() override
63+
{
64+
}
65+
66+
private:
67+
static void _get_device_mem()
68+
{
69+
result1 = (double*)dpnp_memory_alloc_c(size * 2 * sizeof(double));
70+
result2 = result1 + size;
71+
}
72+
73+
protected:
74+
static size_t size;
75+
static double* result1;
76+
static double* result2;
77+
};
78+
79+
double* RandomTestCase::result1 = nullptr;
80+
double* RandomTestCase::result2 = nullptr;
81+
size_t RandomTestCase::size = 10;
82+
3483
template <typename _DataType>
3584
bool check_statistics(_DataType* r, double tM, double tD, double tQ, size_t size)
3685
{
@@ -64,115 +113,75 @@ bool check_statistics(_DataType* r, double tM, double tD, double tQ, size_t size
64113
return true;
65114
}
66115

67-
TEST(TestBackendRandomBeta, test_seed)
116+
TEST_F(RandomTestCase, rng_beta_test_seed)
68117
{
69-
const size_t size = 256;
70-
size_t seed = 10;
71-
double a = 0.4;
72-
double b = 0.5;
73-
74-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
118+
const size_t seed = 10;
119+
const double a = 0.4;
120+
const double b = 0.5;
75121

76-
for (auto device_selector : QueueOptionsDevices)
77-
{
78-
dpnp_queue_initialize_c(device_selector);
79-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
80-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
81-
82-
dpnp_rng_srand_c(seed);
83-
dpnp_rng_beta_c<double>(result1, a, b, size);
122+
dpnp_rng_srand_c(seed);
123+
dpnp_rng_beta_c<double>(result1, a, b, size);
84124

85-
dpnp_rng_srand_c(seed);
86-
dpnp_rng_beta_c<double>(result2, a, b, size);
125+
dpnp_rng_srand_c(seed);
126+
dpnp_rng_beta_c<double>(result2, a, b, size);
87127

88-
for (size_t i = 0; i < size; ++i)
89-
{
90-
EXPECT_NEAR(result1[i], result2[i], 0.004);
91-
}
128+
for (size_t i = 0; i < size; ++i)
129+
{
130+
EXPECT_NEAR(result1[i], result2[i], 0.004);
92131
}
93132
}
94133

95-
TEST(TestBackendRandomF, test_seed)
134+
TEST_F(RandomTestCase, rng_f_test_seed)
96135
{
97-
const size_t size = 256;
98-
size_t seed = 10;
99-
double dfnum = 10.4;
100-
double dfden = 12.5;
136+
const size_t seed = 10;
137+
const double dfnum = 10.4;
138+
const double dfden = 12.5;
101139

102-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
140+
dpnp_rng_srand_c(seed);
141+
dpnp_rng_f_c<double>(result1, dfnum, dfden, size);
103142

104-
for (auto device_selector : QueueOptionsDevices)
105-
{
106-
dpnp_queue_initialize_c(device_selector);
107-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
108-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
109-
110-
dpnp_rng_srand_c(seed);
111-
dpnp_rng_f_c<double>(result1, dfnum, dfden, size);
143+
dpnp_rng_srand_c(seed);
144+
dpnp_rng_f_c<double>(result2, dfnum, dfden, size);
112145

113-
dpnp_rng_srand_c(seed);
114-
dpnp_rng_f_c<double>(result2, dfnum, dfden, size);
115-
116-
for (size_t i = 0; i < size; ++i)
117-
{
118-
EXPECT_NEAR(result1[i], result2[i], 0.004);
119-
}
146+
for (size_t i = 0; i < size; ++i)
147+
{
148+
EXPECT_NEAR(result1[i], result2[i], 0.004);
120149
}
121150
}
122151

123-
TEST(TestBackendRandomNormal, test_seed)
152+
TEST_F(RandomTestCase, rng_normal_test_seed)
124153
{
125-
const size_t size = 256;
126-
size_t seed = 10;
127-
double loc = 2.56;
128-
double scale = 0.8;
129-
130-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
131-
132-
for (auto device_selector : QueueOptionsDevices)
133-
{
134-
dpnp_queue_initialize_c(device_selector);
135-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
136-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
154+
const size_t seed = 10;
155+
const double loc = 2.56;
156+
const double scale = 0.8;
137157

138-
dpnp_rng_srand_c(seed);
139-
dpnp_rng_normal_c<double>(result1, loc, scale, size);
158+
dpnp_rng_srand_c(seed);
159+
dpnp_rng_normal_c<double>(result1, loc, scale, size);
140160

141-
dpnp_rng_srand_c(seed);
142-
dpnp_rng_normal_c<double>(result2, loc, scale, size);
161+
dpnp_rng_srand_c(seed);
162+
dpnp_rng_normal_c<double>(result2, loc, scale, size);
143163

144-
for (size_t i = 0; i < size; ++i)
145-
{
146-
EXPECT_NEAR(result1[i], result2[i], 0.004);
147-
}
164+
for (size_t i = 0; i < size; ++i)
165+
{
166+
EXPECT_NEAR(result1[i], result2[i], 0.004);
148167
}
149168
}
150169

151-
TEST(TestBackendRandomUniform, test_seed)
170+
TEST_F(RandomTestCase, rng_uniform_test_seed)
152171
{
153-
const size_t size = 256;
154-
size_t seed = 10;
155-
long low = 1;
156-
long high = 120;
157-
158-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
159-
160-
for (auto device_selector : QueueOptionsDevices)
161-
{
162-
dpnp_queue_initialize_c(device_selector);
163-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
164-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
172+
const size_t seed = 10;
173+
const long low = 1;
174+
const long high = 120;
165175

166-
dpnp_rng_srand_c(seed);
167-
dpnp_rng_uniform_c<double>(result1, low, high, size);
176+
dpnp_rng_srand_c(seed);
177+
dpnp_rng_uniform_c<double>(result1, low, high, size);
168178

169-
dpnp_rng_srand_c(seed);
170-
dpnp_rng_uniform_c<double>(result2, low, high, size);
179+
dpnp_rng_srand_c(seed);
180+
dpnp_rng_uniform_c<double>(result2, low, high, size);
171181

172-
for (size_t i = 0; i < size; ++i)
173-
{
174-
EXPECT_NEAR(result1[i], result2[i], 0.004);
175-
}
182+
for (size_t i = 0; i < size; ++i)
183+
{
184+
EXPECT_NEAR(result1[i], result2[i], 0.004);
176185
}
177186
}
178187

@@ -189,20 +198,16 @@ TEST(TestBackendRandomUniform, test_statistics)
189198
double tD = ((b - a) * (b - a)) / 12.0;
190199
double tQ = ((b - a) * (b - a) * (b - a) * (b - a)) / 80.0;
191200

192-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
193-
194-
for (auto device_selector : QueueOptionsDevices)
195-
{
196-
dpnp_queue_initialize_c(device_selector);
197-
double* result = (double*)dpnp_memory_alloc_c(size * sizeof(double));
198-
dpnp_rng_srand_c(seed);
199-
dpnp_rng_uniform_c<double>(result, a, b, size);
200-
check_statistics_res = check_statistics<double>(result, tM, tD, tQ, size);
201-
202-
ASSERT_TRUE(check_statistics_res);
203-
}
201+
double* result = (double*)dpnp_memory_alloc_c(size * sizeof(double));
202+
dpnp_rng_srand_c(seed);
203+
dpnp_rng_uniform_c<double>(result, a, b, size);
204+
check_statistics_res = check_statistics<double>(result, tM, tD, tQ, size);
205+
ASSERT_TRUE(check_statistics_res);
206+
dpnp_memory_free_c(result);
204207
}
205208

209+
// TODO:
210+
// Generalization for all DPNPFuncName
206211
TEST(TestBackendRandomSrand, test_func_ptr)
207212
{
208213
void* fptr = nullptr;
@@ -216,9 +221,3 @@ TEST(TestBackendRandomSrand, test_func_ptr)
216221

217222
EXPECT_TRUE(fptr != nullptr);
218223
}
219-
220-
int main(int argc, char** argv)
221-
{
222-
::testing::InitGoogleTest(&argc, argv);
223-
return RUN_ALL_TESTS();
224-
}

0 commit comments

Comments
 (0)