1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "suggest/core/layout/normal_distribution_2d.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <vector>
22 
23 namespace latinime {
24 namespace {
25 
26 static const float ORIGIN_X = 0.0f;
27 static const float ORIGIN_Y = 0.0f;
28 static const float LARGE_STANDARD_DEVIATION = 100.0f;
29 static const float SMALL_STANDARD_DEVIATION = 10.0f;
30 static const float ZERO_RADIAN = 0.0f;
31 
TEST(NormalDistribution2DTest,ProbabilityDensity)32 TEST(NormalDistribution2DTest, ProbabilityDensity) {
33     const NormalDistribution2D distribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y,
34             SMALL_STANDARD_DEVIATION, ZERO_RADIAN);
35 
36     static const float SMALL_COORDINATE = 10.0f;
37     static const float LARGE_COORDINATE = 20.0f;
38     // The probability density of the point near the distribution center is larger than the
39     // probability density of the point that is far from distribution center.
40     EXPECT_GE(distribution.getProbabilityDensity(SMALL_COORDINATE, SMALL_COORDINATE),
41             distribution.getProbabilityDensity(LARGE_COORDINATE, LARGE_COORDINATE));
42     // The probability density of the point shifted toward the direction that has larger standard
43     // deviation is larger than the probability density of the point shifted towards another
44     // direction.
45     EXPECT_GE(distribution.getProbabilityDensity(LARGE_COORDINATE, SMALL_COORDINATE),
46             distribution.getProbabilityDensity(SMALL_COORDINATE, LARGE_COORDINATE));
47 }
48 
TEST(NormalDistribution2DTest,Rotate)49 TEST(NormalDistribution2DTest, Rotate) {
50     static const float COORDINATES[] = {0.0f, 10.0f, 100.0f, -20.0f};
51     static const float EPSILON = 0.01f;
52     const NormalDistribution2D distribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y,
53             SMALL_STANDARD_DEVIATION, ZERO_RADIAN);
54     const NormalDistribution2D rotatedDistribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y,
55             SMALL_STANDARD_DEVIATION, M_PI_4);
56     for (const float x : COORDINATES) {
57         for (const float y : COORDINATES) {
58             // The probability density of the rotated distribution at the point and the probability
59             // density of the original distribution at the rotated point are the same.
60             const float probabilityDensity0 = distribution.getProbabilityDensity(x, y);
61             const float probabilityDensity1 = rotatedDistribution.getProbabilityDensity(-y, x);
62             EXPECT_NEAR(probabilityDensity0, probabilityDensity1, EPSILON);
63         }
64     }
65 }
66 
67 }  // namespace
68 }  // namespace latinime
69