Error.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: Error.h
  4. * Description: Error Header
  5. *
  6. * $Date: 20. June 2019
  7. * $Revision: V1.0.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #ifndef _ASSERT_H_
  29. #define _ASSERT_H_
  30. #include "arm_math_types.h"
  31. #include "arm_math_types_f16.h"
  32. #include <exception>
  33. #include "Test.h"
  34. #include "Pattern.h"
  35. #define UNKNOWN_ERROR 1
  36. #define EQUAL_ERROR 2
  37. #define NEAR_EQUAL_ERROR 3
  38. #define RELATIVE_ERROR 4
  39. #define SNR_ERROR 5
  40. #define DIFFERENT_LENGTH_ERROR 6
  41. #define BOOL_ERROR 7
  42. #define MEMORY_ALLOCATION_ERROR 8
  43. #define EMPTY_PATTERN_ERROR 9
  44. #define TAIL_NOT_EMPTY_ERROR 10
  45. #define CLOSE_ERROR 11
  46. namespace Client {
  47. // Exception used by tests and runner
  48. // to report errors
  49. class Error: public std::exception
  50. {
  51. public:
  52. Error(Testing::errorID_t id,unsigned long nb)
  53. {
  54. this->errorID = id;
  55. this->lineNumber = nb;
  56. this->details[0]='\0';
  57. };
  58. Error(Testing::errorID_t id,unsigned long nb, const char *details)
  59. {
  60. this->errorID = id;
  61. this->lineNumber = nb;
  62. strcpy(this->details,details);
  63. };
  64. Testing::errorID_t errorID;
  65. unsigned long lineNumber;
  66. char details[200];
  67. };
  68. /*
  69. Several test functions to implement tests in the client.
  70. They should not be called directly but through macro
  71. to get the line number.
  72. (SNR functions to finish implementing)
  73. */
  74. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  75. extern void assert_relative_error(unsigned long nb,float16_t &a, float16_t &b, double threshold);
  76. extern void assert_relative_error(unsigned long nb,AnyPattern<float16_t> &pa, AnyPattern<float16_t> &pb, double threshold);
  77. #endif
  78. extern void assert_relative_error(unsigned long nb,float32_t &a, float32_t &b, double threshold);
  79. extern void assert_relative_error(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, double threshold);
  80. extern void assert_relative_error(unsigned long nb,float64_t &a, float64_t &b, double threshold);
  81. extern void assert_relative_error(unsigned long nb,AnyPattern<float64_t> &pa, AnyPattern<float64_t> &pb, double threshold);
  82. /* Similar to numpy isclose */
  83. extern void assert_close_error(unsigned long nb,float64_t &ref, float64_t &val, double absthreshold, double relthreshold);
  84. extern void assert_close_error(unsigned long nb,AnyPattern<float64_t> &pref, AnyPattern<float64_t> &pval, double absthreshold, double relthreshold);
  85. extern void assert_close_error(unsigned long nb,float32_t &ref, float32_t &val, double absthreshold, double relthreshold);
  86. extern void assert_close_error(unsigned long nb,AnyPattern<float32_t> &pref, AnyPattern<float32_t> &pval, double absthreshold, double relthreshold);
  87. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  88. extern void assert_close_error(unsigned long nb,float16_t &ref, float16_t &val, double absthreshold, double relthreshold);
  89. extern void assert_close_error(unsigned long nb,AnyPattern<float16_t> &pref, AnyPattern<float16_t> &pval, double absthreshold, double relthreshold);
  90. #endif
  91. extern void assert_snr_error(unsigned long nb,AnyPattern<float64_t> &pa,AnyPattern<float64_t> &pb, float64_t threshold);
  92. extern void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold);
  93. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  94. extern void assert_snr_error(unsigned long nb,AnyPattern<float16_t> &pa,AnyPattern<float16_t> &pb, float32_t threshold);
  95. #endif
  96. extern void assert_snr_error(unsigned long nb,AnyPattern<q63_t> &pa,AnyPattern<q63_t> &pb, float32_t threshold);
  97. extern void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold);
  98. extern void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold);
  99. extern void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold);
  100. extern void assert_snr_error(unsigned long nb,float64_t pa,float64_t pb, float64_t threshold);
  101. extern void assert_snr_error(unsigned long nb,float32_t pa,float32_t pb, float32_t threshold);
  102. #if !defined (__CC_ARM) && defined(ARM_FLOAT16_SUPPORTED)
  103. extern void assert_snr_error(unsigned long nb,float16_t pa,float16_t pb, float32_t threshold);
  104. #endif
  105. extern void assert_snr_error(unsigned long nb,q63_t pa,q63_t pb, float32_t threshold);
  106. extern void assert_snr_error(unsigned long nb,q31_t pa,q31_t pb, float32_t threshold);
  107. extern void assert_snr_error(unsigned long nb,q15_t pa,q15_t pb, float32_t threshold);
  108. extern void assert_snr_error(unsigned long nb,q7_t pa,q7_t pb, float32_t threshold);
  109. extern void assert_true(unsigned long nb,bool cond);
  110. extern void assert_false(unsigned long nb,bool cond);
  111. extern void assert_not_empty(unsigned long nb, AnyPattern<float64_t> &p);
  112. extern void assert_not_empty(unsigned long nb, AnyPattern<float32_t> &p);
  113. #if !defined( __CC_ARM ) && defined(ARM_FLOAT16_SUPPORTED)
  114. extern void assert_not_empty(unsigned long nb, AnyPattern<float16_t> &p);
  115. #endif
  116. extern void assert_not_empty(unsigned long nb, AnyPattern<q63_t> &p);
  117. extern void assert_not_empty(unsigned long nb, AnyPattern<q31_t> &p);
  118. extern void assert_not_empty(unsigned long nb, AnyPattern<q15_t> &p);
  119. extern void assert_not_empty(unsigned long nb, AnyPattern<q7_t> &p);
  120. extern void assert_not_empty(unsigned long nb, AnyPattern<uint32_t> &p);
  121. extern void assert_not_empty(unsigned long nb, AnyPattern<uint16_t> &p);
  122. extern void assert_not_empty(unsigned long nb, AnyPattern<uint8_t> &p);
  123. }
  124. /*
  125. Macros to use to implement tests.
  126. */
  127. #define ASSERT_EQ(A,B) Client::assert_equal(__LINE__,A,B)
  128. #define ASSERT_NEAR_EQ(A,B,THRESH) Client::assert_near_equal(__LINE__,A,B,THRESH)
  129. #define ASSERT_REL_ERROR(A,B,THRESH) Client::assert_relative_error(__LINE__,A,B,THRESH)
  130. #define ASSERT_CLOSE_ERROR(A,B,ABSTHRESH,RELTHRESH) Client::assert_close_error(__LINE__,A,B,ABSTHRESH,RELTHRESH)
  131. #define ASSERT_SNR(A,B,SNR) Client::assert_snr_error(__LINE__,A,B,SNR)
  132. #define ASSERT_TRUE(A) Client::assert_true(__LINE__,A)
  133. #define ASSERT_FALSE(A) Client::assert_false(__LINE__,A)
  134. #define ASSERT_NOT_EMPTY(A) Client::assert_not_empty(__LINE__,A)
  135. #define ASSERT_EMPTY_TAIL(A) if (!A.isTailEmpty()) throw (Client::Error(TAIL_NOT_EMPTY_ERROR,__LINE__))
  136. namespace Client {
  137. using namespace std;
  138. template <typename T>
  139. void assert_equal(unsigned long nb,T pa, T pb)
  140. {
  141. if (pa != pb)
  142. {
  143. throw (Error(EQUAL_ERROR,nb));
  144. }
  145. };
  146. template <typename T>
  147. void assert_equal(unsigned long nb,AnyPattern<T> &pa, AnyPattern<T> &pb)
  148. {
  149. ASSERT_NOT_EMPTY(pa);
  150. ASSERT_NOT_EMPTY(pb);
  151. if (pa.nbSamples() != pb.nbSamples())
  152. {
  153. throw (Error(EQUAL_ERROR,nb));
  154. }
  155. unsigned long i=0;
  156. char id[40];
  157. T *ptrA = pa.ptr();
  158. T *ptrB = pb.ptr();
  159. for(i=0; i < pa.nbSamples(); i++)
  160. {
  161. try
  162. {
  163. assert_equal(nb,ptrA[i],ptrB[i]);
  164. }
  165. catch(Error &err)
  166. {
  167. sprintf(id," (nb=%lu)",i+1);
  168. strcat(err.details,id);
  169. throw(err);
  170. }
  171. }
  172. };
  173. template <typename T>
  174. void assert_near_equal(unsigned long nb,T pa, T pb, T threshold)
  175. {
  176. if (abs(pa - pb) > threshold)
  177. {
  178. throw (Error(NEAR_EQUAL_ERROR,nb));
  179. }
  180. };
  181. template <>
  182. void assert_near_equal(unsigned long nb,double pa, double pb, double threshold);
  183. template <>
  184. void assert_near_equal(unsigned long nb,float32_t pa, float32_t pb, float32_t threshold);
  185. template <>
  186. void assert_near_equal(unsigned long nb,q63_t pa, q63_t pb, q63_t threshold);
  187. template <>
  188. void assert_near_equal(unsigned long nb,q31_t pa, q31_t pb, q31_t threshold);
  189. template <>
  190. void assert_near_equal(unsigned long nb,q15_t pa, q15_t pb, q15_t threshold);
  191. template <>
  192. void assert_near_equal(unsigned long nb,q7_t pa, q7_t pb, q7_t threshold);
  193. template <typename T>
  194. void assert_near_equal(unsigned long nb,AnyPattern<T> &pa, AnyPattern<T> &pb, T threshold)
  195. {
  196. ASSERT_NOT_EMPTY(pa);
  197. ASSERT_NOT_EMPTY(pb);
  198. if (pa.nbSamples() != pb.nbSamples())
  199. {
  200. throw (Error(NEAR_EQUAL_ERROR,nb));
  201. }
  202. unsigned long i=0;
  203. char id[40];
  204. T *ptrA = pa.ptr();
  205. T *ptrB = pb.ptr();
  206. for(i=0; i < pa.nbSamples(); i++)
  207. {
  208. try
  209. {
  210. assert_near_equal(nb,ptrA[i],ptrB[i],threshold);
  211. }
  212. catch(Error &err)
  213. {
  214. sprintf(id," (nb=%lu)",i+1);
  215. strcat(err.details,id);
  216. throw(err);
  217. }
  218. }
  219. };
  220. }
  221. #endif