• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
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 #ifndef TESTS_UT_COMMON_UT_COMMON_H_
17 #define TESTS_UT_COMMON_UT_COMMON_H_
18 
19 #include <cmath>
20 #include <fstream>
21 #include <iostream>
22 #include "gtest/gtest.h"
23 namespace UT {
24 class Common : public testing::Test {
25  public:
26   // TestCase only enter once
27   static void SetUpTestCase();
28   static void TearDownTestCase();
29 
30   // every TEST_F macro will enter one
31   virtual void SetUp();
32   virtual void TearDown();
33 
34   template <typename T>
PrintData(std::string name,T * output_data,int size)35   void PrintData(std::string name, T *output_data, int size) {
36     std::cout << "The " << name << " is as follows:" << std::endl;
37     if (typeid(output_data[0]) == typeid(uint8_t) || typeid(output_data[0]) == typeid(int8_t)) {
38       for (size_t i = 0; i < std::min(size, 100); i++) {
39         std::cout << (int)output_data[i] << " ";
40       }
41     } else {
42       for (size_t i = 0; i < std::min(size, 100); i++) {
43         std::cout << output_data[i] << " ";
44       }
45     }
46     std::cout << std::endl;
47   }
48 
49   template <typename T>
CompareOutputData(T * output_data,T * correct_data,int size,float err_bound)50   static void CompareOutputData(T *output_data, T *correct_data, int size, float err_bound) {
51     for (size_t i = 0; i < size; i++) {
52       T abs = fabs(output_data[i] - correct_data[i]);
53       ASSERT_LE(abs, err_bound);
54     }
55   }
56 
ReadFile(const char * file,size_t * size,char ** buf)57   void ReadFile(const char *file, size_t *size, char **buf) {
58     ASSERT_NE(nullptr, file);
59     ASSERT_NE(nullptr, size);
60     ASSERT_NE(nullptr, buf);
61     std::string path = std::string(file);
62     std::ifstream ifs(path);
63     ASSERT_EQ(true, ifs.good());
64     ASSERT_EQ(true, ifs.is_open());
65 
66     ifs.seekg(0, std::ios::end);
67     *size = ifs.tellg();
68     *buf = new char[*size];
69 
70     ifs.seekg(0, std::ios::beg);
71     ifs.read(*buf, *size);
72     ifs.close();
73   }
74 };
75 }  // namespace UT
76 #endif  // TESTS_UT_COMMON_UT_COMMON_H_
77