1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/testing/generate_testspec.h"
16
17 #include <random>
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 namespace tflite {
23 namespace testing {
24 namespace {
25
TEST(GenerateRandomTensor,FloatValue)26 TEST(GenerateRandomTensor, FloatValue) {
27 std::mt19937 random_engine;
28 auto random_func = [&]() {
29 return std::uniform_real_distribution<float>(-0.5, 0.5)(random_engine);
30 };
31
32 std::set<float> values;
33 float sum_x_square = 0.0f;
34 float sum_x = 0.0f;
35 for (int i = 0; i < 100; i++) {
36 const auto& data = GenerateRandomTensor<float>({1, 3, 4}, random_func);
37 for (float value : data) {
38 values.insert(value);
39 sum_x_square += value * value;
40 sum_x += value;
41 }
42 }
43
44 // Eech round, generated tensor has different values.
45 EXPECT_GT(values.size(), 200);
46 int num = 1 * 3 * 4 * 100;
47 float stddev = sum_x_square / num - (sum_x / num) * (sum_x / num);
48
49 // Stddev is greater than 1/2 stddev of uniform distribution: (B-A)^2 / 12
50 float minstddev = 1.0f / 12 / 2;
51 EXPECT_GT(stddev, minstddev);
52 }
53
54 } // namespace
55 } // namespace testing
56 } // namespace tflite
57