Error.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <exception>
  31. #include "Test.h"
  32. #include "Pattern.h"
  33. #define UNKNOWN_ERROR 1
  34. #define EQUAL_ERROR 2
  35. #define NEAR_EQUAL_ERROR 3
  36. #define RELATIVE_ERROR 4
  37. #define SNR_ERROR 5
  38. #define DIFFERENT_LENGTH_ERROR 6
  39. #define BOOL_ERROR 7
  40. namespace Client {
  41. // Exception used by tests and runner
  42. // to report errors
  43. class Error: public std::exception
  44. {
  45. public:
  46. Error(Testing::errorID_t id,unsigned long nb)
  47. {
  48. this->errorID = id;
  49. this->lineNumber = nb;
  50. };
  51. Testing::errorID_t errorID;
  52. unsigned long lineNumber;
  53. };
  54. /*
  55. Several test functions to implement tests in the client.
  56. They should not be called directly but through macro
  57. to get the line number.
  58. (SNR functions to finish implementing)
  59. */
  60. template <typename T>
  61. void assert_equal(unsigned long nb,T pa, T pb)
  62. {
  63. if (pa != pb)
  64. {
  65. throw (Error(EQUAL_ERROR,nb));
  66. }
  67. };
  68. template <typename T>
  69. void assert_equal(unsigned long nb,AnyPattern<T> &pa, AnyPattern<T> &pb)
  70. {
  71. if (pa.nbSamples() != pb.nbSamples())
  72. {
  73. throw (Error(EQUAL_ERROR,nb));
  74. }
  75. unsigned long i=0;
  76. T *ptrA = pa.ptr();
  77. T *ptrB = pb.ptr();
  78. for(i=0; i < pa.nbSamples(); i++)
  79. {
  80. assert_equal(nb,ptrA[i],ptrB[i]);
  81. }
  82. };
  83. template <typename T>
  84. void assert_near_equal(unsigned long nb,T pa, T pb, T threshold)
  85. {
  86. if (abs(pa - pb) > threshold)
  87. {
  88. throw (Error(EQUAL_ERROR,nb));
  89. }
  90. };
  91. template <>
  92. void assert_near_equal(unsigned long nb,float32_t pa, float32_t pb, float32_t threshold);
  93. template <typename T>
  94. void assert_near_equal(unsigned long nb,AnyPattern<T> &pa, AnyPattern<T> &pb, T threshold)
  95. {
  96. if (pa.nbSamples() != pb.nbSamples())
  97. {
  98. throw (Error(NEAR_EQUAL_ERROR,nb));
  99. }
  100. unsigned long i=0;
  101. T *ptrA = pa.ptr();
  102. T *ptrB = pb.ptr();
  103. for(i=0; i < pa.nbSamples(); i++)
  104. {
  105. if (abs(ptrA[i] - ptrB[i]) > threshold)
  106. {
  107. throw (Error(NEAR_EQUAL_ERROR,nb));
  108. }
  109. }
  110. };
  111. template <>
  112. void assert_near_equal(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, float32_t threshold);
  113. extern void assert_relative_error(unsigned long nb,float32_t &a, float32_t &b, float32_t threshold);
  114. extern void assert_relative_error(unsigned long nb,AnyPattern<float32_t> &pa, AnyPattern<float32_t> &pb, float32_t threshold);
  115. extern void assert_snr_error(unsigned long nb,AnyPattern<float32_t> &pa,AnyPattern<float32_t> &pb, float32_t threshold);
  116. extern void assert_snr_error(unsigned long nb,AnyPattern<q31_t> &pa,AnyPattern<q31_t> &pb, float32_t threshold);
  117. extern void assert_snr_error(unsigned long nb,AnyPattern<q15_t> &pa,AnyPattern<q15_t> &pb, float32_t threshold);
  118. extern void assert_snr_error(unsigned long nb,AnyPattern<q7_t> &pa,AnyPattern<q7_t> &pb, float32_t threshold);
  119. extern void assert_true(unsigned long nb,bool cond);
  120. extern void assert_false(unsigned long nb,bool cond);
  121. }
  122. /*
  123. Macros to use to implement tests.
  124. */
  125. #define ASSERT_EQ(A,B) Client::assert_equal(__LINE__,A,B)
  126. #define ASSERT_NEAR_EQ(A,B,THRESH) Client::assert_near_equal(__LINE__,A,B,THRESH)
  127. #define ASSERT_REL_ERROR(A,B,THRESH) Client::assert_relative_error(__LINE__,A,B,THRESH)
  128. #define ASSERT_SNR(A,B,SNR) Client::assert_snr_error(__LINE__,A,B,SNR)
  129. #define ASSERT_TRUE(A) Client::assert_true(__LINE__,A)
  130. #define ASSERT_FALSE(A) Client::assert_false(__LINE__,A)
  131. #endif