You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common_test.h 2.8 kB

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